-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserRole.php
More file actions
439 lines (389 loc) · 11.5 KB
/
UserRole.php
File metadata and controls
439 lines (389 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* UserRole Model
*
* @property Language $Language
* @property Plugin $Plugin
* @property Role $Role
* @property UserRole $UserRole
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Role', 'Roles.Model');
/**
* UserRole Model
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\UserRoles\Model
*/
class UserRole extends Role {
/**
* Table name
*
* @var string
*/
public $useTable = 'roles';
/**
* システム管理者権限の定数
*
* @var const
*/
const USER_ROLE_KEY_SYSTEM_ADMINISTRATOR = 'system_administrator';
/**
* 会員管理者権限の定数
*
* @var const
*/
const USER_ROLE_KEY_ADMINISTRATOR = 'administrator';
/**
* 会員一般権限の定数
*
* @var const
*/
const USER_ROLE_KEY_COMMON_USER = 'common_user';
/**
* システム権限
*
* @var const
*/
public static $systemRoles = array(
self::USER_ROLE_KEY_SYSTEM_ADMINISTRATOR,
self::USER_ROLE_KEY_ADMINISTRATOR
);
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.OriginalKey',
'UserRoles.UserRole',
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Language' => array(
'className' => 'M17n.Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'UserRoleSetting' => array(
'className' => 'UserRoles.UserRoleSetting',
'foreignKey' => false,
'conditions' => 'UserRoleSetting.role_key = UserRole.key',
'fields' => '',
'order' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array();
/**
* Called during validation operations, before validation. Please note that custom
* validation rules can be defined in $validate.
*
* @param array $options Options passed from Model::save().
* @return bool True if validate operation should continue, false to abort
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforevalidate
* @see Model::save()
*/
public function beforeValidate($options = array()) {
$this->validate = ValidateMerge::merge($this->validate, array(
'language_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true
),
),
'type' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true
),
'inList' => array(
'rule' => array('inList', array(parent::ROLE_TYPE_USER)),
'message' => __d('net_commons', 'Invalid request.'),
'required' => true
),
),
'name' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(
__d('net_commons', 'Please input %s.'), __d('user_roles', 'User role name')
),
'required' => true
),
),
'description' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => sprintf(
__d('net_commons', 'Please input %s.'), __d('user_roles', 'User role description')
),
'required' => true
),
),
'is_system' => array(
'boolean' => array(
'rule' => array('boolean'),
'message' => __d('net_commons', 'Invalid request.'),
),
),
//key to set in OriginalKeyBehavior.
));
return parent::beforeValidate($options);
}
/**
* Called before each find operation. Return false if you want to halt the find
* call, otherwise return the (modified) query data.
*
* @param array $query Data used to execute this query, i.e. conditions, order, etc.
* @return mixed true if the operation should continue, false if it should abort; or, modified
* $query to continue with new $query
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforefind
*/
public function beforeFind($query) {
$query['conditions']['UserRole.type'] = UserRole::ROLE_TYPE_USER;
return $query;
}
/**
* 会員権限のバリデーション
*
* @param array $data リクエストデータ
* @return bool True on success, false on validation errors
* @throws InternalErrorException
*/
public function validateUserRole($data) {
//UserRoleのバリデーション
// ※$data['UserRole'][0]['key']という形からvalidateMany()を通すことで
// $data['UserRole'][0]['UserRole']['key']となる
return $this->validateMany($data['UserRole']);
}
/**
* 会員権限の登録
*
* @param array $data received post data
* @return bool True on success, false on validation errors
* @throws InternalErrorException
*/
public function saveUserRole($data) {
//トランザクションBegin
$this->begin();
//UserRoleのバリデーション
// ※$data['UserRole'][0]['key']という形からvalidateMany()を通すことで
// $data['UserRole'][0]['UserRole']['key']となる
//$roleKey = $data['UserRole'][0]['key'];
$roleKey = Hash::extract($data, 'UserRole.{n}.key')[0];
if (! $this->validateMany($data['UserRole'])) {
return false;
}
$created = !(bool)$roleKey;
try {
//UserRoleの登録処理
$userRoles = array();
foreach ($data['UserRole'] as $i => $userRole) {
$userRole['UserRole']['key'] = $roleKey;
$userRoles[$i] = $this->save($userRole, false, false);
if (! $userRoles[$i]) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$roleKey = $userRoles[$i]['UserRole']['key'];
}
if ($created) {
$this->__saveCreatedUserRole($roleKey, $data);
} else {
if (! $this->UserRoleSetting->saveUserRoleSetting($data)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return $userRoles;
}
/**
* 会員権限の登録
*
* @param string $roleKey ロールキー
* @param array $data リクエストデータ
* @return bool
* @throws InternalErrorException
*/
private function __saveCreatedUserRole($roleKey, $data) {
$this->loadModels([
'PluginsRole' => 'PluginManager.PluginsRole',
'UserAttributesRole' => 'UserRoles.UserAttributesRole',
]);
$original = $this->UserRoleSetting->find('first', array(
'recursive' => -1,
'conditions' => array(
'role_key' => $data['UserRoleSetting']['origin_role_key']
)
));
//UserRoleSettingの登録処理
$data['UserRoleSetting']['role_key'] = $roleKey;
$data['UserRoleSetting']['is_site_plugins'] = $original['UserRoleSetting']['is_site_plugins'];
if (isset($data['DefaultRolePermission'])) {
$data['DefaultRolePermission'] = Hash::insert(
$data['DefaultRolePermission'], '{s}.role_key', $roleKey
);
}
if (! $this->UserRoleSetting->saveUserRoleSetting($data)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//PluginsRoleのデータ登録処理
if (isset($data['PluginsRole'])) {
$data['PluginsRole'] = Hash::remove(
$data['PluginsRole'], '{n}.PluginsRole[is_usable_plugin=0]'
);
$data['PluginsRole'] = Hash::insert(
$data['PluginsRole'], '{n}.PluginsRole.id', null
);
$data['PluginsRole'] = Hash::insert(
$data['PluginsRole'], '{n}.PluginsRole.role_key', $roleKey
);
if (! $this->PluginsRole->saveMany($data['PluginsRole'])) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
}
//UserAttributesRoleのデータ登録処理
$data['UserAttributesRole'] = Hash::insert(
$data['UserAttributesRole'], '{n}.UserAttributesRole.id', null
);
$data['UserAttributesRole'] = Hash::insert(
$data['UserAttributesRole'], '{n}.UserAttributesRole.role_key', $roleKey
);
$result = $this->UserAttributesRole->saveUserAttributesRoles($data);
if (! $result) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
return true;
}
/**
* UserRoleSettingの登録処理
*
* @param array $data received post data
* @return bool True on success, false on validation errors
* @throws InternalErrorException
*/
public function saveUserRolePlugins($data) {
//トランザクションBegin
$this->begin();
try {
//PluginsRoleのデータ登録処理
foreach ($data['PluginsRole'] as $pluginRole) {
if (Hash::get($pluginRole, 'PluginsRole.is_usable_plugin', false)) {
$this->savePluginsRole(
$pluginRole['PluginsRole']['role_key'], $pluginRole['PluginsRole']['plugin_key']
);
} else {
$this->deletePluginsRole(
$pluginRole['PluginsRole']['role_key'], $pluginRole['PluginsRole']['plugin_key']
);
}
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return true;
}
/**
* 会員権限の削除
*
* @param array $data received post data
* @return mixed On success Model::$data if its not empty or true, false on failure
* @throws InternalErrorException
*/
public function deleteUserRole($data) {
$this->loadModels([
'UserRoleSetting' => 'UserRoles.UserRoleSetting',
'UserAttributesRole' => 'UserRoles.UserAttributesRole',
'PluginsRole' => 'PluginManager.PluginsRole',
]);
//トランザクションBegin
$this->begin();
try {
if (! $this->verifyDeletable($data['key'])) {
return false;
}
//削除処理
if (! $this->deleteAll(array($this->alias . '.key' => $data['key']), false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$conditions = array($this->UserRoleSetting->alias . '.role_key' => $data['key']);
if (! $this->UserRoleSetting->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$conditions = array($this->UserAttributesRole->alias . '.role_key' => $data['key']);
if (! $this->UserAttributesRole->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//トランザクションCommit
$this->commit();
} catch (Exception $ex) {
//トランザクションRollback
$this->rollback($ex);
}
return true;
}
/**
* 削除可能か検証する
*
* @param array $roleKey received post data
* @return bool True:削除可、False:削除不可
* @throws BadRequestException
* @throws InternalErrorException
*/
public function verifyDeletable($roleKey) {
$this->loadModels([
'User' => 'Users.User',
]);
$userRole = $this->find('first', array(
'recursive' => -1,
'conditions' => array('key' => $roleKey),
));
if (! $userRole) {
//リクエストのエラーなのでBadRequestにする
throw new BadRequestException(__d('net_commons', 'Bad Request'));
}
//システムフラグがONになっているものは、削除不可
if (Hash::get($userRole, $this->alias . '.is_system')) {
return false;
}
$count = $this->User->find('count', array(
'recursive' => -1,
'conditions' => array('role_key' => $roleKey),
));
if ($count === false) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if ($count > 0) {
$this->validationErrors['key'][] =
__d('user_roles', 'Can not be deleted because it has this authority is used.');
return false;
}
return true;
}
}