PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/user/ezuseroperationcollection.php

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