7 const ERROR_NOT_AUTHENTICATED = 3;
21 public $session = null;
23 public $account = null;
33 $this->service = $service;
43 if ($this->service->isAuthenticated) {
44 $id = $this->service->id;
45 $service = $this->service->serviceName;
47 $this->account =
Account::model()->findByAttributes(array(
'service' => $service,
'service_id' => $id));
48 if (!$this->account) {
49 $this->account = Account::create($this->service);
51 if (User::create($this->account, $this->service)) {
52 $this->onNewUser($this->account);
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;
64 $data = (object)array_merge((array)$this->account->data, $this->service->getAttributes());
65 $this->account->data = $data;
66 $this->account->save();
68 $profile =
Profile::model()->findByAttributes(array(
'user_id'=>$this->account->user->id));
70 if (!empty($data->firstname)) {
71 $profile->firstname = $data->firstname;
72 $this->account->user->profile->firstname = $data->firstname;
74 if (!empty($data->lastname)) {
75 $profile->lastname = $data->lastname;
76 $this->account->user->profile->lastname = $data->lastname;
78 if (!empty($data->gender)) {
79 $profile->gender = $data->gender;
80 $this->account->user->profile->gender = $data->gender;
83 if (!$profile->save(
false)) {
84 throw new Exception(print_r($profile->getErrors(),
true));
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,
96 $this->errorCode = self::ERROR_NONE;
98 $this->errorCode = self::ERROR_NOT_AUTHENTICATED;
102 $this->errorCode = self::ERROR_NOT_AUTHENTICATED;
104 return !$this->errorCode;
117 public function getErrorDescription()
120 switch ($this->errorCode)
122 case self::ERROR_NOT_AUTHENTICATED:
123 $error =
'Not authenticated';
132 public function onNewUser(
Account $account)
134 $message =
"User '{$account->user->username}' from TPA '{$account->service}' created";
136 Yii::app()->notificationManager->notifyAbout($notification);