eZ Publish  [4.0]
ezusertype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZUserType class
00004 //
00005 // Created on: <30-Apr-2002 13:06:21 bf>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZUserType ezusertype.php
00033   \brief The class eZUserType handles user accounts and association with content objects
00034   \ingroup eZDatatype
00035 
00036 */
00037 
00038 //include_once( "kernel/classes/ezdatatype.php" );
00039 //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00040 //include_once( "kernel/classes/datatypes/ezuser/ezusersetting.php" );
00041 //include_once( "lib/ezutils/classes/ezmail.php" );
00042 
00043 class eZUserType extends eZDataType
00044 {
00045     const DATA_TYPE_STRING = "ezuser";
00046 
00047     function eZUserType( )
00048     {
00049         $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', "User account", 'Datatype name' ),
00050                            array( 'translation_allowed' => false,
00051                                   'serialize_supported' => true ) );
00052     }
00053 
00054     /*!
00055      Delete stored object attribute
00056     */
00057     function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
00058     {
00059         $db = eZDB::instance();
00060         $userID = $contentObjectAttribute->attribute( "contentobject_id" );
00061 
00062         $res = $db->arrayQuery( "SELECT COUNT(*) AS version_count FROM ezcontentobject_version WHERE contentobject_id = $userID" );
00063         $versionCount = $res[0]['version_count'];
00064 
00065         if ( $version == null || $versionCount <= 1 )
00066         {
00067             eZUser::removeUser( $userID );
00068             $db->query( "DELETE FROM ezuser_role WHERE contentobject_id = '$userID'" );
00069         }
00070     }
00071 
00072     /*!
00073      Validates the input and returns true if the input was
00074      valid for this datatype.
00075     */
00076     function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00077     {
00078         if ( $http->hasPostVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) ) )
00079         {
00080             $classAttribute = $contentObjectAttribute->contentClassAttribute();
00081             $loginName = $http->postVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) );
00082             $email = $http->postVariable( $base . "_data_user_email_" . $contentObjectAttribute->attribute( "id" ) );
00083             $password = $http->postVariable( $base . "_data_user_password_" . $contentObjectAttribute->attribute( "id" ) );
00084             $passwordConfirm = $http->postVariable( $base . "_data_user_password_confirm_" . $contentObjectAttribute->attribute( "id" ) );
00085             if ( trim( $loginName ) == '' )
00086             {
00087                 if ( $contentObjectAttribute->validateIsRequired() || trim( $email ) != '' )
00088                 {
00089                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00090                                                                          'The username must be specified.' ) );
00091                     return eZInputValidator::STATE_INVALID;
00092                 }
00093             }
00094             else
00095             {
00096                 $existUser = eZUser::fetchByName( $loginName );
00097                 if ( $existUser != null )
00098                 {
00099                     $userID = $existUser->attribute( 'contentobject_id' );
00100                     if ( $userID !=  $contentObjectAttribute->attribute( "contentobject_id" ) )
00101                     {
00102                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00103                                                                              'The username already exists, please choose another one.' ) );
00104                         return eZInputValidator::STATE_INVALID;
00105                     }
00106                 }
00107                 $isValidate = eZMail::validate( $email );
00108                 if ( !$isValidate )
00109                 {
00110                     $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00111                                                                          'The email address is not valid.' ) );
00112                     return eZInputValidator::STATE_INVALID;
00113                 }
00114 
00115                 $authenticationMatch = eZUser::authenticationMatch();
00116                 if ( $authenticationMatch & eZUser::AUTHENTICATE_EMAIL )
00117                 {
00118                     if ( eZUser::requireUniqueEmail() )
00119                     {
00120                         $userByEmail = eZUser::fetchByEmail( $email );
00121                         if ( $userByEmail != null )
00122                         {
00123                             $userID = $userByEmail->attribute( 'contentobject_id' );
00124                             if ( $userID !=  $contentObjectAttribute->attribute( "contentobject_id" ) )
00125                             {
00126                                 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00127                                                                                      'A user with this email already exists.' ) );
00128                                 return eZInputValidator::STATE_INVALID;
00129                             }
00130                         }
00131                     }
00132                 }
00133                 $ini = eZINI::instance();
00134                 // validate user name
00135                 $regexList = $ini->variable( 'UserSettings', 'UserNameValidationRegex' );
00136                 $errorTextList = $ini->variable( 'UserSettings', 'UserNameValidationErrorText' );
00137                 foreach ( $regexList as $key => $regex )
00138                 {
00139                     if( preg_match( $regex, $loginName) )
00140                     {
00141                         if ( isset( $errorTextList[$key] ) )
00142                             $errorText = $errorTextList[$key];
00143                         else
00144                             $errorText = $ini->variable( 'UserSettings', 'DefaultUserNameValidationErrorText' );
00145                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00146                                                                      $errorText ) );
00147                         return eZInputValidator::STATE_INVALID;
00148                     }
00149                 }
00150                 $generatePasswordIfEmpty = $ini->variable( "UserSettings", "GeneratePasswordIfEmpty" ) == 'true';
00151                 if ( !$generatePasswordIfEmpty || ( $password != "" ) )
00152                 {
00153                     if ( ( $password != $passwordConfirm ) || ( $password == "" ) )
00154                     {
00155                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00156                                                                              'The passwords do not match.',
00157                                                                              'eZUserType' ) );
00158                         return eZInputValidator::STATE_INVALID;
00159                     }
00160                     $minPasswordLength = $ini->hasVariable( 'UserSettings', 'MinPasswordLength' ) ? $ini->variable( 'UserSettings', 'MinPasswordLength' ) : 3;
00161 
00162                     if ( strlen( $password ) < (int) $minPasswordLength )
00163                     {
00164                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00165                                                                              'The password must be at least %1 characters long.',null, array( $minPasswordLength ) ) );
00166                         return eZInputValidator::STATE_INVALID;
00167                     }
00168                     if ( strtolower( $password ) == 'password' )
00169                     {
00170                         $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00171                                                                              'The password must not be "password".' ) );
00172                         return eZInputValidator::STATE_INVALID;
00173                     }
00174                 }
00175             }
00176         }
00177         return eZInputValidator::STATE_ACCEPTED;
00178     }
00179 
00180     /*!
00181      Fetches the http post var integer input and stores it in the data instance.
00182     */
00183     function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
00184     {
00185         if ( $http->hasPostVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) ) )
00186         {
00187             $login = $http->postVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) );
00188             $email = $http->postVariable( $base . "_data_user_email_" . $contentObjectAttribute->attribute( "id" ) );
00189             $password = $http->postVariable( $base . "_data_user_password_" . $contentObjectAttribute->attribute( "id" ) );
00190             $passwordConfirm = $http->postVariable( $base . "_data_user_password_confirm_" . $contentObjectAttribute->attribute( "id" ) );
00191 
00192             $contentObjectID = $contentObjectAttribute->attribute( "contentobject_id" );
00193 
00194             $user = $contentObjectAttribute->content();
00195             if ( $user === null )
00196             {
00197                 $user = eZUser::create( $contentObjectID );
00198             }
00199 
00200             $ini = eZINI::instance();
00201             $generatePasswordIfEmpty = $ini->variable( "UserSettings", "GeneratePasswordIfEmpty" );
00202             if (  $password == "" )
00203             {
00204                 if ( $generatePasswordIfEmpty == 'true' )
00205                 {
00206                     $passwordLength = $ini->variable( "UserSettings", "GeneratePasswordLength" );
00207                     $password = $user->createPassword( $passwordLength );
00208                     $passwordConfirm = $password;
00209                     $http->setSessionVariable( "GeneratedPassword", $password );
00210                 }
00211                 else
00212                 {
00213                     $password = null;
00214                 }
00215             }
00216 
00217             eZDebugSetting::writeDebug( 'kernel-user', $password, "password" );
00218             eZDebugSetting::writeDebug( 'kernel-user', $passwordConfirm, "passwordConfirm" );
00219             eZDebugSetting::writeDebug( 'kernel-user', $login, "login" );
00220             eZDebugSetting::writeDebug( 'kernel-user', $email, "email" );
00221             eZDebugSetting::writeDebug( 'kernel-user', $contentObjectID, "contentObjectID" );
00222             if ( $password == "_ezpassword" )
00223             {
00224                 $password = false;
00225                 $passwordConfirm = false;
00226             }
00227             else
00228                 $http->setSessionVariable( "GeneratedPassword", $password );
00229 
00230             eZDebugSetting::writeDebug( 'kernel-user', "setInformation run", "ezusertype" );
00231             $user->setInformation( $contentObjectID, $login, $email, $password, $passwordConfirm );
00232             $contentObjectAttribute->setContent( $user );
00233             return true;
00234         }
00235         return false;
00236     }
00237 
00238     function storeObjectAttribute( $contentObjectAttribute )
00239     {
00240         $user = $contentObjectAttribute->content();
00241         if ( !( $user instanceof eZUser ) )
00242         {
00243             // create a default user account
00244             $user = eZUser::create( $contentObjectAttribute->attribute( "contentobject_id" ) );
00245             $userID = $contentObjectAttribute->attribute( "contentobject_id" );
00246             $isEnabled = 1;
00247             $userSetting = eZUserSetting::create( $userID, $isEnabled );
00248             $userSetting->store();
00249         }
00250         $user->store();
00251         $contentObjectAttribute->setContent( $user );
00252     }
00253 
00254     /*!
00255      Returns the object title.
00256     */
00257     function title( $contentObjectAttribute, $name = "login" )
00258     {
00259         $user = $this->objectAttributeContent( $contentObjectAttribute );
00260 
00261         $value = $user->attribute( $name );
00262 
00263         return $value;
00264     }
00265 
00266     function hasObjectAttributeContent( $contentObjectAttribute )
00267     {
00268         $user = $this->objectAttributeContent( $contentObjectAttribute );
00269         if ( is_object( $user ) and
00270              $user->isEnabled() )
00271             return true;
00272         return false;
00273     }
00274 
00275     /*!
00276      Returns the user object.
00277     */
00278     function objectAttributeContent( $contentObjectAttribute )
00279     {
00280         $userID = $contentObjectAttribute->attribute( "contentobject_id" );
00281         if ( empty( $GLOBALS['eZUserObject_' . $userID] ) )
00282         {
00283             $GLOBALS['eZUserObject_' . $userID] = eZUser::fetch( $userID );
00284         }
00285         $user = eZUser::fetch( $userID );
00286         eZDebugSetting::writeDebug( 'kernel-user', $user, 'user' );
00287         return $user;
00288     }
00289 
00290     /*!
00291      \reimp
00292     */
00293     function isIndexable()
00294     {
00295         return true;
00296     }
00297 
00298     /*!
00299      \reimp
00300      We can only remove the user attribute if:
00301      - The current user, anonymous user and administrator user is not using this class
00302      - There are more classes with the ezuser datatype
00303     */
00304     function classAttributeRemovableInformation( $contentClassAttribute, $includeAll = true )
00305     {
00306         $result  = array( 'text' => ezi18n( 'kernel/classes/datatypes',
00307                                             "Cannot remove the account:" ),
00308                           'list' => array() );
00309         $currentUser = eZUser::currentUser();
00310         $userObject  = $currentUser->attribute( 'contentobject' );
00311         $ini         = eZINI::instance();
00312         $anonID      = (int)$ini->variable( 'UserSettings', 'AnonymousUserID' );
00313         $classID     = (int)$contentClassAttribute->attribute( 'contentclass_id' );
00314         $db          = eZDB::instance();
00315 
00316         if ( $classID == $userObject->attribute( 'contentclass_id' ) )
00317         {
00318             $result['list'][] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
00319                                                          "The account owner is currently logged in." ) );
00320             if ( !$includeAll )
00321                 return $result;
00322         }
00323 
00324         $sql = "SELECT id FROM ezcontentobject WHERE id = $anonID AND contentclass_id = $classID";
00325         $rows = $db->arrayQuery( $sql );
00326         if ( count( $rows ) > 0 )
00327         {
00328             $result['list'][] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
00329                                                          "The account is currently used by the anonymous user." ) );
00330             if ( !$includeAll )
00331                 return $result;
00332         }
00333 
00334         $sql = "SELECT ezco.id FROM ezcontentobject ezco, ezuser
00335  WHERE ezco.contentclass_id = $classID AND
00336        ezuser.login = 'admin' AND
00337        ezco.id = ezuser.contentobject_id ";
00338         $rows = $db->arrayQuery( $sql );
00339         if ( count( $rows ) > 0 )
00340         {
00341             $result['list'][] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
00342                                                          "The account is currently used the administrator user." ) );
00343             if ( !$includeAll )
00344                 return $result;
00345         }
00346 
00347         $sql = "SELECT count( ezcc.id ) AS count FROM ezcontentclass ezcc, ezcontentclass_attribute ezcca
00348  WHERE ezcc.id != $classID AND
00349        ezcca.data_type_string = 'ezuser' AND
00350        ezcc.id = ezcca.contentclass_id ";
00351         $rows = $db->arrayQuery( $sql );
00352         if ( $rows[0]['count'] == 0 )
00353         {
00354             $result['list'][] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
00355                                                          "You cannot remove the last class holding user accounts." ) );
00356             if ( !$includeAll )
00357                 return $result;
00358         }
00359 
00360         return $result;
00361     }
00362 
00363     /*!
00364      Returns the meta data used for storing search indeces.
00365     */
00366     function metaData( $contentObjectAttribute )
00367     {
00368         $metaString = "";
00369         $user = $contentObjectAttribute->content();
00370 
00371         if ( $user instanceof eZUser )
00372         {
00373             // create a default user account
00374             $metaString .= $user->attribute( 'login' ) . " ";
00375             $metaString .= $user->attribute( 'email' ) . " ";
00376         }
00377         return $metaString;
00378     }
00379 
00380     function toString( $contentObjectAttribute )
00381     {
00382         $userID = $contentObjectAttribute->attribute( "contentobject_id" );
00383         if ( empty( $GLOBALS['eZUserObject_' . $userID] ) )
00384         {
00385             $GLOBALS['eZUserObject_' . $userID] = eZUser::fetch( $userID );
00386         }
00387         $user = $GLOBALS['eZUserObject_' . $userID];
00388 
00389         return implode( '|', array( $user->attribute( 'login' ),
00390                                     $user->attribute( 'email' ),
00391                                     $user->attribute( 'password_hash' ),
00392                                     eZUser::passwordHashTypeName( $user->attribute( 'password_hash_type' ) )  ) );
00393     }
00394 
00395 
00396     function fromString( $contentObjectAttribute, $string )
00397     {
00398         if ( $string == '' )
00399             return true;
00400         $userData = explode( '|', $string );
00401         if( count( $userData ) < 2 )
00402             return false;
00403         $login = $userData[0];
00404         $email = $userData[1];
00405 
00406         if ( eZUser::fetchByName( $login ) || eZUser::fetchByEmail( $email ) )
00407             return false;
00408 
00409         $user = eZUser::create( $contentObjectAttribute->attribute( 'contentobject_id' ) );
00410 
00411         $user->setAttribute( 'login', $login );
00412         $user->setAttribute( 'email', $email );
00413         if ( isset( $userData[2] ) )
00414             $user->setAttribute( 'password_hash', $userData[2] );
00415 
00416         if ( isset( $userData[3] ) )
00417             $user->setAttribute( 'password_hash_type', eZUser::passwordHashTypeID( $userData[3] ) );
00418         $user->store();
00419         return $user;
00420     }
00421 
00422     /*!
00423      \param package
00424      \param content attribute
00425 
00426      \return a DOM representation of the content object attribute
00427     */
00428     function serializeContentObjectAttribute( $package, $objectAttribute )
00429     {
00430         $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00431         $userID = $objectAttribute->attribute( "contentobject_id" );
00432         $user = eZUser::fetch( $userID );
00433         if ( is_object( $user ) )
00434         {
00435             $userNode = $node->ownerDocument->createElement( 'account' );
00436             $userNode->setAttribute( 'login', $user->attribute( 'login' ) );
00437             $userNode->setAttribute( 'email', $user->attribute( 'email' ) );
00438             $userNode->setAttribute( 'password_hash', $user->attribute( 'password_hash' ) );
00439             $userNode->setAttribute( 'password_hash_type', eZUser::passwordHashTypeName( $user->attribute( 'password_hash_type' ) ) );
00440             $node->appendChild( $userNode );
00441         }
00442 
00443         return $node;
00444     }
00445 
00446     /*!
00447      \reimp
00448      \param package
00449      \param contentobject attribute object
00450      \param ezdomnode object
00451     */
00452     function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
00453     {
00454         $userNode = $attributeNode->getElementsByTagName( 'account' )->item( 0 );
00455         if ( is_object( $userNode ) )
00456         {
00457             $userID = $objectAttribute->attribute( 'contentobject_id' );
00458             $user = eZUser::fetch( $userID );
00459             if ( !is_object( $user ) )
00460             {
00461                 $user = eZUser::create( $userID );
00462             }
00463             $user->setAttribute( 'login', $userNode->getAttribute( 'login' ) );
00464             $user->setAttribute( 'email', $userNode->getAttribute( 'email' ) );
00465             $user->setAttribute( 'password_hash', $userNode->getAttribute( 'password_hash' ) );
00466             $user->setAttribute( 'password_hash_type', eZUser::passwordHashTypeID( $userNode->getAttribute( 'password_hash_type' ) ) );
00467             $user->store();
00468         }
00469     }
00470 }
00471 
00472 eZDataType::register( eZUserType::DATA_TYPE_STRING, "eZUserType" );
00473 
00474 ?>