| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * UserRequest.php | 
					
						
							|  |  |  |  * Copyright (c) 2018 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Api\V1\Requests; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-05 06:10:35 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-12-03 07:18:05 +01:00
										 |  |  | use FireflyIII\Rules\IsBoolean; | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class UserRequest | 
					
						
							| 
									
										
										
										
											2019-04-12 04:53:18 +02:00
										 |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class UserRequest extends Request | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Authorize logged in users. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function authorize(): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |         $result = false; | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |         // Only allow authenticated users
 | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |         if (auth()->check()) { | 
					
						
							|  |  |  |             /** @var User $user */ | 
					
						
							|  |  |  |             $user = auth()->user(); | 
					
						
							| 
									
										
										
										
											2018-07-05 06:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |             /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |             $repository = app(UserRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2018-07-05 06:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |             if ($repository->hasRole($user, 'owner')) { | 
					
						
							|  |  |  |                 $result = true; // @codeCoverageIgnore
 | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |         return $result; | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * Get all data from the request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getAll(): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:57 +01:00
										 |  |  |         $blocked = false; | 
					
						
							|  |  |  |         if (null === $this->get('blocked')) { | 
					
						
							|  |  |  |             $blocked = $this->boolean('blocked'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |         $data = [ | 
					
						
							|  |  |  |             'email'        => $this->string('email'), | 
					
						
							| 
									
										
										
										
											2018-12-21 15:42:57 +01:00
										 |  |  |             'blocked'      => $blocked, | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |             'blocked_code' => $this->string('blocked_code'), | 
					
						
							| 
									
										
										
										
											2018-12-03 07:18:05 +01:00
										 |  |  |             'role'         => $this->string('role'), | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-06 07:15:42 +02:00
										 |  |  |      * The rules that the incoming request must be matched against. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function rules(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $rules = [ | 
					
						
							|  |  |  |             'email'        => 'required|email|unique:users,email,', | 
					
						
							| 
									
										
										
										
											2018-12-03 07:18:05 +01:00
										 |  |  |             'blocked'      => [new IsBoolean], | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |             'blocked_code' => 'in:email_changed', | 
					
						
							| 
									
										
										
										
											2018-12-03 07:18:05 +01:00
										 |  |  |             'role'         => 'in:owner,demo', | 
					
						
							| 
									
										
										
										
											2018-03-03 08:12:18 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         switch ($this->method()) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'PUT': | 
					
						
							|  |  |  |             case 'PATCH': | 
					
						
							|  |  |  |                 $user           = $this->route()->parameter('user'); | 
					
						
							|  |  |  |                 $rules['email'] = 'required|email|unique:users,email,' . $user->id; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $rules; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |