@@ -268,7 +268,7 @@ |
||
268 | 268 | * @param string $groupName The name of the requested group. |
269 | 269 | * @param integer $perPage Number of rows per page default 15. |
270 | 270 | * @param string $sortBy The name of the column to sort by. |
271 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
271 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
272 | 272 | * @return \Illuminate\Http\Response |
273 | 273 | */ |
274 | 274 | public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
@@ -11,285 +11,285 @@ |
||
11 | 11 | |
12 | 12 | class UsersController extends BaseApiController |
13 | 13 | { |
14 | - /** |
|
15 | - * List of all route actions that the base api controller |
|
16 | - * will skip permissions check for them. |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
20 | - |
|
21 | - /** |
|
22 | - * List of all route actions that the base api controller |
|
23 | - * will skip login check for them. |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken', 'confirmEmail', 'resendEmailConfirmation']; |
|
27 | - |
|
28 | - /** |
|
29 | - * The validations rules used by the base api controller |
|
30 | - * to check before add. |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected $validationRules = [ |
|
34 | - 'name' => 'nullable|string', |
|
35 | - 'email' => 'required|email|unique:users,email,{id}', |
|
36 | - 'password' => 'nullable|min:6' |
|
37 | - ]; |
|
38 | - |
|
39 | - /** |
|
40 | - * The loginProxy implementation. |
|
41 | - * |
|
42 | - * @var \App\Modules\Acl\Proxy\LoginProxy |
|
43 | - */ |
|
44 | - protected $loginProxy; |
|
45 | - |
|
46 | - /** |
|
47 | - * Init new object. |
|
48 | - * |
|
49 | - * @param LoginProxy $loginProxy |
|
50 | - * @param UserRepository $repo |
|
51 | - * @param CoreConfig $config |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function __construct(LoginProxy $loginProxy, UserRepository $repo, CoreConfig $config) |
|
55 | - { |
|
56 | - $this->loginProxy = $loginProxy; |
|
57 | - parent::__construct($repo, $config, 'App\Modules\Acl\Http\Resources\AclUser'); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Return the logged in user account. |
|
62 | - * |
|
63 | - * @return \Illuminate\Http\Response |
|
64 | - */ |
|
65 | - public function account() |
|
66 | - { |
|
67 | - return new $this->modelResource($this->repo->account($this->relations)); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Block the user. |
|
72 | - * |
|
73 | - * @param integer $id Id of the user. |
|
74 | - * @return \Illuminate\Http\Response |
|
75 | - */ |
|
76 | - public function block($id) |
|
77 | - { |
|
78 | - return new $this->modelResource($this->repo->block($id)); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Unblock the user. |
|
83 | - * |
|
84 | - * @param integer $id Id of the user. |
|
85 | - * @return \Illuminate\Http\Response |
|
86 | - */ |
|
87 | - public function unblock($id) |
|
88 | - { |
|
89 | - return new $this->modelResource($this->repo->unblock($id)); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Logout the user. |
|
94 | - * |
|
95 | - * @return \Illuminate\Http\Response |
|
96 | - */ |
|
97 | - public function logout() |
|
98 | - { |
|
99 | - return new GeneralResource($this->loginProxy->logout()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Handle a registration request. |
|
104 | - * |
|
105 | - * @param \Illuminate\Http\Request $request |
|
106 | - * @return \Illuminate\Http\Response |
|
107 | - */ |
|
108 | - public function register(Request $request) |
|
109 | - { |
|
110 | - $this->validate($request, [ |
|
111 | - 'name' => 'nullable|string', |
|
112 | - 'email' => 'required|email|unique:users,email,{id}', |
|
113 | - 'password' => 'required|min:6' |
|
114 | - ]); |
|
115 | - |
|
116 | - return new $this->modelResource($this->repo->register($request->only('name', 'email', 'password'))); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Handle a login request to the application. |
|
121 | - * |
|
122 | - * @param \Illuminate\Http\Request $request |
|
123 | - * @return \Illuminate\Http\Response |
|
124 | - */ |
|
125 | - public function login(Request $request) |
|
126 | - { |
|
127 | - $this->validate($request, [ |
|
128 | - 'email' => 'required|email', |
|
129 | - 'password' => 'required|min:6', |
|
130 | - 'admin' => 'nullable|boolean' |
|
131 | - ]); |
|
14 | + /** |
|
15 | + * List of all route actions that the base api controller |
|
16 | + * will skip permissions check for them. |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $skipPermissionCheck = ['account', 'logout', 'changePassword', 'saveProfile', 'account']; |
|
20 | + |
|
21 | + /** |
|
22 | + * List of all route actions that the base api controller |
|
23 | + * will skip login check for them. |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $skipLoginCheck = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken', 'confirmEmail', 'resendEmailConfirmation']; |
|
27 | + |
|
28 | + /** |
|
29 | + * The validations rules used by the base api controller |
|
30 | + * to check before add. |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected $validationRules = [ |
|
34 | + 'name' => 'nullable|string', |
|
35 | + 'email' => 'required|email|unique:users,email,{id}', |
|
36 | + 'password' => 'nullable|min:6' |
|
37 | + ]; |
|
38 | + |
|
39 | + /** |
|
40 | + * The loginProxy implementation. |
|
41 | + * |
|
42 | + * @var \App\Modules\Acl\Proxy\LoginProxy |
|
43 | + */ |
|
44 | + protected $loginProxy; |
|
45 | + |
|
46 | + /** |
|
47 | + * Init new object. |
|
48 | + * |
|
49 | + * @param LoginProxy $loginProxy |
|
50 | + * @param UserRepository $repo |
|
51 | + * @param CoreConfig $config |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function __construct(LoginProxy $loginProxy, UserRepository $repo, CoreConfig $config) |
|
55 | + { |
|
56 | + $this->loginProxy = $loginProxy; |
|
57 | + parent::__construct($repo, $config, 'App\Modules\Acl\Http\Resources\AclUser'); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Return the logged in user account. |
|
62 | + * |
|
63 | + * @return \Illuminate\Http\Response |
|
64 | + */ |
|
65 | + public function account() |
|
66 | + { |
|
67 | + return new $this->modelResource($this->repo->account($this->relations)); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Block the user. |
|
72 | + * |
|
73 | + * @param integer $id Id of the user. |
|
74 | + * @return \Illuminate\Http\Response |
|
75 | + */ |
|
76 | + public function block($id) |
|
77 | + { |
|
78 | + return new $this->modelResource($this->repo->block($id)); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Unblock the user. |
|
83 | + * |
|
84 | + * @param integer $id Id of the user. |
|
85 | + * @return \Illuminate\Http\Response |
|
86 | + */ |
|
87 | + public function unblock($id) |
|
88 | + { |
|
89 | + return new $this->modelResource($this->repo->unblock($id)); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Logout the user. |
|
94 | + * |
|
95 | + * @return \Illuminate\Http\Response |
|
96 | + */ |
|
97 | + public function logout() |
|
98 | + { |
|
99 | + return new GeneralResource($this->loginProxy->logout()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Handle a registration request. |
|
104 | + * |
|
105 | + * @param \Illuminate\Http\Request $request |
|
106 | + * @return \Illuminate\Http\Response |
|
107 | + */ |
|
108 | + public function register(Request $request) |
|
109 | + { |
|
110 | + $this->validate($request, [ |
|
111 | + 'name' => 'nullable|string', |
|
112 | + 'email' => 'required|email|unique:users,email,{id}', |
|
113 | + 'password' => 'required|min:6' |
|
114 | + ]); |
|
115 | + |
|
116 | + return new $this->modelResource($this->repo->register($request->only('name', 'email', 'password'))); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Handle a login request to the application. |
|
121 | + * |
|
122 | + * @param \Illuminate\Http\Request $request |
|
123 | + * @return \Illuminate\Http\Response |
|
124 | + */ |
|
125 | + public function login(Request $request) |
|
126 | + { |
|
127 | + $this->validate($request, [ |
|
128 | + 'email' => 'required|email', |
|
129 | + 'password' => 'required|min:6', |
|
130 | + 'admin' => 'nullable|boolean' |
|
131 | + ]); |
|
132 | 132 | |
133 | - $result = $this->loginProxy->login($request->only('email', 'password'), $request->get('admin')); |
|
134 | - return (new $this->modelResource($result['user']))->additional(['meta' => $result['tokens']]); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Handle a social login request of the none admin to the application. |
|
139 | - * |
|
140 | - * @param \Illuminate\Http\Request $request |
|
141 | - * @return \Illuminate\Http\Response |
|
142 | - */ |
|
143 | - public function loginSocial(Request $request) |
|
144 | - { |
|
145 | - $this->validate($request, [ |
|
146 | - 'auth_code' => 'required_without:access_token', |
|
147 | - 'access_token' => 'required_without:auth_code', |
|
148 | - 'type' => 'required|in:facebook,google' |
|
149 | - ]); |
|
150 | - |
|
151 | - $result = $this->repo->loginSocial($request->get('auth_code'), $request->get('access_token'), $request->get('type')); |
|
152 | - return (new $this->modelResource($result['user']))->additional(['meta' => $result['tokens']]); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Assign the given groups to the given user. |
|
157 | - * |
|
158 | - * @param \Illuminate\Http\Request $request |
|
159 | - * @return \Illuminate\Http\Response |
|
160 | - */ |
|
161 | - public function assigngroups(Request $request) |
|
162 | - { |
|
163 | - $this->validate($request, [ |
|
164 | - 'group_ids' => 'required|exists:groups,id', |
|
165 | - 'user_id' => 'required|exists:users,id' |
|
166 | - ]); |
|
167 | - |
|
168 | - return new $this->modelResource($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids'))); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Send a reset link to the given user. |
|
173 | - * |
|
174 | - * @param \Illuminate\Http\Request $request |
|
175 | - * @return \Illuminate\Http\Response |
|
176 | - */ |
|
177 | - public function sendreset(Request $request) |
|
178 | - { |
|
179 | - $this->validate($request, ['email' => 'required|email']); |
|
180 | - |
|
181 | - return new GeneralResource($this->repo->sendReset($request->get('email'))); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Reset the given user's password. |
|
186 | - * |
|
187 | - * @param \Illuminate\Http\Request $request |
|
188 | - * @return \Illuminate\Http\Response |
|
189 | - */ |
|
190 | - public function resetpassword(Request $request) |
|
191 | - { |
|
192 | - $this->validate($request, [ |
|
193 | - 'token' => 'required', |
|
194 | - 'email' => 'required|email', |
|
195 | - 'password' => 'required|confirmed|min:6', |
|
196 | - 'password_confirmation' => 'required', |
|
197 | - ]); |
|
198 | - |
|
199 | - return new GeneralResource($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token'))); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Change the logged in user password. |
|
204 | - * |
|
205 | - * @param \Illuminate\Http\Request $request |
|
206 | - * @return \Illuminate\Http\Response |
|
207 | - */ |
|
208 | - public function changePassword(Request $request) |
|
209 | - { |
|
210 | - $this->validate($request, [ |
|
211 | - 'old_password' => 'required', |
|
212 | - 'password' => 'required|confirmed|min:6', |
|
213 | - 'password_confirmation' => 'required', |
|
214 | - ]); |
|
215 | - |
|
216 | - return new GeneralResource($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation'))); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Confirm email using the confirmation code. |
|
221 | - * |
|
222 | - * @param \Illuminate\Http\Request $request |
|
223 | - * @return \Illuminate\Http\Response |
|
224 | - */ |
|
225 | - public function confirmEmail(Request $request) |
|
226 | - { |
|
227 | - $this->validate($request, [ |
|
228 | - 'confirmation_code' => 'required|string' |
|
229 | - ]); |
|
230 | - |
|
231 | - return new GeneralResource($this->repo->confirmEmail($request->only('confirmation_code'))); |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Resend the email confirmation mail. |
|
236 | - * |
|
237 | - * @param \Illuminate\Http\Request $request |
|
238 | - * @return \Illuminate\Http\Response |
|
239 | - */ |
|
240 | - public function resendEmailConfirmation(Request $request) |
|
241 | - { |
|
242 | - $this->validate($request, [ |
|
243 | - 'email' => 'required|exists:users,email' |
|
244 | - ]); |
|
245 | - |
|
246 | - return new GeneralResource($this->repo->sendConfirmationEmail($request->get('email'))); |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Refresh the expired login token. |
|
251 | - * |
|
252 | - * @param \Illuminate\Http\Request $request |
|
253 | - * @return \Illuminate\Http\Response |
|
254 | - */ |
|
255 | - public function refreshtoken(Request $request) |
|
256 | - { |
|
257 | - $this->validate($request, [ |
|
258 | - 'refreshtoken' => 'required', |
|
259 | - ]); |
|
260 | - |
|
261 | - return new GeneralResource($this->loginProxy->refreshtoken($request->get('refreshtoken'))); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Paginate all users with in the given group. |
|
266 | - * |
|
267 | - * @param \Illuminate\Http\Request $request |
|
268 | - * @param string $groupName The name of the requested group. |
|
269 | - * @param integer $perPage Number of rows per page default 15. |
|
270 | - * @param string $sortBy The name of the column to sort by. |
|
271 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
272 | - * @return \Illuminate\Http\Response |
|
273 | - */ |
|
274 | - public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
275 | - { |
|
276 | - return $this->modelResource::collection($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc)); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Save the given data to the logged in user. |
|
281 | - * |
|
282 | - * @param \Illuminate\Http\Request $request |
|
283 | - * @return \Illuminate\Http\Response |
|
284 | - */ |
|
285 | - public function saveProfile(Request $request) |
|
286 | - { |
|
287 | - $this->validate($request, [ |
|
288 | - 'profile_picture' => 'nullable|string', |
|
289 | - 'name' => 'nullable|string', |
|
290 | - 'email' => 'required|email|unique:users,email,'.\Auth::id() |
|
291 | - ]); |
|
292 | - |
|
293 | - return new $this->modelResource($this->repo->saveProfile($request->only('name', 'email', 'profile_picture'))); |
|
294 | - } |
|
133 | + $result = $this->loginProxy->login($request->only('email', 'password'), $request->get('admin')); |
|
134 | + return (new $this->modelResource($result['user']))->additional(['meta' => $result['tokens']]); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Handle a social login request of the none admin to the application. |
|
139 | + * |
|
140 | + * @param \Illuminate\Http\Request $request |
|
141 | + * @return \Illuminate\Http\Response |
|
142 | + */ |
|
143 | + public function loginSocial(Request $request) |
|
144 | + { |
|
145 | + $this->validate($request, [ |
|
146 | + 'auth_code' => 'required_without:access_token', |
|
147 | + 'access_token' => 'required_without:auth_code', |
|
148 | + 'type' => 'required|in:facebook,google' |
|
149 | + ]); |
|
150 | + |
|
151 | + $result = $this->repo->loginSocial($request->get('auth_code'), $request->get('access_token'), $request->get('type')); |
|
152 | + return (new $this->modelResource($result['user']))->additional(['meta' => $result['tokens']]); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Assign the given groups to the given user. |
|
157 | + * |
|
158 | + * @param \Illuminate\Http\Request $request |
|
159 | + * @return \Illuminate\Http\Response |
|
160 | + */ |
|
161 | + public function assigngroups(Request $request) |
|
162 | + { |
|
163 | + $this->validate($request, [ |
|
164 | + 'group_ids' => 'required|exists:groups,id', |
|
165 | + 'user_id' => 'required|exists:users,id' |
|
166 | + ]); |
|
167 | + |
|
168 | + return new $this->modelResource($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids'))); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Send a reset link to the given user. |
|
173 | + * |
|
174 | + * @param \Illuminate\Http\Request $request |
|
175 | + * @return \Illuminate\Http\Response |
|
176 | + */ |
|
177 | + public function sendreset(Request $request) |
|
178 | + { |
|
179 | + $this->validate($request, ['email' => 'required|email']); |
|
180 | + |
|
181 | + return new GeneralResource($this->repo->sendReset($request->get('email'))); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Reset the given user's password. |
|
186 | + * |
|
187 | + * @param \Illuminate\Http\Request $request |
|
188 | + * @return \Illuminate\Http\Response |
|
189 | + */ |
|
190 | + public function resetpassword(Request $request) |
|
191 | + { |
|
192 | + $this->validate($request, [ |
|
193 | + 'token' => 'required', |
|
194 | + 'email' => 'required|email', |
|
195 | + 'password' => 'required|confirmed|min:6', |
|
196 | + 'password_confirmation' => 'required', |
|
197 | + ]); |
|
198 | + |
|
199 | + return new GeneralResource($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token'))); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Change the logged in user password. |
|
204 | + * |
|
205 | + * @param \Illuminate\Http\Request $request |
|
206 | + * @return \Illuminate\Http\Response |
|
207 | + */ |
|
208 | + public function changePassword(Request $request) |
|
209 | + { |
|
210 | + $this->validate($request, [ |
|
211 | + 'old_password' => 'required', |
|
212 | + 'password' => 'required|confirmed|min:6', |
|
213 | + 'password_confirmation' => 'required', |
|
214 | + ]); |
|
215 | + |
|
216 | + return new GeneralResource($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation'))); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Confirm email using the confirmation code. |
|
221 | + * |
|
222 | + * @param \Illuminate\Http\Request $request |
|
223 | + * @return \Illuminate\Http\Response |
|
224 | + */ |
|
225 | + public function confirmEmail(Request $request) |
|
226 | + { |
|
227 | + $this->validate($request, [ |
|
228 | + 'confirmation_code' => 'required|string' |
|
229 | + ]); |
|
230 | + |
|
231 | + return new GeneralResource($this->repo->confirmEmail($request->only('confirmation_code'))); |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Resend the email confirmation mail. |
|
236 | + * |
|
237 | + * @param \Illuminate\Http\Request $request |
|
238 | + * @return \Illuminate\Http\Response |
|
239 | + */ |
|
240 | + public function resendEmailConfirmation(Request $request) |
|
241 | + { |
|
242 | + $this->validate($request, [ |
|
243 | + 'email' => 'required|exists:users,email' |
|
244 | + ]); |
|
245 | + |
|
246 | + return new GeneralResource($this->repo->sendConfirmationEmail($request->get('email'))); |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Refresh the expired login token. |
|
251 | + * |
|
252 | + * @param \Illuminate\Http\Request $request |
|
253 | + * @return \Illuminate\Http\Response |
|
254 | + */ |
|
255 | + public function refreshtoken(Request $request) |
|
256 | + { |
|
257 | + $this->validate($request, [ |
|
258 | + 'refreshtoken' => 'required', |
|
259 | + ]); |
|
260 | + |
|
261 | + return new GeneralResource($this->loginProxy->refreshtoken($request->get('refreshtoken'))); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Paginate all users with in the given group. |
|
266 | + * |
|
267 | + * @param \Illuminate\Http\Request $request |
|
268 | + * @param string $groupName The name of the requested group. |
|
269 | + * @param integer $perPage Number of rows per page default 15. |
|
270 | + * @param string $sortBy The name of the column to sort by. |
|
271 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
272 | + * @return \Illuminate\Http\Response |
|
273 | + */ |
|
274 | + public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1) |
|
275 | + { |
|
276 | + return $this->modelResource::collection($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc)); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Save the given data to the logged in user. |
|
281 | + * |
|
282 | + * @param \Illuminate\Http\Request $request |
|
283 | + * @return \Illuminate\Http\Response |
|
284 | + */ |
|
285 | + public function saveProfile(Request $request) |
|
286 | + { |
|
287 | + $this->validate($request, [ |
|
288 | + 'profile_picture' => 'nullable|string', |
|
289 | + 'name' => 'nullable|string', |
|
290 | + 'email' => 'required|email|unique:users,email,'.\Auth::id() |
|
291 | + ]); |
|
292 | + |
|
293 | + return new $this->modelResource($this->repo->saveProfile($request->only('name', 'email', 'profile_picture'))); |
|
294 | + } |
|
295 | 295 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * |
145 | 145 | * @param array $credentials |
146 | 146 | * @param boolean $skipConfirmEmail |
147 | - * @return array |
|
147 | + * @return boolean |
|
148 | 148 | */ |
149 | 149 | public function register($credentials, $skipConfirmEmail = false) |
150 | 150 | { |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | * Save the given data to the logged in user. |
345 | 345 | * |
346 | 346 | * @param array $data |
347 | - * @return void |
|
347 | + * @return boolean |
|
348 | 348 | */ |
349 | 349 | public function saveProfile($data) |
350 | 350 | { |
@@ -6,391 +6,391 @@ |
||
6 | 6 | |
7 | 7 | class UserRepository extends AbstractRepository |
8 | 8 | { |
9 | - /** |
|
10 | - * Return the model full namespace. |
|
11 | - * |
|
12 | - * @return string |
|
13 | - */ |
|
14 | - protected function getModel() |
|
15 | - { |
|
16 | - return 'App\Modules\Acl\AclUser'; |
|
17 | - } |
|
18 | - |
|
19 | - |
|
20 | - /** |
|
21 | - * Return the logged in user account. |
|
22 | - * |
|
23 | - * @param array $relations |
|
24 | - * @return boolean |
|
25 | - */ |
|
26 | - public function account($relations = []) |
|
27 | - { |
|
28 | - $permissions = []; |
|
29 | - $user = \Core::users()->find(\Auth::id(), $relations); |
|
30 | - foreach ($user->groups()->get() as $group) { |
|
31 | - $group->permissions->each(function ($permission) use (&$permissions) { |
|
32 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
33 | - }); |
|
34 | - } |
|
35 | - $user->permissions = $permissions; |
|
36 | - |
|
37 | - return $user; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Check if the logged in user or the given user |
|
42 | - * has the given permissions on the given model. |
|
43 | - * |
|
44 | - * @param string $nameOfPermission |
|
45 | - * @param string $model |
|
46 | - * @param mixed $user |
|
47 | - * @return boolean |
|
48 | - */ |
|
49 | - public function can($nameOfPermission, $model, $user = false) |
|
50 | - { |
|
51 | - $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
52 | - $permissions = []; |
|
53 | - |
|
54 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model) { |
|
55 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
56 | - }); |
|
9 | + /** |
|
10 | + * Return the model full namespace. |
|
11 | + * |
|
12 | + * @return string |
|
13 | + */ |
|
14 | + protected function getModel() |
|
15 | + { |
|
16 | + return 'App\Modules\Acl\AclUser'; |
|
17 | + } |
|
18 | + |
|
19 | + |
|
20 | + /** |
|
21 | + * Return the logged in user account. |
|
22 | + * |
|
23 | + * @param array $relations |
|
24 | + * @return boolean |
|
25 | + */ |
|
26 | + public function account($relations = []) |
|
27 | + { |
|
28 | + $permissions = []; |
|
29 | + $user = \Core::users()->find(\Auth::id(), $relations); |
|
30 | + foreach ($user->groups()->get() as $group) { |
|
31 | + $group->permissions->each(function ($permission) use (&$permissions) { |
|
32 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
33 | + }); |
|
34 | + } |
|
35 | + $user->permissions = $permissions; |
|
36 | + |
|
37 | + return $user; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Check if the logged in user or the given user |
|
42 | + * has the given permissions on the given model. |
|
43 | + * |
|
44 | + * @param string $nameOfPermission |
|
45 | + * @param string $model |
|
46 | + * @param mixed $user |
|
47 | + * @return boolean |
|
48 | + */ |
|
49 | + public function can($nameOfPermission, $model, $user = false) |
|
50 | + { |
|
51 | + $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
|
52 | + $permissions = []; |
|
53 | + |
|
54 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model) { |
|
55 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
56 | + }); |
|
57 | 57 | |
58 | - return in_array($nameOfPermission, $permissions); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check if the logged in user has the given group. |
|
63 | - * |
|
64 | - * @param string[] $groups |
|
65 | - * @param mixed $user |
|
66 | - * @return boolean |
|
67 | - */ |
|
68 | - public function hasGroup($groups, $user = false) |
|
69 | - { |
|
70 | - $user = $user ?: $this->find(\Auth::id()); |
|
71 | - return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Assign the given group ids to the given user. |
|
76 | - * |
|
77 | - * @param integer $userId |
|
78 | - * @param array $groupIds |
|
79 | - * @return object |
|
80 | - */ |
|
81 | - public function assignGroups($userId, $groupIds) |
|
82 | - { |
|
83 | - \DB::transaction(function () use ($userId, $groupIds) { |
|
84 | - $user = $this->find($userId); |
|
85 | - $user->groups()->detach(); |
|
86 | - $user->groups()->attach($groupIds); |
|
87 | - }); |
|
88 | - |
|
89 | - return $this->find($userId); |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Handle a login request to the application. |
|
95 | - * |
|
96 | - * @param array $credentials |
|
97 | - * @param boolean $adminLogin |
|
98 | - * @return object |
|
99 | - */ |
|
100 | - public function login($credentials, $adminLogin = false) |
|
101 | - { |
|
102 | - if (! $user = $this->first(['email' => $credentials['email']])) { |
|
103 | - \ErrorHandler::loginFailed(); |
|
104 | - } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
|
105 | - \ErrorHandler::loginFailed(); |
|
106 | - } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
107 | - \ErrorHandler::loginFailed(); |
|
108 | - } elseif ($user->blocked) { |
|
109 | - \ErrorHandler::userIsBlocked(); |
|
110 | - } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
111 | - \ErrorHandler::emailNotConfirmed(); |
|
112 | - } |
|
113 | - |
|
114 | - return $user; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Handle a social login request of the none admin to the application. |
|
119 | - * |
|
120 | - * @param string $authCode |
|
121 | - * @param string $accessToken |
|
122 | - * @param string $type |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - public function loginSocial($authCode, $accessToken, $type) |
|
126 | - { |
|
127 | - $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
128 | - $user = \Socialite::driver($type)->userFromToken($access_token); |
|
129 | - |
|
130 | - if (! $user->email) { |
|
131 | - \ErrorHandler::noSocialEmail(); |
|
132 | - } |
|
133 | - |
|
134 | - if ( ! $this->model->where('email', $user->email)->first()) { |
|
135 | - $this->register(['email' => $user->email, 'password' => ''], true); |
|
136 | - } |
|
137 | - |
|
138 | - $loginProxy = \App::make('App\Modules\Acl\Proxy\LoginProxy'); |
|
139 | - return $loginProxy->login(['email' => $user->email, 'password' => config('skeleton.social_pass')], 0); |
|
140 | - } |
|
58 | + return in_array($nameOfPermission, $permissions); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check if the logged in user has the given group. |
|
63 | + * |
|
64 | + * @param string[] $groups |
|
65 | + * @param mixed $user |
|
66 | + * @return boolean |
|
67 | + */ |
|
68 | + public function hasGroup($groups, $user = false) |
|
69 | + { |
|
70 | + $user = $user ?: $this->find(\Auth::id()); |
|
71 | + return $user->groups->whereIn('name', $groups)->count() ? true : false; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Assign the given group ids to the given user. |
|
76 | + * |
|
77 | + * @param integer $userId |
|
78 | + * @param array $groupIds |
|
79 | + * @return object |
|
80 | + */ |
|
81 | + public function assignGroups($userId, $groupIds) |
|
82 | + { |
|
83 | + \DB::transaction(function () use ($userId, $groupIds) { |
|
84 | + $user = $this->find($userId); |
|
85 | + $user->groups()->detach(); |
|
86 | + $user->groups()->attach($groupIds); |
|
87 | + }); |
|
88 | + |
|
89 | + return $this->find($userId); |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Handle a login request to the application. |
|
95 | + * |
|
96 | + * @param array $credentials |
|
97 | + * @param boolean $adminLogin |
|
98 | + * @return object |
|
99 | + */ |
|
100 | + public function login($credentials, $adminLogin = false) |
|
101 | + { |
|
102 | + if (! $user = $this->first(['email' => $credentials['email']])) { |
|
103 | + \ErrorHandler::loginFailed(); |
|
104 | + } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
|
105 | + \ErrorHandler::loginFailed(); |
|
106 | + } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
107 | + \ErrorHandler::loginFailed(); |
|
108 | + } elseif ($user->blocked) { |
|
109 | + \ErrorHandler::userIsBlocked(); |
|
110 | + } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
111 | + \ErrorHandler::emailNotConfirmed(); |
|
112 | + } |
|
113 | + |
|
114 | + return $user; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Handle a social login request of the none admin to the application. |
|
119 | + * |
|
120 | + * @param string $authCode |
|
121 | + * @param string $accessToken |
|
122 | + * @param string $type |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + public function loginSocial($authCode, $accessToken, $type) |
|
126 | + { |
|
127 | + $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
128 | + $user = \Socialite::driver($type)->userFromToken($access_token); |
|
129 | + |
|
130 | + if (! $user->email) { |
|
131 | + \ErrorHandler::noSocialEmail(); |
|
132 | + } |
|
133 | + |
|
134 | + if ( ! $this->model->where('email', $user->email)->first()) { |
|
135 | + $this->register(['email' => $user->email, 'password' => ''], true); |
|
136 | + } |
|
137 | + |
|
138 | + $loginProxy = \App::make('App\Modules\Acl\Proxy\LoginProxy'); |
|
139 | + return $loginProxy->login(['email' => $user->email, 'password' => config('skeleton.social_pass')], 0); |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Handle a registration request. |
|
144 | - * |
|
145 | - * @param array $credentials |
|
146 | - * @param boolean $skipConfirmEmail |
|
147 | - * @return array |
|
148 | - */ |
|
149 | - public function register($credentials, $skipConfirmEmail = false) |
|
150 | - { |
|
151 | - $user = $this->save($credentials); |
|
152 | - |
|
153 | - if ($skipConfirmEmail) { |
|
154 | - $user->confirmed = 1; |
|
155 | - $user->save(); |
|
156 | - } elseif (! config('skeleton.disable_confirm_email')) { |
|
157 | - $this->sendConfirmationEmail($user->email); |
|
158 | - } |
|
159 | - |
|
160 | - return $user; |
|
161 | - } |
|
142 | + /** |
|
143 | + * Handle a registration request. |
|
144 | + * |
|
145 | + * @param array $credentials |
|
146 | + * @param boolean $skipConfirmEmail |
|
147 | + * @return array |
|
148 | + */ |
|
149 | + public function register($credentials, $skipConfirmEmail = false) |
|
150 | + { |
|
151 | + $user = $this->save($credentials); |
|
152 | + |
|
153 | + if ($skipConfirmEmail) { |
|
154 | + $user->confirmed = 1; |
|
155 | + $user->save(); |
|
156 | + } elseif (! config('skeleton.disable_confirm_email')) { |
|
157 | + $this->sendConfirmationEmail($user->email); |
|
158 | + } |
|
159 | + |
|
160 | + return $user; |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * Block the user. |
|
165 | - * |
|
166 | - * @param integer $userId |
|
167 | - * @return object |
|
168 | - */ |
|
169 | - public function block($userId) |
|
170 | - { |
|
171 | - if (! $user = $this->find($userId)) { |
|
172 | - \ErrorHandler::notFound('user'); |
|
173 | - } |
|
174 | - if (! $this->hasGroup(['Admin'])) { |
|
175 | - \ErrorHandler::noPermissions(); |
|
176 | - } elseif (\Auth::id() == $userId) { |
|
177 | - \ErrorHandler::noPermissions(); |
|
178 | - } elseif ($user->groups->pluck('name')->search('Admin', true) !== false) { |
|
179 | - \ErrorHandler::noPermissions(); |
|
180 | - } |
|
181 | - |
|
182 | - $user->blocked = 1; |
|
183 | - $user->save(); |
|
163 | + /** |
|
164 | + * Block the user. |
|
165 | + * |
|
166 | + * @param integer $userId |
|
167 | + * @return object |
|
168 | + */ |
|
169 | + public function block($userId) |
|
170 | + { |
|
171 | + if (! $user = $this->find($userId)) { |
|
172 | + \ErrorHandler::notFound('user'); |
|
173 | + } |
|
174 | + if (! $this->hasGroup(['Admin'])) { |
|
175 | + \ErrorHandler::noPermissions(); |
|
176 | + } elseif (\Auth::id() == $userId) { |
|
177 | + \ErrorHandler::noPermissions(); |
|
178 | + } elseif ($user->groups->pluck('name')->search('Admin', true) !== false) { |
|
179 | + \ErrorHandler::noPermissions(); |
|
180 | + } |
|
181 | + |
|
182 | + $user->blocked = 1; |
|
183 | + $user->save(); |
|
184 | 184 | |
185 | - return $user; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Unblock the user. |
|
190 | - * |
|
191 | - * @param integer $userId |
|
192 | - * @return object |
|
193 | - */ |
|
194 | - public function unblock($userId) |
|
195 | - { |
|
196 | - if (! $this->hasGroup(['Admin'])) { |
|
197 | - \ErrorHandler::noPermissions(); |
|
198 | - } |
|
199 | - |
|
200 | - $user = $this->find($userId); |
|
201 | - $user->blocked = 0; |
|
202 | - $user->save(); |
|
203 | - |
|
204 | - return $user; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Send a reset link to the given user. |
|
209 | - * |
|
210 | - * @param string $email |
|
211 | - * @return void |
|
212 | - */ |
|
213 | - public function sendReset($email) |
|
214 | - { |
|
215 | - if (! $user = $this->model->where('email', $email)->first()) { |
|
216 | - \ErrorHandler::notFound('email'); |
|
217 | - } |
|
218 | - |
|
219 | - $token = \Password::getRepository()->create($user); |
|
220 | - \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Reset the given user's password. |
|
225 | - * |
|
226 | - * @param array $credentials |
|
227 | - * @return string|null |
|
228 | - */ |
|
229 | - public function resetPassword($credentials) |
|
230 | - { |
|
231 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
232 | - $user->password = $password; |
|
233 | - $user->save(); |
|
234 | - }); |
|
235 | - |
|
236 | - switch ($response) { |
|
237 | - case \Password::PASSWORD_RESET: |
|
238 | - return 'success'; |
|
239 | - |
|
240 | - case \Password::INVALID_TOKEN: |
|
241 | - \ErrorHandler::invalidResetToken('token'); |
|
242 | - //no break |
|
243 | - |
|
244 | - case \Password::INVALID_PASSWORD: |
|
245 | - \ErrorHandler::invalidResetPassword('email'); |
|
246 | - //no break |
|
247 | - |
|
248 | - case \Password::INVALID_USER: |
|
249 | - \ErrorHandler::notFound('user'); |
|
250 | - //no break |
|
251 | - |
|
252 | - default: |
|
253 | - \ErrorHandler::generalError(); |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Change the logged in user password. |
|
259 | - * |
|
260 | - * @param array $credentials |
|
261 | - * @return void |
|
262 | - */ |
|
263 | - public function changePassword($credentials) |
|
264 | - { |
|
265 | - $user = \Auth::user(); |
|
266 | - if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
267 | - \ErrorHandler::invalidOldPassword(); |
|
268 | - } |
|
269 | - |
|
270 | - $user->password = $credentials['password']; |
|
271 | - $user->save(); |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Confirm email using the confirmation code. |
|
276 | - * |
|
277 | - * @param string $confirmationCode |
|
278 | - * @return void |
|
279 | - */ |
|
280 | - public function confirmEmail($confirmationCode) |
|
281 | - { |
|
282 | - if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
283 | - \ErrorHandler::invalidConfirmationCode(); |
|
284 | - } |
|
285 | - |
|
286 | - $user->confirmed = 1; |
|
287 | - $user->confirmation_code = null; |
|
288 | - $user->save(); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Send the confirmation mail. |
|
293 | - * |
|
294 | - * @param string $email |
|
295 | - * @return void |
|
296 | - */ |
|
297 | - public function sendConfirmationEmail($email) |
|
298 | - { |
|
299 | - $user = $this->first(['email' => $email]); |
|
300 | - if ($user->confirmed) { |
|
301 | - \ErrorHandler::emailAlreadyConfirmed(); |
|
302 | - } |
|
303 | - |
|
304 | - $user->confirmed = 0; |
|
305 | - $user->confirmation_code = sha1(microtime()); |
|
306 | - $user->save(); |
|
307 | - \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Paginate all users in the given group based on the given conditions. |
|
312 | - * |
|
313 | - * @param string $groupName |
|
314 | - * @param array $relations |
|
315 | - * @param integer $perPage |
|
316 | - * @param string $sortBy |
|
317 | - * @param boolean $desc |
|
318 | - * @return \Illuminate\Http\Response |
|
319 | - */ |
|
320 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
321 | - { |
|
322 | - unset($conditions['page']); |
|
323 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
324 | - $sort = $desc ? 'desc' : 'asc'; |
|
325 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
326 | - |
|
327 | - $model->whereHas('groups', function ($q) use ($groupName) { |
|
328 | - $q->where('name', $groupName); |
|
329 | - }); |
|
185 | + return $user; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Unblock the user. |
|
190 | + * |
|
191 | + * @param integer $userId |
|
192 | + * @return object |
|
193 | + */ |
|
194 | + public function unblock($userId) |
|
195 | + { |
|
196 | + if (! $this->hasGroup(['Admin'])) { |
|
197 | + \ErrorHandler::noPermissions(); |
|
198 | + } |
|
199 | + |
|
200 | + $user = $this->find($userId); |
|
201 | + $user->blocked = 0; |
|
202 | + $user->save(); |
|
203 | + |
|
204 | + return $user; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Send a reset link to the given user. |
|
209 | + * |
|
210 | + * @param string $email |
|
211 | + * @return void |
|
212 | + */ |
|
213 | + public function sendReset($email) |
|
214 | + { |
|
215 | + if (! $user = $this->model->where('email', $email)->first()) { |
|
216 | + \ErrorHandler::notFound('email'); |
|
217 | + } |
|
218 | + |
|
219 | + $token = \Password::getRepository()->create($user); |
|
220 | + \Core::notifications()->notify($user, 'ResetPassword', $token); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Reset the given user's password. |
|
225 | + * |
|
226 | + * @param array $credentials |
|
227 | + * @return string|null |
|
228 | + */ |
|
229 | + public function resetPassword($credentials) |
|
230 | + { |
|
231 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
232 | + $user->password = $password; |
|
233 | + $user->save(); |
|
234 | + }); |
|
235 | + |
|
236 | + switch ($response) { |
|
237 | + case \Password::PASSWORD_RESET: |
|
238 | + return 'success'; |
|
239 | + |
|
240 | + case \Password::INVALID_TOKEN: |
|
241 | + \ErrorHandler::invalidResetToken('token'); |
|
242 | + //no break |
|
243 | + |
|
244 | + case \Password::INVALID_PASSWORD: |
|
245 | + \ErrorHandler::invalidResetPassword('email'); |
|
246 | + //no break |
|
247 | + |
|
248 | + case \Password::INVALID_USER: |
|
249 | + \ErrorHandler::notFound('user'); |
|
250 | + //no break |
|
251 | + |
|
252 | + default: |
|
253 | + \ErrorHandler::generalError(); |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Change the logged in user password. |
|
259 | + * |
|
260 | + * @param array $credentials |
|
261 | + * @return void |
|
262 | + */ |
|
263 | + public function changePassword($credentials) |
|
264 | + { |
|
265 | + $user = \Auth::user(); |
|
266 | + if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
267 | + \ErrorHandler::invalidOldPassword(); |
|
268 | + } |
|
269 | + |
|
270 | + $user->password = $credentials['password']; |
|
271 | + $user->save(); |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Confirm email using the confirmation code. |
|
276 | + * |
|
277 | + * @param string $confirmationCode |
|
278 | + * @return void |
|
279 | + */ |
|
280 | + public function confirmEmail($confirmationCode) |
|
281 | + { |
|
282 | + if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
283 | + \ErrorHandler::invalidConfirmationCode(); |
|
284 | + } |
|
285 | + |
|
286 | + $user->confirmed = 1; |
|
287 | + $user->confirmation_code = null; |
|
288 | + $user->save(); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Send the confirmation mail. |
|
293 | + * |
|
294 | + * @param string $email |
|
295 | + * @return void |
|
296 | + */ |
|
297 | + public function sendConfirmationEmail($email) |
|
298 | + { |
|
299 | + $user = $this->first(['email' => $email]); |
|
300 | + if ($user->confirmed) { |
|
301 | + \ErrorHandler::emailAlreadyConfirmed(); |
|
302 | + } |
|
303 | + |
|
304 | + $user->confirmed = 0; |
|
305 | + $user->confirmation_code = sha1(microtime()); |
|
306 | + $user->save(); |
|
307 | + \Core::notifications()->notify($user, 'ConfirmEmail'); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Paginate all users in the given group based on the given conditions. |
|
312 | + * |
|
313 | + * @param string $groupName |
|
314 | + * @param array $relations |
|
315 | + * @param integer $perPage |
|
316 | + * @param string $sortBy |
|
317 | + * @param boolean $desc |
|
318 | + * @return \Illuminate\Http\Response |
|
319 | + */ |
|
320 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
321 | + { |
|
322 | + unset($conditions['page']); |
|
323 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
324 | + $sort = $desc ? 'desc' : 'asc'; |
|
325 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
326 | + |
|
327 | + $model->whereHas('groups', function ($q) use ($groupName) { |
|
328 | + $q->where('name', $groupName); |
|
329 | + }); |
|
330 | 330 | |
331 | 331 | |
332 | - if (count($conditions['conditionValues'])) { |
|
333 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
334 | - } |
|
335 | - |
|
336 | - if ($perPage) { |
|
337 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
338 | - } |
|
339 | - |
|
340 | - return $model->orderBy($sortBy, $sort)->get(); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Save the given data to the logged in user. |
|
345 | - * |
|
346 | - * @param array $data |
|
347 | - * @return void |
|
348 | - */ |
|
349 | - public function saveProfile($data) |
|
350 | - { |
|
351 | - if (Arr::has($data, 'profile_picture')) { |
|
352 | - $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
353 | - } |
|
332 | + if (count($conditions['conditionValues'])) { |
|
333 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
334 | + } |
|
335 | + |
|
336 | + if ($perPage) { |
|
337 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
338 | + } |
|
339 | + |
|
340 | + return $model->orderBy($sortBy, $sort)->get(); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Save the given data to the logged in user. |
|
345 | + * |
|
346 | + * @param array $data |
|
347 | + * @return void |
|
348 | + */ |
|
349 | + public function saveProfile($data) |
|
350 | + { |
|
351 | + if (Arr::has($data, 'profile_picture')) { |
|
352 | + $data['profile_picture'] = \Media::uploadImageBas64($data['profile_picture'], 'admins/profile_pictures'); |
|
353 | + } |
|
354 | 354 | |
355 | - $data['id'] = \Auth::id(); |
|
356 | - return $this->save($data); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Ensure access token hasn't expired or revoked. |
|
361 | - * |
|
362 | - * @param string $accessToken |
|
363 | - * @return boolean |
|
364 | - */ |
|
365 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
366 | - { |
|
367 | - $accessTokenId = json_decode($accessToken, true)['id']; |
|
368 | - $accessToken = \DB::table('oauth_access_tokens') |
|
369 | - ->where('id', $accessTokenId) |
|
370 | - ->first(); |
|
355 | + $data['id'] = \Auth::id(); |
|
356 | + return $this->save($data); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Ensure access token hasn't expired or revoked. |
|
361 | + * |
|
362 | + * @param string $accessToken |
|
363 | + * @return boolean |
|
364 | + */ |
|
365 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
366 | + { |
|
367 | + $accessTokenId = json_decode($accessToken, true)['id']; |
|
368 | + $accessToken = \DB::table('oauth_access_tokens') |
|
369 | + ->where('id', $accessTokenId) |
|
370 | + ->first(); |
|
371 | 371 | |
372 | - if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
373 | - return true; |
|
374 | - } |
|
375 | - |
|
376 | - return false; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Revoke the given access token and all |
|
381 | - * associated refresh tokens. |
|
382 | - * |
|
383 | - * @param oject $accessToken |
|
384 | - * @return void |
|
385 | - */ |
|
386 | - public function revokeAccessToken($accessToken) |
|
387 | - { |
|
388 | - \DB::table('oauth_refresh_tokens') |
|
389 | - ->where('access_token_id', $accessToken->id) |
|
390 | - ->update([ |
|
391 | - 'revoked' => true |
|
392 | - ]); |
|
393 | - |
|
394 | - $accessToken->revoke(); |
|
395 | - } |
|
372 | + if (\Carbon\Carbon::parse($accessToken->expires_at)->isPast() || $accessToken->revoked) { |
|
373 | + return true; |
|
374 | + } |
|
375 | + |
|
376 | + return false; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Revoke the given access token and all |
|
381 | + * associated refresh tokens. |
|
382 | + * |
|
383 | + * @param oject $accessToken |
|
384 | + * @return void |
|
385 | + */ |
|
386 | + public function revokeAccessToken($accessToken) |
|
387 | + { |
|
388 | + \DB::table('oauth_refresh_tokens') |
|
389 | + ->where('access_token_id', $accessToken->id) |
|
390 | + ->update([ |
|
391 | + 'revoked' => true |
|
392 | + ]); |
|
393 | + |
|
394 | + $accessToken->revoke(); |
|
395 | + } |
|
396 | 396 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | $permissions = []; |
29 | 29 | $user = \Core::users()->find(\Auth::id(), $relations); |
30 | 30 | foreach ($user->groups()->get() as $group) { |
31 | - $group->permissions->each(function ($permission) use (&$permissions) { |
|
31 | + $group->permissions->each(function($permission) use (&$permissions) { |
|
32 | 32 | $permissions[$permission->model][$permission->id] = $permission->name; |
33 | 33 | }); |
34 | 34 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | $user = $user ?: $this->find(\Auth::id(), ['groups.permissions']); |
52 | 52 | $permissions = []; |
53 | 53 | |
54 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model) { |
|
54 | + $user->groups->pluck('permissions')->each(function($permission) use (&$permissions, $model) { |
|
55 | 55 | $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
56 | 56 | }); |
57 | 57 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | */ |
81 | 81 | public function assignGroups($userId, $groupIds) |
82 | 82 | { |
83 | - \DB::transaction(function () use ($userId, $groupIds) { |
|
83 | + \DB::transaction(function() use ($userId, $groupIds) { |
|
84 | 84 | $user = $this->find($userId); |
85 | 85 | $user->groups()->detach(); |
86 | 86 | $user->groups()->attach($groupIds); |
@@ -99,15 +99,15 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function login($credentials, $adminLogin = false) |
101 | 101 | { |
102 | - if (! $user = $this->first(['email' => $credentials['email']])) { |
|
102 | + if ( ! $user = $this->first(['email' => $credentials['email']])) { |
|
103 | 103 | \ErrorHandler::loginFailed(); |
104 | 104 | } elseif ($adminLogin && ! $user->groups->whereIn('name', ['Admin'])->count()) { |
105 | 105 | \ErrorHandler::loginFailed(); |
106 | - } elseif (! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
106 | + } elseif ( ! $adminLogin && $user->groups->whereIn('name', ['Admin'])->count()) { |
|
107 | 107 | \ErrorHandler::loginFailed(); |
108 | 108 | } elseif ($user->blocked) { |
109 | 109 | \ErrorHandler::userIsBlocked(); |
110 | - } elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
110 | + } elseif ( ! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
111 | 111 | \ErrorHandler::emailNotConfirmed(); |
112 | 112 | } |
113 | 113 | |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | $access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
128 | 128 | $user = \Socialite::driver($type)->userFromToken($access_token); |
129 | 129 | |
130 | - if (! $user->email) { |
|
130 | + if ( ! $user->email) { |
|
131 | 131 | \ErrorHandler::noSocialEmail(); |
132 | 132 | } |
133 | 133 | |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | if ($skipConfirmEmail) { |
154 | 154 | $user->confirmed = 1; |
155 | 155 | $user->save(); |
156 | - } elseif (! config('skeleton.disable_confirm_email')) { |
|
156 | + } elseif ( ! config('skeleton.disable_confirm_email')) { |
|
157 | 157 | $this->sendConfirmationEmail($user->email); |
158 | 158 | } |
159 | 159 | |
@@ -168,10 +168,10 @@ discard block |
||
168 | 168 | */ |
169 | 169 | public function block($userId) |
170 | 170 | { |
171 | - if (! $user = $this->find($userId)) { |
|
171 | + if ( ! $user = $this->find($userId)) { |
|
172 | 172 | \ErrorHandler::notFound('user'); |
173 | 173 | } |
174 | - if (! $this->hasGroup(['Admin'])) { |
|
174 | + if ( ! $this->hasGroup(['Admin'])) { |
|
175 | 175 | \ErrorHandler::noPermissions(); |
176 | 176 | } elseif (\Auth::id() == $userId) { |
177 | 177 | \ErrorHandler::noPermissions(); |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | */ |
194 | 194 | public function unblock($userId) |
195 | 195 | { |
196 | - if (! $this->hasGroup(['Admin'])) { |
|
196 | + if ( ! $this->hasGroup(['Admin'])) { |
|
197 | 197 | \ErrorHandler::noPermissions(); |
198 | 198 | } |
199 | 199 | |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | */ |
213 | 213 | public function sendReset($email) |
214 | 214 | { |
215 | - if (! $user = $this->model->where('email', $email)->first()) { |
|
215 | + if ( ! $user = $this->model->where('email', $email)->first()) { |
|
216 | 216 | \ErrorHandler::notFound('email'); |
217 | 217 | } |
218 | 218 | |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | */ |
229 | 229 | public function resetPassword($credentials) |
230 | 230 | { |
231 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
231 | + $response = \Password::reset($credentials, function($user, $password) { |
|
232 | 232 | $user->password = $password; |
233 | 233 | $user->save(); |
234 | 234 | }); |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | public function changePassword($credentials) |
264 | 264 | { |
265 | 265 | $user = \Auth::user(); |
266 | - if (! \Hash::check($credentials['old_password'], $user->password)) { |
|
266 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) { |
|
267 | 267 | \ErrorHandler::invalidOldPassword(); |
268 | 268 | } |
269 | 269 | |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | */ |
280 | 280 | public function confirmEmail($confirmationCode) |
281 | 281 | { |
282 | - if (! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
282 | + if ( ! $user = $this->first(['confirmation_code' => $confirmationCode])) { |
|
283 | 283 | \ErrorHandler::invalidConfirmationCode(); |
284 | 284 | } |
285 | 285 | |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | $sort = $desc ? 'desc' : 'asc'; |
325 | 325 | $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
326 | 326 | |
327 | - $model->whereHas('groups', function ($q) use ($groupName) { |
|
327 | + $model->whereHas('groups', function($q) use ($groupName) { |
|
328 | 328 | $q->where('name', $groupName); |
329 | 329 | }); |
330 | 330 |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * Init new object. |
35 | 35 | * |
36 | 36 | * @param mixed $repo |
37 | - * @param CoreConfig $config |
|
37 | + * @param \App\Modules\Core\Utl\CoreConfig $config |
|
38 | 38 | * @param string $modelResource |
39 | 39 | * @return void |
40 | 40 | */ |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | * Fetch all records with relations from storage. |
60 | 60 | * |
61 | 61 | * @param string $sortBy The name of the column to sort by. |
62 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
62 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
63 | 63 | * @return \Illuminate\Http\Response |
64 | 64 | */ |
65 | 65 | public function index($sortBy = 'created_at', $desc = 1) |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * @param string $query The search text. |
86 | 86 | * @param integer $perPage Number of rows per page default 15. |
87 | 87 | * @param string $sortBy The name of the column to sort by. |
88 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
88 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
89 | 89 | * @return \Illuminate\Http\Response |
90 | 90 | */ |
91 | 91 | public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | * |
100 | 100 | * @param \Illuminate\Http\Request $request |
101 | 101 | * @param string $sortBy The name of the column to sort by. |
102 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
102 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
103 | 103 | * @return \Illuminate\Http\Response |
104 | 104 | */ |
105 | 105 | public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | * |
125 | 125 | * @param integer $perPage Number of rows per page default 15. |
126 | 126 | * @param string $sortBy The name of the column to sort by. |
127 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
127 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
128 | 128 | * @return \Illuminate\Http\Response |
129 | 129 | */ |
130 | 130 | public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * @param \Illuminate\Http\Request $request |
140 | 140 | * @param integer $perPage Number of rows per page default 15. |
141 | 141 | * @param string $sortBy The name of the column to sort by. |
142 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
142 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
143 | 143 | * @return \Illuminate\Http\Response |
144 | 144 | */ |
145 | 145 | public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | * @param \Illuminate\Http\Request $request |
190 | 190 | * @param integer $perPage Number of rows per page default 15. |
191 | 191 | * @param string $sortBy The name of the column to sort by. |
192 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
192 | + * @param integer $desc Sort ascending or descinding (1: desc, 0: asc). |
|
193 | 193 | * @return \Illuminate\Http\Response |
194 | 194 | */ |
195 | 195 | public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
@@ -9,303 +9,303 @@ |
||
9 | 9 | |
10 | 10 | class BaseApiController extends Controller |
11 | 11 | { |
12 | - /** |
|
13 | - * The config implementation. |
|
14 | - * |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $config; |
|
18 | - |
|
19 | - /** |
|
20 | - * The relations implementation. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $relations; |
|
25 | - |
|
26 | - /** |
|
27 | - * The repo implementation. |
|
28 | - * |
|
29 | - * @var object |
|
30 | - */ |
|
31 | - protected $repo; |
|
32 | - |
|
33 | - /** |
|
34 | - * Init new object. |
|
35 | - * |
|
36 | - * @param mixed $repo |
|
37 | - * @param CoreConfig $config |
|
38 | - * @param string $modelResource |
|
39 | - * @return void |
|
40 | - */ |
|
41 | - public function __construct($repo, $config, $modelResource) |
|
42 | - { |
|
43 | - $this->repo = $repo; |
|
44 | - $this->modelResource = $modelResource; |
|
45 | - $this->config = $config->getConfig(); |
|
46 | - $this->modelName = explode('\\', get_called_class()); |
|
47 | - $this->modelName = lcfirst(str_replace('Controller', '', end($this->modelName))); |
|
48 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
49 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
50 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
51 | - $route = explode('@', \Route::currentRouteAction())[1]; |
|
52 | - |
|
53 | - $this->setSessions(); |
|
54 | - $this->checkPermission($route); |
|
55 | - $this->setRelations($route); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Fetch all records with relations from storage. |
|
60 | - * |
|
61 | - * @param string $sortBy The name of the column to sort by. |
|
62 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
63 | - * @return \Illuminate\Http\Response |
|
64 | - */ |
|
65 | - public function index($sortBy = 'created_at', $desc = 1) |
|
66 | - { |
|
67 | - return $this->modelResource::collection($this->repo->all($this->relations, $sortBy, $desc)); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Fetch the single object with relations from storage. |
|
72 | - * |
|
73 | - * @param integer $id Id of the requested model. |
|
74 | - * @return \Illuminate\Http\Response |
|
75 | - */ |
|
76 | - public function find($id) |
|
77 | - { |
|
78 | - return new $this->modelResource($this->repo->find($id, $this->relations)); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Paginate all records with relations from storage |
|
83 | - * that matche the given query. |
|
84 | - * |
|
85 | - * @param string $query The search text. |
|
86 | - * @param integer $perPage Number of rows per page default 15. |
|
87 | - * @param string $sortBy The name of the column to sort by. |
|
88 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
89 | - * @return \Illuminate\Http\Response |
|
90 | - */ |
|
91 | - public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
92 | - { |
|
93 | - return $this->modelResource::collection($this->repo->search($query, $perPage, $this->relations, $sortBy, $desc)); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Fetch records from the storage based on the given |
|
98 | - * condition. |
|
99 | - * |
|
100 | - * @param \Illuminate\Http\Request $request |
|
101 | - * @param string $sortBy The name of the column to sort by. |
|
102 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
103 | - * @return \Illuminate\Http\Response |
|
104 | - */ |
|
105 | - public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
106 | - { |
|
107 | - return $this->modelResource::collection($this->repo->findBy($request->all(), $this->relations, $sortBy, $desc)); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Fetch the first record from the storage based on the given |
|
112 | - * condition. |
|
113 | - * |
|
114 | - * @param \Illuminate\Http\Request $request |
|
115 | - * @return \Illuminate\Http\Response |
|
116 | - */ |
|
117 | - public function first(Request $request) |
|
118 | - { |
|
119 | - return new $this->modelResource($this->repo->first($request->all(), $this->relations)); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Paginate all records with relations from storage. |
|
124 | - * |
|
125 | - * @param integer $perPage Number of rows per page default 15. |
|
126 | - * @param string $sortBy The name of the column to sort by. |
|
127 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
128 | - * @return \Illuminate\Http\Response |
|
129 | - */ |
|
130 | - public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
131 | - { |
|
132 | - return $this->modelResource::collection($this->repo->paginate($perPage, $this->relations, $sortBy, $desc)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Fetch all records with relations based on |
|
137 | - * the given condition from storage in pages. |
|
138 | - * |
|
139 | - * @param \Illuminate\Http\Request $request |
|
140 | - * @param integer $perPage Number of rows per page default 15. |
|
141 | - * @param string $sortBy The name of the column to sort by. |
|
142 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
143 | - * @return \Illuminate\Http\Response |
|
144 | - */ |
|
145 | - public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
146 | - { |
|
147 | - return $this->modelResource::collection($this->repo->paginateBy($request->all(), $perPage, $this->relations, $sortBy, $desc)); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Save the given model to storage. |
|
152 | - * |
|
153 | - * @param \Illuminate\Http\Request $request |
|
154 | - * @return \Illuminate\Http\Response |
|
155 | - */ |
|
156 | - public function save(Request $request) |
|
157 | - { |
|
158 | - foreach ($this->validationRules as &$rule) { |
|
159 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) { |
|
160 | - $rule .= ',deleted_at,NULL'; |
|
161 | - } |
|
162 | - |
|
163 | - if ($request->has('id')) { |
|
164 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
165 | - } else { |
|
166 | - $rule = str_replace(',{id}', '', $rule); |
|
167 | - } |
|
168 | - } |
|
12 | + /** |
|
13 | + * The config implementation. |
|
14 | + * |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $config; |
|
18 | + |
|
19 | + /** |
|
20 | + * The relations implementation. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $relations; |
|
25 | + |
|
26 | + /** |
|
27 | + * The repo implementation. |
|
28 | + * |
|
29 | + * @var object |
|
30 | + */ |
|
31 | + protected $repo; |
|
32 | + |
|
33 | + /** |
|
34 | + * Init new object. |
|
35 | + * |
|
36 | + * @param mixed $repo |
|
37 | + * @param CoreConfig $config |
|
38 | + * @param string $modelResource |
|
39 | + * @return void |
|
40 | + */ |
|
41 | + public function __construct($repo, $config, $modelResource) |
|
42 | + { |
|
43 | + $this->repo = $repo; |
|
44 | + $this->modelResource = $modelResource; |
|
45 | + $this->config = $config->getConfig(); |
|
46 | + $this->modelName = explode('\\', get_called_class()); |
|
47 | + $this->modelName = lcfirst(str_replace('Controller', '', end($this->modelName))); |
|
48 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
49 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
50 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
51 | + $route = explode('@', \Route::currentRouteAction())[1]; |
|
52 | + |
|
53 | + $this->setSessions(); |
|
54 | + $this->checkPermission($route); |
|
55 | + $this->setRelations($route); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Fetch all records with relations from storage. |
|
60 | + * |
|
61 | + * @param string $sortBy The name of the column to sort by. |
|
62 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
63 | + * @return \Illuminate\Http\Response |
|
64 | + */ |
|
65 | + public function index($sortBy = 'created_at', $desc = 1) |
|
66 | + { |
|
67 | + return $this->modelResource::collection($this->repo->all($this->relations, $sortBy, $desc)); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Fetch the single object with relations from storage. |
|
72 | + * |
|
73 | + * @param integer $id Id of the requested model. |
|
74 | + * @return \Illuminate\Http\Response |
|
75 | + */ |
|
76 | + public function find($id) |
|
77 | + { |
|
78 | + return new $this->modelResource($this->repo->find($id, $this->relations)); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Paginate all records with relations from storage |
|
83 | + * that matche the given query. |
|
84 | + * |
|
85 | + * @param string $query The search text. |
|
86 | + * @param integer $perPage Number of rows per page default 15. |
|
87 | + * @param string $sortBy The name of the column to sort by. |
|
88 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
89 | + * @return \Illuminate\Http\Response |
|
90 | + */ |
|
91 | + public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
92 | + { |
|
93 | + return $this->modelResource::collection($this->repo->search($query, $perPage, $this->relations, $sortBy, $desc)); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Fetch records from the storage based on the given |
|
98 | + * condition. |
|
99 | + * |
|
100 | + * @param \Illuminate\Http\Request $request |
|
101 | + * @param string $sortBy The name of the column to sort by. |
|
102 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
103 | + * @return \Illuminate\Http\Response |
|
104 | + */ |
|
105 | + public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
106 | + { |
|
107 | + return $this->modelResource::collection($this->repo->findBy($request->all(), $this->relations, $sortBy, $desc)); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Fetch the first record from the storage based on the given |
|
112 | + * condition. |
|
113 | + * |
|
114 | + * @param \Illuminate\Http\Request $request |
|
115 | + * @return \Illuminate\Http\Response |
|
116 | + */ |
|
117 | + public function first(Request $request) |
|
118 | + { |
|
119 | + return new $this->modelResource($this->repo->first($request->all(), $this->relations)); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Paginate all records with relations from storage. |
|
124 | + * |
|
125 | + * @param integer $perPage Number of rows per page default 15. |
|
126 | + * @param string $sortBy The name of the column to sort by. |
|
127 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
128 | + * @return \Illuminate\Http\Response |
|
129 | + */ |
|
130 | + public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
131 | + { |
|
132 | + return $this->modelResource::collection($this->repo->paginate($perPage, $this->relations, $sortBy, $desc)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Fetch all records with relations based on |
|
137 | + * the given condition from storage in pages. |
|
138 | + * |
|
139 | + * @param \Illuminate\Http\Request $request |
|
140 | + * @param integer $perPage Number of rows per page default 15. |
|
141 | + * @param string $sortBy The name of the column to sort by. |
|
142 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
143 | + * @return \Illuminate\Http\Response |
|
144 | + */ |
|
145 | + public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
146 | + { |
|
147 | + return $this->modelResource::collection($this->repo->paginateBy($request->all(), $perPage, $this->relations, $sortBy, $desc)); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Save the given model to storage. |
|
152 | + * |
|
153 | + * @param \Illuminate\Http\Request $request |
|
154 | + * @return \Illuminate\Http\Response |
|
155 | + */ |
|
156 | + public function save(Request $request) |
|
157 | + { |
|
158 | + foreach ($this->validationRules as &$rule) { |
|
159 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) { |
|
160 | + $rule .= ',deleted_at,NULL'; |
|
161 | + } |
|
162 | + |
|
163 | + if ($request->has('id')) { |
|
164 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
165 | + } else { |
|
166 | + $rule = str_replace(',{id}', '', $rule); |
|
167 | + } |
|
168 | + } |
|
169 | 169 | |
170 | - $this->validate($request, $this->validationRules); |
|
171 | - |
|
172 | - return $this->modelResource::collection($this->repo->save($request->all())); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Delete by the given id from storage. |
|
177 | - * |
|
178 | - * @param integer $id Id of the deleted model. |
|
179 | - * @return \Illuminate\Http\Response |
|
180 | - */ |
|
181 | - public function delete($id) |
|
182 | - { |
|
183 | - return new GeneralResource($this->repo->delete($id)); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Return the deleted models in pages based on the given conditions. |
|
188 | - * |
|
189 | - * @param \Illuminate\Http\Request $request |
|
190 | - * @param integer $perPage Number of rows per page default 15. |
|
191 | - * @param string $sortBy The name of the column to sort by. |
|
192 | - * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
193 | - * @return \Illuminate\Http\Response |
|
194 | - */ |
|
195 | - public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
196 | - { |
|
197 | - return $this->modelResource::collection($this->repo->deleted($request->all(), $perPage, $sortBy, $desc)); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Restore the deleted model. |
|
202 | - * |
|
203 | - * @param integer $id Id of the restored model. |
|
204 | - * @return \Illuminate\Http\Response |
|
205 | - */ |
|
206 | - public function restore($id) |
|
207 | - { |
|
208 | - return new GeneralResource($this->repo->restore($id)); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Check if the logged in user can do the given permission. |
|
213 | - * |
|
214 | - * @param string $permission |
|
215 | - * @return void |
|
216 | - */ |
|
217 | - private function checkPermission($permission) |
|
218 | - { |
|
219 | - \Auth::shouldUse('api'); |
|
220 | - $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
170 | + $this->validate($request, $this->validationRules); |
|
171 | + |
|
172 | + return $this->modelResource::collection($this->repo->save($request->all())); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Delete by the given id from storage. |
|
177 | + * |
|
178 | + * @param integer $id Id of the deleted model. |
|
179 | + * @return \Illuminate\Http\Response |
|
180 | + */ |
|
181 | + public function delete($id) |
|
182 | + { |
|
183 | + return new GeneralResource($this->repo->delete($id)); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Return the deleted models in pages based on the given conditions. |
|
188 | + * |
|
189 | + * @param \Illuminate\Http\Request $request |
|
190 | + * @param integer $perPage Number of rows per page default 15. |
|
191 | + * @param string $sortBy The name of the column to sort by. |
|
192 | + * @param boolean $desc Sort ascending or descinding (1: desc, 0: asc). |
|
193 | + * @return \Illuminate\Http\Response |
|
194 | + */ |
|
195 | + public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
196 | + { |
|
197 | + return $this->modelResource::collection($this->repo->deleted($request->all(), $perPage, $sortBy, $desc)); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Restore the deleted model. |
|
202 | + * |
|
203 | + * @param integer $id Id of the restored model. |
|
204 | + * @return \Illuminate\Http\Response |
|
205 | + */ |
|
206 | + public function restore($id) |
|
207 | + { |
|
208 | + return new GeneralResource($this->repo->restore($id)); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Check if the logged in user can do the given permission. |
|
213 | + * |
|
214 | + * @param string $permission |
|
215 | + * @return void |
|
216 | + */ |
|
217 | + private function checkPermission($permission) |
|
218 | + { |
|
219 | + \Auth::shouldUse('api'); |
|
220 | + $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
221 | 221 | |
222 | - if (! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) { |
|
223 | - $user = \Auth::user(); |
|
224 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
225 | - $isPasswordClient = $user->token()->client->password_client; |
|
226 | - $this->updateLocaleAndTimezone($user); |
|
227 | - |
|
228 | - if ($user->blocked) { |
|
229 | - \ErrorHandler::userIsBlocked(); |
|
230 | - } |
|
231 | - |
|
232 | - if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->modelName))) { |
|
233 | - } elseif (! $isPasswordClient && $user->tokenCan($this->modelName.'-'.$permission)) { |
|
234 | - } else { |
|
235 | - \ErrorHandler::noPermissions(); |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Set sessions based on the given headers in the request. |
|
242 | - * |
|
243 | - * @return void |
|
244 | - */ |
|
245 | - private function setSessions() |
|
246 | - { |
|
247 | - \Session::put('time-zone', \Request::header('time-zone') ?: 0); |
|
248 | - |
|
249 | - $locale = \Request::header('locale'); |
|
250 | - switch ($locale) { |
|
251 | - case 'en': |
|
252 | - \App::setLocale('en'); |
|
253 | - \Session::put('locale', 'en'); |
|
254 | - break; |
|
255 | - |
|
256 | - case 'ar': |
|
257 | - \App::setLocale('ar'); |
|
258 | - \Session::put('locale', 'ar'); |
|
259 | - break; |
|
260 | - |
|
261 | - case 'all': |
|
262 | - \App::setLocale('en'); |
|
263 | - \Session::put('locale', 'all'); |
|
264 | - break; |
|
265 | - |
|
266 | - default: |
|
267 | - \App::setLocale('en'); |
|
268 | - \Session::put('locale', 'en'); |
|
269 | - break; |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Set relation based on the called api. |
|
275 | - * |
|
276 | - * @param string $route |
|
277 | - * @return void |
|
278 | - */ |
|
279 | - private function setRelations($route) |
|
280 | - { |
|
281 | - $route = $route !== 'index' ? $route : 'list'; |
|
282 | - $relations = Arr::get($this->config['relations'], $this->modelName, false); |
|
283 | - $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Update the logged in user locale and time zone. |
|
288 | - * |
|
289 | - * @param object $user |
|
290 | - * @return void |
|
291 | - */ |
|
292 | - private function updateLocaleAndTimezone($user) |
|
293 | - { |
|
294 | - $update = false; |
|
295 | - $locale = \Session::get('locale'); |
|
296 | - $timezone = \Session::get('time-zone'); |
|
297 | - if ($locale && $locale !== 'all' && $locale !== $user->locale) { |
|
298 | - $user->locale = $locale; |
|
299 | - $update = true; |
|
300 | - } |
|
301 | - |
|
302 | - if ($timezone && $timezone !== $user->timezone) { |
|
303 | - $user->timezone = $timezone; |
|
304 | - $update = true; |
|
305 | - } |
|
306 | - |
|
307 | - if ($update) { |
|
308 | - $user->save(); |
|
309 | - } |
|
310 | - } |
|
222 | + if (! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) { |
|
223 | + $user = \Auth::user(); |
|
224 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
225 | + $isPasswordClient = $user->token()->client->password_client; |
|
226 | + $this->updateLocaleAndTimezone($user); |
|
227 | + |
|
228 | + if ($user->blocked) { |
|
229 | + \ErrorHandler::userIsBlocked(); |
|
230 | + } |
|
231 | + |
|
232 | + if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->modelName))) { |
|
233 | + } elseif (! $isPasswordClient && $user->tokenCan($this->modelName.'-'.$permission)) { |
|
234 | + } else { |
|
235 | + \ErrorHandler::noPermissions(); |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Set sessions based on the given headers in the request. |
|
242 | + * |
|
243 | + * @return void |
|
244 | + */ |
|
245 | + private function setSessions() |
|
246 | + { |
|
247 | + \Session::put('time-zone', \Request::header('time-zone') ?: 0); |
|
248 | + |
|
249 | + $locale = \Request::header('locale'); |
|
250 | + switch ($locale) { |
|
251 | + case 'en': |
|
252 | + \App::setLocale('en'); |
|
253 | + \Session::put('locale', 'en'); |
|
254 | + break; |
|
255 | + |
|
256 | + case 'ar': |
|
257 | + \App::setLocale('ar'); |
|
258 | + \Session::put('locale', 'ar'); |
|
259 | + break; |
|
260 | + |
|
261 | + case 'all': |
|
262 | + \App::setLocale('en'); |
|
263 | + \Session::put('locale', 'all'); |
|
264 | + break; |
|
265 | + |
|
266 | + default: |
|
267 | + \App::setLocale('en'); |
|
268 | + \Session::put('locale', 'en'); |
|
269 | + break; |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Set relation based on the called api. |
|
275 | + * |
|
276 | + * @param string $route |
|
277 | + * @return void |
|
278 | + */ |
|
279 | + private function setRelations($route) |
|
280 | + { |
|
281 | + $route = $route !== 'index' ? $route : 'list'; |
|
282 | + $relations = Arr::get($this->config['relations'], $this->modelName, false); |
|
283 | + $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Update the logged in user locale and time zone. |
|
288 | + * |
|
289 | + * @param object $user |
|
290 | + * @return void |
|
291 | + */ |
|
292 | + private function updateLocaleAndTimezone($user) |
|
293 | + { |
|
294 | + $update = false; |
|
295 | + $locale = \Session::get('locale'); |
|
296 | + $timezone = \Session::get('time-zone'); |
|
297 | + if ($locale && $locale !== 'all' && $locale !== $user->locale) { |
|
298 | + $user->locale = $locale; |
|
299 | + $update = true; |
|
300 | + } |
|
301 | + |
|
302 | + if ($timezone && $timezone !== $user->timezone) { |
|
303 | + $user->timezone = $timezone; |
|
304 | + $update = true; |
|
305 | + } |
|
306 | + |
|
307 | + if ($update) { |
|
308 | + $user->save(); |
|
309 | + } |
|
310 | + } |
|
311 | 311 | } |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | \Auth::shouldUse('api'); |
220 | 220 | $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
221 | 221 | |
222 | - if (! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) { |
|
222 | + if ( ! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) { |
|
223 | 223 | $user = \Auth::user(); |
224 | 224 | $permission = $permission !== 'index' ? $permission : 'list'; |
225 | 225 | $isPasswordClient = $user->token()->client->password_client; |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | } |
231 | 231 | |
232 | 232 | if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->modelName))) { |
233 | - } elseif (! $isPasswordClient && $user->tokenCan($this->modelName.'-'.$permission)) { |
|
233 | + } elseif ( ! $isPasswordClient && $user->tokenCan($this->modelName.'-'.$permission)) { |
|
234 | 234 | } else { |
235 | 235 | \ErrorHandler::noPermissions(); |
236 | 236 | } |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | |
302 | 302 | if ($timezone && $timezone !== $user->timezone) { |
303 | 303 | $user->timezone = $timezone; |
304 | - $update = true; |
|
304 | + $update = true; |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | if ($update) { |
@@ -34,7 +34,7 @@ |
||
34 | 34 | * @param \Illuminate\Http\Request $request |
35 | 35 | * @param string $reportName Name of the requested report |
36 | 36 | * @param integer $perPage Number of rows per page default all data. |
37 | - * @return \Illuminate\Http\Response |
|
37 | + * @return \Illuminate\Http\JsonResponse |
|
38 | 38 | */ |
39 | 39 | public function getReport(Request $request, $reportName, $perPage = 0) |
40 | 40 | { |
@@ -9,35 +9,35 @@ |
||
9 | 9 | |
10 | 10 | class ReportsController extends BaseApiController |
11 | 11 | { |
12 | - /** |
|
13 | - * List of all route actions that the base api controller |
|
14 | - * will skip permissions check for them. |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $skipPermissionCheck = ['getReport']; |
|
12 | + /** |
|
13 | + * List of all route actions that the base api controller |
|
14 | + * will skip permissions check for them. |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $skipPermissionCheck = ['getReport']; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Init new object. |
|
21 | - * |
|
22 | - * @param ReportRepository $repo |
|
23 | - * @param CoreConfig $config |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function __construct(ReportRepository $repo, CoreConfig $config) |
|
27 | - { |
|
28 | - parent::__construct($repo, $config, 'App\Modules\Reporting\Http\Resources\Report'); |
|
29 | - } |
|
19 | + /** |
|
20 | + * Init new object. |
|
21 | + * |
|
22 | + * @param ReportRepository $repo |
|
23 | + * @param CoreConfig $config |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function __construct(ReportRepository $repo, CoreConfig $config) |
|
27 | + { |
|
28 | + parent::__construct($repo, $config, 'App\Modules\Reporting\Http\Resources\Report'); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * Render the given report name with the given conditions. |
|
33 | - * |
|
34 | - * @param \Illuminate\Http\Request $request |
|
35 | - * @param string $reportName Name of the requested report |
|
36 | - * @param integer $perPage Number of rows per page default all data. |
|
37 | - * @return \Illuminate\Http\Response |
|
38 | - */ |
|
39 | - public function getReport(Request $request, $reportName, $perPage = 0) |
|
40 | - { |
|
41 | - return \Response::json($this->repo->getReport($reportName, $request->all(), $perPage), 200); |
|
42 | - } |
|
31 | + /** |
|
32 | + * Render the given report name with the given conditions. |
|
33 | + * |
|
34 | + * @param \Illuminate\Http\Request $request |
|
35 | + * @param string $reportName Name of the requested report |
|
36 | + * @param integer $perPage Number of rows per page default all data. |
|
37 | + * @return \Illuminate\Http\Response |
|
38 | + */ |
|
39 | + public function getReport(Request $request, $reportName, $perPage = 0) |
|
40 | + { |
|
41 | + return \Response::json($this->repo->getReport($reportName, $request->all(), $perPage), 200); |
|
42 | + } |
|
43 | 43 | } |
@@ -7,67 +7,67 @@ |
||
7 | 7 | |
8 | 8 | class Handler extends ExceptionHandler |
9 | 9 | { |
10 | - /** |
|
11 | - * A list of the exception types that are not reported. |
|
12 | - * |
|
13 | - * @var array |
|
14 | - */ |
|
15 | - protected $dontReport = [ |
|
16 | - \League\OAuth2\Server\Exception\OAuthServerException::class, |
|
17 | - ]; |
|
10 | + /** |
|
11 | + * A list of the exception types that are not reported. |
|
12 | + * |
|
13 | + * @var array |
|
14 | + */ |
|
15 | + protected $dontReport = [ |
|
16 | + \League\OAuth2\Server\Exception\OAuthServerException::class, |
|
17 | + ]; |
|
18 | 18 | |
19 | - /** |
|
20 | - * A list of the inputs that are never flashed for validation exceptions. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $dontFlash = [ |
|
25 | - 'password', |
|
26 | - 'password_confirmation', |
|
27 | - ]; |
|
19 | + /** |
|
20 | + * A list of the inputs that are never flashed for validation exceptions. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $dontFlash = [ |
|
25 | + 'password', |
|
26 | + 'password_confirmation', |
|
27 | + ]; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Report or log an exception. |
|
31 | - * |
|
32 | - * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
|
33 | - * |
|
34 | - * @param \Exception $exception |
|
35 | - * @return void |
|
36 | - */ |
|
37 | - public function report(Exception $exception) |
|
38 | - { |
|
39 | - parent::report($exception); |
|
40 | - } |
|
29 | + /** |
|
30 | + * Report or log an exception. |
|
31 | + * |
|
32 | + * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
|
33 | + * |
|
34 | + * @param \Exception $exception |
|
35 | + * @return void |
|
36 | + */ |
|
37 | + public function report(Exception $exception) |
|
38 | + { |
|
39 | + parent::report($exception); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Render an exception into an HTTP response. |
|
44 | - * |
|
45 | - * @param \Illuminate\Http\Request $request |
|
46 | - * @param \Exception $exception |
|
47 | - * @return \Illuminate\Http\Response |
|
48 | - */ |
|
49 | - public function render($request, Exception $exception) |
|
50 | - { |
|
51 | - if ($request->wantsJson()) { |
|
52 | - if ($exception instanceof \Illuminate\Auth\AuthenticationException) { |
|
53 | - \ErrorHandler::unAuthorized(); |
|
54 | - } |
|
55 | - if ($exception instanceof \Illuminate\Database\QueryException) { |
|
56 | - \ErrorHandler::dbQueryError(); |
|
57 | - } elseif ($exception instanceof \predis\connection\connectionexception) { |
|
58 | - \ErrorHandler::redisNotRunning(); |
|
59 | - } elseif ($exception instanceof \GuzzleHttp\Exception\ClientException) { |
|
60 | - \ErrorHandler::connectionError(); |
|
61 | - } elseif ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) { |
|
62 | - $errors = $exception->getStatusCode() === 404 ? 'not found' : $exception->getMessage(); |
|
63 | - return \Response::json(['errors' => [$errors]], $exception->getStatusCode()); |
|
64 | - } elseif ($exception instanceof \Illuminate\Validation\ValidationException) { |
|
65 | - return \Response::json(['errors' => $exception->errors()], 422); |
|
66 | - } elseif (! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) { |
|
67 | - return parent::render($request, $exception); |
|
68 | - } |
|
69 | - } |
|
42 | + /** |
|
43 | + * Render an exception into an HTTP response. |
|
44 | + * |
|
45 | + * @param \Illuminate\Http\Request $request |
|
46 | + * @param \Exception $exception |
|
47 | + * @return \Illuminate\Http\Response |
|
48 | + */ |
|
49 | + public function render($request, Exception $exception) |
|
50 | + { |
|
51 | + if ($request->wantsJson()) { |
|
52 | + if ($exception instanceof \Illuminate\Auth\AuthenticationException) { |
|
53 | + \ErrorHandler::unAuthorized(); |
|
54 | + } |
|
55 | + if ($exception instanceof \Illuminate\Database\QueryException) { |
|
56 | + \ErrorHandler::dbQueryError(); |
|
57 | + } elseif ($exception instanceof \predis\connection\connectionexception) { |
|
58 | + \ErrorHandler::redisNotRunning(); |
|
59 | + } elseif ($exception instanceof \GuzzleHttp\Exception\ClientException) { |
|
60 | + \ErrorHandler::connectionError(); |
|
61 | + } elseif ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) { |
|
62 | + $errors = $exception->getStatusCode() === 404 ? 'not found' : $exception->getMessage(); |
|
63 | + return \Response::json(['errors' => [$errors]], $exception->getStatusCode()); |
|
64 | + } elseif ($exception instanceof \Illuminate\Validation\ValidationException) { |
|
65 | + return \Response::json(['errors' => $exception->errors()], 422); |
|
66 | + } elseif (! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) { |
|
67 | + return parent::render($request, $exception); |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | - return parent::render($request, $exception); |
|
72 | - } |
|
71 | + return parent::render($request, $exception); |
|
72 | + } |
|
73 | 73 | } |
@@ -63,7 +63,7 @@ |
||
63 | 63 | return \Response::json(['errors' => [$errors]], $exception->getStatusCode()); |
64 | 64 | } elseif ($exception instanceof \Illuminate\Validation\ValidationException) { |
65 | 65 | return \Response::json(['errors' => $exception->errors()], 422); |
66 | - } elseif (! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) { |
|
66 | + } elseif ( ! $exception instanceof \Symfony\Component\ErrorHandler\Error\FatalError) { |
|
67 | 67 | return parent::render($request, $exception); |
68 | 68 | } |
69 | 69 | } |
@@ -13,27 +13,27 @@ |
||
13 | 13 | |
14 | 14 | Route::group(['prefix' => 'notifications'], function () { |
15 | 15 | |
16 | - Route::group(['prefix' => 'notifications'], function () { |
|
16 | + Route::group(['prefix' => 'notifications'], function () { |
|
17 | 17 | |
18 | - Route::get('list/{perPage?}', 'NotificationsController@list'); |
|
19 | - Route::get('unread/{perPage?}', 'NotificationsController@unread'); |
|
20 | - Route::get('markAsRead/{id}', 'NotificationsController@markAsRead'); |
|
21 | - Route::get('markAllAsRead', 'NotificationsController@markAllAsRead'); |
|
22 | - }); |
|
18 | + Route::get('list/{perPage?}', 'NotificationsController@list'); |
|
19 | + Route::get('unread/{perPage?}', 'NotificationsController@unread'); |
|
20 | + Route::get('markAsRead/{id}', 'NotificationsController@markAsRead'); |
|
21 | + Route::get('markAllAsRead', 'NotificationsController@markAllAsRead'); |
|
22 | + }); |
|
23 | 23 | |
24 | - Route::group(['prefix' => 'push_notification_devices'], function () { |
|
24 | + Route::group(['prefix' => 'push_notification_devices'], function () { |
|
25 | 25 | |
26 | - Route::get('list/{sortBy?}/{desc?}', 'PushNotificationDevicesController@index'); |
|
27 | - Route::get('find/{id}', 'PushNotificationDevicesController@find'); |
|
28 | - Route::get('search/{query?}/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@search'); |
|
29 | - Route::get('paginate/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@paginate'); |
|
30 | - Route::get('delete/{id}', 'PushNotificationDevicesController@delete'); |
|
31 | - Route::get('restore/{id}', 'PushNotificationDevicesController@restore'); |
|
32 | - Route::post('first', 'PushNotificationDevicesController@first'); |
|
33 | - Route::post('findby/{sortBy?}/{desc?}', 'PushNotificationDevicesController@findby'); |
|
34 | - Route::post('paginateby/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@paginateby'); |
|
35 | - Route::post('save', 'PushNotificationDevicesController@save'); |
|
36 | - Route::post('deleted/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@deleted'); |
|
37 | - Route::post('register/device', 'PushNotificationDevicesController@registerDevice'); |
|
38 | - }); |
|
26 | + Route::get('list/{sortBy?}/{desc?}', 'PushNotificationDevicesController@index'); |
|
27 | + Route::get('find/{id}', 'PushNotificationDevicesController@find'); |
|
28 | + Route::get('search/{query?}/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@search'); |
|
29 | + Route::get('paginate/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@paginate'); |
|
30 | + Route::get('delete/{id}', 'PushNotificationDevicesController@delete'); |
|
31 | + Route::get('restore/{id}', 'PushNotificationDevicesController@restore'); |
|
32 | + Route::post('first', 'PushNotificationDevicesController@first'); |
|
33 | + Route::post('findby/{sortBy?}/{desc?}', 'PushNotificationDevicesController@findby'); |
|
34 | + Route::post('paginateby/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@paginateby'); |
|
35 | + Route::post('save', 'PushNotificationDevicesController@save'); |
|
36 | + Route::post('deleted/{perPage?}/{sortBy?}/{desc?}', 'PushNotificationDevicesController@deleted'); |
|
37 | + Route::post('register/device', 'PushNotificationDevicesController@registerDevice'); |
|
38 | + }); |
|
39 | 39 | }); |
@@ -11,9 +11,9 @@ discard block |
||
11 | 11 | | |
12 | 12 | */ |
13 | 13 | |
14 | -Route::group(['prefix' => 'notifications'], function () { |
|
14 | +Route::group(['prefix' => 'notifications'], function() { |
|
15 | 15 | |
16 | - Route::group(['prefix' => 'notifications'], function () { |
|
16 | + Route::group(['prefix' => 'notifications'], function() { |
|
17 | 17 | |
18 | 18 | Route::get('list/{perPage?}', 'NotificationsController@list'); |
19 | 19 | Route::get('unread/{perPage?}', 'NotificationsController@unread'); |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | Route::get('markAllAsRead', 'NotificationsController@markAllAsRead'); |
22 | 22 | }); |
23 | 23 | |
24 | - Route::group(['prefix' => 'push_notification_devices'], function () { |
|
24 | + Route::group(['prefix' => 'push_notification_devices'], function() { |
|
25 | 25 | |
26 | 26 | Route::get('list/{sortBy?}/{desc?}', 'PushNotificationDevicesController@index'); |
27 | 27 | Route::get('find/{id}', 'PushNotificationDevicesController@find'); |
@@ -5,31 +5,31 @@ |
||
5 | 5 | |
6 | 6 | class PushNotificationsDevices extends Migration |
7 | 7 | { |
8 | - /** |
|
9 | - * Run the migrations. |
|
10 | - * |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function up() |
|
14 | - { |
|
15 | - Schema::create('push_notification_devices', function (Blueprint $table) { |
|
16 | - $table->increments('id'); |
|
17 | - $table->string('device_token'); |
|
18 | - $table->integer('user_id'); |
|
19 | - $table->text('access_token')->nullable(); |
|
20 | - $table->unique(array('device_token', 'user_id')); |
|
21 | - $table->softDeletes(); |
|
22 | - $table->timestamps(); |
|
23 | - }); |
|
24 | - } |
|
8 | + /** |
|
9 | + * Run the migrations. |
|
10 | + * |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function up() |
|
14 | + { |
|
15 | + Schema::create('push_notification_devices', function (Blueprint $table) { |
|
16 | + $table->increments('id'); |
|
17 | + $table->string('device_token'); |
|
18 | + $table->integer('user_id'); |
|
19 | + $table->text('access_token')->nullable(); |
|
20 | + $table->unique(array('device_token', 'user_id')); |
|
21 | + $table->softDeletes(); |
|
22 | + $table->timestamps(); |
|
23 | + }); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Reverse the migrations. |
|
28 | - * |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function down() |
|
32 | - { |
|
33 | - Schema::dropIfExists('push_notifications_devices'); |
|
34 | - } |
|
26 | + /** |
|
27 | + * Reverse the migrations. |
|
28 | + * |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function down() |
|
32 | + { |
|
33 | + Schema::dropIfExists('push_notifications_devices'); |
|
34 | + } |
|
35 | 35 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | */ |
13 | 13 | public function up() |
14 | 14 | { |
15 | - Schema::create('push_notification_devices', function (Blueprint $table) { |
|
15 | + Schema::create('push_notification_devices', function(Blueprint $table) { |
|
16 | 16 | $table->increments('id'); |
17 | 17 | $table->string('device_token'); |
18 | 18 | $table->integer('user_id'); |
@@ -6,21 +6,21 @@ |
||
6 | 6 | |
7 | 7 | class Notification extends JsonResource |
8 | 8 | { |
9 | - /** |
|
10 | - * Transform the resource into an array. |
|
11 | - * |
|
12 | - * @param \Illuminate\Http\Request $request |
|
13 | - * @return array |
|
14 | - */ |
|
15 | - public function toArray($request) |
|
16 | - { |
|
17 | - return [ |
|
18 | - 'id' => $this->id, |
|
19 | - 'type' => $this->type, |
|
20 | - 'data' => $this->data, |
|
21 | - 'readAt' => $this->read_at, |
|
22 | - 'createdAt' => $this->created_at, |
|
23 | - 'updatedAt' => $this->updated_at, |
|
24 | - ]; |
|
25 | - } |
|
9 | + /** |
|
10 | + * Transform the resource into an array. |
|
11 | + * |
|
12 | + * @param \Illuminate\Http\Request $request |
|
13 | + * @return array |
|
14 | + */ |
|
15 | + public function toArray($request) |
|
16 | + { |
|
17 | + return [ |
|
18 | + 'id' => $this->id, |
|
19 | + 'type' => $this->type, |
|
20 | + 'data' => $this->data, |
|
21 | + 'readAt' => $this->read_at, |
|
22 | + 'createdAt' => $this->created_at, |
|
23 | + 'updatedAt' => $this->updated_at, |
|
24 | + ]; |
|
25 | + } |
|
26 | 26 | } |
@@ -7,22 +7,22 @@ |
||
7 | 7 | |
8 | 8 | class PushNotificationDevice extends JsonResource |
9 | 9 | { |
10 | - /** |
|
11 | - * Transform the resource into an array. |
|
12 | - * |
|
13 | - * @param \Illuminate\Http\Request $request |
|
14 | - * @return array |
|
15 | - */ |
|
16 | - public function toArray($request) |
|
17 | - { |
|
18 | - return [ |
|
19 | - 'id' => $this->id, |
|
20 | - 'deviceToken' => $this->device_token, |
|
21 | - 'accessToken' => $this->access_token, |
|
22 | - 'user' => new UserResource($this->whenLoaded('user')), |
|
23 | - 'timeZone' => $this->time_zone, |
|
24 | - 'createdAt' => $this->created_at, |
|
25 | - 'updatedAt' => $this->updated_at, |
|
26 | - ]; |
|
27 | - } |
|
10 | + /** |
|
11 | + * Transform the resource into an array. |
|
12 | + * |
|
13 | + * @param \Illuminate\Http\Request $request |
|
14 | + * @return array |
|
15 | + */ |
|
16 | + public function toArray($request) |
|
17 | + { |
|
18 | + return [ |
|
19 | + 'id' => $this->id, |
|
20 | + 'deviceToken' => $this->device_token, |
|
21 | + 'accessToken' => $this->access_token, |
|
22 | + 'user' => new UserResource($this->whenLoaded('user')), |
|
23 | + 'timeZone' => $this->time_zone, |
|
24 | + 'createdAt' => $this->created_at, |
|
25 | + 'updatedAt' => $this->updated_at, |
|
26 | + ]; |
|
27 | + } |
|
28 | 28 | } |