-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserRoleBehavior.php
More file actions
175 lines (152 loc) · 5.18 KB
/
UserRoleBehavior.php
File metadata and controls
175 lines (152 loc) · 5.18 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
<?php
/**
* DefaultUserRole Behavior
*
* @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('ModelBehavior', 'Model');
App::uses('UserAttribute', 'UserAttributes.Model');
App::uses('DataType', 'DataTypes.Model');
/**
* DefaultUserRole Behavior
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\UserRoles\Model\Behavior
*/
class UserRoleBehavior extends ModelBehavior {
/**
* Save default UserAttributesRole
*
* @param Model $model Model using this behavior
* @param array $data User role data
* @return bool True on success
* @throws InternalErrorException
*/
public function saveDefaultUserAttributesRole(Model $model, $data) {
$model->loadModels([
'UserAttributesRole' => 'UserRoles.UserAttributesRole',
]);
//UserAttributesRoleデフォルトのデータ取得
$userAttributesRole = $model->UserAttributesRole->find('all', array(
'recursive' => -1,
'conditions' => array('role_key' => $data['UserRoleSetting']['origin_role_key'])
));
if (! $userAttributesRole) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
//UserAttributesRoleの登録処理
foreach (['id', 'created', 'created_user', 'modified', 'modified_user'] as $field) {
$userAttributesRole = Hash::remove($userAttributesRole, '{n}.UserAttributesRole.' . $field);
}
$userAttributesRole = Hash::insert(
$userAttributesRole,
'{n}.UserAttributesRole.role_key',
$data['UserRoleSetting']['role_key']
);
if (! $model->UserAttributesRole->saveMany($userAttributesRole, array('validate' => false))) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
return true;
}
/**
* Save PluginsRole
*
* @param Model $model Model using this behavior
* @param string $roleKey roles.key
* @param string $pluginKey plugins.key
* @return bool True on success
* @throws InternalErrorException
*/
public function savePluginsRole(Model $model, $roleKey, $pluginKey) {
$model->loadModels([
'PluginsRole' => 'PluginManager.PluginsRole',
]);
$conditions = array(
'role_key' => $roleKey,
'plugin_key' => $pluginKey
);
//PluginsRoleのデータ取得
$pluginsRole = $model->PluginsRole->find('first', array(
'recursive' => -1,
'conditions' => $conditions
));
if (! $pluginsRole) {
$pluginsRole = $model->PluginsRole->create($conditions);
}
//PluginsRoleの登録処理
if (! $model->PluginsRole->save($pluginsRole, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if ($pluginKey === 'user_manager') {
$this->__updateOnlyAdministorator($model, $roleKey, true);
}
return true;
}
/**
* Delete PluginsRole
*
* @param Model $model Model using this behavior
* @param string $roleKey roles.key
* @param string $pluginKey plugins.key
* @return bool True on success
* @throws InternalErrorException
*/
public function deletePluginsRole(Model $model, $roleKey, $pluginKey) {
$model->loadModels([
'PluginsRole' => 'PluginManager.PluginsRole',
]);
$conditions = array(
$model->PluginsRole->alias . '.role_key' => $roleKey,
$model->PluginsRole->alias . '.plugin_key' => $pluginKey
);
//PluginsRoleの削除処理
if (! $model->PluginsRole->deleteAll($conditions, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
if ($pluginKey === 'user_manager') {
$this->__updateOnlyAdministorator($model, $roleKey, false);
}
return true;
}
/**
* 管理者のみの項目に対する更新
*
* @param Model $model Model using this behavior
* @param string $roleKey ロールキー
* @param bool $enableUserManager 有効かどうか
* @return bool True on success
* @throws InternalErrorException
*/
private function __updateOnlyAdministorator(Model $model, $roleKey, $enableUserManager) {
$model->loadModels([
'UserAttributeSetting' => 'UserAttributes.UserAttributeSetting',
'UserAttributesRole' => 'UserRoles.UserAttributesRole',
]);
$userAttrSettings = $model->UserAttributeSetting->find('all', array(
'recursive' => -1,
));
$UserAttributesRoles = $model->UserAttributesRole->find('all', array(
'recursive' => -1,
'fields' => array('id', 'role_key', 'user_attribute_key'),
'conditions' => array('role_key' => $roleKey)
));
$data['UserAttributesRole'] = array();
foreach ($userAttrSettings as $i => $userAttrSetting) {
$userAttributeKey = $userAttrSetting['UserAttributeSetting']['user_attribute_key'];
$userAttrRole = Hash::extract($UserAttributesRoles,
'{n}.UserAttributesRole[user_attribute_key=' . $userAttributeKey . ']');
$data['UserAttributesRole'][$i] = Hash::merge(
$model->UserAttributesRole->create(Hash::get($userAttrRole, '0')),
$model->UserAttributesRole->defaultUserAttributeRole($userAttrSetting, $enableUserManager)
);
}
if (! $model->UserAttributesRole->saveUserAttributesRoles($data)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
return true;
}
}