Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
SUserIdentity.php
1 <?php
2 /**
3  *
4  */
5 class SUserIdentity extends CUserIdentity
6 {
7  const ERROR_NOT_AUTHENTICATED = 3;
8 
9  /**
10  * @var EAuthServiceBase the authorization service instance.
11  */
12  protected $service;
13 
14  /**
15  * @var string the unique identifier for the identity.
16  */
17  protected $id;
18 
19  public $user = null;
20 
21  public $session = null;
22 
23  public $account = null;
24 
25  public $email;
26 
27  /**
28  * Constructor.
29  * @param EAuthServiceBase $service the authorization service instance.
30  */
31  public function __construct($service)
32  {
33  $this->service = $service;
34  }
35 
36  /**
37  * Authenticates a user based on {@link service}.
38  * This method is required by {@link IUserIdentity}.
39  * @return boolean whether authentication succeeds.
40  */
41  public function authenticate()
42  {
43  if ($this->service->isAuthenticated) {
44  $id = $this->service->id;
45  $service = $this->service->serviceName;
46 
47  $this->account = Account::model()->findByAttributes(array('service' => $service, 'service_id' => $id));
48  if (!$this->account) {
49  $this->account = Account::create($this->service);
50  if ($this->account) {
51  if (User::create($this->account, $this->service)) {
52  $this->onNewUser($this->account);
53  }
54  }
55  }
56 
57  if ($this->account->user->status == 1) {
58  $this->id = $this->account->user_id;
59  $this->username = $this->account->user->username;
60  $this->email = $this->account->user->email;
61 
62  /*echo __FILE__.'<br/>';*/
63  //if new data from service
64  $data = (object)array_merge((array)$this->account->data, $this->service->getAttributes());
65  $this->account->data = $data;
66  $this->account->save();
67 
68  $profile = Profile::model()->findByAttributes(array('user_id'=>$this->account->user->id));
69 
70  if (!empty($data->firstname)) {
71  $profile->firstname = $data->firstname;
72  $this->account->user->profile->firstname = $data->firstname;
73  }
74  if (!empty($data->lastname)) {
75  $profile->lastname = $data->lastname;
76  $this->account->user->profile->lastname = $data->lastname;
77  }
78  if (!empty($data->gender)) {
79  $profile->gender = $data->gender;
80  $this->account->user->profile->gender = $data->gender;
81  }
82 
83  if (!$profile->save(false)) {
84  throw new Exception(print_r($profile->getErrors(), true));
85  }
86 
87  Yii::app()->getModule('personalisation');
88  $this->setPersistentStates(array(
89  'email' => $this->email,
90  'firstname' => $this->account->user->profile->firstname,
91  'lastname' => $this->account->user->profile->lastname,
92  'gender' => $this->account->user->profile->gender,
93  'personalisation' => PersonalisationAttribute::flatListForUser($this->account->user->id)
94  ));
95 
96  $this->errorCode = self::ERROR_NONE;
97  } else {
98  $this->errorCode = self::ERROR_NOT_AUTHENTICATED;
99  }
100  }
101  else {
102  $this->errorCode = self::ERROR_NOT_AUTHENTICATED;
103  }
104  return !$this->errorCode;
105  }
106 
107  /**
108  * Returns the unique identifier for the identity.
109  * This method is required by {@link IUserIdentity}.
110  * @return string the unique identifier for the identity.
111  */
112  public function getId()
113  {
114  return $this->id;
115  }
116 
117  public function getErrorDescription()
118  {
119  $error = '';
120  switch ($this->errorCode)
121  {
122  case self::ERROR_NOT_AUTHENTICATED:
123  $error = 'Not authenticated';
124  break;
125  default:
126  break;
127  }
128 
129  return $error;
130  }
131 
132  public function onNewUser(Account $account)
133  {
134  $message = "User '{$account->user->username}' from TPA '{$account->service}' created";
135  $notification = new Notification('TPA', $message);
136  Yii::app()->notificationManager->notifyAbout($notification);
137  }
138 }