@@ -7,152 +7,152 @@ |
||
7 | 7 | |
8 | 8 | interface UserServiceInterface extends BaseServiceInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Return the logged in user account. |
|
12 | - * |
|
13 | - * @param array $relations |
|
14 | - * @return Model |
|
15 | - */ |
|
16 | - public function account(array $relations = ['roles.permissions']): Model; |
|
17 | - |
|
18 | - /** |
|
19 | - * Check if the logged in user or the given user |
|
20 | - * has the given permissions on the given model. |
|
21 | - * |
|
22 | - * @param string $permissionName |
|
23 | - * @param string $model |
|
24 | - * @param int $userId |
|
25 | - * @return bool |
|
26 | - */ |
|
27 | - public function can(string $permissionName, string $model, int $userId = 0): bool; |
|
28 | - |
|
29 | - /** |
|
30 | - * Check if the logged in or the given user has the given role. |
|
31 | - * |
|
32 | - * @param array $roles |
|
33 | - * @param int $user |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - public function hasRoles(array $roles, int $user = 0): bool; |
|
37 | - |
|
38 | - /** |
|
39 | - * Assign the given role ids to the given user. |
|
40 | - * |
|
41 | - * @param int $userId |
|
42 | - * @param array $roleIds |
|
43 | - * @return Model |
|
44 | - */ |
|
45 | - public function assignRoles(int $userId, array $roleIds): Model; |
|
46 | - |
|
47 | - /** |
|
48 | - * Handle the login request to the application. |
|
49 | - * |
|
50 | - * @param string $email |
|
51 | - * @param string $password |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function login(string $email, string $password): array; |
|
55 | - |
|
56 | - /** |
|
57 | - * Handle the social login request to the application. |
|
58 | - * |
|
59 | - * @param string $authCode |
|
60 | - * @param string $accessToken |
|
61 | - * @param string $type |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - public function loginSocial(string $authCode, string $accessToken, string $type): array; |
|
65 | - |
|
66 | - /** |
|
67 | - * Handle the registration request. |
|
68 | - * |
|
69 | - * @param array $data |
|
70 | - * @param bool $skipConfirmEmail |
|
71 | - * @param int $roleId |
|
72 | - * @return Model |
|
73 | - */ |
|
74 | - public function register(array $data, bool $skipConfirmEmail = false, int $roleId = 0): Model; |
|
75 | - |
|
76 | - /** |
|
77 | - * Block the user. |
|
78 | - * |
|
79 | - * @param int $userId |
|
80 | - * @return Model |
|
81 | - */ |
|
82 | - public function block(int $userId): Model; |
|
83 | - |
|
84 | - /** |
|
85 | - * Unblock the user. |
|
86 | - * |
|
87 | - * @param int $userId |
|
88 | - * @return Model |
|
89 | - */ |
|
90 | - public function unblock(int $userId): Model; |
|
91 | - |
|
92 | - /** |
|
93 | - * Send a reset link to the given user. |
|
94 | - * |
|
95 | - * @param string $email |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public function sendReset(string $email): bool; |
|
99 | - |
|
100 | - /** |
|
101 | - * Reset the given user's password. |
|
102 | - * |
|
103 | - * @param string $email |
|
104 | - * @param string $password |
|
105 | - * @param string $passwordConfirmation |
|
106 | - * @param string $token |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function resetPassword(string $email, string $password, string $passwordConfirmation, string $token): string; |
|
110 | - |
|
111 | - /** |
|
112 | - * Change the logged in user password. |
|
113 | - * |
|
114 | - * @param string $password |
|
115 | - * @param string $oldPassword |
|
116 | - * @return bool |
|
117 | - */ |
|
118 | - public function changePassword(string $password, string $oldPassword): bool; |
|
119 | - |
|
120 | - /** |
|
121 | - * Confirm email using the confirmation code. |
|
122 | - * |
|
123 | - * @param string $confirmationCode |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function confirmEmail(string $confirmationCode): bool; |
|
127 | - |
|
128 | - /** |
|
129 | - * Send the confirmation mail. |
|
130 | - * |
|
131 | - * @param string $email |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - public function sendConfirmationEmail(string $email): bool; |
|
135 | - |
|
136 | - /** |
|
137 | - * Save the given data to the logged in user. |
|
138 | - * |
|
139 | - * @param array $data |
|
140 | - * @return Model |
|
141 | - */ |
|
142 | - public function saveProfile(array $data): Model; |
|
143 | - |
|
144 | - /** |
|
145 | - * Logs out the user, revoke access token and refresh token. |
|
146 | - * |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function logout(): bool; |
|
150 | - |
|
151 | - /** |
|
152 | - * Attempt to refresh the access token using the given refresh token. |
|
153 | - * |
|
154 | - * @param string $refreshToken |
|
155 | - * @return array |
|
156 | - */ |
|
157 | - public function refreshToken(string $refreshToken): array; |
|
10 | + /** |
|
11 | + * Return the logged in user account. |
|
12 | + * |
|
13 | + * @param array $relations |
|
14 | + * @return Model |
|
15 | + */ |
|
16 | + public function account(array $relations = ['roles.permissions']): Model; |
|
17 | + |
|
18 | + /** |
|
19 | + * Check if the logged in user or the given user |
|
20 | + * has the given permissions on the given model. |
|
21 | + * |
|
22 | + * @param string $permissionName |
|
23 | + * @param string $model |
|
24 | + * @param int $userId |
|
25 | + * @return bool |
|
26 | + */ |
|
27 | + public function can(string $permissionName, string $model, int $userId = 0): bool; |
|
28 | + |
|
29 | + /** |
|
30 | + * Check if the logged in or the given user has the given role. |
|
31 | + * |
|
32 | + * @param array $roles |
|
33 | + * @param int $user |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + public function hasRoles(array $roles, int $user = 0): bool; |
|
37 | + |
|
38 | + /** |
|
39 | + * Assign the given role ids to the given user. |
|
40 | + * |
|
41 | + * @param int $userId |
|
42 | + * @param array $roleIds |
|
43 | + * @return Model |
|
44 | + */ |
|
45 | + public function assignRoles(int $userId, array $roleIds): Model; |
|
46 | + |
|
47 | + /** |
|
48 | + * Handle the login request to the application. |
|
49 | + * |
|
50 | + * @param string $email |
|
51 | + * @param string $password |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function login(string $email, string $password): array; |
|
55 | + |
|
56 | + /** |
|
57 | + * Handle the social login request to the application. |
|
58 | + * |
|
59 | + * @param string $authCode |
|
60 | + * @param string $accessToken |
|
61 | + * @param string $type |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + public function loginSocial(string $authCode, string $accessToken, string $type): array; |
|
65 | + |
|
66 | + /** |
|
67 | + * Handle the registration request. |
|
68 | + * |
|
69 | + * @param array $data |
|
70 | + * @param bool $skipConfirmEmail |
|
71 | + * @param int $roleId |
|
72 | + * @return Model |
|
73 | + */ |
|
74 | + public function register(array $data, bool $skipConfirmEmail = false, int $roleId = 0): Model; |
|
75 | + |
|
76 | + /** |
|
77 | + * Block the user. |
|
78 | + * |
|
79 | + * @param int $userId |
|
80 | + * @return Model |
|
81 | + */ |
|
82 | + public function block(int $userId): Model; |
|
83 | + |
|
84 | + /** |
|
85 | + * Unblock the user. |
|
86 | + * |
|
87 | + * @param int $userId |
|
88 | + * @return Model |
|
89 | + */ |
|
90 | + public function unblock(int $userId): Model; |
|
91 | + |
|
92 | + /** |
|
93 | + * Send a reset link to the given user. |
|
94 | + * |
|
95 | + * @param string $email |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public function sendReset(string $email): bool; |
|
99 | + |
|
100 | + /** |
|
101 | + * Reset the given user's password. |
|
102 | + * |
|
103 | + * @param string $email |
|
104 | + * @param string $password |
|
105 | + * @param string $passwordConfirmation |
|
106 | + * @param string $token |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function resetPassword(string $email, string $password, string $passwordConfirmation, string $token): string; |
|
110 | + |
|
111 | + /** |
|
112 | + * Change the logged in user password. |
|
113 | + * |
|
114 | + * @param string $password |
|
115 | + * @param string $oldPassword |
|
116 | + * @return bool |
|
117 | + */ |
|
118 | + public function changePassword(string $password, string $oldPassword): bool; |
|
119 | + |
|
120 | + /** |
|
121 | + * Confirm email using the confirmation code. |
|
122 | + * |
|
123 | + * @param string $confirmationCode |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function confirmEmail(string $confirmationCode): bool; |
|
127 | + |
|
128 | + /** |
|
129 | + * Send the confirmation mail. |
|
130 | + * |
|
131 | + * @param string $email |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + public function sendConfirmationEmail(string $email): bool; |
|
135 | + |
|
136 | + /** |
|
137 | + * Save the given data to the logged in user. |
|
138 | + * |
|
139 | + * @param array $data |
|
140 | + * @return Model |
|
141 | + */ |
|
142 | + public function saveProfile(array $data): Model; |
|
143 | + |
|
144 | + /** |
|
145 | + * Logs out the user, revoke access token and refresh token. |
|
146 | + * |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function logout(): bool; |
|
150 | + |
|
151 | + /** |
|
152 | + * Attempt to refresh the access token using the given refresh token. |
|
153 | + * |
|
154 | + * @param string $refreshToken |
|
155 | + * @return array |
|
156 | + */ |
|
157 | + public function refreshToken(string $refreshToken): array; |
|
158 | 158 | } |
@@ -19,367 +19,367 @@ |
||
19 | 19 | |
20 | 20 | class UserService extends BaseService implements UserServiceInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * @var PermissionServiceInterface |
|
24 | - */ |
|
25 | - protected $permissionService; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var LoginProxy |
|
29 | - */ |
|
30 | - protected $loginProxy; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var NotificationServiceInterface |
|
34 | - */ |
|
35 | - protected $notificationService; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var OauthClientServiceInterface |
|
39 | - */ |
|
40 | - protected $oauthClientService; |
|
41 | - |
|
42 | - /** |
|
43 | - * Init new object. |
|
44 | - * |
|
45 | - * @param UserRepositoryInterface $repo |
|
46 | - * @param PermissionServiceInterface $permissionService |
|
47 | - * @param LoginProxy $loginProxy |
|
48 | - * @param NotificationServiceInterface $notificationService |
|
49 | - * @param OauthClientServiceInterface $oauthClientService |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - public function __construct( |
|
53 | - UserRepositoryInterface $repo, |
|
54 | - PermissionServiceInterface $permissionService, |
|
55 | - LoginProxy $loginProxy, |
|
56 | - NotificationServiceInterface $notificationService, |
|
57 | - OauthClientServiceInterface $oauthClientService |
|
58 | - ) { |
|
59 | - $this->permissionService = $permissionService; |
|
60 | - $this->loginProxy = $loginProxy; |
|
61 | - $this->notificationService = $notificationService; |
|
62 | - $this->oauthClientService = $oauthClientService; |
|
63 | - parent::__construct($repo); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Return the logged in user account. |
|
68 | - * |
|
69 | - * @param array $relations |
|
70 | - * @return Model |
|
71 | - */ |
|
72 | - public function account(array $relations = ['roles.permissions']): Model |
|
73 | - { |
|
74 | - $permissions = []; |
|
75 | - $user = $this->repo->find(Auth::id(), $relations); |
|
76 | - foreach ($user->roles as $role) { |
|
77 | - $role->permissions->each(function ($permission) use (&$permissions) { |
|
78 | - $permissions[] = $permission; |
|
79 | - }); |
|
80 | - } |
|
81 | - $user->permissions = $permissions; |
|
82 | - |
|
83 | - return $user; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Check if the logged in user or the given user |
|
88 | - * has the given permissions on the given model. |
|
89 | - * |
|
90 | - * @param string $permissionName |
|
91 | - * @param string $model |
|
92 | - * @param int $userId |
|
93 | - * @return bool |
|
94 | - */ |
|
95 | - public function can(string $permissionName, string $model, int $userId = 0): bool |
|
96 | - { |
|
97 | - $permission = $this->permissionService->first([ |
|
98 | - 'and' => [ |
|
99 | - 'model' => $model, |
|
100 | - 'name' => $permissionName, |
|
101 | - 'roles' => [ |
|
102 | - 'op' => 'has', |
|
103 | - 'val' => [ |
|
104 | - 'users' => [ |
|
105 | - 'op' => 'has', |
|
106 | - 'val' => [ |
|
107 | - 'users.id' => $userId ?: Auth::id() |
|
108 | - ] |
|
109 | - ] |
|
110 | - ] |
|
111 | - ] |
|
112 | - ] |
|
113 | - ]); |
|
114 | - |
|
115 | - return $permission ? true : false; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Check if the logged in or the given user has the given role. |
|
120 | - * |
|
121 | - * @param array $roles |
|
122 | - * @param int $user |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function hasRoles(array $roles, int $user = 0): bool |
|
126 | - { |
|
127 | - return $this->repo->countRoles($user ?: Auth::id(), $roles) ? true : false; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Assign the given role ids to the given user. |
|
132 | - * |
|
133 | - * @param int $userId |
|
134 | - * @param array $roleIds |
|
135 | - * @return Model |
|
136 | - */ |
|
137 | - public function assignRoles(int $userId, array $roleIds): Model |
|
138 | - { |
|
139 | - $user = new Model(); |
|
140 | - DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
141 | - $user = $this->repo->find($userId); |
|
142 | - $this->repo->detachRoles($user); |
|
143 | - $this->repo->attachRoles($user, $roleIds); |
|
144 | - }); |
|
145 | - |
|
146 | - return $user; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Handle the login request to the application. |
|
151 | - * |
|
152 | - * @param string $email |
|
153 | - * @param string $password |
|
154 | - * @return array |
|
155 | - */ |
|
156 | - public function login(string $email, string $password): array |
|
157 | - { |
|
158 | - if (!$user = $this->repo->first(['email' => $email])) { |
|
159 | - Errors::loginFailed(); |
|
160 | - } elseif ($user->blocked) { |
|
161 | - Errors::userIsBlocked(); |
|
162 | - } elseif (!config('user.disable_confirm_email') && !$user->confirmed) { |
|
163 | - Errors::emailNotConfirmed(); |
|
164 | - } |
|
165 | - |
|
166 | - return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Handle the social login request to the application. |
|
171 | - * |
|
172 | - * @param string $authCode |
|
173 | - * @param string $accessToken |
|
174 | - * @param string $type |
|
175 | - * @return array |
|
176 | - */ |
|
177 | - public function loginSocial(string $authCode, string $accessToken, string $type): array |
|
178 | - { |
|
179 | - $accessToken = $authCode ? Arr::get(Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
180 | - $user = Socialite::driver($type)->userFromToken($accessToken)->user; |
|
181 | - |
|
182 | - if (!Arr::has($user, 'email')) { |
|
183 | - Errors::noSocialEmail(); |
|
184 | - } |
|
185 | - |
|
186 | - if (!$this->repo->first(['email' => $user['email']]) && !$this->repo->deleted(['email' => $user['email']])->total()) { |
|
187 | - $this->register(Arr::get($user, 'name'), $user['email'], '', true); |
|
188 | - } |
|
189 | - |
|
190 | - return $this->login($user['email'], config('user.social_pass')); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Handle the registration request. |
|
195 | - * |
|
196 | - * @param array $data |
|
197 | - * @param bool $skipConfirmEmail |
|
198 | - * @param int $roleId |
|
199 | - * @return Model |
|
200 | - */ |
|
201 | - public function register(array $data, bool $skipConfirmEmail = false, int $roleId = 0): Model |
|
202 | - { |
|
203 | - $data['confirmed'] = $skipConfirmEmail; |
|
204 | - |
|
205 | - if ($roleId) { |
|
206 | - $data['roles'] = [['id' => $roleId]]; |
|
207 | - } |
|
208 | - |
|
209 | - $user = $this->repo->save($data); |
|
210 | - |
|
211 | - if (!$skipConfirmEmail && !config('user.disable_confirm_email')) { |
|
212 | - $this->sendConfirmationEmail($user->email); |
|
213 | - } |
|
214 | - |
|
215 | - return $user; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Block the user. |
|
220 | - * |
|
221 | - * @param int $userId |
|
222 | - * @return Model |
|
223 | - */ |
|
224 | - public function block(int $userId): Model |
|
225 | - { |
|
226 | - if (Auth::id() == $userId) { |
|
227 | - Errors::noPermissions(); |
|
228 | - } |
|
229 | - |
|
230 | - return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Unblock the user. |
|
235 | - * |
|
236 | - * @param int $userId |
|
237 | - * @return Model |
|
238 | - */ |
|
239 | - public function unblock(int $userId): Model |
|
240 | - { |
|
241 | - return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Send a reset link to the given user. |
|
246 | - * |
|
247 | - * @param string $email |
|
248 | - * @return bool |
|
249 | - */ |
|
250 | - public function sendReset(string $email): bool |
|
251 | - { |
|
252 | - if (!$user = $this->repo->first(['email' => $email])) { |
|
253 | - Errors::notFound('email'); |
|
254 | - } |
|
255 | - |
|
256 | - $token = Password::createToken($user); |
|
257 | - $this->notificationService->notify($user, 'ResetPassword', $token); |
|
258 | - |
|
259 | - return true; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Reset the given user's password. |
|
264 | - * |
|
265 | - * @param string $email |
|
266 | - * @param string $password |
|
267 | - * @param string $passwordConfirmation |
|
268 | - * @param string $token |
|
269 | - * @return string |
|
270 | - */ |
|
271 | - public function resetPassword(string $email, string $password, string $passwordConfirmation, string $token): string |
|
272 | - { |
|
273 | - $response = Password::reset([ |
|
274 | - 'email' => $email, |
|
275 | - 'password' => $password, |
|
276 | - 'password_confirmation' => $passwordConfirmation, |
|
277 | - 'token' => $token |
|
278 | - ], function ($user, $password) { |
|
279 | - $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
280 | - }); |
|
281 | - |
|
282 | - switch ($response) { |
|
283 | - case Password::PASSWORD_RESET: |
|
284 | - return 'success'; |
|
285 | - break; |
|
286 | - |
|
287 | - case Password::INVALID_TOKEN: |
|
288 | - Errors::invalidResetToken(); |
|
289 | - break; |
|
290 | - |
|
291 | - case Password::INVALID_USER: |
|
292 | - Errors::notFound('user'); |
|
293 | - break; |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Change the logged in user password. |
|
299 | - * |
|
300 | - * @param string $password |
|
301 | - * @param string $oldPassword |
|
302 | - * @return bool |
|
303 | - */ |
|
304 | - public function changePassword(string $password, string $oldPassword): bool |
|
305 | - { |
|
306 | - $user = Auth::user(); |
|
307 | - if (!Hash::check($oldPassword, $user->password)) { |
|
308 | - Errors::invalidOldPassword(); |
|
309 | - } |
|
310 | - |
|
311 | - $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
312 | - |
|
313 | - return true; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Confirm email using the confirmation code. |
|
318 | - * |
|
319 | - * @param string $confirmationCode |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - public function confirmEmail(string $confirmationCode): bool |
|
323 | - { |
|
324 | - if (!$user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
325 | - Errors::invalidConfirmationCode(); |
|
326 | - } |
|
327 | - |
|
328 | - $this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
329 | - |
|
330 | - return true; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Send the confirmation mail. |
|
335 | - * |
|
336 | - * @param string $email |
|
337 | - * @return bool |
|
338 | - */ |
|
339 | - public function sendConfirmationEmail(string $email): bool |
|
340 | - { |
|
341 | - $user = $this->repo->first(['email' => $email]); |
|
342 | - if ($user->confirmed) { |
|
343 | - Errors::emailAlreadyConfirmed(); |
|
344 | - } |
|
345 | - |
|
346 | - $this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
347 | - $this->notificationService->notify($user, 'ConfirmEmail'); |
|
348 | - |
|
349 | - return true; |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Save the given data to the logged in user. |
|
354 | - * |
|
355 | - * @param array $data |
|
356 | - * @return Model |
|
357 | - */ |
|
358 | - public function saveProfile(array $data): Model |
|
359 | - { |
|
360 | - $data['id'] = Auth::id(); |
|
361 | - return $this->repo->save($data); |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Logs out the user, revoke access token and refresh token. |
|
366 | - * |
|
367 | - * @return bool |
|
368 | - */ |
|
369 | - public function logout(): bool |
|
370 | - { |
|
371 | - $this->oauthClientService->revokeAccessToken(Auth::user()->token()); |
|
372 | - return true; |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Attempt to refresh the access token using the given refresh token. |
|
377 | - * |
|
378 | - * @param string $refreshToken |
|
379 | - * @return array |
|
380 | - */ |
|
381 | - public function refreshToken(string $refreshToken): array |
|
382 | - { |
|
383 | - return $this->loginProxy->refreshToken($refreshToken); |
|
384 | - } |
|
22 | + /** |
|
23 | + * @var PermissionServiceInterface |
|
24 | + */ |
|
25 | + protected $permissionService; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var LoginProxy |
|
29 | + */ |
|
30 | + protected $loginProxy; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var NotificationServiceInterface |
|
34 | + */ |
|
35 | + protected $notificationService; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var OauthClientServiceInterface |
|
39 | + */ |
|
40 | + protected $oauthClientService; |
|
41 | + |
|
42 | + /** |
|
43 | + * Init new object. |
|
44 | + * |
|
45 | + * @param UserRepositoryInterface $repo |
|
46 | + * @param PermissionServiceInterface $permissionService |
|
47 | + * @param LoginProxy $loginProxy |
|
48 | + * @param NotificationServiceInterface $notificationService |
|
49 | + * @param OauthClientServiceInterface $oauthClientService |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + public function __construct( |
|
53 | + UserRepositoryInterface $repo, |
|
54 | + PermissionServiceInterface $permissionService, |
|
55 | + LoginProxy $loginProxy, |
|
56 | + NotificationServiceInterface $notificationService, |
|
57 | + OauthClientServiceInterface $oauthClientService |
|
58 | + ) { |
|
59 | + $this->permissionService = $permissionService; |
|
60 | + $this->loginProxy = $loginProxy; |
|
61 | + $this->notificationService = $notificationService; |
|
62 | + $this->oauthClientService = $oauthClientService; |
|
63 | + parent::__construct($repo); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Return the logged in user account. |
|
68 | + * |
|
69 | + * @param array $relations |
|
70 | + * @return Model |
|
71 | + */ |
|
72 | + public function account(array $relations = ['roles.permissions']): Model |
|
73 | + { |
|
74 | + $permissions = []; |
|
75 | + $user = $this->repo->find(Auth::id(), $relations); |
|
76 | + foreach ($user->roles as $role) { |
|
77 | + $role->permissions->each(function ($permission) use (&$permissions) { |
|
78 | + $permissions[] = $permission; |
|
79 | + }); |
|
80 | + } |
|
81 | + $user->permissions = $permissions; |
|
82 | + |
|
83 | + return $user; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Check if the logged in user or the given user |
|
88 | + * has the given permissions on the given model. |
|
89 | + * |
|
90 | + * @param string $permissionName |
|
91 | + * @param string $model |
|
92 | + * @param int $userId |
|
93 | + * @return bool |
|
94 | + */ |
|
95 | + public function can(string $permissionName, string $model, int $userId = 0): bool |
|
96 | + { |
|
97 | + $permission = $this->permissionService->first([ |
|
98 | + 'and' => [ |
|
99 | + 'model' => $model, |
|
100 | + 'name' => $permissionName, |
|
101 | + 'roles' => [ |
|
102 | + 'op' => 'has', |
|
103 | + 'val' => [ |
|
104 | + 'users' => [ |
|
105 | + 'op' => 'has', |
|
106 | + 'val' => [ |
|
107 | + 'users.id' => $userId ?: Auth::id() |
|
108 | + ] |
|
109 | + ] |
|
110 | + ] |
|
111 | + ] |
|
112 | + ] |
|
113 | + ]); |
|
114 | + |
|
115 | + return $permission ? true : false; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Check if the logged in or the given user has the given role. |
|
120 | + * |
|
121 | + * @param array $roles |
|
122 | + * @param int $user |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function hasRoles(array $roles, int $user = 0): bool |
|
126 | + { |
|
127 | + return $this->repo->countRoles($user ?: Auth::id(), $roles) ? true : false; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Assign the given role ids to the given user. |
|
132 | + * |
|
133 | + * @param int $userId |
|
134 | + * @param array $roleIds |
|
135 | + * @return Model |
|
136 | + */ |
|
137 | + public function assignRoles(int $userId, array $roleIds): Model |
|
138 | + { |
|
139 | + $user = new Model(); |
|
140 | + DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
141 | + $user = $this->repo->find($userId); |
|
142 | + $this->repo->detachRoles($user); |
|
143 | + $this->repo->attachRoles($user, $roleIds); |
|
144 | + }); |
|
145 | + |
|
146 | + return $user; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Handle the login request to the application. |
|
151 | + * |
|
152 | + * @param string $email |
|
153 | + * @param string $password |
|
154 | + * @return array |
|
155 | + */ |
|
156 | + public function login(string $email, string $password): array |
|
157 | + { |
|
158 | + if (!$user = $this->repo->first(['email' => $email])) { |
|
159 | + Errors::loginFailed(); |
|
160 | + } elseif ($user->blocked) { |
|
161 | + Errors::userIsBlocked(); |
|
162 | + } elseif (!config('user.disable_confirm_email') && !$user->confirmed) { |
|
163 | + Errors::emailNotConfirmed(); |
|
164 | + } |
|
165 | + |
|
166 | + return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Handle the social login request to the application. |
|
171 | + * |
|
172 | + * @param string $authCode |
|
173 | + * @param string $accessToken |
|
174 | + * @param string $type |
|
175 | + * @return array |
|
176 | + */ |
|
177 | + public function loginSocial(string $authCode, string $accessToken, string $type): array |
|
178 | + { |
|
179 | + $accessToken = $authCode ? Arr::get(Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
180 | + $user = Socialite::driver($type)->userFromToken($accessToken)->user; |
|
181 | + |
|
182 | + if (!Arr::has($user, 'email')) { |
|
183 | + Errors::noSocialEmail(); |
|
184 | + } |
|
185 | + |
|
186 | + if (!$this->repo->first(['email' => $user['email']]) && !$this->repo->deleted(['email' => $user['email']])->total()) { |
|
187 | + $this->register(Arr::get($user, 'name'), $user['email'], '', true); |
|
188 | + } |
|
189 | + |
|
190 | + return $this->login($user['email'], config('user.social_pass')); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Handle the registration request. |
|
195 | + * |
|
196 | + * @param array $data |
|
197 | + * @param bool $skipConfirmEmail |
|
198 | + * @param int $roleId |
|
199 | + * @return Model |
|
200 | + */ |
|
201 | + public function register(array $data, bool $skipConfirmEmail = false, int $roleId = 0): Model |
|
202 | + { |
|
203 | + $data['confirmed'] = $skipConfirmEmail; |
|
204 | + |
|
205 | + if ($roleId) { |
|
206 | + $data['roles'] = [['id' => $roleId]]; |
|
207 | + } |
|
208 | + |
|
209 | + $user = $this->repo->save($data); |
|
210 | + |
|
211 | + if (!$skipConfirmEmail && !config('user.disable_confirm_email')) { |
|
212 | + $this->sendConfirmationEmail($user->email); |
|
213 | + } |
|
214 | + |
|
215 | + return $user; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Block the user. |
|
220 | + * |
|
221 | + * @param int $userId |
|
222 | + * @return Model |
|
223 | + */ |
|
224 | + public function block(int $userId): Model |
|
225 | + { |
|
226 | + if (Auth::id() == $userId) { |
|
227 | + Errors::noPermissions(); |
|
228 | + } |
|
229 | + |
|
230 | + return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Unblock the user. |
|
235 | + * |
|
236 | + * @param int $userId |
|
237 | + * @return Model |
|
238 | + */ |
|
239 | + public function unblock(int $userId): Model |
|
240 | + { |
|
241 | + return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Send a reset link to the given user. |
|
246 | + * |
|
247 | + * @param string $email |
|
248 | + * @return bool |
|
249 | + */ |
|
250 | + public function sendReset(string $email): bool |
|
251 | + { |
|
252 | + if (!$user = $this->repo->first(['email' => $email])) { |
|
253 | + Errors::notFound('email'); |
|
254 | + } |
|
255 | + |
|
256 | + $token = Password::createToken($user); |
|
257 | + $this->notificationService->notify($user, 'ResetPassword', $token); |
|
258 | + |
|
259 | + return true; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Reset the given user's password. |
|
264 | + * |
|
265 | + * @param string $email |
|
266 | + * @param string $password |
|
267 | + * @param string $passwordConfirmation |
|
268 | + * @param string $token |
|
269 | + * @return string |
|
270 | + */ |
|
271 | + public function resetPassword(string $email, string $password, string $passwordConfirmation, string $token): string |
|
272 | + { |
|
273 | + $response = Password::reset([ |
|
274 | + 'email' => $email, |
|
275 | + 'password' => $password, |
|
276 | + 'password_confirmation' => $passwordConfirmation, |
|
277 | + 'token' => $token |
|
278 | + ], function ($user, $password) { |
|
279 | + $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
280 | + }); |
|
281 | + |
|
282 | + switch ($response) { |
|
283 | + case Password::PASSWORD_RESET: |
|
284 | + return 'success'; |
|
285 | + break; |
|
286 | + |
|
287 | + case Password::INVALID_TOKEN: |
|
288 | + Errors::invalidResetToken(); |
|
289 | + break; |
|
290 | + |
|
291 | + case Password::INVALID_USER: |
|
292 | + Errors::notFound('user'); |
|
293 | + break; |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Change the logged in user password. |
|
299 | + * |
|
300 | + * @param string $password |
|
301 | + * @param string $oldPassword |
|
302 | + * @return bool |
|
303 | + */ |
|
304 | + public function changePassword(string $password, string $oldPassword): bool |
|
305 | + { |
|
306 | + $user = Auth::user(); |
|
307 | + if (!Hash::check($oldPassword, $user->password)) { |
|
308 | + Errors::invalidOldPassword(); |
|
309 | + } |
|
310 | + |
|
311 | + $this->repo->save(['id' => $user->id, 'password' => $password]); |
|
312 | + |
|
313 | + return true; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Confirm email using the confirmation code. |
|
318 | + * |
|
319 | + * @param string $confirmationCode |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + public function confirmEmail(string $confirmationCode): bool |
|
323 | + { |
|
324 | + if (!$user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
325 | + Errors::invalidConfirmationCode(); |
|
326 | + } |
|
327 | + |
|
328 | + $this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
329 | + |
|
330 | + return true; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Send the confirmation mail. |
|
335 | + * |
|
336 | + * @param string $email |
|
337 | + * @return bool |
|
338 | + */ |
|
339 | + public function sendConfirmationEmail(string $email): bool |
|
340 | + { |
|
341 | + $user = $this->repo->first(['email' => $email]); |
|
342 | + if ($user->confirmed) { |
|
343 | + Errors::emailAlreadyConfirmed(); |
|
344 | + } |
|
345 | + |
|
346 | + $this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
347 | + $this->notificationService->notify($user, 'ConfirmEmail'); |
|
348 | + |
|
349 | + return true; |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Save the given data to the logged in user. |
|
354 | + * |
|
355 | + * @param array $data |
|
356 | + * @return Model |
|
357 | + */ |
|
358 | + public function saveProfile(array $data): Model |
|
359 | + { |
|
360 | + $data['id'] = Auth::id(); |
|
361 | + return $this->repo->save($data); |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Logs out the user, revoke access token and refresh token. |
|
366 | + * |
|
367 | + * @return bool |
|
368 | + */ |
|
369 | + public function logout(): bool |
|
370 | + { |
|
371 | + $this->oauthClientService->revokeAccessToken(Auth::user()->token()); |
|
372 | + return true; |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Attempt to refresh the access token using the given refresh token. |
|
377 | + * |
|
378 | + * @param string $refreshToken |
|
379 | + * @return array |
|
380 | + */ |
|
381 | + public function refreshToken(string $refreshToken): array |
|
382 | + { |
|
383 | + return $this->loginProxy->refreshToken($refreshToken); |
|
384 | + } |
|
385 | 385 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | $permissions = []; |
75 | 75 | $user = $this->repo->find(Auth::id(), $relations); |
76 | 76 | foreach ($user->roles as $role) { |
77 | - $role->permissions->each(function ($permission) use (&$permissions) { |
|
77 | + $role->permissions->each(function($permission) use (&$permissions) { |
|
78 | 78 | $permissions[] = $permission; |
79 | 79 | }); |
80 | 80 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | public function assignRoles(int $userId, array $roleIds): Model |
138 | 138 | { |
139 | 139 | $user = new Model(); |
140 | - DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
140 | + DB::transaction(function() use ($userId, $roleIds, &$user) { |
|
141 | 141 | $user = $this->repo->find($userId); |
142 | 142 | $this->repo->detachRoles($user); |
143 | 143 | $this->repo->attachRoles($user, $roleIds); |
@@ -155,11 +155,11 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public function login(string $email, string $password): array |
157 | 157 | { |
158 | - if (!$user = $this->repo->first(['email' => $email])) { |
|
158 | + if ( ! $user = $this->repo->first(['email' => $email])) { |
|
159 | 159 | Errors::loginFailed(); |
160 | 160 | } elseif ($user->blocked) { |
161 | 161 | Errors::userIsBlocked(); |
162 | - } elseif (!config('user.disable_confirm_email') && !$user->confirmed) { |
|
162 | + } elseif ( ! config('user.disable_confirm_email') && ! $user->confirmed) { |
|
163 | 163 | Errors::emailNotConfirmed(); |
164 | 164 | } |
165 | 165 | |
@@ -179,11 +179,11 @@ discard block |
||
179 | 179 | $accessToken = $authCode ? Arr::get(Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
180 | 180 | $user = Socialite::driver($type)->userFromToken($accessToken)->user; |
181 | 181 | |
182 | - if (!Arr::has($user, 'email')) { |
|
182 | + if ( ! Arr::has($user, 'email')) { |
|
183 | 183 | Errors::noSocialEmail(); |
184 | 184 | } |
185 | 185 | |
186 | - if (!$this->repo->first(['email' => $user['email']]) && !$this->repo->deleted(['email' => $user['email']])->total()) { |
|
186 | + if ( ! $this->repo->first(['email' => $user['email']]) && ! $this->repo->deleted(['email' => $user['email']])->total()) { |
|
187 | 187 | $this->register(Arr::get($user, 'name'), $user['email'], '', true); |
188 | 188 | } |
189 | 189 | |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | |
209 | 209 | $user = $this->repo->save($data); |
210 | 210 | |
211 | - if (!$skipConfirmEmail && !config('user.disable_confirm_email')) { |
|
211 | + if ( ! $skipConfirmEmail && ! config('user.disable_confirm_email')) { |
|
212 | 212 | $this->sendConfirmationEmail($user->email); |
213 | 213 | } |
214 | 214 | |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | */ |
250 | 250 | public function sendReset(string $email): bool |
251 | 251 | { |
252 | - if (!$user = $this->repo->first(['email' => $email])) { |
|
252 | + if ( ! $user = $this->repo->first(['email' => $email])) { |
|
253 | 253 | Errors::notFound('email'); |
254 | 254 | } |
255 | 255 | |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | 'password' => $password, |
276 | 276 | 'password_confirmation' => $passwordConfirmation, |
277 | 277 | 'token' => $token |
278 | - ], function ($user, $password) { |
|
278 | + ], function($user, $password) { |
|
279 | 279 | $this->repo->save(['id' => $user->id, 'password' => $password]); |
280 | 280 | }); |
281 | 281 | |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | public function changePassword(string $password, string $oldPassword): bool |
305 | 305 | { |
306 | 306 | $user = Auth::user(); |
307 | - if (!Hash::check($oldPassword, $user->password)) { |
|
307 | + if ( ! Hash::check($oldPassword, $user->password)) { |
|
308 | 308 | Errors::invalidOldPassword(); |
309 | 309 | } |
310 | 310 | |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | */ |
322 | 322 | public function confirmEmail(string $confirmationCode): bool |
323 | 323 | { |
324 | - if (!$user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
324 | + if ( ! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
325 | 325 | Errors::invalidConfirmationCode(); |
326 | 326 | } |
327 | 327 |
@@ -10,35 +10,35 @@ |
||
10 | 10 | |
11 | 11 | class ModuleServiceProvider extends ServiceProvider |
12 | 12 | { |
13 | - /** |
|
14 | - * Bootstrap the module services. |
|
15 | - * |
|
16 | - * @return void |
|
17 | - */ |
|
18 | - public function boot() |
|
19 | - { |
|
20 | - $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'users'); |
|
21 | - $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'users'); |
|
13 | + /** |
|
14 | + * Bootstrap the module services. |
|
15 | + * |
|
16 | + * @return void |
|
17 | + */ |
|
18 | + public function boot() |
|
19 | + { |
|
20 | + $this->loadTranslationsFrom(__DIR__.'/../Resources/Lang', 'users'); |
|
21 | + $this->loadViewsFrom(__DIR__.'/../Resources/Views', 'users'); |
|
22 | 22 | |
23 | - $this->loadMigrationsFrom(module_path('users', 'Database/Migrations', 'app')); |
|
24 | - if (!$this->app->configurationIsCached()) { |
|
25 | - $this->loadConfigsFrom(module_path('users', 'Config', 'app')); |
|
26 | - } |
|
27 | - } |
|
23 | + $this->loadMigrationsFrom(module_path('users', 'Database/Migrations', 'app')); |
|
24 | + if (!$this->app->configurationIsCached()) { |
|
25 | + $this->loadConfigsFrom(module_path('users', 'Config', 'app')); |
|
26 | + } |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Register the module services. |
|
31 | - * |
|
32 | - * @return void |
|
33 | - */ |
|
34 | - public function register() |
|
35 | - { |
|
36 | - $this->app->register(RouteServiceProvider::class); |
|
29 | + /** |
|
30 | + * Register the module services. |
|
31 | + * |
|
32 | + * @return void |
|
33 | + */ |
|
34 | + public function register() |
|
35 | + { |
|
36 | + $this->app->register(RouteServiceProvider::class); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Bind interfaces to implmentations. |
|
40 | - */ |
|
41 | - $this->app->bind(UserServiceInterface::class, UserService::class); |
|
42 | - $this->app->bind(UserRepositoryInterface::class, UserRepository::class); |
|
43 | - } |
|
38 | + /** |
|
39 | + * Bind interfaces to implmentations. |
|
40 | + */ |
|
41 | + $this->app->bind(UserServiceInterface::class, UserService::class); |
|
42 | + $this->app->bind(UserRepositoryInterface::class, UserRepository::class); |
|
43 | + } |
|
44 | 44 | } |
@@ -10,61 +10,61 @@ |
||
10 | 10 | class AclUserObserver |
11 | 11 | { |
12 | 12 | |
13 | - public function saving($model) |
|
14 | - { |
|
15 | - if ($model->isDirty('profile_picture')) { |
|
16 | - Core::fileManager()->delete($model->getOriginal('profile_picture'), 'path'); |
|
17 | - } |
|
18 | - } |
|
13 | + public function saving($model) |
|
14 | + { |
|
15 | + if ($model->isDirty('profile_picture')) { |
|
16 | + Core::fileManager()->delete($model->getOriginal('profile_picture'), 'path'); |
|
17 | + } |
|
18 | + } |
|
19 | 19 | |
20 | - public function saved($model) |
|
21 | - { |
|
22 | - // |
|
23 | - } |
|
20 | + public function saved($model) |
|
21 | + { |
|
22 | + // |
|
23 | + } |
|
24 | 24 | |
25 | - public function creating($model) |
|
26 | - { |
|
27 | - // |
|
28 | - } |
|
25 | + public function creating($model) |
|
26 | + { |
|
27 | + // |
|
28 | + } |
|
29 | 29 | |
30 | - public function created($model) |
|
31 | - { |
|
32 | - // |
|
33 | - } |
|
30 | + public function created($model) |
|
31 | + { |
|
32 | + // |
|
33 | + } |
|
34 | 34 | |
35 | - public function updating($model) |
|
36 | - { |
|
37 | - // |
|
38 | - } |
|
35 | + public function updating($model) |
|
36 | + { |
|
37 | + // |
|
38 | + } |
|
39 | 39 | |
40 | - public function updated($model) |
|
41 | - { |
|
42 | - if ($model->isDirty('blocked') && $model->blocked) { |
|
43 | - $model->tokens()->each(function ($token) { |
|
44 | - \Core::oauthClients()->revokeAccessToken($token); |
|
45 | - }); |
|
46 | - } |
|
47 | - } |
|
40 | + public function updated($model) |
|
41 | + { |
|
42 | + if ($model->isDirty('blocked') && $model->blocked) { |
|
43 | + $model->tokens()->each(function ($token) { |
|
44 | + \Core::oauthClients()->revokeAccessToken($token); |
|
45 | + }); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - public function deleting($model) |
|
50 | - { |
|
51 | - if ($model->getOriginal('id') == \Auth::id()) { |
|
52 | - \Errors::noPermissions(); |
|
53 | - } |
|
54 | - } |
|
49 | + public function deleting($model) |
|
50 | + { |
|
51 | + if ($model->getOriginal('id') == \Auth::id()) { |
|
52 | + \Errors::noPermissions(); |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - public function deleted($model) |
|
57 | - { |
|
58 | - // |
|
59 | - } |
|
56 | + public function deleted($model) |
|
57 | + { |
|
58 | + // |
|
59 | + } |
|
60 | 60 | |
61 | - public function restoring($model) |
|
62 | - { |
|
63 | - // |
|
64 | - } |
|
61 | + public function restoring($model) |
|
62 | + { |
|
63 | + // |
|
64 | + } |
|
65 | 65 | |
66 | - public function restored($model) |
|
67 | - { |
|
68 | - // |
|
69 | - } |
|
66 | + public function restored($model) |
|
67 | + { |
|
68 | + // |
|
69 | + } |
|
70 | 70 | } |
@@ -12,108 +12,108 @@ |
||
12 | 12 | |
13 | 13 | class AclUser extends User |
14 | 14 | { |
15 | - use SoftDeletes, HasApiTokens; |
|
16 | - protected $table = 'users'; |
|
17 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
18 | - protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
19 | - protected $guarded = ['id']; |
|
20 | - public $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone', 'blocked', 'confirmed']; |
|
21 | - |
|
22 | - /** |
|
23 | - * Encrypt the password attribute before |
|
24 | - * saving it in the storage. |
|
25 | - * |
|
26 | - * @param string $value |
|
27 | - */ |
|
28 | - public function setPasswordAttribute($value) |
|
29 | - { |
|
30 | - $this->attributes['password'] = \Hash::make($value); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * Get the entity's notifications. |
|
35 | - */ |
|
36 | - public function notifications() |
|
37 | - { |
|
38 | - return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Get the entity's read notifications. |
|
43 | - */ |
|
44 | - public function readNotifications() |
|
45 | - { |
|
46 | - return $this->notifications()->whereNotNull('read_at'); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Get the entity's unread notifications. |
|
51 | - */ |
|
52 | - public function unreadNotifications() |
|
53 | - { |
|
54 | - return $this->notifications()->whereNull('read_at'); |
|
55 | - } |
|
56 | - |
|
57 | - public function roles() |
|
58 | - { |
|
59 | - return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
60 | - } |
|
61 | - |
|
62 | - public function oauthClients() |
|
63 | - { |
|
64 | - return $this->hasMany(OauthClient::class, 'user_id'); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Return fcm device tokens that will be used in sending fcm notifications. |
|
69 | - * |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function routeNotificationForFCM() |
|
73 | - { |
|
74 | - $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
75 | - $tokens = []; |
|
76 | - |
|
77 | - foreach ($devices as $device) { |
|
78 | - if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
79 | - $device->forceDelete(); |
|
80 | - continue; |
|
81 | - } |
|
82 | - |
|
83 | - $tokens[] = $device->device_token; |
|
84 | - } |
|
85 | - |
|
86 | - return $tokens; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * The channels the user receives notification broadcasts on. |
|
91 | - * |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public function receivesBroadcastNotificationsOn() |
|
95 | - { |
|
96 | - return 'users.'.$this->id; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Custom password validation. |
|
101 | - * |
|
102 | - * @param string $password |
|
103 | - * @return boolean |
|
104 | - */ |
|
105 | - public function validateForPassportPasswordGrant($password) |
|
106 | - { |
|
107 | - if ($password == config('user.social_pass')) { |
|
108 | - return true; |
|
109 | - } |
|
110 | - |
|
111 | - return \Hash::check($password, $this->password); |
|
112 | - } |
|
15 | + use SoftDeletes, HasApiTokens; |
|
16 | + protected $table = 'users'; |
|
17 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
18 | + protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
19 | + protected $guarded = ['id']; |
|
20 | + public $fillable = ['profile_picture', 'name', 'email', 'password', 'locale', 'timezone', 'blocked', 'confirmed']; |
|
21 | + |
|
22 | + /** |
|
23 | + * Encrypt the password attribute before |
|
24 | + * saving it in the storage. |
|
25 | + * |
|
26 | + * @param string $value |
|
27 | + */ |
|
28 | + public function setPasswordAttribute($value) |
|
29 | + { |
|
30 | + $this->attributes['password'] = \Hash::make($value); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * Get the entity's notifications. |
|
35 | + */ |
|
36 | + public function notifications() |
|
37 | + { |
|
38 | + return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Get the entity's read notifications. |
|
43 | + */ |
|
44 | + public function readNotifications() |
|
45 | + { |
|
46 | + return $this->notifications()->whereNotNull('read_at'); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Get the entity's unread notifications. |
|
51 | + */ |
|
52 | + public function unreadNotifications() |
|
53 | + { |
|
54 | + return $this->notifications()->whereNull('read_at'); |
|
55 | + } |
|
56 | + |
|
57 | + public function roles() |
|
58 | + { |
|
59 | + return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->whereNull('role_user.deleted_at')->withTimestamps(); |
|
60 | + } |
|
61 | + |
|
62 | + public function oauthClients() |
|
63 | + { |
|
64 | + return $this->hasMany(OauthClient::class, 'user_id'); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Return fcm device tokens that will be used in sending fcm notifications. |
|
69 | + * |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function routeNotificationForFCM() |
|
73 | + { |
|
74 | + $devices = \Core::pushNotificationDevices()->findBy(['user_id' => $this->id]); |
|
75 | + $tokens = []; |
|
76 | + |
|
77 | + foreach ($devices as $device) { |
|
78 | + if (\Core::oauthClients()->accessTokenExpiredOrRevoked($device->access_token)) { |
|
79 | + $device->forceDelete(); |
|
80 | + continue; |
|
81 | + } |
|
82 | + |
|
83 | + $tokens[] = $device->device_token; |
|
84 | + } |
|
85 | + |
|
86 | + return $tokens; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * The channels the user receives notification broadcasts on. |
|
91 | + * |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public function receivesBroadcastNotificationsOn() |
|
95 | + { |
|
96 | + return 'users.'.$this->id; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Custom password validation. |
|
101 | + * |
|
102 | + * @param string $password |
|
103 | + * @return boolean |
|
104 | + */ |
|
105 | + public function validateForPassportPasswordGrant($password) |
|
106 | + { |
|
107 | + if ($password == config('user.social_pass')) { |
|
108 | + return true; |
|
109 | + } |
|
110 | + |
|
111 | + return \Hash::check($password, $this->password); |
|
112 | + } |
|
113 | 113 | |
114 | - public static function boot() |
|
115 | - { |
|
116 | - parent::boot(); |
|
117 | - AclUser::observe(AclUserObserver::class); |
|
118 | - } |
|
114 | + public static function boot() |
|
115 | + { |
|
116 | + parent::boot(); |
|
117 | + AclUser::observe(AclUserObserver::class); |
|
118 | + } |
|
119 | 119 | } |
@@ -7,14 +7,14 @@ |
||
7 | 7 | |
8 | 8 | class SettingRepository extends BaseRepository implements SettingRepositoryInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Init new object. |
|
12 | - * |
|
13 | - * @param Setting $model |
|
14 | - * @return void |
|
15 | - */ |
|
16 | - public function __construct(Setting $model) |
|
17 | - { |
|
18 | - parent::__construct($model); |
|
19 | - } |
|
10 | + /** |
|
11 | + * Init new object. |
|
12 | + * |
|
13 | + * @param Setting $model |
|
14 | + * @return void |
|
15 | + */ |
|
16 | + public function __construct(Setting $model) |
|
17 | + { |
|
18 | + parent::__construct($model); |
|
19 | + } |
|
20 | 20 | } |
@@ -9,39 +9,39 @@ |
||
9 | 9 | |
10 | 10 | class SettingController extends BaseApiController |
11 | 11 | { |
12 | - /** |
|
13 | - * Path of the sotre form request. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $storeFormRequest = 'App\Modules\Core\Http\Requests\StoreSetting'; |
|
12 | + /** |
|
13 | + * Path of the sotre form request. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $storeFormRequest = 'App\Modules\Core\Http\Requests\StoreSetting'; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Path of the model resource |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $modelResource = 'App\Modules\Core\Http\Resources\Setting'; |
|
19 | + /** |
|
20 | + * Path of the model resource |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $modelResource = 'App\Modules\Core\Http\Resources\Setting'; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Init new object. |
|
28 | - * |
|
29 | - * @param SettingServiceInterface $service |
|
30 | - * @return void |
|
31 | - */ |
|
32 | - public function __construct(SettingServiceInterface $service) |
|
33 | - { |
|
34 | - parent::__construct($service); |
|
35 | - } |
|
26 | + /** |
|
27 | + * Init new object. |
|
28 | + * |
|
29 | + * @param SettingServiceInterface $service |
|
30 | + * @return void |
|
31 | + */ |
|
32 | + public function __construct(SettingServiceInterface $service) |
|
33 | + { |
|
34 | + parent::__construct($service); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Save list of settings. |
|
39 | - * |
|
40 | - * @param Request $request |
|
41 | - * @return \Illuminate\Http\Response |
|
42 | - */ |
|
43 | - public function saveMany(Request $request) |
|
44 | - { |
|
45 | - return new GeneralResource($this->service->saveMany($request->all())); |
|
46 | - } |
|
37 | + /** |
|
38 | + * Save list of settings. |
|
39 | + * |
|
40 | + * @param Request $request |
|
41 | + * @return \Illuminate\Http\Response |
|
42 | + */ |
|
43 | + public function saveMany(Request $request) |
|
44 | + { |
|
45 | + return new GeneralResource($this->service->saveMany($request->all())); |
|
46 | + } |
|
47 | 47 | } |
@@ -12,68 +12,68 @@ |
||
12 | 12 | |
13 | 13 | class CheckPermissions |
14 | 14 | { |
15 | - protected $route; |
|
16 | - protected $auth; |
|
17 | - protected $authMiddleware; |
|
18 | - protected $userService; |
|
19 | - protected $arr; |
|
15 | + protected $route; |
|
16 | + protected $auth; |
|
17 | + protected $authMiddleware; |
|
18 | + protected $userService; |
|
19 | + protected $arr; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Init new object. |
|
23 | - * |
|
24 | - * @param Route $route |
|
25 | - * @param Auth $auth |
|
26 | - * @param AuthMiddleware $authMiddleware |
|
27 | - * @param UserService $userService |
|
28 | - * @param Arr $arr |
|
29 | - * |
|
30 | - * @return void |
|
31 | - */ |
|
32 | - public function __construct(Route $route, Auth $auth, AuthMiddleware $authMiddleware, UserService $userService, Arr $arr) |
|
33 | - { |
|
34 | - $this->route = $route; |
|
35 | - $this->auth = $auth; |
|
36 | - $this->authMiddleware = $authMiddleware; |
|
37 | - $this->userService = $userService; |
|
38 | - $this->arr = $arr; |
|
39 | - } |
|
21 | + /** |
|
22 | + * Init new object. |
|
23 | + * |
|
24 | + * @param Route $route |
|
25 | + * @param Auth $auth |
|
26 | + * @param AuthMiddleware $authMiddleware |
|
27 | + * @param UserService $userService |
|
28 | + * @param Arr $arr |
|
29 | + * |
|
30 | + * @return void |
|
31 | + */ |
|
32 | + public function __construct(Route $route, Auth $auth, AuthMiddleware $authMiddleware, UserService $userService, Arr $arr) |
|
33 | + { |
|
34 | + $this->route = $route; |
|
35 | + $this->auth = $auth; |
|
36 | + $this->authMiddleware = $authMiddleware; |
|
37 | + $this->userService = $userService; |
|
38 | + $this->arr = $arr; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Handle an incoming request. |
|
43 | - * |
|
44 | - * @param \Illuminate\Http\Request $request |
|
45 | - * @param \Closure $next |
|
46 | - * @return mixed |
|
47 | - */ |
|
48 | - public function handle($request, Closure $next) |
|
49 | - { |
|
50 | - $routeActions = explode('@', $this->route->currentRouteAction()); |
|
51 | - $reflectionClass = new \ReflectionClass($routeActions[0]); |
|
52 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
53 | - $skipPermissionCheck = $this->arr->get($classProperties, 'skipPermissionCheck', []); |
|
54 | - $skipLoginCheck = $this->arr->get($classProperties, 'skipLoginCheck', []); |
|
55 | - $modelName = explode('\\', $routeActions[0]); |
|
56 | - $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
57 | - $permission = $routeActions[1]; |
|
41 | + /** |
|
42 | + * Handle an incoming request. |
|
43 | + * |
|
44 | + * @param \Illuminate\Http\Request $request |
|
45 | + * @param \Closure $next |
|
46 | + * @return mixed |
|
47 | + */ |
|
48 | + public function handle($request, Closure $next) |
|
49 | + { |
|
50 | + $routeActions = explode('@', $this->route->currentRouteAction()); |
|
51 | + $reflectionClass = new \ReflectionClass($routeActions[0]); |
|
52 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
53 | + $skipPermissionCheck = $this->arr->get($classProperties, 'skipPermissionCheck', []); |
|
54 | + $skipLoginCheck = $this->arr->get($classProperties, 'skipLoginCheck', []); |
|
55 | + $modelName = explode('\\', $routeActions[0]); |
|
56 | + $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
57 | + $permission = $routeActions[1]; |
|
58 | 58 | |
59 | - $this->auth->shouldUse('api'); |
|
60 | - if (! in_array($permission, $skipLoginCheck)) { |
|
61 | - $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $permission) { |
|
62 | - $user = $this->auth->user(); |
|
63 | - $isPasswordClient = $user->token() ? $user->token()->client->password_client : false; |
|
59 | + $this->auth->shouldUse('api'); |
|
60 | + if (! in_array($permission, $skipLoginCheck)) { |
|
61 | + $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $permission) { |
|
62 | + $user = $this->auth->user(); |
|
63 | + $isPasswordClient = $user->token() ? $user->token()->client->password_client : false; |
|
64 | 64 | |
65 | - if ($user->blocked) { |
|
66 | - Errors::userIsBlocked(); |
|
67 | - } |
|
65 | + if ($user->blocked) { |
|
66 | + Errors::userIsBlocked(); |
|
67 | + } |
|
68 | 68 | |
69 | - if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
|
70 | - } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
71 | - } else { |
|
72 | - Errors::noPermissions(); |
|
73 | - } |
|
74 | - }); |
|
75 | - } |
|
69 | + if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
|
70 | + } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
71 | + } else { |
|
72 | + Errors::noPermissions(); |
|
73 | + } |
|
74 | + }); |
|
75 | + } |
|
76 | 76 | |
77 | - return $next($request); |
|
78 | - } |
|
77 | + return $next($request); |
|
78 | + } |
|
79 | 79 | } |
@@ -57,8 +57,8 @@ discard block |
||
57 | 57 | $permission = $routeActions[1]; |
58 | 58 | |
59 | 59 | $this->auth->shouldUse('api'); |
60 | - if (! in_array($permission, $skipLoginCheck)) { |
|
61 | - $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $permission) { |
|
60 | + if ( ! in_array($permission, $skipLoginCheck)) { |
|
61 | + $this->authMiddleware->handle($request, function($request) use ($modelName, $skipPermissionCheck, $permission) { |
|
62 | 62 | $user = $this->auth->user(); |
63 | 63 | $isPasswordClient = $user->token() ? $user->token()->client->password_client : false; |
64 | 64 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | } |
68 | 68 | |
69 | 69 | if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
70 | - } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
70 | + } elseif ( ! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
71 | 71 | } else { |
72 | 72 | Errors::noPermissions(); |
73 | 73 | } |
@@ -10,254 +10,254 @@ |
||
10 | 10 | |
11 | 11 | abstract class BaseService implements BaseServiceInterface |
12 | 12 | { |
13 | - /** |
|
14 | - * @var BaseRepositoryInterface |
|
15 | - */ |
|
16 | - public $repo; |
|
13 | + /** |
|
14 | + * @var BaseRepositoryInterface |
|
15 | + */ |
|
16 | + public $repo; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Init new object. |
|
20 | - * |
|
21 | - * @param BaseRepositoryInterface $repo |
|
22 | - * @return void |
|
23 | - */ |
|
24 | - public function __construct(BaseRepositoryInterface $repo) |
|
25 | - { |
|
26 | - $this->repo = $repo; |
|
27 | - } |
|
18 | + /** |
|
19 | + * Init new object. |
|
20 | + * |
|
21 | + * @param BaseRepositoryInterface $repo |
|
22 | + * @return void |
|
23 | + */ |
|
24 | + public function __construct(BaseRepositoryInterface $repo) |
|
25 | + { |
|
26 | + $this->repo = $repo; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Fetch records with relations based on the given params. |
|
31 | - * |
|
32 | - * @param string $local |
|
33 | - * @param array $relations |
|
34 | - * @param array $conditions |
|
35 | - * @param int $perPage |
|
36 | - * @param string $sortBy |
|
37 | - * @param bool $desc |
|
38 | - * @param bool $trashed |
|
39 | - * @return LengthAwarePaginator |
|
40 | - */ |
|
41 | - public function list(string $local, array $relations = [], array $conditions = [], int $perPage = 15, string $sortBy = 'created_at', bool $desc = true, bool $trashed = false): LengthAwarePaginator |
|
42 | - { |
|
43 | - $translatable = $this->repo->model->translatable ?? []; |
|
44 | - $filters = $this->constructFilters($conditions, $local); |
|
45 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . $local : $sortBy; |
|
29 | + /** |
|
30 | + * Fetch records with relations based on the given params. |
|
31 | + * |
|
32 | + * @param string $local |
|
33 | + * @param array $relations |
|
34 | + * @param array $conditions |
|
35 | + * @param int $perPage |
|
36 | + * @param string $sortBy |
|
37 | + * @param bool $desc |
|
38 | + * @param bool $trashed |
|
39 | + * @return LengthAwarePaginator |
|
40 | + */ |
|
41 | + public function list(string $local, array $relations = [], array $conditions = [], int $perPage = 15, string $sortBy = 'created_at', bool $desc = true, bool $trashed = false): LengthAwarePaginator |
|
42 | + { |
|
43 | + $translatable = $this->repo->model->translatable ?? []; |
|
44 | + $filters = $this->constructFilters($conditions, $local); |
|
45 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . $local : $sortBy; |
|
46 | 46 | |
47 | - if ($trashed) { |
|
48 | - return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
49 | - } |
|
47 | + if ($trashed) { |
|
48 | + return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
49 | + } |
|
50 | 50 | |
51 | - if (count($filters)) { |
|
52 | - return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
53 | - } |
|
51 | + if (count($filters)) { |
|
52 | + return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
53 | + } |
|
54 | 54 | |
55 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
56 | - } |
|
55 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Fetch all records with relations from the storage. |
|
60 | - * |
|
61 | - * @param array $relations |
|
62 | - * @param string $sortBy |
|
63 | - * @param boolean $desc |
|
64 | - * @param array $columns |
|
65 | - * @return collection |
|
66 | - */ |
|
67 | - public function all(array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): Collection |
|
68 | - { |
|
69 | - return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
70 | - } |
|
58 | + /** |
|
59 | + * Fetch all records with relations from the storage. |
|
60 | + * |
|
61 | + * @param array $relations |
|
62 | + * @param string $sortBy |
|
63 | + * @param boolean $desc |
|
64 | + * @param array $columns |
|
65 | + * @return collection |
|
66 | + */ |
|
67 | + public function all(array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): Collection |
|
68 | + { |
|
69 | + return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Fetch all records with relations from storage in pages. |
|
74 | - * |
|
75 | - * @param integer $perPage |
|
76 | - * @param array $relations |
|
77 | - * @param string $sortBy |
|
78 | - * @param boolean $desc |
|
79 | - * @param array $columns |
|
80 | - * @return LengthAwarePaginator |
|
81 | - */ |
|
82 | - public function paginate(int $perPage = 15, array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): LengthAwarePaginator |
|
83 | - { |
|
84 | - return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
85 | - } |
|
72 | + /** |
|
73 | + * Fetch all records with relations from storage in pages. |
|
74 | + * |
|
75 | + * @param integer $perPage |
|
76 | + * @param array $relations |
|
77 | + * @param string $sortBy |
|
78 | + * @param boolean $desc |
|
79 | + * @param array $columns |
|
80 | + * @return LengthAwarePaginator |
|
81 | + */ |
|
82 | + public function paginate(int $perPage = 15, array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): LengthAwarePaginator |
|
83 | + { |
|
84 | + return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Fetch all records with relations based on |
|
89 | - * the given condition from storage in pages. |
|
90 | - * |
|
91 | - * @param array $conditions array of conditions |
|
92 | - * @param integer $perPage |
|
93 | - * @param array $relations |
|
94 | - * @param string $sortBy |
|
95 | - * @param boolean $desc |
|
96 | - * @param array $columns |
|
97 | - * @return LengthAwarePaginator |
|
98 | - */ |
|
99 | - public function paginateBy(array $conditions, int $perPage = 15, array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): LengthAwarePaginator |
|
100 | - { |
|
101 | - return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
102 | - } |
|
87 | + /** |
|
88 | + * Fetch all records with relations based on |
|
89 | + * the given condition from storage in pages. |
|
90 | + * |
|
91 | + * @param array $conditions array of conditions |
|
92 | + * @param integer $perPage |
|
93 | + * @param array $relations |
|
94 | + * @param string $sortBy |
|
95 | + * @param boolean $desc |
|
96 | + * @param array $columns |
|
97 | + * @return LengthAwarePaginator |
|
98 | + */ |
|
99 | + public function paginateBy(array $conditions, int $perPage = 15, array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): LengthAwarePaginator |
|
100 | + { |
|
101 | + return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Save the given model to the storage. |
|
106 | - * |
|
107 | - * @param array $data |
|
108 | - * @return Model |
|
109 | - */ |
|
110 | - public function save(array $data): Model |
|
111 | - { |
|
112 | - $model = $this->repo->save($data); |
|
104 | + /** |
|
105 | + * Save the given model to the storage. |
|
106 | + * |
|
107 | + * @param array $data |
|
108 | + * @return Model |
|
109 | + */ |
|
110 | + public function save(array $data): Model |
|
111 | + { |
|
112 | + $model = $this->repo->save($data); |
|
113 | 113 | |
114 | - return $model; |
|
115 | - } |
|
114 | + return $model; |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Delete record from the storage based on the given |
|
119 | - * condition. |
|
120 | - * |
|
121 | - * @param string $value condition value |
|
122 | - * @param string $attribute condition column name |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function delete(string $value, string $attribute = 'id'): bool |
|
126 | - { |
|
127 | - return $this->repo->delete($value, $attribute); |
|
128 | - } |
|
117 | + /** |
|
118 | + * Delete record from the storage based on the given |
|
119 | + * condition. |
|
120 | + * |
|
121 | + * @param string $value condition value |
|
122 | + * @param string $attribute condition column name |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function delete(string $value, string $attribute = 'id'): bool |
|
126 | + { |
|
127 | + return $this->repo->delete($value, $attribute); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Fetch records from the storage based on the given |
|
132 | - * id. |
|
133 | - * |
|
134 | - * @param int $id |
|
135 | - * @param array $relations |
|
136 | - * @param array $columns |
|
137 | - * @return object |
|
138 | - */ |
|
139 | - public function find(int $id, array $relations = [], array $columns = ['*']): Model |
|
140 | - { |
|
141 | - return $this->repo->find($id, $relations, $columns); |
|
142 | - } |
|
130 | + /** |
|
131 | + * Fetch records from the storage based on the given |
|
132 | + * id. |
|
133 | + * |
|
134 | + * @param int $id |
|
135 | + * @param array $relations |
|
136 | + * @param array $columns |
|
137 | + * @return object |
|
138 | + */ |
|
139 | + public function find(int $id, array $relations = [], array $columns = ['*']): Model |
|
140 | + { |
|
141 | + return $this->repo->find($id, $relations, $columns); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * Fetch records from the storage based on the given |
|
146 | - * condition. |
|
147 | - * |
|
148 | - * @param array $conditions array of conditions |
|
149 | - * @param array $relations |
|
150 | - * @param string $sortBy |
|
151 | - * @param bool $desc |
|
152 | - * @param array $columns |
|
153 | - * @return collection |
|
154 | - */ |
|
155 | - public function findBy(array $conditions, array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): Collection |
|
156 | - { |
|
157 | - return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
158 | - } |
|
144 | + /** |
|
145 | + * Fetch records from the storage based on the given |
|
146 | + * condition. |
|
147 | + * |
|
148 | + * @param array $conditions array of conditions |
|
149 | + * @param array $relations |
|
150 | + * @param string $sortBy |
|
151 | + * @param bool $desc |
|
152 | + * @param array $columns |
|
153 | + * @return collection |
|
154 | + */ |
|
155 | + public function findBy(array $conditions, array $relations = [], string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): Collection |
|
156 | + { |
|
157 | + return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Fetch the first record from the storage based on the given |
|
162 | - * condition. |
|
163 | - * |
|
164 | - * @param array $conditions array of conditions |
|
165 | - * @param array $relations |
|
166 | - * @param array $columns |
|
167 | - * @return Model |
|
168 | - */ |
|
169 | - public function first(array $conditions, array $relations = [], array $columns = ['*']): Model |
|
170 | - { |
|
171 | - return $this->repo->first($conditions, $relations, $columns); |
|
172 | - } |
|
160 | + /** |
|
161 | + * Fetch the first record from the storage based on the given |
|
162 | + * condition. |
|
163 | + * |
|
164 | + * @param array $conditions array of conditions |
|
165 | + * @param array $relations |
|
166 | + * @param array $columns |
|
167 | + * @return Model |
|
168 | + */ |
|
169 | + public function first(array $conditions, array $relations = [], array $columns = ['*']): Model |
|
170 | + { |
|
171 | + return $this->repo->first($conditions, $relations, $columns); |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Return the deleted models in pages based on the given conditions. |
|
176 | - * |
|
177 | - * @param array $conditions array of conditions |
|
178 | - * @param int $perPage |
|
179 | - * @param string $sortBy |
|
180 | - * @param bool $desc |
|
181 | - * @param array $columns |
|
182 | - * @return LengthAwarePaginator |
|
183 | - */ |
|
184 | - public function deleted(array $conditions, int $perPage = 15, string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): LengthAwarePaginator |
|
185 | - { |
|
186 | - return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
187 | - } |
|
174 | + /** |
|
175 | + * Return the deleted models in pages based on the given conditions. |
|
176 | + * |
|
177 | + * @param array $conditions array of conditions |
|
178 | + * @param int $perPage |
|
179 | + * @param string $sortBy |
|
180 | + * @param bool $desc |
|
181 | + * @param array $columns |
|
182 | + * @return LengthAwarePaginator |
|
183 | + */ |
|
184 | + public function deleted(array $conditions, int $perPage = 15, string $sortBy = 'created_at', bool $desc = true, array $columns = ['*']): LengthAwarePaginator |
|
185 | + { |
|
186 | + return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
187 | + } |
|
188 | 188 | |
189 | - /** |
|
190 | - * Restore the deleted model. |
|
191 | - * |
|
192 | - * @param int $id |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - public function restore(int $id): bool |
|
196 | - { |
|
197 | - return $this->repo->restore($id); |
|
198 | - } |
|
189 | + /** |
|
190 | + * Restore the deleted model. |
|
191 | + * |
|
192 | + * @param int $id |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + public function restore(int $id): bool |
|
196 | + { |
|
197 | + return $this->repo->restore($id); |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Prepare filters for repo. |
|
202 | - * |
|
203 | - * @param array $conditions |
|
204 | - * @param string $local |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - protected function constructFilters(array $conditions, string $local): array |
|
208 | - { |
|
209 | - $filters = []; |
|
210 | - $translatable = $this->repo->model->translatable ?? []; |
|
211 | - foreach ($conditions as $key => $value) { |
|
212 | - if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
213 | - /** |
|
214 | - * Prepare key based on the the requested lang if it was translatable. |
|
215 | - */ |
|
216 | - $key = in_array($key, $translatable) ? $key . '->' . $local : $key; |
|
200 | + /** |
|
201 | + * Prepare filters for repo. |
|
202 | + * |
|
203 | + * @param array $conditions |
|
204 | + * @param string $local |
|
205 | + * @return array |
|
206 | + */ |
|
207 | + protected function constructFilters(array $conditions, string $local): array |
|
208 | + { |
|
209 | + $filters = []; |
|
210 | + $translatable = $this->repo->model->translatable ?? []; |
|
211 | + foreach ($conditions as $key => $value) { |
|
212 | + if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
213 | + /** |
|
214 | + * Prepare key based on the the requested lang if it was translatable. |
|
215 | + */ |
|
216 | + $key = in_array($key, $translatable) ? $key . '->' . $local : $key; |
|
217 | 217 | |
218 | - /** |
|
219 | - * Convert 0/1 or true/false to boolean in case of not foreign key. |
|
220 | - */ |
|
221 | - if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && !is_null($value)) { |
|
222 | - $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
218 | + /** |
|
219 | + * Convert 0/1 or true/false to boolean in case of not foreign key. |
|
220 | + */ |
|
221 | + if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && !is_null($value)) { |
|
222 | + $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
223 | 223 | |
224 | - /** |
|
225 | - * Use in operator in case of foreign and comma seperated values. |
|
226 | - */ |
|
227 | - } elseif (!is_array($value) && strpos($key, '_id') && $value) { |
|
228 | - $filters[$key] = [ |
|
229 | - 'op' => 'in', |
|
230 | - 'val' => explode(',', $value) |
|
231 | - ]; |
|
224 | + /** |
|
225 | + * Use in operator in case of foreign and comma seperated values. |
|
226 | + */ |
|
227 | + } elseif (!is_array($value) && strpos($key, '_id') && $value) { |
|
228 | + $filters[$key] = [ |
|
229 | + 'op' => 'in', |
|
230 | + 'val' => explode(',', $value) |
|
231 | + ]; |
|
232 | 232 | |
233 | - /** |
|
234 | - * Use null operator in case of 0 value and foreign. |
|
235 | - */ |
|
236 | - } elseif (strpos($key, '_id') && $value == 0) { |
|
237 | - $filters[$key] = [ |
|
238 | - 'op' => 'null' |
|
239 | - ]; |
|
233 | + /** |
|
234 | + * Use null operator in case of 0 value and foreign. |
|
235 | + */ |
|
236 | + } elseif (strpos($key, '_id') && $value == 0) { |
|
237 | + $filters[$key] = [ |
|
238 | + 'op' => 'null' |
|
239 | + ]; |
|
240 | 240 | |
241 | - /** |
|
242 | - * Consider values as a sub conditions if it is array. |
|
243 | - */ |
|
244 | - } elseif (is_array($value)) { |
|
245 | - $filters[$key] = $value; |
|
241 | + /** |
|
242 | + * Consider values as a sub conditions if it is array. |
|
243 | + */ |
|
244 | + } elseif (is_array($value)) { |
|
245 | + $filters[$key] = $value; |
|
246 | 246 | |
247 | - /** |
|
248 | - * Default string filteration. |
|
249 | - */ |
|
250 | - } elseif ($value) { |
|
251 | - $key = 'LOWER(' . $key . ')'; |
|
252 | - $value = strtolower($value); |
|
253 | - $filters[$key] = [ |
|
254 | - 'op' => 'like', |
|
255 | - 'val' => '%' . $value . '%' |
|
256 | - ]; |
|
257 | - } |
|
258 | - } |
|
259 | - } |
|
247 | + /** |
|
248 | + * Default string filteration. |
|
249 | + */ |
|
250 | + } elseif ($value) { |
|
251 | + $key = 'LOWER(' . $key . ')'; |
|
252 | + $value = strtolower($value); |
|
253 | + $filters[$key] = [ |
|
254 | + 'op' => 'like', |
|
255 | + 'val' => '%' . $value . '%' |
|
256 | + ]; |
|
257 | + } |
|
258 | + } |
|
259 | + } |
|
260 | 260 | |
261 | - return $filters; |
|
262 | - } |
|
261 | + return $filters; |
|
262 | + } |
|
263 | 263 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | { |
43 | 43 | $translatable = $this->repo->model->translatable ?? []; |
44 | 44 | $filters = $this->constructFilters($conditions, $local); |
45 | - $sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . $local : $sortBy; |
|
45 | + $sortBy = in_array($sortBy, $translatable) ? $sortBy.'->'.$local : $sortBy; |
|
46 | 46 | |
47 | 47 | if ($trashed) { |
48 | 48 | return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
@@ -213,18 +213,18 @@ discard block |
||
213 | 213 | /** |
214 | 214 | * Prepare key based on the the requested lang if it was translatable. |
215 | 215 | */ |
216 | - $key = in_array($key, $translatable) ? $key . '->' . $local : $key; |
|
216 | + $key = in_array($key, $translatable) ? $key.'->'.$local : $key; |
|
217 | 217 | |
218 | 218 | /** |
219 | 219 | * Convert 0/1 or true/false to boolean in case of not foreign key. |
220 | 220 | */ |
221 | - if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && !is_null($value)) { |
|
221 | + if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
222 | 222 | $filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
223 | 223 | |
224 | 224 | /** |
225 | 225 | * Use in operator in case of foreign and comma seperated values. |
226 | 226 | */ |
227 | - } elseif (!is_array($value) && strpos($key, '_id') && $value) { |
|
227 | + } elseif ( ! is_array($value) && strpos($key, '_id') && $value) { |
|
228 | 228 | $filters[$key] = [ |
229 | 229 | 'op' => 'in', |
230 | 230 | 'val' => explode(',', $value) |
@@ -248,11 +248,11 @@ discard block |
||
248 | 248 | * Default string filteration. |
249 | 249 | */ |
250 | 250 | } elseif ($value) { |
251 | - $key = 'LOWER(' . $key . ')'; |
|
251 | + $key = 'LOWER('.$key.')'; |
|
252 | 252 | $value = strtolower($value); |
253 | 253 | $filters[$key] = [ |
254 | 254 | 'op' => 'like', |
255 | - 'val' => '%' . $value . '%' |
|
255 | + 'val' => '%'.$value.'%' |
|
256 | 256 | ]; |
257 | 257 | } |
258 | 258 | } |