mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-28 14:10:51 -08:00
Validator for users switching companies
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
b64ed254e0
commit
17cc632a3b
|
@ -6,6 +6,7 @@ use App\Models\Setting;
|
||||||
use Illuminate\Contracts\Validation\Validator;
|
use Illuminate\Contracts\Validation\Validator;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||||
|
use App\Rules\UserCannotSwitchCompaniesIfItemsAssigned;
|
||||||
|
|
||||||
class SaveUserRequest extends FormRequest
|
class SaveUserRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
@ -35,12 +36,9 @@ class SaveUserRequest extends FormRequest
|
||||||
'department_id' => 'nullable|exists:departments,id',
|
'department_id' => 'nullable|exists:departments,id',
|
||||||
'manager_id' => 'nullable|exists:users,id',
|
'manager_id' => 'nullable|exists:users,id',
|
||||||
'company_id' => [
|
'company_id' => [
|
||||||
// determines if the user is being moved between companies and checks to see if they have any items assigned
|
'nullable',
|
||||||
function ($attribute, $value, $fail) {
|
'exists:companies,id',
|
||||||
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'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -60,11 +58,13 @@ class SaveUserRequest extends FormRequest
|
||||||
$rules['first_name'] = 'required|string|min:1';
|
$rules['first_name'] = 'required|string|min:1';
|
||||||
$rules['username'] = 'required_unless:ldap_import,1|string|min:1';
|
$rules['username'] = 'required_unless:ldap_import,1|string|min:1';
|
||||||
$rules['password'] = Setting::passwordComplexityRulesSaving('update').'|confirmed';
|
$rules['password'] = Setting::passwordComplexityRulesSaving('update').'|confirmed';
|
||||||
|
$rules['company_id'] = [new UserCannotSwitchCompaniesIfItemsAssigned()];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Save only what's passed
|
// Save only what's passed
|
||||||
case 'PATCH':
|
case 'PATCH':
|
||||||
$rules['password'] = Setting::passwordComplexityRulesSaving('update');
|
$rules['password'] = Setting::passwordComplexityRulesSaving('update');
|
||||||
|
$rules['company_id'] = [new UserCannotSwitchCompaniesIfItemsAssigned()];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
26
app/Rules/UserCannotSwitchCompaniesIfItemsAssigned.php
Normal file
26
app/Rules/UserCannotSwitchCompaniesIfItemsAssigned.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use App\Models\Setting;
|
||||||
|
use App\Models\User;
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Contracts\Validation\DataAwareRule;
|
||||||
|
|
||||||
|
class UserCannotSwitchCompaniesIfItemsAssigned implements ValidationRule
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the validation rule.
|
||||||
|
*
|
||||||
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
||||||
|
*/
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail) : void
|
||||||
|
{
|
||||||
|
$user = User::find(request()->route('user')->id);
|
||||||
|
if (($value) && ($user->allAssignedCount() > 0) && (Setting::getSettings()->full_multiple_companies_support)) {
|
||||||
|
$fail(trans('admin/users/message.error.multi_company_items_assigned'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue