PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/user/ezuseroperationcollection.php

https://github.com/granitegreg/ezpublish
PHP | 393 lines | 249 code | 40 blank | 104 comment | 40 complexity | b3183be2b768712d790d8717c90bfc65 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. //
  3. // Definition of eZUserOperationCollection class
  4. //
  5. // Created on: <27-Apr-2009 13:51:17 rp/ar>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2011 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*! \file
  31. */
  32. /*!
  33. \class eZUserOperationCollection ezuseroperationcollection.php
  34. \brief The class eZUserOperationCollection does
  35. */
  36. class eZUserOperationCollection
  37. {
  38. /*!
  39. Constructor
  40. */
  41. function eZUserOperationCollection()
  42. {
  43. }
  44. /**
  45. * Changes user settings
  46. *
  47. * @param int $userID
  48. * @param int $isEnabled
  49. * @param int $maxLogin
  50. *
  51. * @return array An array with operation status, always true if userID is ok
  52. */
  53. static public function setSettings( $userID, $isEnabled, $maxLogin )
  54. {
  55. $userSetting = eZUserSetting::fetch( $userID );
  56. if ( $userSetting )
  57. {
  58. $userSetting->setAttribute( 'max_login', $maxLogin );
  59. $isUserEnabled = $isEnabled != 0;
  60. if ( $userSetting->attribute( 'is_enabled' ) != $isUserEnabled )
  61. {
  62. eZContentCacheManager::clearContentCacheIfNeeded( $userID );
  63. eZContentCacheManager::generateObjectViewCache( $userID );
  64. }
  65. $userSetting->setAttribute( "is_enabled", $isUserEnabled );
  66. $userSetting->store();
  67. if ( !$isUserEnabled )
  68. {
  69. eZUser::removeSessionData( $userID );
  70. }
  71. return array( 'status' => true );
  72. }
  73. else
  74. {
  75. eZDebug::writeError( "Failed to change settings of user $userID ", __METHOD__ );
  76. return array( 'status' => false );
  77. }
  78. }
  79. /**
  80. * Send activativation to the user
  81. *
  82. * If the user is enabled, igore
  83. */
  84. static public function sendActivationEmail( $userID )
  85. {
  86. eZDebugSetting::writeNotice( 'kernel-user', 'Sending activation email.', 'user register' );
  87. $ini = eZINI::instance();
  88. $tpl = eZTemplate::factory();
  89. $user = eZUser::fetch( $userID );
  90. $tpl->setVariable( 'user', $user );
  91. $object= eZContentObject::fetch( $userID );
  92. $tpl->setVariable( 'object', $object );
  93. $hostname = eZSys::hostname();
  94. $tpl->setVariable( 'hostname', $hostname );
  95. // Check whether account activation is required.
  96. $verifyUserType = $ini->variable( 'UserSettings', 'VerifyUserType' );
  97. $sendUserMail = !!$verifyUserType;
  98. // For compatibility with old setting
  99. if ( $verifyUserType === 'email'
  100. && $ini->hasVariable( 'UserSettings', 'VerifyUserEmail' )
  101. && $ini->variable( 'UserSettings', 'VerifyUserEmail' ) !== 'enabled' )
  102. {
  103. $verifyUserType = false;
  104. }
  105. if ( $verifyUserType === 'email' ) // and if it is email type
  106. {
  107. // Disable user account and send verification mail to the user
  108. // Create enable account hash and send it to the newly registered user
  109. $hash = md5( mt_rand() . time() . $userID );
  110. if ( eZOperationHandler::operationIsAvailable( 'user_activation' ) )
  111. {
  112. $operationResult = eZOperationHandler::execute( 'user',
  113. 'activation', array( 'user_id' => $userID,
  114. 'user_hash' => $hash,
  115. 'is_enabled' => false ) );
  116. }
  117. else
  118. {
  119. eZUserOperationCollection::activation( $userID, $hash, false );
  120. }
  121. $tpl->setVariable( 'hash', $hash );
  122. $sendUserMail = true;
  123. }
  124. else if ( $verifyUserType )// custom account activation
  125. {
  126. $verifyUserTypeClass = false;
  127. // load custom verify user settings
  128. if ( $ini->hasGroup( 'VerifyUserType_' . $verifyUserType ) )
  129. {
  130. if ( $ini->hasVariable( 'VerifyUserType_' . $verifyUserType, 'File' ) )
  131. include_once( $ini->variable( 'VerifyUserType_' . $verifyUserType, 'File' ) );
  132. $verifyUserTypeClass = $ini->variable( 'VerifyUserType_' . $verifyUserType, 'Class' );
  133. }
  134. // try to call the verify user class with function verifyUser
  135. $user = eZContentObject::fetch( $userID );
  136. if ( $verifyUserTypeClass && method_exists( $verifyUserTypeClass, 'verifyUser' ) )
  137. $sendUserMail = call_user_func( array( $verifyUserTypeClass, 'verifyUser' ), $user, $tpl );
  138. else
  139. eZDebug::writeWarning( "Unknown VerifyUserType '$verifyUserType'", 'user/register' );
  140. }
  141. // send verification mail to user if email type or custum verify user type returned true
  142. if ( $sendUserMail )
  143. {
  144. $templateResult = $tpl->fetch( 'design:user/registrationinfo.tpl' );
  145. if ( $tpl->hasVariable( 'content_type' ) )
  146. $mail->setContentType( $tpl->variable( 'content_type' ) );
  147. $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
  148. if ( $tpl->hasVariable( 'email_sender' ) )
  149. $emailSender = $tpl->variable( 'email_sender' );
  150. else if ( !$emailSender )
  151. $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
  152. if ( $tpl->hasVariable( 'subject' ) )
  153. $subject = $tpl->variable( 'subject' );
  154. else
  155. $subject = ezpI18n::tr( 'kernel/user/register', 'Registration info' );
  156. $mail = new eZMail();
  157. $mail->setSender( $emailSender );
  158. $user = eZUser::fetch( $userID );
  159. $receiver = $user->attribute( 'email' );
  160. $mail->setReceiver( $receiver );
  161. $mail->setSubject( $subject );
  162. $mail->setBody( $templateResult );
  163. $mailResult = eZMailTransport::send( $mail );
  164. }
  165. return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
  166. }
  167. static public function checkActivation( $userID )
  168. {
  169. // check if the account is enabled
  170. $user = eZUser::fetch( $userID );
  171. if( $user->attribute( 'is_enabled' ) )
  172. {
  173. eZDebugSetting::writeNotice( 'kernel-user', 'The user is enabled.', 'user register/check activation' );
  174. return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
  175. }
  176. else
  177. {
  178. eZDebugSetting::writeNotice( 'kernel-user', 'The user is not enabled.', 'user register/check activation' );
  179. return array( 'status' => eZModuleOperationInfo::STATUS_HALTED );
  180. }
  181. }
  182. /**
  183. * publish the object
  184. */
  185. static public function publishUserContentObject( $userID )
  186. {
  187. $object = eZContentObject::fetch( $userID );
  188. if( $object->attribute( 'current_version' ) !== '1' )
  189. {
  190. eZDebug::writeError( 'Current version is wrong for the user object. User ID: ' . $userID , 'user/register' );
  191. return array( 'status' => eZModuleOperationInfo::STATUS_CANCELLED );
  192. }
  193. eZDebugSetting::writeNotice( 'kernel-user' , 'publishing user object', 'user register' );
  194. // if the object is already published, continue the operation
  195. if( $object->attribute( 'status' ) )
  196. {
  197. eZDebugSetting::writeNotice( 'kernel-user', 'User object publish is published.', 'user register' );
  198. return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
  199. }
  200. $result = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID, 'version' => 1 ) );
  201. if( $result['status'] === eZModuleOperationInfo::STATUS_HALTED )
  202. {
  203. eZDebugSetting::writeNotice( 'kernel-user', 'User object publish is in pending.', 'user register' );
  204. return array( 'status' => eZModuleOperationInfo::STATUS_HALTED );
  205. }
  206. return $result;
  207. }
  208. /**
  209. * Send the notification after registeration
  210. */
  211. static public function sendUserNotification( $userID )
  212. {
  213. eZDebugSetting::writeNotice( 'Sending approval notification to the user.' , 'kernel-user', 'user register' );
  214. $user = eZUser::fetch( $userID );
  215. $ini = eZINI::instance();
  216. // Send mail
  217. $tpl = eZTemplate::factory();
  218. $tpl->setVariable( 'user', $user );
  219. $templateResult = $tpl->fetch( 'design:user/registrationapproved.tpl' );
  220. $mail = new eZMail();
  221. if ( $tpl->hasVariable( 'content_type' ) )
  222. $mail->setContentType( $tpl->variable( 'content_type' ) );
  223. $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
  224. if ( $tpl->hasVariable( 'email_sender' ) )
  225. $emailSender = $tpl->variable( 'email_sender' );
  226. else if ( !$emailSender )
  227. $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
  228. if ( $tpl->hasVariable( 'subject' ) )
  229. $subject = $tpl->variable( 'subject' );
  230. else
  231. $subject = ezpI18n::tr( 'kernel/user/register', 'User registration approved' );
  232. $mail->setSender( $emailSender );
  233. $receiver = $user->attribute( 'email' );
  234. $mail->setReceiver( $receiver );
  235. $mail->setSubject( $subject );
  236. $mail->setBody( $templateResult );
  237. $mailResult = eZMailTransport::send( $mail );
  238. return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
  239. }
  240. /**
  241. * Activate user with user or deactivate and create new eZUserAccountKey with user hash
  242. * depending on $enableUser being true or not.
  243. *
  244. * @param int $userID
  245. * @param string $userHash
  246. * @param bool $enableUser
  247. *
  248. * @return array An array with operation status, always true if userID is ok
  249. */
  250. static public function activation( $userID, $userHash, $enableUser = false )
  251. {
  252. $user = eZUser::fetch( $userID );
  253. $userSetting = eZUserSetting::fetch( $userID );
  254. if ( $user && $userSetting )
  255. {
  256. $userChange = $userSetting->attribute( 'is_enabled' ) != $enableUser;
  257. if ( $enableUser )
  258. {
  259. $userSetting->setAttribute( 'is_enabled', 1 );
  260. $userSetting->store();
  261. eZUserAccountKey::removeByUserID( $userID );
  262. }
  263. else
  264. {
  265. $userSetting->setAttribute( 'is_enabled', 0 );
  266. $userSetting->store();
  267. $accountKey = eZUserAccountKey::createNew( $userID, $userHash, time() );
  268. $accountKey->store();
  269. }
  270. if ( $userChange )
  271. {
  272. if ( !$enableUser )
  273. {
  274. eZUser::removeSessionData( $userID );
  275. }
  276. eZContentCacheManager::clearContentCacheIfNeeded( $userID );
  277. }
  278. return array( 'status' => true );
  279. }
  280. else
  281. {
  282. eZDebug::writeError( "Failed to activate user $userID (could not fetch)", __METHOD__ );
  283. return array( 'status' => false );
  284. }
  285. }
  286. /**
  287. * Change user password
  288. *
  289. * @param int $userID
  290. * @param string $newPassword
  291. *
  292. * @return array An array with operation status, always true if userID is ok
  293. */
  294. static public function password( $userID, $newPassword )
  295. {
  296. $user = eZUser::fetch( $userID );
  297. if ( $user instanceof eZUser )
  298. {
  299. $login = $user->attribute( 'login' );
  300. $type = $user->attribute( 'password_hash_type' );
  301. $site = $user->site();
  302. $newHash = $user->createHash( $login, $newPassword, $site, $type );
  303. $user->setAttribute( 'password_hash', $newHash );
  304. $user->store();
  305. return array( 'status' => true );
  306. }
  307. else
  308. {
  309. eZDebug::writeError( "Failed to change password of user $userID (could not fetch user)", __METHOD__ );
  310. return array( 'status' => false );
  311. }
  312. }
  313. /**
  314. * Generate forgotpassword object
  315. *
  316. * @param int $userID
  317. * @param string $passwordHash
  318. * @param int $time
  319. *
  320. * @return array An array with operation status, always true if userID is ok
  321. */
  322. static public function forgotpassword( $userID, $passwordHash, $time )
  323. {
  324. $user = eZUser::fetch( $userID );
  325. if ( $user instanceof eZUser )
  326. {
  327. $forgotPasswdObj = eZForgotPassword::createNew( $userID, $passwordHash, $time );
  328. $forgotPasswdObj->store();
  329. return array( 'status' => true );
  330. }
  331. else
  332. {
  333. eZDebug::writeError( "Failed to generate password hash for user $userID (could not fetch user)", __METHOD__ );
  334. return array( 'status' => false );
  335. }
  336. }
  337. /**
  338. * Set user preferences
  339. * Only needed for operations, call eZPreferences::setValue() directly if you want to set user preferences
  340. *
  341. * @param string $key
  342. * @param string $value
  343. *
  344. * @return array An array with operation status, always true
  345. */
  346. static public function preferences( $key, $value )
  347. {
  348. eZPreferences::setValue( $key, $value );
  349. return array( 'status' => true );
  350. }
  351. }
  352. ?>