Completed
Push — master ( 55e0f0...d0f882 )
by Alexey
11:22
created

Assignment::getUserRoleName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace modules\rbac\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\helpers\ArrayHelper;
8
use modules\rbac\Module;
9
10
/**
11
 * Class Assignment
12
 * @package modules\rbac\models
13
 */
14
class Assignment extends Model
15
{
16
    public $user;
17
    public $id;
18
    public $username;
19
    public $role;
20
    public $isNewRecord = false;
21
22
    /**
23
     * @return array
24
     */
25
    public function rules()
26
    {
27
        return [
28
            [['role'], 'safe'],
29
        ];
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function attributeLabels()
36
    {
37
        return [
38
            'role' => Module::t('module', 'Role'),
39
        ];
40
    }
41
42
    /**
43
     * Список всех ролей
44
     * @return array
45
     */
46
    public function getRolesArray()
47
    {
48
        return ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description');
49
    }
50
51
    /**
52
     * @param $id
53
     * @return string
54
     */
55
    public function getRoleName($id = null)
56
    {
57
        $id = $id ? $id : $this->user->id;
58
        $auth = Yii::$app->authManager;
59
        $roles = $auth->getRolesByUser($id);
60
        $role = '';
61
        foreach ($roles as $item) {
62
            $role .= $item->description ? $item->description . ', ' : $item->name . ', ';
63
        }
64
        return chop($role, ' ,');
65
    }
66
67
    /**
68
     * @param $id
69
     * @return mixed|null
70
     */
71
    public function getUserRoleName($id = null)
72
    {
73
        $id = $id ? $id : $this->user->id;
74
        if ($role = Yii::$app->authManager->getRolesByUser($id))
75
            return ArrayHelper::getValue($role, function ($role, $defaultValue) {
0 ignored issues
show
Unused Code introduced by
The parameter $defaultValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
                foreach ($role as $key => $value) {
77
                    return $value->description;
78
                }
79
                return null;
80
            });
81
        return null;
82
    }
83
84
    /**
85
     * Получаем роль пользователя
86
     * @param null $id
87
     * @return mixed|null
88
     */
89
    public function getRoleUser($id = null)
90
    {
91
        $id = $id ? $id : $this->user->id;
92
        if ($role = Yii::$app->authManager->getRolesByUser($id))
93
            return ArrayHelper::getValue($role, function ($role, $defaultValue) {
0 ignored issues
show
Unused Code introduced by
The parameter $defaultValue is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
                foreach ($role as $key => $value) {
95
                    return $value->name;
96
                }
97
                return null;
98
            });
99
        return null;
100
    }
101
102
    /**
103
     * @param null $user_id
104
     * @return mixed|null
105
     */
106
    /* public function getUserRoleValue($user_id = null)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
     {
108
         if ($user_id) {
109
             if ($role = Yii::$app->authManager->getRolesByUser($user_id))
110
                 return ArrayHelper::getValue($role, function ($role) {
111
                     foreach ($role as $key => $value) {
112
                         return $value->name;
113
                     }
114
                     return null;
115
                 });
116
         } else {
117
             if ($role = Yii::$app->authManager->getRolesByUser($this->id))
118
                 return ArrayHelper::getValue($role, function ($role) {
119
                     foreach ($role as $key => $value) {
120
                         return $value->name;
121
                     }
122
                     return null;
123
                 });
124
         }
125
         return null;
126
     }*/
127
}
128