89 lines
2.9 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Requests\Auth;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
2017-09-14 22:21:00 +03:00
class User extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
2019-11-16 10:21:14 +03:00
$picture = 'nullable';
2021-05-14 18:29:24 +03:00
if ($this->files->get('picture')) {
$picture = 'mimes:' . config('filesystems.mimes')
. '|between:0,' . config('filesystems.max_size') * 1024
. '|dimensions:max_width=' . config('filesystems.max_width') . ',max_height=' . config('filesystems.max_height');
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
$email = 'required|email:rfc,dns';
2021-04-29 15:19:05 +03:00
2017-09-14 22:21:00 +03:00
if ($this->getMethod() == 'PATCH') {
2020-10-05 11:11:59 +03:00
// Updating user
2019-11-16 10:21:14 +03:00
$id = is_numeric($this->user) ? $this->user : $this->user->getAttribute('id');
2020-10-05 11:11:59 +03:00
$companies = $this->user->can('read-common-companies') ? 'required' : '';
2022-06-01 10:15:55 +03:00
$roles = $this->user->can('read-auth-roles') ? 'required|string' : '';
2021-04-29 15:19:05 +03:00
if ($this->user->contact) {
2021-04-29 16:09:10 +03:00
$email .= '|unique:contacts,NULL,'
. $this->user->contact->id . ',id'
. ',company_id,' . company_id()
. ',type,customer'
. ',deleted_at,NULL';
2021-04-29 15:19:05 +03:00
}
2017-09-14 22:21:00 +03:00
} else {
2020-10-05 11:11:59 +03:00
// Creating user
2017-09-14 22:21:00 +03:00
$id = null;
2020-10-05 11:11:59 +03:00
$companies = 'required';
2022-06-01 10:15:55 +03:00
$roles = 'required|string';
2017-09-14 22:21:00 +03:00
}
2021-04-29 16:09:10 +03:00
$email .= '|unique:users,email,' . $id . ',id,deleted_at,NULL';
2021-04-29 15:19:05 +03:00
2022-06-01 10:15:55 +03:00
$change_password = $this->request->get('change_password') == true || $this->request->get('change_password') != null;
$current_password = $change_password ? '|current_password' : '';
$password = $change_password ? '|confirmed' : '';
2017-09-14 22:21:00 +03:00
return [
'name' => 'required|string',
'email' => $email,
'current_password' => 'required_if:change_password,true' . $current_password,
'password' => 'required_if:change_password,true' . $password,
'companies' => $companies,
'roles' => $roles,
'picture' => $picture,
2022-12-16 11:24:57 +03:00
'landing_page' => 'required|string',
];
}
public function messages()
{
$picture_dimensions = trans('validation.custom.invalid_dimension', [
'attribute' => Str::lower(trans_choice('general.pictures', 1)),
'width' => config('filesystems.max_width'),
'height' => config('filesystems.max_height'),
]);
return [
'picture.dimensions' => $picture_dimensions,
2017-09-14 22:21:00 +03:00
];
}
}