forked from Nodge/yii-eauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EAuthUserIdentity.php
90 lines (78 loc) · 2.1 KB
/
EAuthUserIdentity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* EAuthUserIdentity class file.
*
* @author Maxim Zemskov <nodge@yandex.ru>
* @link http://github.com/Nodge/yii-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
/**
* EAuthUserIdentity is a base User Identity class to authenticate with EAuth.
*
* @package application.extensions.eauth
*/
class EAuthUserIdentity extends CBaseUserIdentity {
const ERROR_NOT_AUTHENTICATED = 3;
/**
* @var EAuthServiceBase the authorization service instance.
*/
protected $service;
/**
* @var string the unique identifier for the identity.
*/
protected $id;
/**
* @var string the display name for the identity.
*/
protected $name;
/**
* Constructor.
*
* @param EAuthServiceBase $service the authorization service instance.
*/
public function __construct($service) {
$this->service = $service;
}
/**
* Authenticates a user based on {@link service}.
* This method is required by {@link IUserIdentity}.
*
* @return boolean whether authentication succeeds.
*/
public function authenticate() {
if ($this->service->isAuthenticated) {
$this->id = $this->service->id;
$this->name = $this->service->getAttribute('name');
$this->setState('id', $this->id);
$this->setState('name', $this->name);
$this->setState('service', $this->service->serviceName);
// You can save all given attributes in session.
//$attributes = $this->service->getAttributes();
//$session = Yii::app()->session;
//$session['eauth_attributes'][$this->service->serviceName] = $attributes;
$this->errorCode = self::ERROR_NONE;
}
else {
$this->errorCode = self::ERROR_NOT_AUTHENTICATED;
}
return !$this->errorCode;
}
/**
* Returns the unique identifier for the identity.
* This method is required by {@link IUserIdentity}.
*
* @return string the unique identifier for the identity.
*/
public function getId() {
return $this->id;
}
/**
* Returns the display name for the identity.
* This method is required by {@link IUserIdentity}.
*
* @return string the display name for the identity.
*/
public function getName() {
return $this->name;
}
}