67 lines
1.5 KiB
PHP
67 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Rules;
|
||
|
|
|
||
|
|
use Illuminate\Contracts\Validation\Rule;
|
||
|
|
|
||
|
|
class HasMinimumRole implements Rule
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The minimum role the user must have
|
||
|
|
* @var integer
|
||
|
|
*/
|
||
|
|
protected $minimumRole;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Ordre permettant une comparaison
|
||
|
|
* ! EN DUR est n'est pas la value de arcadia puisqu'il n'a pas de sens.
|
||
|
|
*
|
||
|
|
* @var array
|
||
|
|
*/
|
||
|
|
protected $roles = [
|
||
|
|
'ADMIN' => 1, 'REDACTEUR' => 2,
|
||
|
|
'CONTRIBUTEUR' => 3, 'LECTEUR' => 4
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new rule instance.
|
||
|
|
*
|
||
|
|
* @param string $role
|
||
|
|
* @throws \ErrorException If role is unknown.
|
||
|
|
*/
|
||
|
|
public function __construct(string $role)
|
||
|
|
{
|
||
|
|
if (!in_array(\intval($role), \array_keys($this->roles))) {
|
||
|
|
throw new \ErrorException("Unknown role $role");
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->minimumRole = \intval($this->roles[$role]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Determine if the validation rule passes.
|
||
|
|
*
|
||
|
|
* @param mixed $attribute
|
||
|
|
* @param mixed $value
|
||
|
|
* @return boolean
|
||
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
|
||
|
|
*/
|
||
|
|
public function passes($attribute, $value)
|
||
|
|
{
|
||
|
|
if (!\ctype_digit(strval($value))) {
|
||
|
|
return false;
|
||
|
|
};
|
||
|
|
return \intval($value) <= $this->minimumRole;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the validation error message.
|
||
|
|
*
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function message()
|
||
|
|
{
|
||
|
|
return 'L\'attribut :attribute :input n\'est pas suffisament élevé.';
|
||
|
|
}
|
||
|
|
}
|