/
AuthPluginBase.php
138 lines (121 loc) · 3.56 KB
/
AuthPluginBase.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
abstract class AuthPluginBase extends PluginBase {
/**
* These constants reflect the error codes to be used by the identity, they
* are copied from LSUserIdentity and CBaseUserIdentity for easier access.
*/
const ERROR_NONE = 0;
const ERROR_USERNAME_INVALID = 1;
const ERROR_PASSWORD_INVALID = 2;
const ERROR_IP_LOCKED_OUT = 98;
const ERROR_UNKNOWN_HANDLER = 99;
const ERROR_UNKNOWN_IDENTITY = 100;
protected $_username = null;
protected $_password = null;
/**
* Get the password (if set)
*
* @return string|null
*/
protected function getPassword()
{
return $this->_password;
}
/**
* Get the username (if set)
*
* @return string|null
*/
protected function getUserName()
{
return $this->_username;
}
/**
* Set username and password
*
* @return null
*/
public function afterLoginFormSubmit()
{
// Here we handle post data
$request = $this->api->getRequest();
if ($request->getIsPostRequest()) {
$this->setUsername( $request->getPost('user'));
$this->setPassword($request->getPost('password'));
}
}
/**
* Set authentication result to success for the given user object.
*
* @param User $user
* @return AuthPluginBase
*/
public function setAuthSuccess(User $user)
{
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->id = $user->uid;
$identity->user = $user;
$identity = $this->getEvent()->set('identity', $identity);
$event->set('result', new LSAuthResult(self::ERROR_NONE));
return $this;
}
/**
* Set authentication result to failure.
*
* @param int $code Any of the constants defined in this class
* @param string $message An optional message to return about the failure
* @return AuthPluginBase
*/
public function setAuthFailure($code = self::ERROR_UNKNOWN_IDENTITY, $message = '')
{
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->id = null;
$event->set('result', new LSAuthResult($code, $message));
return $this;
}
/**
* Set this plugin to handle the authentication
*
* @return AuthPluginBase
*/
public function setAuthPlugin()
{
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->plugin = get_class($this);
$this->getEvent()->stop();
return $this;
}
/**
* Set the password to use for authentication
*
* @param string $password
* @return AuthPluginBase
*/
protected function setPassword($password)
{
$this->_password = $password;
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->password = $password;
$event->set('identity', $identity);
return $this;
}
/**
* Set the username to use for authentication
*
* @param string $username The username
* @return AuthPluginBase
*/
protected function setUsername($username)
{
$this->_username = $username;
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->username = $username;
$event->set('identity', $identity);
return $this;
}
}