|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace WebHemi\Auth; |
|
14
|
|
|
|
|
15
|
|
|
use WebHemi\Data\Entity\User\UserEntity; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Result. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Result |
|
21
|
|
|
{ |
|
22
|
|
|
const FAILURE = 0; |
|
23
|
|
|
const FAILURE_IDENTITY_NOT_FOUND = -1; |
|
24
|
|
|
const FAILURE_CREDENTIAL_INVALID = -2; |
|
25
|
|
|
const FAILURE_OTHER = -3; |
|
26
|
|
|
const SUCCESS = 1; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
private $code; |
|
30
|
|
|
/** @var null|UserEntity */ |
|
31
|
|
|
private $userEntity; |
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
private $messages = [ |
|
34
|
|
|
self::FAILURE => 'Authentication failed.', |
|
35
|
|
|
self::FAILURE_IDENTITY_NOT_FOUND => 'User is not found.', |
|
36
|
|
|
self::FAILURE_CREDENTIAL_INVALID => 'The provided credentials are not valid.', |
|
37
|
|
|
self::FAILURE_OTHER => 'Authentication failed because of unknown reason.', |
|
38
|
|
|
self::SUCCESS => 'Authenticated.', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Checks the authentication result. |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function isValid() |
|
47
|
|
|
{ |
|
48
|
2 |
|
return $this->code == 1; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sets the result code. |
|
53
|
|
|
* |
|
54
|
|
|
* @param int $code |
|
55
|
|
|
* @return Result |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function setCode($code) |
|
58
|
|
|
{ |
|
59
|
3 |
|
if (!isset($this->messages[$code])) { |
|
60
|
2 |
|
$code = -3; |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
$this->code = $code; |
|
64
|
|
|
|
|
65
|
3 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets the result code. |
|
70
|
|
|
* |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
3 |
|
public function getCode() |
|
74
|
|
|
{ |
|
75
|
3 |
|
return $this->code; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the authenticated user. |
|
80
|
|
|
* |
|
81
|
|
|
* @param UserEntity $userEntity |
|
82
|
|
|
* @return Result |
|
83
|
|
|
*/ |
|
84
|
3 |
|
public function setIdentity(UserEntity $userEntity) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$this->userEntity = $userEntity; |
|
87
|
|
|
|
|
88
|
3 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Gets the authenticated user if any. |
|
93
|
|
|
* |
|
94
|
|
|
* @return null|UserEntity |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function getIdentity() |
|
97
|
|
|
{ |
|
98
|
1 |
|
return $this->userEntity; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets the result message. |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getMessage() |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->messages[$this->code]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|