BizRuleModel::rules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace yii2mod\rbac\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\rbac\Rule;
8
9
/**
10
 * Class BizRuleModel
11
 *
12
 * @package yii2mod\rbac\models
13
 */
14
class BizRuleModel extends Model
15
{
16
    /**
17
     * @var string name of the rule
18
     */
19
    public $name;
20
21
    /**
22
     * @var int UNIX timestamp representing the rule creation time
23
     */
24
    public $createdAt;
25
26
    /**
27
     * @var int UNIX timestamp representing the rule updating time
28
     */
29
    public $updatedAt;
30
31
    /**
32
     * @var string Rule className
33
     */
34
    public $className;
35
36
    /**
37
     * @var \yii\rbac\ManagerInterface
38
     */
39
    protected $manager;
40
41
    /**
42
     * @var Rule
43
     */
44
    private $_item;
45
46
    /**
47
     * BizRuleModel constructor.
48
     *
49
     * @param \yii\rbac\Rule $item
50
     * @param array $config
51
     */
52
    public function __construct($item = null, $config = [])
53
    {
54
        $this->_item = $item;
55
        $this->manager = Yii::$app->authManager;
56
57
        if ($item !== null) {
58
            $this->name = $item->name;
59
            $this->className = get_class($item);
60
        }
61
62
        parent::__construct($config);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function rules(): array
69
    {
70
        return [
71
            [['name', 'className'], 'trim'],
72
            [['name', 'className'], 'required'],
73
            ['className', 'string'],
74
            ['name', 'string', 'max' => 64],
75
            ['className', 'classExists'],
76
        ];
77
    }
78
79
    /**
80
     * Validate className
81
     */
82
    public function classExists()
83
    {
84
        if (!class_exists($this->className)) {
85
            $message = Yii::t('yii2mod.rbac', "Unknown class '{class}'", ['class' => $this->className]);
86
            $this->addError('className', $message);
87
88
            return;
89
        }
90
91
        if (!is_subclass_of($this->className, Rule::class)) {
92
            $message = Yii::t('yii2mod.rbac', "'{class}' must extend from 'yii\\rbac\\Rule' or its child class", [
93
                'class' => $this->className, ]);
94
            $this->addError('className', $message);
95
        }
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function attributeLabels(): array
102
    {
103
        return [
104
            'name' => Yii::t('yii2mod.rbac', 'Name'),
105
            'className' => Yii::t('yii2mod.rbac', 'Class Name'),
106
        ];
107
    }
108
109
    /**
110
     * Check if record is new
111
     *
112
     * @return bool
113
     */
114
    public function getIsNewRecord(): bool
115
    {
116
        return $this->_item === null;
117
    }
118
119
    /**
120
     * Create object
121
     *
122
     * @param $id
123
     *
124
     * @return BizRuleModel|null
125
     */
126
    public static function find(int $id)
127
    {
128
        $item = Yii::$app->authManager->getRule($id);
129
130
        if ($item !== null) {
131
            return new static($item);
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * Save rule
139
     *
140
     * @return bool
141
     */
142
    public function save(): bool
143
    {
144
        if ($this->validate()) {
145
            $class = $this->className;
146
            if ($this->_item === null) {
147
                $this->_item = new $class();
148
                $isNew = true;
149
                $oldName = false;
150
            } else {
151
                $isNew = false;
152
                $oldName = $this->_item->name;
153
            }
154
155
            $this->_item->name = $this->name;
156
157
            if ($isNew) {
158
                $this->manager->add($this->_item);
159
            } else {
160
                $this->manager->update($oldName, $this->_item);
0 ignored issues
show
Security Bug introduced by
It seems like $oldName defined by false on line 149 can also be of type false; however, yii\rbac\ManagerInterface::update() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
161
            }
162
163
            return true;
164
        }
165
166
        return false;
167
    }
168
169
    /**
170
     * @return null|Rule
171
     */
172
    public function getItem()
173
    {
174
        return $this->_item;
175
    }
176
}
177