148 lines
3.3 KiB
PHP
148 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace common\components;
|
|
|
|
use \Yii;
|
|
|
|
class RoleDefinition
|
|
{
|
|
|
|
public static $ROLE_ADMIN = "admin";
|
|
public static $ROLE_RECEPTION = "reception";
|
|
public static $ROLE_EMPLOYEE = "employee";
|
|
public static $ROLE_TRAINER = "trainer";
|
|
|
|
|
|
public static function roleLabels()
|
|
{
|
|
return [
|
|
'reception' => Yii::t('common/role', 'Reception'),
|
|
'admin' => Yii::t('common/role', 'Administrator'),
|
|
'employee' => Yii::t('common/role', 'Employee'),
|
|
'Trainer' => Yii::t('common/role', 'Edző'),
|
|
];
|
|
}
|
|
|
|
public static function getRoleLabel($role)
|
|
{
|
|
$result = null;
|
|
$roleLabels = self::roleLabels();
|
|
if (array_key_exists($role, $roleLabels)) {
|
|
$result = $roleLabels[$role];
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
public static function roleDefinitions()
|
|
{
|
|
return [
|
|
'employee' => [
|
|
'canAllow' => ['employee'],
|
|
],
|
|
'admin' => [
|
|
'canAllow' => ['admin', 'reception', 'employee'],
|
|
],
|
|
'reception' => [
|
|
'canAllow' => [],
|
|
],
|
|
];
|
|
}
|
|
|
|
|
|
public static function getRoleDefinition($role)
|
|
{
|
|
$defs = self::roleDefinitions();
|
|
$result = null;
|
|
if (array_key_exists($role, $defs)) {
|
|
$result = $defs[$role];
|
|
}
|
|
$result = $defs[$role];
|
|
return $result;
|
|
}
|
|
|
|
public static function getRolesCanAllow($role)
|
|
{
|
|
$result = [];
|
|
$def = self::getRoleDefinition($role);
|
|
if (isset($def)) {
|
|
$result = $def['canAllow'];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function can($role)
|
|
{
|
|
$result = false;
|
|
if (!Yii::$app->user->isGuest) {
|
|
if (isset($role)) {
|
|
if (is_array($role)) {
|
|
foreach ($role as $r) {
|
|
$result |= Yii::$app->user->can($r);
|
|
}
|
|
} else if (is_string($role)) {
|
|
$result = Yii::$app->user->can($role);
|
|
}
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function canAny($roles)
|
|
{
|
|
foreach ($roles as $role) {
|
|
if (self::can($role)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function isAdmin()
|
|
{
|
|
return self::can('admin');
|
|
}
|
|
|
|
public static function isReception()
|
|
{
|
|
return self::can('reception');
|
|
}
|
|
|
|
public static function isEmployee()
|
|
{
|
|
return self::can('employee');
|
|
}
|
|
|
|
public static function isTrainer()
|
|
{
|
|
return self::can('trainer');
|
|
}
|
|
|
|
|
|
public static function isLoggedUser()
|
|
{
|
|
return self::isTrainer() || self::isAdmin() || self::isEmployee()
|
|
|| self::isReception();
|
|
}
|
|
|
|
/*
|
|
* [
|
|
* 'role1' => 'template1',
|
|
* 'role2' => 'template2,
|
|
* ]
|
|
* */
|
|
public static function getRoleTemplate($templates)
|
|
{
|
|
$result = "";
|
|
foreach ($templates as $role => $template) {
|
|
if (Yii::$app->user->can($role)) {
|
|
$result = $template;
|
|
break;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
}
|