Role::class ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'role', 'name', 'email', 'password', 'site_id' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; public function __construct($attributes = []) { self::$editableProperties = [ 'role' => [ 'label' => trans('Role'), 'type' => CrudType::enum(), 'enum' => Role::class, 'readonly' => function () { return auth()->user()->role->equals(Role::admin()) ? false : true; }, 'validate' => [ 'required', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed function ($attribute, $value, $fail) { if (!in_array($value, Role::toValues())) { $fail(trans('Veuillez choisir entre :values', [ 'value' => implode(',', Role::toLabels()) ])); } } ], 'actions' => [ CrudAction::viewAny(), CrudAction::view(), CrudAction::create(), CrudAction::update() ] ], 'name' => [ 'label' => trans('Nom'), 'type' => CrudType::string(), 'validate' => [ 'string', 'max:255' ], 'actions' => [ CrudAction::viewAny(), CrudAction::view(), CrudAction::create(), CrudAction::update(), ] ], 'email' => [ 'label' => trans('Email'), 'type' => CrudType::email(), 'nullable' => false, 'validate' => [ 'max:255', // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed function ($attribute, $value, $fail) { $q = User::query(); if ($this->id) { $q = $q->where('id', '!=', $this->id); } if ($q->where('email', $value)->exists()) { $fail(__(':attribute est déjà utilisé', [ 'attribute' => $attribute ])); } } ], 'actions' => [ CrudAction::viewAny(), CrudAction::view(), CrudAction::create(), CrudAction::update(), ] ], 'password' => [ 'label' => trans('Mot de passe'), 'placeholder' => '#M1s3Cure.P4ss0rd!', 'type' => CrudType::password(), 'nullable' => true, 'required' => false, 'validate' => [ 'nullable', 'string', 'max:255' ], 'actions' => [ CrudAction::create(), CrudAction::update() ] ], 'sites' => [ 'label' => trans('Sites'), 'type' => CrudType::belongsToMany(), 'belongsToMany' => Site::class, 'belongsToManyLabel' => 'name', 'readonly' => function () { return auth()->user()->role->equals(Role::admin()) ? false : true; }, 'actions' => [ CrudAction::viewAny(), CrudAction::view(), CrudAction::create(), CrudAction::update(), ] ] ]; parent::__construct($attributes); } protected static function booted() { static::creating(function ($user) { $user->password = Hash::make($user->password); $user->remember_token = Str::random(10); }); static::updating(function ($user) { if (Hash::needsRehash($user->password)) { $user->password = Hash::make($user->password); } $user->remember_token = Str::random(10); }); } /** * Belongs To Many Sites * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function sites() { return $this->belongsToMany(Site::class, 'user_site'); } /** * Get User Selected Site * * @return Site|null */ public function selectedSite() { return Site::where('id', $this->selected_site_id)->first(); } }