Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
UserIdentity.php
1 <?php
2 
3 /**
4  * UserIdentity represents the data needed to identity a user.
5  * It contains the authentication method that checks if the provided
6  * data can identity the user.
7  */
8 class UserIdentity extends CUserIdentity
9 {
10  private $_id;
11 
12  public $email;
13 
14  public $firstname;
15 
16  public $lastname;
17 
18  public $gender;
19 
20  public $personalisation;
21 
22  /**
23  * @var string can be 'email' or 'username'. It defines what user field we will use for authentication
24  */
25 
26  const ERROR_AUTH_FIELD_INVALID = 3;
27  const ERROR_STATUS_NOTACTIV = 4;
28  const ERROR_STATUS_BAN = 5;
29 
30 
31  /**
32  * Authenticates a user.
33  * The example implementation makes sure if the username and password
34  * are both 'demo'.
35  * In practical applications, this should be changed to authenticate
36  * against some persistent user identity storage (e.g. database).
37  * @return boolean whether authentication succeeds.
38  */
39  public function authenticate()
40  {
41  if (Yii::app()->getModule('user')->authField == 'email') {
42  $user = User::model()->with('profile')->notsafe()->findByAttributes(array('email' => $this->username));
43  } else {
44  $user = User::model()->with('profile')->notsafe()->findByAttributes(array('username' => $this->username));
45  }
46 
47  if ($user === null)
48  $this->errorCode = self::ERROR_AUTH_FIELD_INVALID;
49  else if (Yii::app()->getModule('user')->encrypting($this->password) !== $user->password)
50  $this->errorCode = self::ERROR_PASSWORD_INVALID;
51  else if ($user->status == 0 && Yii::app()->getModule('user')->loginNotActiv == false)
52  $this->errorCode = self::ERROR_STATUS_NOTACTIV;
53  else if ($user->status == -1)
54  $this->errorCode = self::ERROR_STATUS_BAN;
55  else {
56  $this->_id = $user->id;
57  $this->email = $user->email;
58  $this->username = !empty($user->username) ? $user->username : $user->email;
59  $this->errorCode = self::ERROR_NONE;
60  Yii::app()->getModule('personalisation');
61  $this->setPersistentStates(array(
62  'email' => $this->email,
63  'firstname' => $user->profile->firstname,
64  'lastname' => $user->profile->lastname,
65  'gender' => $user->profile->gender,
66  'personalisation' => PersonalisationAttribute::flatListForUser($user->id)
67  ));
68 
69  }
70  return !$this->errorCode;
71  }
72 
73  /**
74  * @return integer the ID of the user record
75  */
76  public function getId()
77  {
78  return $this->_id;
79  }
80 
81 }