PageRenderTime 31ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/user/ezuseroperationcollection.php

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