PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/user/ezuseroperationcollection.php

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