[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezuser/ -> ezusertype.php (source)

   1  <?php
   2  //
   3  // Definition of eZUserType class
   4  //
   5  // Created on: <30-Apr-2002 13:06:21 bf>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZUserType ezusertype.php
  31    \brief The class eZUserType handles user accounts and association with content objects
  32    \ingroup eZDatatype
  33  
  34  */
  35  
  36  include_once ( "kernel/classes/ezdatatype.php" );
  37  include_once ( "kernel/classes/datatypes/ezuser/ezuser.php" );
  38  include_once ( "kernel/classes/datatypes/ezuser/ezusersetting.php" );
  39  include_once ( "lib/ezutils/classes/ezmail.php" );
  40  
  41  define( "EZ_DATATYPESTRING_USER", "ezuser" );
  42  
  43  class eZUserType extends eZDataType
  44  {
  45      function eZUserType( )
  46      {
  47          $this->eZDataType( EZ_DATATYPESTRING_USER, ezi18n( 'kernel/classes/datatypes', "User account", 'Datatype name' ),
  48                             array( 'translation_allowed' => false,
  49                                    'serialize_supported' => true ) );
  50      }
  51  
  52      /*!
  53       Delete stored object attribute
  54      */
  55      function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null )
  56      {
  57          $db =& eZDB::instance();
  58          $userID = $contentObjectAttribute->attribute( "contentobject_id" );
  59  
  60          $res = $db->arrayQuery( "SELECT COUNT(*) AS version_count FROM ezcontentobject_version WHERE contentobject_id = $userID" );
  61          $versionCount = $res[0]['version_count'];
  62  
  63          if ( $version == null || $versionCount <= 1 )
  64          {
  65              eZUser::removeUser( $userID );
  66              $db->query( "DELETE FROM ezuser_role WHERE contentobject_id = '$userID'" );
  67          }
  68      }
  69  
  70      /*!
  71       Validates the input and returns true if the input was
  72       valid for this datatype.
  73      */
  74      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  75      {
  76          if ( $http->hasPostVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) ) )
  77          {
  78              $classAttribute =& $contentObjectAttribute->contentClassAttribute();
  79              $loginName = $http->postVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) );
  80              $email = $http->postVariable( $base . "_data_user_email_" . $contentObjectAttribute->attribute( "id" ) );
  81              $password = $http->postVariable( $base . "_data_user_password_" . $contentObjectAttribute->attribute( "id" ) );
  82              $passwordConfirm = $http->postVariable( $base . "_data_user_password_confirm_" . $contentObjectAttribute->attribute( "id" ) );
  83              if ( trim( $loginName ) == '' )
  84              {
  85                  if ( $contentObjectAttribute->validateIsRequired() || trim( $email ) != '' )
  86                  {
  87                      $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  88                                                                           'The username must be specified.' ) );
  89                      return EZ_INPUT_VALIDATOR_STATE_INVALID;
  90                  }
  91              }
  92              else
  93              {
  94                  $existUser = eZUser::fetchByName( $loginName );
  95                  if ( $existUser != null )
  96                  {
  97                      $userID = $existUser->attribute( 'contentobject_id' );
  98                      if ( $userID !=  $contentObjectAttribute->attribute( "contentobject_id" ) )
  99                      {
 100                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 101                                                                               'The username already exists, please choose another one.' ) );
 102                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 103                      }
 104                  }
 105                  $isValidate = eZMail::validate( $email );
 106                  if ( !$isValidate )
 107                  {
 108                      $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 109                                                                           'The email address is not valid.' ) );
 110                      return EZ_INPUT_VALIDATOR_STATE_INVALID;
 111                  }
 112  
 113                  $authenticationMatch = eZUser::authenticationMatch();
 114                  if ( $authenticationMatch & EZ_USER_AUTHENTICATE_EMAIL )
 115                  {
 116                      if ( eZUser::requireUniqueEmail() )
 117                      {
 118                          $userByEmail = eZUser::fetchByEmail( $email );
 119                          if ( $userByEmail != null )
 120                          {
 121                              $userID = $userByEmail->attribute( 'contentobject_id' );
 122                              if ( $userID !=  $contentObjectAttribute->attribute( "contentobject_id" ) )
 123                              {
 124                                  $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 125                                                                                       'A user with this email already exists.' ) );
 126                                  return EZ_INPUT_VALIDATOR_STATE_INVALID;
 127                              }
 128                          }
 129                      }
 130                  }
 131                  $ini =& eZINI::instance();
 132                  $generatePasswordIfEmpty = $ini->variable( "UserSettings", "GeneratePasswordIfEmpty" ) == 'true';
 133                  if ( !$generatePasswordIfEmpty || ( $password != "" ) )
 134                  {
 135                      if ( ( $password != $passwordConfirm ) || ( $password == "" ) )
 136                      {
 137                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 138                                                                               'The passwords do not match.',
 139                                                                               'eZUserType' ) );
 140                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 141                      }
 142                      $minPasswordLength = $ini->hasVariable( 'UserSettings', 'MinPasswordLength' ) ? $ini->variable( 'UserSettings', 'MinPasswordLength' ) : 3;
 143  
 144                      if ( strlen( $password ) < (int) $minPasswordLength )
 145                      {
 146                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 147                                                                               'The password must be at least %1 characters long.',null, array( $minPasswordLength ) ) );
 148                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 149                      }
 150                      if ( strtolower( $password ) == 'password' )
 151                      {
 152                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 153                                                                               'The password mustn\'t be "password".' ) );
 154                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 155                      }
 156                  }
 157              }
 158          }
 159          return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 160      }
 161  
 162      /*!
 163       Fetches the http post var integer input and stores it in the data instance.
 164      */
 165      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 166      {
 167          if ( $http->hasPostVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) ) )
 168          {
 169              $login = $http->postVariable( $base . "_data_user_login_" . $contentObjectAttribute->attribute( "id" ) );
 170              $email = $http->postVariable( $base . "_data_user_email_" . $contentObjectAttribute->attribute( "id" ) );
 171              $password = $http->postVariable( $base . "_data_user_password_" . $contentObjectAttribute->attribute( "id" ) );
 172              $passwordConfirm = $http->postVariable( $base . "_data_user_password_confirm_" . $contentObjectAttribute->attribute( "id" ) );
 173  
 174              $contentObjectID = $contentObjectAttribute->attribute( "contentobject_id" );
 175  
 176              $user =& $contentObjectAttribute->content();
 177              if ( $user === null )
 178              {
 179                  $user = eZUser::create( $contentObjectID );
 180              }
 181  
 182              $ini =& eZINI::instance();
 183              $generatePasswordIfEmpty = $ini->variable( "UserSettings", "GeneratePasswordIfEmpty" );
 184              if (  $password == "" )
 185              {
 186                  if ( $generatePasswordIfEmpty == 'true' )
 187                  {
 188                      $passwordLength = $ini->variable( "UserSettings", "GeneratePasswordLength" );
 189                      $password = $user->createPassword( $passwordLength );
 190                      $passwordConfirm = $password;
 191                      $http->setSessionVariable( "GeneratedPassword", $password );
 192                  }
 193                  else
 194                  {
 195                      $password = null;
 196                  }
 197              }
 198  
 199              eZDebugSetting::writeDebug( 'kernel-user', $password, "password" );
 200              eZDebugSetting::writeDebug( 'kernel-user', $passwordConfirm, "passwordConfirm" );
 201              eZDebugSetting::writeDebug( 'kernel-user', $login, "login" );
 202              eZDebugSetting::writeDebug( 'kernel-user', $email, "email" );
 203              eZDebugSetting::writeDebug( 'kernel-user', $contentObjectID, "contentObjectID" );
 204              if ( $password == "_ezpassword" )
 205              {
 206                  $password = false;
 207                  $passwordConfirm = false;
 208              }
 209              else
 210                  $http->setSessionVariable( "GeneratedPassword", $password );
 211  
 212              eZDebugSetting::writeDebug( 'kernel-user', "setInformation run", "ezusertype" );
 213              $user->setInformation( $contentObjectID, $login, $email, $password, $passwordConfirm );
 214              $contentObjectAttribute->setContent( $user );
 215              return true;
 216          }
 217          return false;
 218      }
 219  
 220      function storeObjectAttribute( &$contentObjectAttribute )
 221      {
 222          $user =& $contentObjectAttribute->content();
 223          if ( get_class( $user ) != "ezuser" )
 224          {
 225              // create a default user account
 226              $user = eZUser::create( $contentObjectAttribute->attribute( "contentobject_id" ) );
 227              $userID = $contentObjectAttribute->attribute( "contentobject_id" );
 228              $isEnabled = 1;
 229              $userSetting = eZUserSetting::create( $userID, $isEnabled );
 230              $userSetting->store();
 231          }
 232          $user->store();
 233          $contentObjectAttribute->setContent( $user );
 234      }
 235  
 236      /*!
 237       Returns the object title.
 238      */
 239      function title( &$contentObjectAttribute, $name = "login" )
 240      {
 241          $user = $this->objectAttributeContent( $contentObjectAttribute );
 242  
 243          $value = $user->attribute( $name );
 244  
 245          return $value;
 246      }
 247  
 248      function hasObjectAttributeContent( &$contentObjectAttribute )
 249      {
 250          $user = $this->objectAttributeContent( $contentObjectAttribute );
 251          if ( is_object( $user ) and
 252               $user->isEnabled() )
 253              return true;
 254          return false;
 255      }
 256  
 257      /*!
 258       Returns the user object.
 259      */
 260      function &objectAttributeContent( &$contentObjectAttribute )
 261      {
 262          $userID = $contentObjectAttribute->attribute( "contentobject_id" );
 263          $user =& $GLOBALS['eZUserObject_' . $userID];
 264          if ( !isset( $user ) or
 265               get_class( $user ) != 'ezuser' )
 266              $user = eZUser::fetch( $userID );
 267          eZDebugSetting::writeDebug( 'kernel-user', $user, 'user' );
 268          return $user;
 269      }
 270  
 271      /*!
 272       \reimp
 273      */
 274      function isIndexable()
 275      {
 276          return true;
 277      }
 278  
 279      /*!
 280       \reimp
 281       We can only remove the user attribute if:
 282       - The current user, anonymous user and administrator use is not using this class
 283       - There are more classes with the ezuser datatype
 284      */
 285      function classAttributeRemovableInformation( &$contentClassAttribute, $includeAll = true )
 286      {
 287          $result  = array( 'text' => ezi18n( 'kernel/classes/datatypes',
 288                                              "Cannot remove the account:" ),
 289                            'list' => array() );
 290          $reasons =& $result['list'];
 291  
 292          $currentUser =& eZUser::currentUser();
 293          $userObject  =& $currentUser->attribute( 'contentobject' );
 294          $ini         =& eZINI::instance();
 295          $anonID      = (int)$ini->variable( 'UserSettings', 'AnonymousUserID' );
 296          $classID     = (int)$contentClassAttribute->attribute( 'contentclass_id' );
 297          $db          =& eZDB::instance();
 298  
 299          if ( $classID == $userObject->attribute( 'contentclass_id' ) )
 300          {
 301              $reasons[] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
 302                                                    "The account owner is currently logged in." ) );
 303              if ( !$includeAll )
 304                  return $result;
 305          }
 306  
 307          $sql = "SELECT id FROM ezcontentobject WHERE id = $anonID AND contentclass_id = $classID";
 308          $rows = $db->arrayQuery( $sql );
 309          if ( count( $rows ) > 0 )
 310          {
 311              $reasons[] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
 312                                                    "The account is currently used by the anonymous user." ) );
 313              if ( !$includeAll )
 314                  return $result;
 315          }
 316  
 317          $sql = "SELECT ezco.id FROM ezcontentobject ezco, ezuser
 318   WHERE ezco.contentclass_id = $classID AND
 319         ezuser.login = 'admin' AND
 320         ezco.id = ezuser.contentobject_id ";
 321          $rows = $db->arrayQuery( $sql );
 322          if ( count( $rows ) > 0 )
 323          {
 324              $reasons[] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
 325                                                    "The account is currenty used the administrator user." ) );
 326              if ( !$includeAll )
 327                  return $result;
 328          }
 329  
 330          $sql = "SELECT count( ezcc.id ) AS count FROM ezcontentclass ezcc, ezcontentclass_attribute ezcca
 331   WHERE ezcc.id != $classID AND
 332         ezcca.data_type_string = 'ezuser' AND
 333         ezcc.id = ezcca.contentclass_id ";
 334          $rows = $db->arrayQuery( $sql );
 335          if ( $rows[0]['count'] == 0 )
 336          {
 337              $reasons[] = array( 'text' => ezi18n( 'kernel/classes/datatypes',
 338                                                    "You can not remove the last class holding user accounts." ) );
 339              if ( !$includeAll )
 340                  return $result;
 341          }
 342  
 343          return $result;
 344      }
 345  
 346      /*!
 347       Returns the meta data used for storing search indeces.
 348      */
 349      function metaData( $contentObjectAttribute )
 350      {
 351          $metaString = "";
 352          $user =& $contentObjectAttribute->content();
 353  
 354          if ( get_class( $user ) == "ezuser" )
 355          {
 356              // create a default user account
 357              $metaString .= $user->attribute( 'login' ) . " ";
 358              $metaString .= $user->attribute( 'email' ) . " ";
 359          }
 360          return $metaString;
 361      }
 362  
 363      function toString( $contentObjectAttribute )
 364      {
 365          $userID = $contentObjectAttribute->attribute( "contentobject_id" );
 366          $user =& $GLOBALS['eZUserObject_' . $userID];
 367          if ( !isset( $user ) or
 368               get_class( $user ) != 'ezuser' )
 369              $user = eZUser::fetch( $userID );
 370  
 371          return implode( '|', array( $user->attribute( 'login' ),
 372                                      $user->attribute( 'email' ),
 373                                      $user->attribute( 'password_hash' ),
 374                                      eZUser::passwordHashTypeName( $user->attribute( 'password_hash_type' ) )  ) );
 375      }
 376  
 377  
 378      function fromString( &$contentObjectAttribute, $string )
 379      {
 380          if ( $string == '' )
 381              return true;
 382          $userData = explode( '|', $string );
 383          if( count( $userData ) < 2 )
 384              return false;
 385          $login = $userData[0];
 386          $email = $userData[1];
 387  
 388          if ( eZUser::fetchByName( $login ) || eZUser::fetchByEmail( $email ) )
 389              return false;
 390  
 391          $user = eZUser::create( $contentObjectAttribute->attribute( 'contentobject_id' ) );
 392  
 393          $user->setAttribute( 'login', $userNode->attributeValue( 'login' ) );
 394          $user->setAttribute( 'email', $userNode->attributeValue( 'email' ) );
 395          if ( isset( $userData[2] ) )
 396              $user->setAttribute( 'password_hash', $userNode->attributeValue( 'password_hash' ) );
 397  
 398          if ( isset( $userData[3] ) )
 399              $user->setAttribute( 'password_hash_type', eZUser::passwordHashTypeID( $userNode->attributeValue( 'passsword_hash_type' ) ) );
 400          $user->store();
 401          return $user;
 402      }
 403  
 404      /*!
 405       \param package
 406       \param content attribute
 407  
 408       \return a DOM representation of the content object attribute
 409      */
 410      function serializeContentObjectAttribute( &$package, &$objectAttribute )
 411      {
 412          $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
 413          $userID = $objectAttribute->attribute( "contentobject_id" );
 414          $user = eZUser::fetch( $userID );
 415          if ( is_object( $user ) )
 416          {
 417              $userNode = eZDOMDocument::createElementNode( 'account' );
 418              $userNode->appendAttribute( eZDOMDocument::createAttributeNode( 'login', $user->attribute( 'login' ) ) );
 419              $userNode->appendAttribute( eZDOMDocument::createAttributeNode( 'email', $user->attribute( 'email' ) ) );
 420              $userNode->appendAttribute( eZDOMDocument::createAttributeNode( 'password_hash', $user->attribute( 'password_hash' ) ) );
 421              $userNode->appendAttribute( eZDOMDocument::createAttributeNode( 'password_hash_type', eZUser::passwordHashTypeName( $user->attribute( 'password_hash_type' ) ) ) );
 422              $node->appendChild( $userNode );
 423          }
 424  
 425          return $node;
 426      }
 427  
 428      /*!
 429       \reimp
 430       \param package
 431       \param contentobject attribute object
 432       \param ezdomnode object
 433      */
 434      function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
 435      {
 436          $userNode = $attributeNode->elementByName( 'account' );
 437          if ( is_object( $userNode ) )
 438          {
 439              $userID = $objectAttribute->attribute( 'contentobject_id' );
 440              $user = eZUser::fetch( $userID );
 441              if ( !is_object( $user ) )
 442              {
 443                  $user = eZUser::create( $userID );
 444              }
 445              $user->setAttribute( 'login', $userNode->attributeValue( 'login' ) );
 446              $user->setAttribute( 'email', $userNode->attributeValue( 'email' ) );
 447              $user->setAttribute( 'password_hash', $userNode->attributeValue( 'password_hash' ) );
 448              $user->setAttribute( 'password_hash_type', eZUser::passwordHashTypeID( $userNode->attributeValue( 'passsword_hash_type' ) ) );
 449              $user->store();
 450          }
 451      }
 452  }
 453  
 454  eZDataType::register( EZ_DATATYPESTRING_USER, "ezusertype" );
 455  
 456  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7