eZ Publish  [trunk]
ezuseroperationcollection.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZUserOperationCollection class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZUserOperationCollection ezuseroperationcollection.php
00013   \brief The class eZUserOperationCollection does
00014 
00015 */
00016 
00017 class eZUserOperationCollection
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZUserOperationCollection()
00023     {
00024     }
00025 
00026    /**
00027      * Changes user settings
00028      *
00029      * @param int $userID
00030      * @param int $isEnabled
00031      * @param int $maxLogin
00032      *
00033      * @return array An array with operation status, always true if userID is ok
00034      */
00035     static public function setSettings( $userID, $isEnabled, $maxLogin )
00036     {
00037         $userSetting = eZUserSetting::fetch( $userID );
00038 
00039         if ( $userSetting )
00040         {
00041             $userSetting->setAttribute( 'max_login', $maxLogin );
00042             $isUserEnabled = $isEnabled != 0;
00043 
00044             if ( $userSetting->attribute( 'is_enabled' ) != $isUserEnabled )
00045             {
00046                 eZContentCacheManager::clearContentCacheIfNeeded( $userID );
00047                 eZContentCacheManager::generateObjectViewCache( $userID );
00048             }
00049 
00050             $userSetting->setAttribute( "is_enabled", $isUserEnabled );
00051             $userSetting->store();
00052 
00053             if ( !$isUserEnabled )
00054             {
00055                 eZUser::removeSessionData( $userID );
00056             }
00057             else
00058             {
00059                 eZUserAccountKey::removeByUserID( $userID );
00060             }
00061             return array( 'status' => true );
00062         }
00063         else
00064         {
00065             eZDebug::writeError( "Failed to change settings of user $userID ", __METHOD__ );
00066             return array( 'status' => false );
00067         }
00068     }
00069 
00070     /**
00071      * Send activativation to the user
00072      *
00073      * If the user is enabled, igore
00074      */
00075     static public function sendActivationEmail( $userID )
00076     {
00077         eZDebugSetting::writeNotice( 'kernel-user',  'Sending activation email.', 'user register' );
00078         $ini = eZINI::instance();
00079 
00080         $tpl = eZTemplate::factory();
00081         $user = eZUser::fetch( $userID );
00082         $tpl->setVariable( 'user', $user );
00083         $object= eZContentObject::fetch( $userID );
00084         $tpl->setVariable( 'object', $object );
00085         $hostname = eZSys::hostname();
00086         $tpl->setVariable( 'hostname', $hostname );
00087 
00088         // Check whether account activation is required.
00089         $verifyUserType = $ini->variable( 'UserSettings', 'VerifyUserType' );
00090         $sendUserMail = !!$verifyUserType;
00091         // For compatibility with old setting
00092         if ( $verifyUserType === 'email'
00093           && $ini->hasVariable( 'UserSettings', 'VerifyUserEmail' )
00094           && $ini->variable( 'UserSettings', 'VerifyUserEmail' ) !== 'enabled' )
00095         {
00096             $verifyUserType = false;
00097         }
00098 
00099         if ( $verifyUserType === 'email' ) // and if it is email type
00100         {
00101             // Disable user account and send verification mail to the user
00102 
00103             // Create enable account hash and send it to the newly registered user
00104             $hash = md5( mt_rand() . time() . $userID );
00105 
00106             if ( eZOperationHandler::operationIsAvailable( 'user_activation' ) )
00107             {
00108                 $operationResult = eZOperationHandler::execute( 'user',
00109                                                                 'activation', array( 'user_id'    => $userID,
00110                                                                                      'user_hash'  => $hash,
00111                                                                                      'is_enabled' => false ) );
00112             }
00113             else
00114             {
00115                 eZUserOperationCollection::activation( $userID, $hash, false );
00116             }
00117 
00118             $tpl->setVariable( 'hash', $hash );
00119 
00120             $sendUserMail = true;
00121         }
00122         else if ( $verifyUserType )// custom account activation
00123         {
00124             $verifyUserTypeClass = false;
00125             // load custom verify user settings
00126             if ( $ini->hasGroup( 'VerifyUserType_' . $verifyUserType ) )
00127             {
00128                 if ( $ini->hasVariable( 'VerifyUserType_' . $verifyUserType, 'File' ) )
00129                     include_once( $ini->variable( 'VerifyUserType_' . $verifyUserType, 'File' ) );
00130                 $verifyUserTypeClass = $ini->variable( 'VerifyUserType_' . $verifyUserType, 'Class' );
00131             }
00132             // try to call the verify user class with function verifyUser
00133             $user = eZContentObject::fetch( $userID );
00134             if ( $verifyUserTypeClass && method_exists( $verifyUserTypeClass, 'verifyUser' ) )
00135                 $sendUserMail  = call_user_func( array( $verifyUserTypeClass, 'verifyUser' ), $user, $tpl );
00136             else
00137                 eZDebug::writeWarning( "Unknown VerifyUserType '$verifyUserType'", 'user/register' );
00138         }
00139 
00140         // send verification mail to user if email type or custum verify user type returned true
00141         if ( $sendUserMail )
00142         {
00143             $templateResult = $tpl->fetch( 'design:user/registrationinfo.tpl' );
00144             if ( $tpl->hasVariable( 'content_type' ) )
00145                 $contentType = $tpl->variable( 'content_type' );
00146             else
00147                 $contentType = $ini->variable( 'MailSettings', 'ContentType' );
00148 
00149             $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
00150             if ( $tpl->hasVariable( 'email_sender' ) )
00151                 $emailSender = $tpl->variable( 'email_sender' );
00152             else if ( !$emailSender )
00153                 $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
00154 
00155             if ( $tpl->hasVariable( 'subject' ) )
00156                 $subject = $tpl->variable( 'subject' );
00157             else
00158                 $subject = ezpI18n::tr( 'kernel/user/register', 'Registration info' );
00159 
00160             $mail = new eZMail();
00161             $mail->setSender( $emailSender );
00162             $mail->setContentType( $contentType );
00163             $user = eZUser::fetch( $userID );
00164             $receiver = $user->attribute( 'email' );
00165             $mail->setReceiver( $receiver );
00166             $mail->setSubject( $subject );
00167             $mail->setBody( $templateResult );
00168             $mailResult = eZMailTransport::send( $mail );
00169         }
00170         return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
00171     }
00172 
00173 
00174     static public function checkActivation( $userID )
00175     {
00176         // check if the account is enabled
00177         $user = eZUser::fetch( $userID );
00178         if( $user->attribute( 'is_enabled' ) )
00179         {
00180             eZDebugSetting::writeNotice( 'kernel-user',  'The user is enabled.', 'user register/check activation' );
00181             return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
00182         }
00183         else
00184         {
00185             eZDebugSetting::writeNotice( 'kernel-user',  'The user is not enabled.', 'user register/check activation' );
00186             return array( 'status' => eZModuleOperationInfo::STATUS_HALTED );
00187         }
00188     }
00189 
00190     /**
00191      *  publish the object
00192      */
00193     static public function publishUserContentObject( $userID )
00194     {
00195         $object = eZContentObject::fetch( $userID );
00196         if( $object->attribute( 'current_version' ) !== '1' )
00197         {
00198             eZDebug::writeError( 'Current version is wrong for the user object. User ID: ' . $userID , 'user/register' );
00199             return array( 'status' => eZModuleOperationInfo::STATUS_CANCELLED );
00200         }
00201         eZDebugSetting::writeNotice( 'kernel-user' , 'publishing user object', 'user register' );
00202         // if the object is already published, continue the operation
00203         if( $object->attribute( 'status' ) )
00204         {
00205             eZDebugSetting::writeNotice( 'kernel-user', 'User object publish is published.', 'user register' );
00206             return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
00207         }
00208         $result = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $userID, 'version' => 1 ) );
00209         if( $result['status'] === eZModuleOperationInfo::STATUS_HALTED )
00210         {
00211             eZDebugSetting::writeNotice( 'kernel-user', 'User object publish is in pending.', 'user register' );
00212             return array( 'status' => eZModuleOperationInfo::STATUS_HALTED );
00213         }
00214         return $result;
00215     }
00216 
00217     /**
00218      *  Send the notification after registeration
00219      */
00220     static public function sendUserNotification( $userID )
00221     {
00222         eZDebugSetting::writeNotice( 'Sending approval notification to the user.' , 'kernel-user', 'user register' );
00223         $user = eZUser::fetch( $userID );
00224         $ini = eZINI::instance();
00225         // Send mail
00226         $tpl = eZTemplate::factory();
00227         $tpl->setVariable( 'user',  $user );
00228         $templateResult = $tpl->fetch( 'design:user/registrationapproved.tpl' );
00229 
00230         $mail = new eZMail();
00231         if ( $tpl->hasVariable( 'content_type' ) )
00232             $mail->setContentType( $tpl->variable( 'content_type' ) );
00233 
00234         $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
00235         if ( $tpl->hasVariable( 'email_sender' ) )
00236             $emailSender = $tpl->variable( 'email_sender' );
00237         else if ( !$emailSender )
00238             $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
00239 
00240         if ( $tpl->hasVariable( 'subject' ) )
00241             $subject = $tpl->variable( 'subject' );
00242         else
00243             $subject = ezpI18n::tr( 'kernel/user/register', 'User registration approved' );
00244 
00245         $mail->setSender( $emailSender );
00246         $receiver = $user->attribute( 'email' );
00247         $mail->setReceiver( $receiver );
00248         $mail->setSubject( $subject );
00249         $mail->setBody( $templateResult );
00250         $mailResult = eZMailTransport::send( $mail );
00251 
00252         return array( 'status' => eZModuleOperationInfo::STATUS_CONTINUE );
00253     }
00254 
00255    /**
00256      * Activate user with user or deactivate and create new eZUserAccountKey with user hash
00257      * depending on $enableUser being true or not.
00258      *
00259      * @param int $userID
00260      * @param string $userHash
00261      * @param bool $enableUser
00262      *
00263      * @return array An array with operation status, always true if userID is ok
00264      */
00265     static public function activation( $userID, $userHash, $enableUser = false )
00266     {
00267         $user = eZUser::fetch( $userID );
00268         $userSetting = eZUserSetting::fetch( $userID );
00269 
00270         if ( $user && $userSetting )
00271         {
00272             $userChange = $userSetting->attribute( 'is_enabled' ) != $enableUser;
00273 
00274             if ( $enableUser )
00275             {
00276                 $userSetting->setAttribute( 'is_enabled', 1 );
00277                 $userSetting->store();
00278 
00279                 eZUserAccountKey::removeByUserID( $userID );
00280             }
00281             else
00282             {
00283                 $userSetting->setAttribute( 'is_enabled', 0 );
00284                 $userSetting->store();
00285 
00286                 $accountKey = eZUserAccountKey::createNew( $userID, $userHash, time() );
00287                 $accountKey->store();
00288             }
00289 
00290             if ( $userChange )
00291             {
00292                 if ( !$enableUser )
00293                 {
00294                     eZUser::removeSessionData( $userID );
00295                 }
00296                 eZContentCacheManager::clearContentCacheIfNeeded( $userID );
00297             }
00298             return array( 'status' => true );
00299         }
00300         else
00301         {
00302             eZDebug::writeError( "Failed to activate user $userID (could not fetch)", __METHOD__ );
00303             return array( 'status' => false );
00304         }
00305     }
00306 
00307    /**
00308      * Change user password
00309      *
00310      * @param int $userID
00311      * @param string $newPassword
00312      *
00313      * @return array An array with operation status, always true if userID is ok
00314      */
00315     static public function password( $userID, $newPassword )
00316     {
00317         $user = eZUser::fetch( $userID );
00318 
00319         if ( $user instanceof eZUser )
00320         {
00321             $login   = $user->attribute( 'login' );
00322             $type    = $user->attribute( 'password_hash_type' );
00323             $site    = $user->site();
00324             $newHash = $user->createHash( $login, $newPassword, $site, $type );
00325             $user->setAttribute( 'password_hash', $newHash );
00326             $user->store();
00327             return array( 'status' => true );
00328         }
00329         else
00330         {
00331             eZDebug::writeError( "Failed to change password of user $userID (could not fetch user)", __METHOD__ );
00332             return array( 'status' => false );
00333         }
00334     }
00335 
00336    /**
00337      * Generate forgotpassword object
00338      *
00339      * @param int $userID
00340      * @param string $passwordHash
00341      * @param int $time
00342      *
00343      * @return array An array with operation status, always true if userID is ok
00344      */
00345     static public function forgotpassword( $userID, $passwordHash, $time )
00346     {
00347         $user = eZUser::fetch( $userID );
00348 
00349         if ( $user instanceof eZUser )
00350         {
00351             $forgotPasswdObj = eZForgotPassword::createNew( $userID, $passwordHash, $time );
00352             $forgotPasswdObj->store();
00353             return array( 'status' => true );
00354         }
00355         else
00356         {
00357             eZDebug::writeError( "Failed to generate password hash for user $userID (could not fetch user)", __METHOD__ );
00358             return array( 'status' => false );
00359         }
00360     }
00361 
00362    /**
00363      * Set user preferences
00364      * Only needed for operations, call eZPreferences::setValue() directly if you want to set user preferences
00365      *
00366      * @param string $key
00367      * @param string $value
00368      *
00369      * @return array An array with operation status, always true
00370      */
00371     static public function preferences( $key, $value )
00372     {
00373         eZPreferences::setValue( $key, $value );
00374         return array( 'status' => true );
00375     }
00376 }
00377 ?>