assign trainers to user; add email to jwt token
This commit is contained in:
@@ -1,118 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace common\components;
|
||||
|
||||
use \Yii;
|
||||
|
||||
class RoleDefinition{
|
||||
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 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 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 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 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'];
|
||||
}
|
||||
public static function getRolesCanAllow($role)
|
||||
{
|
||||
$result = [];
|
||||
$def = self::getRoleDefinition($role);
|
||||
if (isset($def)) {
|
||||
$result = $def['canAllow'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
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 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 isAdmin(){
|
||||
return self::can('admin');
|
||||
}
|
||||
public static function canAny($roles)
|
||||
{
|
||||
foreach ($roles as $role) {
|
||||
if (self::can($role)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isReception(){
|
||||
return self::can('reception');
|
||||
}
|
||||
public static function isAdmin()
|
||||
{
|
||||
return self::can('admin');
|
||||
}
|
||||
|
||||
public static function isEmployee(){
|
||||
return self::can('employee');
|
||||
}
|
||||
public static function isReception()
|
||||
{
|
||||
return self::can('reception');
|
||||
}
|
||||
|
||||
public static function isTrainer(){
|
||||
return self::can('trainer');
|
||||
}
|
||||
public static function isEmployee()
|
||||
{
|
||||
return self::can('employee');
|
||||
}
|
||||
|
||||
public static function isTrainer()
|
||||
{
|
||||
return self::can('trainer');
|
||||
}
|
||||
|
||||
|
||||
public static function isLoggedUser(){
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
* [
|
||||
* '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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,22 +12,22 @@ use yii\helpers\ArrayHelper;
|
||||
* This is the model class for table "account".
|
||||
*
|
||||
* @property integer $id_account
|
||||
* @property string $name
|
||||
* @property string $name
|
||||
* @property integer $status
|
||||
* @property integer $type
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property integer $log_card_read_in_reception
|
||||
*/
|
||||
class Account extends \yii\db\ActiveRecord
|
||||
{
|
||||
|
||||
const STATUS_DELETED = 0;
|
||||
const STATUS_ACTIVE = 10;
|
||||
|
||||
const TYPE_ALL = 0;
|
||||
const TYPE_VALUE_HIDDEN = 10;
|
||||
|
||||
|
||||
const STATUS_DELETED = 0;
|
||||
const STATUS_ACTIVE = 10;
|
||||
|
||||
const TYPE_ALL = 0;
|
||||
const TYPE_VALUE_HIDDEN = 10;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@@ -41,13 +41,15 @@ class Account extends \yii\db\ActiveRecord
|
||||
*/
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[ 'class' => TimestampBehavior::className(),
|
||||
'value' => function(){ return date('Y-m-d H:i:s' ); }
|
||||
]
|
||||
];
|
||||
return [
|
||||
['class' => TimestampBehavior::className(),
|
||||
'value' => function () {
|
||||
return date('Y-m-d H:i:s');
|
||||
}
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@@ -55,8 +57,8 @@ class Account extends \yii\db\ActiveRecord
|
||||
{
|
||||
return [
|
||||
[['name', 'type'], 'required'],
|
||||
[['name', ], 'unique'],
|
||||
[['status', 'type','log_card_read_in_reception'], 'integer'],
|
||||
[['name',], 'unique'],
|
||||
[['status', 'type', 'log_card_read_in_reception'], 'integer'],
|
||||
[['name'], 'string', 'max' => 64]
|
||||
];
|
||||
}
|
||||
@@ -76,45 +78,51 @@ class Account extends \yii\db\ActiveRecord
|
||||
'log_card_read_in_reception' => Yii::t('common/account', 'Log Card Read in Reception'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getUserAccountAssignments(){
|
||||
return $this->hasMany(UserAccountAssignment::className(), ['id_account' => 'id_account']);
|
||||
|
||||
public function getUserAccountAssignments()
|
||||
{
|
||||
return $this->hasMany(UserAccountAssignment::className(), ['id_account' => 'id_account']);
|
||||
}
|
||||
|
||||
static function statuses() {
|
||||
return [
|
||||
self::STATUS_ACTIVE => Yii::t('common/account', 'Active'),
|
||||
self::STATUS_DELETED => Yii::t('common/account', 'Inactive'),
|
||||
];
|
||||
|
||||
static function statuses()
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => Yii::t('common/account', 'Active'),
|
||||
self::STATUS_DELETED => Yii::t('common/account', 'Inactive'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getStatusHuman(){
|
||||
$result = null;
|
||||
$s = self::statuses();
|
||||
if ( array_key_exists($this->status, $s)){
|
||||
$result = $s[$this->status];
|
||||
}
|
||||
return $result;
|
||||
|
||||
public function getStatusHuman()
|
||||
{
|
||||
$result = null;
|
||||
$s = self::statuses();
|
||||
if (array_key_exists($this->status, $s)) {
|
||||
$result = $s[$this->status];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
static function types() {
|
||||
return [
|
||||
self::TYPE_ALL => Yii::t('common/account', 'Account'),
|
||||
self::TYPE_VALUE_HIDDEN => Yii::t('common/account', 'Only the name is visible'),
|
||||
];
|
||||
|
||||
static function types()
|
||||
{
|
||||
return [
|
||||
self::TYPE_ALL => Yii::t('common/account', 'Account'),
|
||||
self::TYPE_VALUE_HIDDEN => Yii::t('common/account', 'Only the name is visible'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTypeHuman(){
|
||||
$result = null;
|
||||
$s = self::types();
|
||||
if ( array_key_exists($this->type, $s)){
|
||||
$result = $s[$this->type];
|
||||
}
|
||||
return $result;
|
||||
|
||||
public function getTypeHuman()
|
||||
{
|
||||
$result = null;
|
||||
$s = self::types();
|
||||
if (array_key_exists($this->type, $s)) {
|
||||
$result = $s[$this->type];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function isInactive(){
|
||||
return $this->status == self::STATUS_DELETED;
|
||||
|
||||
public function isInactive()
|
||||
{
|
||||
return $this->status == self::STATUS_DELETED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,76 +134,82 @@ class Account extends \yii\db\ActiveRecord
|
||||
* three arm gate, but we want to track the customers.
|
||||
* @return bool
|
||||
*/
|
||||
public function isLogCardReadInReceptionOn(){
|
||||
public function isLogCardReadInReceptionOn()
|
||||
{
|
||||
return $this->log_card_read_in_reception == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive
|
||||
* @param null $forceIncludeAccount the next account should be included too, even if it is not
|
||||
* @param null $forceIncludeAccount the next account should be included too, even if it is not
|
||||
* allowed for user
|
||||
* @return array|null|\yii\db\ActiveRecord[]
|
||||
*/
|
||||
public static function readAccounts($forceIncludeAccount = null){
|
||||
$accounts = null;
|
||||
|
||||
if ( $forceIncludeAccount == null) {
|
||||
$accounts = Account::find()->andWhere(['status' => Account::STATUS_ACTIVE])->all();
|
||||
}else{
|
||||
$accounts = Account::find()->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount ] ])->all();
|
||||
}
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
public static function read($forceIncludeAccount = null){
|
||||
$accounts = null;
|
||||
$query = Account::find();
|
||||
|
||||
$query->innerJoinWith('userAccountAssignments');
|
||||
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
|
||||
public static function readAccounts($forceIncludeAccount = null)
|
||||
{
|
||||
$accounts = null;
|
||||
|
||||
if ( $forceIncludeAccount == null){
|
||||
$query->andWhere(['status' => Account::STATUS_ACTIVE])->all();
|
||||
}else{
|
||||
$query->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount ] ])->all();
|
||||
}
|
||||
if ($forceIncludeAccount == null) {
|
||||
$accounts = Account::find()->andWhere(['status' => Account::STATUS_ACTIVE])->all();
|
||||
} else {
|
||||
$accounts = Account::find()->andWhere(['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount]])->all();
|
||||
}
|
||||
|
||||
$query->orderBy( ['name' => SORT_ASC]);
|
||||
|
||||
$accounts = $query->all();
|
||||
return $accounts;
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
public static function writeDefault($account){
|
||||
$session = Yii::$app->session;
|
||||
$session->set('id_account', $account->id_account);
|
||||
|
||||
|
||||
public static function read($forceIncludeAccount = null)
|
||||
{
|
||||
$accounts = null;
|
||||
$query = Account::find();
|
||||
|
||||
$query->innerJoinWith('userAccountAssignments');
|
||||
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
|
||||
|
||||
if ($forceIncludeAccount == null) {
|
||||
$query->andWhere(['status' => Account::STATUS_ACTIVE])->all();
|
||||
} else {
|
||||
$query->andWhere(['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount]])->all();
|
||||
}
|
||||
|
||||
$query->orderBy(['name' => SORT_ASC]);
|
||||
|
||||
$accounts = $query->all();
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
|
||||
public static function writeDefault($account)
|
||||
{
|
||||
$session = Yii::$app->session;
|
||||
$session->set('id_account', $account->id_account);
|
||||
|
||||
}
|
||||
|
||||
/** read id_transfer from session (default account )
|
||||
*
|
||||
*
|
||||
* @return int id_transfer
|
||||
* */
|
||||
public static function readDefault( ){
|
||||
$session = Yii::$app->session;
|
||||
$result = $session->get('id_account');
|
||||
return $result;
|
||||
public static function readDefault()
|
||||
{
|
||||
$session = Yii::$app->session;
|
||||
$result = $session->get('id_account');
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* read default transfer object
|
||||
* return the default account or null, if not found
|
||||
* @return \common\models\Account
|
||||
* */
|
||||
public static function readDefaultObject( ){
|
||||
$account = null;
|
||||
$id_account = self::readDefault();
|
||||
if ( isset($id_account)){
|
||||
$account = Account::findOne($id_account);
|
||||
}
|
||||
return $account;
|
||||
public static function readDefaultObject()
|
||||
{
|
||||
$account = null;
|
||||
$id_account = self::readDefault();
|
||||
if (isset($id_account)) {
|
||||
$account = Account::findOne($id_account);
|
||||
}
|
||||
return $account;
|
||||
}
|
||||
|
||||
|
||||
@@ -204,23 +218,24 @@ class Account extends \yii\db\ActiveRecord
|
||||
* @param $idAccount integer The id of the account to read
|
||||
* @return array|null|\yii\db\ActiveRecord
|
||||
*/
|
||||
public static function readOne($idAccount){
|
||||
$accounts = null;
|
||||
|
||||
$query = Account::find();
|
||||
$query->innerJoinWith('userAccountAssignments');
|
||||
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
|
||||
$query->andWhere(['status' => Account::STATUS_ACTIVE]);
|
||||
$query->andWhere(['account.id_account' => $idAccount]);
|
||||
$accounts = $query->one();
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
public static function toAccaountMap($accounts){
|
||||
return ArrayHelper::map( $accounts,'id_account','name' );
|
||||
public static function readOne($idAccount)
|
||||
{
|
||||
$accounts = null;
|
||||
|
||||
$query = Account::find();
|
||||
$query->innerJoinWith('userAccountAssignments');
|
||||
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
|
||||
$query->andWhere(['status' => Account::STATUS_ACTIVE]);
|
||||
$query->andWhere(['account.id_account' => $idAccount]);
|
||||
$accounts = $query->one();
|
||||
|
||||
return $accounts;
|
||||
}
|
||||
|
||||
public static function toAccaountMap($accounts)
|
||||
{
|
||||
return ArrayHelper::map($accounts, 'id_account', 'name');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
{
|
||||
const STATUS_DELETED = 0;
|
||||
const STATUS_ACTIVE = 10;
|
||||
|
||||
|
||||
const ROLE_RECEPTION = 'receptionist';
|
||||
|
||||
/**
|
||||
@@ -192,18 +192,22 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
{
|
||||
$this->password_reset_token = null;
|
||||
}
|
||||
|
||||
|
||||
public function getUserAccountAssignments(){
|
||||
return $this->hasMany(UserAccountAssignment::className(), ['id_user' =>'id']);
|
||||
}
|
||||
|
||||
|
||||
public function getUserTrainerAssignments(){
|
||||
return $this->hasMany(UserTrainerAssignment::className(), ['id_user' =>'id']);
|
||||
}
|
||||
|
||||
static function statuses() {
|
||||
return [
|
||||
self::STATUS_ACTIVE => Yii::t('app', 'Aktív'),
|
||||
self::STATUS_DELETED => Yii::t('app', 'Inaktív'),
|
||||
] ;
|
||||
}
|
||||
|
||||
|
||||
public function getStatusHuman(){
|
||||
$result = null;
|
||||
$s = self::statuses($this->status);
|
||||
@@ -212,8 +216,8 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function attributeLabels(){
|
||||
return [
|
||||
'status' => 'Státusz',
|
||||
@@ -224,14 +228,14 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
'statusHuman' => Yii::t('backend/user', 'Status'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* @return \yii\rbac\Role[]*/
|
||||
public function getRoles(){
|
||||
$roles = \Yii::$app->authManager->getRolesByUser($this->id );
|
||||
return $roles;
|
||||
return $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,28 +243,28 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
* */
|
||||
public function getRoleString(){
|
||||
$roles = \Yii::$app->authManager->getRolesByUser($this->id );
|
||||
|
||||
|
||||
return implode(', ', array_map(function ($role) { return sprintf("%s", RoleDefinition::getRoleLabel($role->name)); }, $roles ));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* $param int $forceIncludeAccount id warehouse, that should be included in list, even if it is inactive
|
||||
* */
|
||||
public static function read($forceIncludeObjectWithId = null){
|
||||
$users = null;
|
||||
$query = User::find();
|
||||
|
||||
|
||||
if ( RoleDefinition::isReception()){
|
||||
$query->andWhere(['id' => Yii::$app->user->id ]);
|
||||
}
|
||||
|
||||
|
||||
if ( $forceIncludeObjectWithId == null){
|
||||
$users = $query->andWhere(['status' => User::STATUS_ACTIVE])->all();
|
||||
}else{
|
||||
$users = $query->andWhere( ['or', ['status' => User::STATUS_ACTIVE], ['id' => $forceIncludeObjectWithId ] ])->all();
|
||||
}
|
||||
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace common\modules\event\models;
|
||||
|
||||
use common\components\Helper;
|
||||
use common\components\RoleDefinition;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\data\ActiveDataProvider;
|
||||
@@ -86,6 +87,7 @@ class EventSearch extends Event
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
$dataProvider = new ActiveDataProvider([
|
||||
'query' => $query,
|
||||
'sort' => [
|
||||
|
||||
Reference in New Issue
Block a user