-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWorkflowComponent.php
More file actions
282 lines (254 loc) · 7.63 KB
/
WorkflowComponent.php
File metadata and controls
282 lines (254 loc) · 7.63 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
<?php
/**
* Workflow Component
*
* @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('Component', 'Controller');
/**
* Workflow Component
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Workflow\Controller\Component
*/
class WorkflowComponent extends Component {
/**
* status published
*
* @var string
*/
const STATUS_PUBLISHED = '1';
/**
* status approved
* 後で削除
*
* @var string
*/
const STATUS_APPROVED = '2';
/**
* status approved
*
* @var string
*/
const STATUS_APPROVAL_WAITING = '2';
/**
* in draft status
*
* @var string
*/
const STATUS_IN_DRAFT = '3';
/**
* status disaproved
*
* @var string
*/
const STATUS_DISAPPROVED = '4';
/**
* Called before the Controller::beforeFilter().
*
* @param Controller $controller Controller with components to initialize
* @return void
* @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::initialize
*/
public function initialize(Controller $controller) {
$this->controller = $controller;
}
/**
* Called after the Controller::beforeFilter() and before the controller action
*
* @param Controller $controller Controller with components to startup
* @return void
*/
public function startup(Controller $controller) {
if (! in_array('Users.DisplayUser', $controller->helpers, true)) {
$controller->helpers[] = 'Users.DisplayUser';
}
}
/**
* Parse content status from request
*
* @return mixed status on success, false on error
* @throws BadRequestException
*/
public function parseStatus() {
$matches = preg_grep('/^save_\d/', array_keys($this->controller->data));
if ($matches) {
list(, $status) = explode('_', array_shift($matches));
return $status;
} else {
return $this->controller->setAction('throwBadRequest');
}
}
/**
* Function to get the data of BlockRolePermmissions.
* e.g.) BlockRolePermmissions controller
*
* @param array $permissions パーミッションリスト
* @param int $roomId ルームID
* @param string $blockKey ブロックKey
* @return array Role and Permissions data
* - The `Role` merged of Role and RoomRole
* - The `Permission` sets in priority of BlockRolePermission and RoomRolePermission and DefaultRolePermission.
*/
public function getBlockRolePermissions($permissions, $roomId = null, $blockKey = null) {
//modelのロード
$models = array(
'BlockRolePermission' => 'Blocks.BlockRolePermission',
'DefaultRolePermission' => 'Roles.DefaultRolePermission',
'Role' => 'Roles.Role',
'RolesRoom' => 'Rooms.RolesRoom',
'RoomRole' => 'Rooms.RoomRole',
'RoomRolePermission' => 'Rooms.RoomRolePermission',
);
foreach ($models as $model => $class) {
$this->$model = ClassRegistry::init($class, true);
}
if (! isset($blockKey)) {
$blockKey = Current::read('Block.key');
}
//RoomRolePermissions取得
$results = $this->getRoomRolePermissions(
$permissions, DefaultRolePermission::TYPE_ROOM_ROLE, $roomId
);
$defaultPermissions = Hash::remove($results['DefaultRolePermission'], '{s}.{s}.id');
$roles = $results['Role'];
$rolesRooms = $results['RolesRoom'];
$roomRolePermissions = Hash::remove($results['RoomRolePermission'], '{s}.{s}.id');
$roomRoles = $results['RoomRole'];
//BlockRolePermission取得
$blockPermissions = $this->BlockRolePermission->find('all', array(
'recursive' => 0,
'conditions' => array(
'BlockRolePermission.roles_room_id' => array_values($rolesRooms),
'BlockRolePermission.block_key' => $blockKey,
'BlockRolePermission.permission' => $permissions,
),
));
$blockPermissions = Hash::combine(
$blockPermissions,
'{n}.RolesRoom.role_key',
'{n}.BlockRolePermission',
'{n}.BlockRolePermission.permission'
);
//戻り値の設定
$results = array(
'BlockRolePermissions' => Hash::merge(
$defaultPermissions, $roomRolePermissions, $blockPermissions
),
'Roles' => Hash::merge($roomRoles, $roles)
);
//block_keyのセット
$results['BlockRolePermissions'] = Hash::insert(
$results['BlockRolePermissions'], '{s}.{s}.block_key', $blockKey
);
foreach ($rolesRooms as $roleKey => $rolesRoomId) {
$results['BlockRolePermissions'] = Hash::insert(
$results['BlockRolePermissions'],
'{s}.' . $roleKey . '.roles_room_id',
$rolesRoomId
);
}
return $results;
}
/**
* Function to get the data of RoomRolePermmissions.
* e.g.) RoomRolePermmissions controller
*
* @param array $permissions パーミッションリスト
* @param string $type タイプ(DefaultRolePermissions.type)
* @param int $roomId ルームID
* @return array Role and Permissions and Rooms data
* - The `DefaultPermissions` data.
* - The `Roles` data.
* - The `RolesRooms` data.
* - The `RoomRolePermissions` data.
* - The `RoomRoles` data.
*/
public function getRoomRolePermissions($permissions, $type, $roomId = null) {
//戻り値の設定
$results = array(
'DefaultRolePermission' => null,
'Role' => null,
'RolesRoom' => null,
'RoomRolePermission' => null,
'RoomRole' => null,
);
//modelのロード
$models = array(
'DefaultRolePermission' => 'Roles.DefaultRolePermission',
'Role' => 'Roles.Role',
'RolesRoom' => 'Rooms.RolesRoom',
'RoomRole' => 'Rooms.RoomRole',
'RoomRolePermission' => 'Rooms.RoomRolePermission',
);
foreach ($models as $model => $class) {
$this->$model = ClassRegistry::init($class, true);
}
if (! $roomId) {
$roomId = Current::read('Room.id');
}
//RoomRole取得
$roomRoles = $this->RoomRole->find('all', array(
'recursive' => -1,
));
$results['RoomRole'] = Hash::combine($roomRoles, '{n}.RoomRole.role_key', '{n}.RoomRole');
//Role取得
$roles = $this->Role->find('all', array(
'recursive' => -1,
'conditions' => array(
'Role.type' => Role::ROLE_TYPE_ROOM,
'Role.language_id' => Current::read('Language.id'),
),
));
$results['Role'] = Hash::combine($roles, '{n}.Role.key', '{n}.Role');
//DefaultRolePermission取得
$defaultPermissions = $this->DefaultRolePermission->find('all', array(
'recursive' => -1,
'fields' => array('DefaultRolePermission.*', 'DefaultRolePermission.value AS default'),
'conditions' => array(
'DefaultRolePermission.type' => $type,
'DefaultRolePermission.permission' => $permissions,
),
));
$results['DefaultRolePermission'] = Hash::combine(
$defaultPermissions,
'{n}.DefaultRolePermission.role_key',
'{n}.DefaultRolePermission',
'{n}.DefaultRolePermission.permission'
);
if (! isset($roomId)) {
return $results;
}
$roleKeys = Hash::combine($defaultPermissions, '{n}.DefaultRolePermission.role_key');
//RolesRoomのIDリストを取得
$results['RolesRoom'] = $this->RolesRoom->find('list', array(
'recursive' => -1,
'fields' => array('role_key', 'id'),
'conditions' => array(
'RolesRoom.room_id' => $roomId,
'RolesRoom.role_key' => array_keys($roleKeys),
),
));
//RoomRolePermission取得
$roomRolePermissions = $this->RoomRolePermission->find('all', array(
'recursive' => 0,
'conditions' => array(
'RoomRolePermission.roles_room_id' => $results['RolesRoom'],
'RoomRolePermission.permission' => $permissions,
),
));
$results['RoomRolePermission'] = Hash::combine(
$roomRolePermissions,
'{n}.RolesRoom.role_key',
'{n}.RoomRolePermission',
'{n}.RoomRolePermission.permission'
);
//$results['RoomRolePermission'] = Hash::remove($roomRolePermissions, '{s}.{s}.id');
//戻り値の設定
return $results;
}
}