firefly-iii/app/Api/V1/Requests/UserUpdateRequest.php

101 lines
2.6 KiB
PHP
Raw Normal View History

2018-03-03 01:12:18 -06:00
<?php
2018-05-11 03:08:34 -05:00
2018-03-03 01:12:18 -06:00
/**
2019-08-22 10:06:43 -05:00
* UserUpdateRequest.php
2018-03-03 01:12:18 -06:00
* 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 03:08:34 -05:00
declare(strict_types=1);
2018-03-03 01:12:18 -06:00
namespace FireflyIII\Api\V1\Requests;
2018-07-04 23:10:35 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-12-03 00:18:05 -06:00
use FireflyIII\Rules\IsBoolean;
2018-03-03 01:12:18 -06:00
use FireflyIII\User;
/**
2019-08-22 10:06:43 -05:00
* Class UserUpdateRequest
2018-03-03 01:12:18 -06:00
*/
2019-08-22 10:06:43 -05:00
class UserUpdateRequest extends Request
2018-03-03 01:12:18 -06:00
{
/**
* Authorize logged in users.
*
2018-03-03 01:12:18 -06:00
* @return bool
*/
public function authorize(): bool
{
$result = false;
2018-03-03 01:12:18 -06:00
// Only allow authenticated users
if (auth()->check()) {
/** @var User $user */
$user = auth()->user();
2018-07-04 23:10:35 -05:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
2018-07-04 23:10:35 -05:00
if ($repository->hasRole($user, 'owner')) {
$result = true; // @codeCoverageIgnore
}
2018-03-03 01:12:18 -06:00
}
return $result;
2018-03-03 01:12:18 -06:00
}
/**
* Get all data from the request.
*
2018-03-03 01:12:18 -06:00
* @return array
*/
public function getAll(): array
{
2018-12-21 08:42:57 -06:00
$blocked = false;
2019-08-22 10:06:43 -05:00
if (null !== $this->get('blocked')) {
2018-12-21 08:42:57 -06:00
$blocked = $this->boolean('blocked');
}
2018-03-03 01:12:18 -06:00
$data = [
'email' => $this->string('email'),
2018-12-21 08:42:57 -06:00
'blocked' => $blocked,
2018-03-03 01:12:18 -06:00
'blocked_code' => $this->string('blocked_code'),
2018-12-03 00:18:05 -06:00
'role' => $this->string('role'),
2018-03-03 01:12:18 -06:00
];
return $data;
}
/**
* The rules that the incoming request must be matched against.
*
2018-03-03 01:12:18 -06:00
* @return array
*/
public function rules(): array
{
2019-08-22 10:06:43 -05:00
$user = $this->route()->parameter('user');
2018-03-03 01:12:18 -06:00
$rules = [
2019-08-22 10:06:43 -05:00
'email' => sprintf('email|unique:users,email,%d', $user->id),
2018-12-03 00:18:05 -06:00
'blocked' => [new IsBoolean],
2018-03-03 01:12:18 -06:00
'blocked_code' => 'in:email_changed',
2019-08-22 10:06:43 -05:00
'role' => 'in:owner,demo,',
2018-03-03 01:12:18 -06:00
];
return $rules;
}
2018-03-05 12:35:58 -06:00
}