|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Service; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class ServiceLogic |
|
6
|
|
|
* |
|
7
|
|
|
* @package Service |
|
8
|
|
|
* @subpackage ServiceLogic |
|
9
|
|
|
* @author Dodonov A.A. |
|
10
|
|
|
* @version v.1.0 (2019/08/17) |
|
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class stores all service's logic |
|
16
|
|
|
* |
|
17
|
|
|
* @author Dodonov A.A. |
|
18
|
|
|
*/ |
|
19
|
|
|
class ServiceLogic extends \Mezon\Service\ServiceBaseLogic |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Method creates connection |
|
24
|
|
|
* |
|
25
|
|
|
* @return array session id |
|
26
|
|
|
*/ |
|
27
|
|
|
public function connect(): array |
|
28
|
|
|
{ |
|
29
|
|
|
$login = $this->getParam($this->securityProvider->getLoginFieldName(), false); |
|
|
|
|
|
|
30
|
|
|
$password = $this->getParam('password', false); |
|
31
|
|
|
|
|
32
|
|
|
if ($login === false || $password === false) { |
|
33
|
|
|
throw (new \Exception('Fields login and/or password were not set', - 1)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return [ |
|
37
|
|
|
$this->securityProvider->getSessionIdFieldName() => $this->securityProvider->connect($login, $password) |
|
|
|
|
|
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Method sets token |
|
43
|
|
|
* |
|
44
|
|
|
* @return array Session id |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setToken(): array |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
$this->securityProvider->getSessionIdFieldName() => $this->securityProvider->createSession( |
|
|
|
|
|
|
50
|
|
|
$this->getParam('token')) |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Method returns session user's id |
|
56
|
|
|
* |
|
57
|
|
|
* @return int Session user's id |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSelfId(): array |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
|
|
|
|
|
62
|
|
|
'id' => $this->getSelfIdValue() |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Method returns session user's login |
|
68
|
|
|
* |
|
69
|
|
|
* @return string Session user's login |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getSelfLogin(): array |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
|
|
|
|
|
74
|
|
|
$this->securityProvider->getLoginFieldName() => $this->getSelfLoginValue() |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Method returns session id |
|
80
|
|
|
* |
|
81
|
|
|
* @return string Session id |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getSessionId(): string |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getParam($this->securityProvider->getSessionIdFieldName()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Method allows to login under another user |
|
90
|
|
|
* |
|
91
|
|
|
* @return array Session id |
|
92
|
|
|
*/ |
|
93
|
|
|
public function loginAs(): array |
|
94
|
|
|
{ |
|
95
|
|
|
$loginFieldName = $this->securityProvider->getLoginFieldName(); |
|
96
|
|
|
|
|
97
|
|
|
// we can login using either user's login or id |
|
98
|
|
|
if (($loginOrId = $this->getParam($loginFieldName, '')) !== '') { |
|
99
|
|
|
// we are log in using login |
|
100
|
|
|
$loginFieldName = 'login'; |
|
101
|
|
|
} elseif (($loginOrId = $this->getParam('id', '')) !== '') { |
|
102
|
|
|
// we are log in using id |
|
103
|
|
|
$loginFieldName = 'id'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return [ |
|
107
|
|
|
$this->securityProvider->getSessionIdFieldName() => $this->securityProvider->loginAs( |
|
|
|
|
|
|
108
|
|
|
$this->getSessionId(), |
|
109
|
|
|
$loginOrId, |
|
110
|
|
|
$loginFieldName) |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Method returns self id |
|
116
|
|
|
* |
|
117
|
|
|
* @return int Session user's id |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getSelfIdValue(): int |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->securityProvider->getSelfId(); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Method returns self login |
|
126
|
|
|
* |
|
127
|
|
|
* @return string Session user's login |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getSelfLoginValue(): string |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->securityProvider->getSelfLogin(); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Checking does user has permit |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $permit |
|
138
|
|
|
* Permit to check |
|
139
|
|
|
* @return bool true or false if the session user has permit or not |
|
140
|
|
|
*/ |
|
141
|
|
|
public function hasPermit(string $permit): bool |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->securityProvider->hasPermit($this->getSessionId(), $permit); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* The same as hasPermit but throwing exception for session user no permit |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $permit |
|
150
|
|
|
* Permit name |
|
151
|
|
|
*/ |
|
152
|
|
|
public function validatePermit(string $permit) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->securityProvider->validatePermit($this->getSessionId(), $permit); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|