2016-06-06 14:15:50 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2017-08-22 20:32:39 -07:00
|
|
|
use App\Models\Setting;
|
2019-07-18 14:32:23 -07:00
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2021-06-10 13:15:52 -07:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
2016-06-06 14:15:50 -07:00
|
|
|
|
2019-07-18 14:32:23 -07:00
|
|
|
class SaveUserRequest extends FormRequest
|
2016-06-06 14:15:50 -07:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:00:24 -08:00
|
|
|
public function response(array $errors)
|
|
|
|
{
|
|
|
|
return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag);
|
|
|
|
}
|
|
|
|
|
2016-06-06 14:15:50 -07:00
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2024-08-14 11:53:29 -07:00
|
|
|
//dd($this->user);
|
2020-05-23 11:58:44 -07:00
|
|
|
$rules = [
|
2023-09-07 13:28:32 -07:00
|
|
|
'department_id' => 'nullable|exists:departments,id',
|
2021-06-10 13:15:52 -07:00
|
|
|
'manager_id' => 'nullable|exists:users,id',
|
2024-08-14 11:53:29 -07:00
|
|
|
'company_id' => [
|
|
|
|
// determines if the user is being moved between companies and checks to see if they have any items assigned
|
|
|
|
function ($attribute, $value, $fail) {
|
|
|
|
dd($this->user);
|
|
|
|
if (($this->has('company_id')) && ($this->user->allAssignedCount() > 0) && (Setting::getSettings()->full_multiple_companies_support)) {
|
|
|
|
$fail(trans('admin/users/message.error.multi_company_items_assigned'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2020-05-23 11:58:44 -07:00
|
|
|
];
|
2017-08-22 20:32:39 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
switch ($this->method()) {
|
2017-08-22 21:15:35 -07:00
|
|
|
|
2017-10-24 16:56:46 -07:00
|
|
|
// Brand new user
|
2017-08-22 21:15:35 -07:00
|
|
|
case 'POST':
|
|
|
|
$rules['first_name'] = 'required|string|min:1';
|
2017-08-31 10:43:36 -07:00
|
|
|
$rules['username'] = 'required_unless:ldap_import,1|string|min:1';
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($this->request->get('ldap_import') == false) {
|
2019-02-13 23:01:19 -08:00
|
|
|
$rules['password'] = Setting::passwordComplexityRulesSaving('store').'|confirmed';
|
2018-04-20 14:02:52 -07:00
|
|
|
}
|
2017-10-24 16:56:46 -07:00
|
|
|
break;
|
2017-08-22 21:15:35 -07:00
|
|
|
|
|
|
|
// Save all fields
|
|
|
|
case 'PUT':
|
|
|
|
$rules['first_name'] = 'required|string|min:1';
|
2017-08-31 10:43:36 -07:00
|
|
|
$rules['username'] = 'required_unless:ldap_import,1|string|min:1';
|
2019-02-13 23:01:19 -08:00
|
|
|
$rules['password'] = Setting::passwordComplexityRulesSaving('update').'|confirmed';
|
2017-10-24 16:56:46 -07:00
|
|
|
break;
|
2017-08-22 21:15:35 -07:00
|
|
|
|
|
|
|
// Save only what's passed
|
|
|
|
case 'PATCH':
|
|
|
|
$rules['password'] = Setting::passwordComplexityRulesSaving('update');
|
2017-10-24 16:56:46 -07:00
|
|
|
break;
|
2017-08-22 21:15:35 -07:00
|
|
|
|
2023-02-06 12:44:17 -08:00
|
|
|
default:
|
|
|
|
break;
|
2017-08-22 20:32:39 -07:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return $rules;
|
2016-06-06 14:15:50 -07:00
|
|
|
}
|
|
|
|
}
|