2019-04-22 19:01:39 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
use LibreNMS\Authentication\LegacyAuth;
|
|
|
|
|
use LibreNMS\Config;
|
2023-08-28 00:13:40 -05:00
|
|
|
use Silber\Bouncer\BouncerFacade as Bouncer;
|
2019-04-22 19:01:39 -05:00
|
|
|
|
|
|
|
|
class StoreUserRequest extends FormRequest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-05-24 22:21:54 +02:00
|
|
|
public function authorize(): bool
|
2019-04-22 19:01:39 -05:00
|
|
|
{
|
2023-08-28 00:13:40 -05:00
|
|
|
if ($this->user()->can('create', User::class)) {
|
|
|
|
|
if ($this->user()->cannot('manage', Bouncer::role())) {
|
|
|
|
|
unset($this['roles']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2019-04-22 19:01:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-05-24 22:21:54 +02:00
|
|
|
public function rules(): array
|
2019-04-22 19:01:39 -05:00
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'username' => [
|
|
|
|
|
'required',
|
|
|
|
|
'alpha_dash',
|
|
|
|
|
'max:255',
|
|
|
|
|
Rule::unique('users', 'username')->where('auth_type', LegacyAuth::getType()),
|
|
|
|
|
],
|
2019-08-21 20:36:22 -05:00
|
|
|
'realname' => 'nullable|max:64|alpha_space',
|
2019-04-22 19:01:39 -05:00
|
|
|
'email' => 'nullable|email|max:64',
|
2019-08-21 20:36:22 -05:00
|
|
|
'descr' => 'nullable|max:30|alpha_space',
|
2023-08-28 00:13:40 -05:00
|
|
|
'roles' => 'array',
|
|
|
|
|
'roles.*' => Rule::in(Bouncer::role()->pluck('name')),
|
2019-04-22 19:01:39 -05:00
|
|
|
'new_password' => 'required|confirmed|min:' . Config::get('password.min_length', 8),
|
|
|
|
|
'dashboard' => 'int',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|