1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
/**
|
4
|
|
|
* This is the model class for table "user_roles".
|
5
|
|
|
*
|
6
|
|
|
* The followings are the available columns in table 'user_roles':
|
7
|
|
|
* @property integer $id
|
8
|
|
|
* @property string $name
|
9
|
|
|
* @property string $created
|
10
|
|
|
* @property string $updated
|
11
|
|
|
*
|
12
|
|
|
* The followings are the available model relations:
|
13
|
|
|
* @property Users[] $users
|
14
|
|
|
*/
|
15
|
|
|
class UserRoles extends CiiModel
|
|
|
|
|
16
|
|
|
{
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Returns a key => value array of userRole => bitwise permissions
|
20
|
|
|
*
|
21
|
|
|
* Permissions apply per role, with the exception of publisher and admin, whose permissions apply to everything
|
22
|
|
|
* Note that these permissions only apply to content, and management of CiiMS' settings
|
23
|
|
|
*
|
24
|
|
|
* --------------------------------------------------------------------------------
|
25
|
|
|
* | role/id | manage | publish other | publish | delete | update | create | read |
|
26
|
|
|
* --------------------------------------------------------------------------------
|
27
|
|
|
* | user/1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
|
28
|
|
|
* --------------------------------------------------------------------------------
|
29
|
|
|
* | clb/5 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
|
30
|
|
|
* --------------------------------------------------------------------------------
|
31
|
|
|
* | auth/7 | 0 | 0 | 1 | 1 | 1 | 1 | 1 |
|
32
|
|
|
* --------------------------------------------------------------------------------
|
33
|
|
|
* | pub/8 | 0 | 1 | 1 | 1 | 1 | 1 | 1 |
|
34
|
|
|
* --------------------------------------------------------------------------------
|
35
|
|
|
* | admin/9 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|
36
|
|
|
* --------------------------------------------------------------------------------
|
37
|
|
|
* @return array
|
38
|
|
|
*/
|
39
|
|
|
public function getPermissions()
|
40
|
|
|
{
|
41
|
|
|
return array(
|
42
|
|
|
'1' => 1, // User
|
43
|
|
|
'2' => 0, // Pending
|
44
|
|
|
'3' => 0, // Suspended
|
45
|
|
|
'5' => 7, // Collaborator
|
46
|
|
|
'7' => 16, // Author
|
47
|
|
|
'8' => 32, // Publisher
|
48
|
|
|
'9' => 64 // Admin
|
49
|
|
|
);
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
public function isA($roleName, $role=false)
|
53
|
|
|
{
|
54
|
|
|
if ($role === false)
|
55
|
|
|
$role = Yii::app()->user->role;
|
56
|
|
|
|
57
|
|
|
$roleName = strtolower($roleName);
|
58
|
|
|
|
59
|
|
|
switch ($roleName)
|
60
|
|
|
{
|
61
|
|
|
case 'user':
|
62
|
|
|
return $role <= 1;
|
63
|
|
|
case 'collaborator':
|
64
|
|
|
return $role == 5;
|
65
|
|
|
case 'author':
|
66
|
|
|
return $role == 7;
|
67
|
|
|
case 'publisher':
|
68
|
|
|
return $role == 8;
|
69
|
|
|
case 'admin':
|
70
|
|
|
return $role == 9;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
return false;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/**
|
77
|
|
|
* Returns the bitwise permissions associated to each activity
|
78
|
|
|
* @return array
|
79
|
|
|
*/
|
80
|
|
|
public function getActivities()
|
81
|
|
|
{
|
82
|
|
|
return array(
|
83
|
|
|
'read' => 1,
|
84
|
|
|
'comment' => 1,
|
85
|
|
|
'create' => 3,
|
86
|
|
|
'update' => 4,
|
87
|
|
|
'modify' => 7,
|
88
|
|
|
'delete' => 8,
|
89
|
|
|
'publish' => 16,
|
90
|
|
|
'publishOther' => 32,
|
91
|
|
|
'manage' => 64
|
92
|
|
|
);
|
93
|
|
|
}
|
94
|
|
|
|
95
|
|
|
/**
|
96
|
|
|
* Determines if a user with a given role has permission to perform a given activity
|
97
|
|
|
* @param string $permission The permissions we want to lookup
|
98
|
|
|
* @param int $role The user role. If not provided, will be applied to the current user
|
99
|
|
|
* @return boolean
|
100
|
|
|
*/
|
101
|
|
|
public function hasPermission($permission, $role=NULL)
|
102
|
|
|
{
|
103
|
|
|
if ($role === NULL)
|
104
|
|
|
{
|
105
|
|
|
if (isset($this->id))
|
106
|
|
|
$role = $this->id;
|
107
|
|
|
else if (Yii::app()->user->isGuest)
|
108
|
|
|
$role = 1;
|
109
|
|
|
else
|
110
|
|
|
$role = Yii::app()->user->role;
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
$permissions = $this->getPermissions();
|
114
|
|
|
$activities = $this->getActivities();
|
115
|
|
|
|
116
|
|
|
// If the permission doesn't exist for that role, return false;
|
117
|
|
|
if (!isset($permissions[$role]))
|
118
|
|
|
return false;
|
119
|
|
|
|
120
|
|
|
return $activities[$permission] <= $permissions[$role];
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* Returns the static model of the specified AR class.
|
125
|
|
|
* @param string $className active record class name.
|
126
|
|
|
* @return UserRoles the static model class
|
127
|
|
|
*/
|
128
|
|
|
public static function model($className=__CLASS__)
|
129
|
|
|
{
|
130
|
|
|
return parent::model($className);
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* @return string the associated database table name
|
135
|
|
|
*/
|
136
|
|
|
public function tableName()
|
137
|
|
|
{
|
138
|
|
|
return 'user_roles';
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
/**
|
142
|
|
|
* @return array validation rules for model attributes.
|
143
|
|
|
*/
|
144
|
|
|
public function rules()
|
145
|
|
|
{
|
146
|
|
|
// NOTE: you should only define rules for those attributes that
|
147
|
|
|
// will receive user inputs.
|
148
|
|
|
return array(
|
149
|
|
|
array('name', 'required'),
|
150
|
|
|
array('name', 'length', 'max'=>100),
|
151
|
|
|
// The following rule is used by search().
|
152
|
|
|
array('id, name, created, updated', 'safe', 'on'=>'search'),
|
153
|
|
|
);
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
/**
|
157
|
|
|
* @return array relational rules.
|
158
|
|
|
*/
|
159
|
|
|
public function relations()
|
160
|
|
|
{
|
161
|
|
|
// NOTE: you may need to adjust the relation name and the related
|
162
|
|
|
// class name for the relations automatically generated below.
|
163
|
|
|
return array(
|
164
|
|
|
'users' => array(self::HAS_MANY, 'Users', 'user_role'),
|
165
|
|
|
);
|
166
|
|
|
}
|
167
|
|
|
|
168
|
|
|
/**
|
169
|
|
|
* @return array customized attribute labels (name=>label)
|
170
|
|
|
*/
|
171
|
|
|
public function attributeLabels()
|
172
|
|
|
{
|
173
|
|
|
return array(
|
174
|
|
|
'id' => Yii::t('ciims.models.UserRoles', 'ID'),
|
175
|
|
|
'name' => Yii::t('ciims.models.UserRoles', 'Name'),
|
176
|
|
|
'created' => Yii::t('ciims.models.UserRoles', 'Created'),
|
177
|
|
|
'updated' => Yii::t('ciims.models.UserRoles', 'Updated'),
|
178
|
|
|
);
|
179
|
|
|
}
|
180
|
|
|
|
181
|
|
|
/**
|
182
|
|
|
* Retrieves a list of models based on the current search/filter conditions.
|
183
|
|
|
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
|
184
|
|
|
*/
|
185
|
|
|
public function search()
|
186
|
|
|
{
|
187
|
|
|
$criteria=new CDbCriteria;
|
188
|
|
|
|
189
|
|
|
$criteria->compare('id',$this->id);
|
190
|
|
|
$criteria->compare('name',$this->name,true);
|
191
|
|
|
$criteria->compare('created',$this->created,true);
|
192
|
|
|
$criteria->compare('updated',$this->updated,true);
|
193
|
|
|
|
194
|
|
|
return new CActiveDataProvider($this, array(
|
195
|
|
|
'criteria'=>$criteria,
|
196
|
|
|
));
|
197
|
|
|
}
|
198
|
|
|
}
|
199
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.