1 | <?php |
||
5 | class UserService |
||
6 | { |
||
7 | private $userStorage; |
||
8 | private $session; |
||
9 | |||
10 | |||
11 | |||
12 | /** |
||
13 | * Constructor for UserService |
||
14 | * @param object $di dependency injection. |
||
15 | */ |
||
16 | 17 | public function __construct(\Anax\DI\DIFactoryConfig $di) |
|
22 | |||
23 | |||
24 | |||
25 | /** |
||
26 | * Create user. |
||
27 | * |
||
28 | * @param object $user User object to store. |
||
29 | * @return void |
||
30 | */ |
||
31 | 1 | public function createUser(User $user) |
|
41 | |||
42 | |||
43 | |||
44 | /** |
||
45 | * Update user. |
||
46 | * |
||
47 | * @param object $user User object to update. |
||
48 | * @return void |
||
49 | */ |
||
50 | 2 | public function updateUser($user) |
|
54 | |||
55 | |||
56 | |||
57 | /** |
||
58 | * Delete user. Validates if user is admin to be able to delete |
||
59 | * |
||
60 | * @param integer $id user id. |
||
61 | * |
||
62 | * @return boolean |
||
63 | */ |
||
64 | 2 | public function deleteUser($id) |
|
71 | |||
72 | |||
73 | |||
74 | /** |
||
75 | * Dynamicly get user by propertie. |
||
76 | * |
||
77 | * @param string $field field to search by. |
||
78 | * |
||
79 | * @param array $data to search for. |
||
80 | * |
||
81 | * @return User |
||
82 | * |
||
83 | */ |
||
84 | 10 | public function getUserByField($field, $data) |
|
85 | { |
||
86 | 10 | $user = new User(); |
|
87 | 10 | $userVarArray = get_object_vars($user); |
|
88 | 10 | $arrayKeys = array_keys($userVarArray); |
|
89 | 10 | $userData = $this->userStorage->getUserByField($field, $data); |
|
90 | 10 | if (empty($userData)) { |
|
91 | 2 | return $user; |
|
92 | } |
||
93 | 9 | foreach ($arrayKeys as $key) { |
|
94 | 9 | $user->{$key} = $userData->$key; |
|
95 | 9 | } |
|
96 | 9 | return $user; |
|
97 | } |
||
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * Find all users stored. |
||
103 | * |
||
104 | * @return array Of users |
||
105 | */ |
||
106 | 2 | public function findAllUsers() |
|
110 | |||
111 | |||
112 | |||
113 | /** |
||
114 | * Check if user is logged in. |
||
115 | * |
||
116 | * @return boolean |
||
117 | */ |
||
118 | 2 | public function checkLoggedin() |
|
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * Login user and redirect to admin. |
||
127 | * |
||
128 | * @return boolean |
||
129 | */ |
||
130 | 2 | public function login($username, $password) |
|
131 | { |
||
132 | 2 | $user = $this->getUserByField("username", $username); |
|
133 | |||
134 | 2 | if ($password === null) { |
|
135 | 2 | throw new Exception("Empty password field."); |
|
136 | } |
||
137 | |||
138 | 1 | if ($user->id === null) { |
|
139 | 1 | throw new Exception("Error, not valid credentials."); |
|
140 | } |
||
141 | |||
142 | 1 | if ($user->deleted !== null) { |
|
143 | throw new Exception("User deleted."); |
||
144 | } |
||
145 | |||
146 | 1 | if ((int)$user->enabled === 0) { |
|
147 | 1 | throw new Exception("Error, disabled account."); |
|
148 | } |
||
149 | |||
150 | 1 | if ($this->validatePassword($password, $user->password)) { |
|
151 | 1 | $this->session->set("user", $user); |
|
152 | 1 | return true; |
|
153 | } |
||
154 | 1 | throw new Exception("Error, not valid credentials."); |
|
155 | } |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * Check if a user is logged in and returns that user |
||
161 | * |
||
162 | * @return obj user or null |
||
163 | */ |
||
164 | 6 | public function getCurrentLoggedInUser() |
|
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * Validate pasword |
||
173 | * |
||
174 | * @method password_verify Method to verify password |
||
175 | * |
||
176 | * @param string $password Password to be validated. |
||
177 | * |
||
178 | * @return boolean Return true if valid else false. |
||
179 | */ |
||
180 | 1 | private function validatePassword($password, $dbpassword) |
|
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * Check if logged in user is valid and admin. |
||
189 | * |
||
190 | * @return boolean Returns true or false if user is valid administrator. |
||
191 | */ |
||
192 | 2 | public function validLoggedInAdmin() |
|
203 | |||
204 | |||
205 | |||
206 | /** |
||
207 | * Generate gravatar from email or return default avatar. |
||
208 | * |
||
209 | * @param string $email email adress |
||
210 | * @return string Gravatar url. |
||
211 | */ |
||
212 | 1 | public function generateGravatarUrl($email = "") |
|
219 | } |
||
220 |