|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Core\Service; |
|
5
|
|
|
|
|
6
|
|
|
use Ramsey\Uuid\Uuid; |
|
7
|
|
|
use Core\Mapper\AdminUsersMapper; |
|
8
|
|
|
use Zend\Db\TableGateway\TableGateway; |
|
9
|
|
|
use Zend\Db\Sql\Predicate\Expression; |
|
10
|
|
|
use Zend\Paginator\Adapter\DbSelect; |
|
11
|
|
|
use Zend\Paginator\Paginator; |
|
12
|
|
|
use Zend\Crypt\Password\Bcrypt; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AdminUserService. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Core\Service |
|
18
|
|
|
*/ |
|
19
|
|
|
class AdminUserService |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var AdminUsersMapper |
|
23
|
|
|
*/ |
|
24
|
|
|
private $adminUsersMapper; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Bcrypt |
|
28
|
|
|
*/ |
|
29
|
|
|
private $crypt; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* AdminUserService constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Bcrypt $crypt bcrypt password encryption method |
|
35
|
|
|
* @param AdminUsersMapper $adminUsersMapper mapper for admin us |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Bcrypt $crypt, AdminUsersMapper $adminUsersMapper) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->crypt = $crypt; |
|
40
|
|
|
$this->adminUsersMapper = $adminUsersMapper; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Performs user login or throws exception if credentials are not valid. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $email user email |
|
47
|
|
|
* @param string $password user password |
|
48
|
|
|
* @return array|\ArrayObject|null |
|
49
|
|
|
* @throws \Exception if user does not exist or password is not valid |
|
50
|
|
|
*/ |
|
51
|
|
|
public function loginUser($email, $password) |
|
52
|
|
|
{ |
|
53
|
|
|
$user = $this->adminUsersMapper->getByEmail($email); |
|
54
|
|
|
|
|
55
|
|
|
if(!$user){ |
|
56
|
|
|
throw new \Exception('User does not exist.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if(!$this->crypt->verify($password, $user->password)){ |
|
60
|
|
|
throw new \Exception('Password does not match.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if($user->status != 1){ |
|
64
|
|
|
throw new \Exception('User is not active.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->adminUsersMapper->updateLogin($user->admin_user_uuid); |
|
68
|
|
|
|
|
69
|
|
|
return $user; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return pagination object to paginate results on view |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $page Current page set to pagination to display |
|
76
|
|
|
* @param int $limit Limit set to pagination |
|
77
|
|
|
* @param string $userId UUID from DB |
|
78
|
|
|
* @return Paginator |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getPagination($page, $limit, $userId) |
|
81
|
|
|
{ |
|
82
|
|
|
$select = $this->adminUsersMapper->getPaginationSelect($userId); |
|
83
|
|
|
$paginatorAdapter = new DbSelect($select, $this->adminUsersMapper->getAdapter()); |
|
|
|
|
|
|
84
|
|
|
$paginator = new Paginator($paginatorAdapter); |
|
85
|
|
|
|
|
86
|
|
|
$paginator->setCurrentPageNumber($page); |
|
87
|
|
|
$paginator->setItemCountPerPage($limit); |
|
88
|
|
|
|
|
89
|
|
|
return $paginator; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return one user for given UUID |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $userId UUID from DB |
|
96
|
|
|
* @return array|\ArrayObject|null |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getUser($userId) |
|
99
|
|
|
{ |
|
100
|
|
|
$user = $this->adminUsersMapper->get($userId); |
|
101
|
|
|
|
|
102
|
|
|
return $user; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Update or Insert user. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Array $data Data from POST |
|
109
|
|
|
* @param null $userId UUID of user if we want to edit or 0 to add new user |
|
110
|
|
|
* @throws \Exception |
|
111
|
|
|
*/ |
|
112
|
|
|
public function save($data, $userId = 0) |
|
113
|
|
|
{ |
|
114
|
|
|
//@todo Validate data |
|
115
|
|
|
if($data['password'] == ''){ |
|
116
|
|
|
unset($data['password']); |
|
117
|
|
|
} |
|
118
|
|
|
else{ |
|
119
|
|
|
$data['password'] = $this->crypt->create($data['password']); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if($userId){ |
|
123
|
|
|
$this->adminUsersMapper->update($data, ['admin_user_uuid' => $userId]); |
|
124
|
|
|
} |
|
125
|
|
|
else{ |
|
126
|
|
|
$data['admin_user_uuid'] = Uuid::uuid1()->toString(); |
|
127
|
|
|
$this->adminUsersMapper->insert($data); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Delete user by given UUID |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $userId UUID from DB |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function delete($userId) |
|
138
|
|
|
{ |
|
139
|
|
|
return (bool)$this->adminUsersMapper->delete(['admin_user_uuid' => $userId]); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: