69 lines
1.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;
class User extends FormRequest
{
2021-04-29 15:19:05 +03:00
2017-09-14 22:21:00 +03:00
/**
* 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=1000,max_height=1000';
2019-11-16 10:21:14 +03:00
}
2021-04-29 16:09:10 +03:00
$email = 'required|email';
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
$password = '';
$companies = $this->user->can('read-common-companies') ? 'required' : '';
$roles = $this->user->can('read-auth-roles') ? 'required' : '';
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
$password = 'required|';
$companies = 'required';
$roles = 'required';
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
2017-09-14 22:21:00 +03:00
return [
'name' => 'required|string',
2021-04-29 15:19:05 +03:00
'email' => $email,
2020-10-05 11:11:59 +03:00
'password' => $password . 'confirmed',
'companies' => $companies,
'roles' => $roles,
2019-11-16 10:21:14 +03:00
'picture' => $picture,
2017-09-14 22:21:00 +03:00
];
}
}