-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkAuthorityController.php
More file actions
195 lines (174 loc) · 5.32 KB
/
LinkAuthorityController.php
File metadata and controls
195 lines (174 loc) · 5.32 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
<?php
/**
* LinkAuthority Controller
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('LinksAppController', 'Links.Controller');
/**
* LinkAuthority Controller
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @package NetCommons\Links\Controller
*/
class LinkAuthorityController extends LinksAppController {
/**
* use model
*
* @var array
*/
public $uses = array(
'Blocks.BlockRolePermission',
'Rooms.RolesRoom',
'Rooms.RoomRolePermission',
'Roles.Role',
'Frames.Frame'
);
/**
* use component
*
* @var array
*/
public $components = array(
'NetCommons.NetCommonsFrame',
'NetCommons.NetCommonsRoomRole',
);
/**
* beforeFilter
*
* @return void
*/
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow();
}
/**
* view method
*
* @param int $frameId frames.id
* @param string $lang ex)"en" or "ja" etc.
* @return CakeResponse A response object containing the rendered view.
*/
public function view($frameId = 0, $type = 'list') {
if ($this->response->statusCode() !== 200) {
return $this->render(false);
}
return $this->render('LinkAuthority/view', false);
}
public function form($frameId = 0) {
// frameIdからroom取得
$frame = $this->Frame->findById($frameId);
$roles = $this->RolesRoom->findAllByRoomId($frame['Frame']['room_id']);
foreach($roles as $role){
$roleKey = $role['RolesRoom']['role_key'];
$rolePermission[$roleKey] = false;
}
unset($rolePermission['room_administrator']); //ルーム管理者は制限しないので取り除く
$this->set('rolePermission', $rolePermission);
$this->set('frameId', $frameId);
}
public function edit($frameId = 0) {
if (! $this->request->isPost()) {
throw new MethodNotAllowedException();
}
//保存
if ($this->saveBlockRolePermission($frameId, $this->data)) {
$result = array(
'name' => __d('net_commons', 'Successfully finished.'),
);
$this->set(compact('result'));
$this->set('_serialize', 'result');
return $this->render(false);
} else {
$error = __d('net_commons', 'Failed to register data.');
// $error .= $this->formatValidationErrors($this->LinkFrameSetting->validationErrors);
throw new ForbiddenException($error);
}
}
public function get($frameId = 0) {
// frameIdからroom取得
$frame = $this->Frame->findById($frameId);
$roomId = $frame['Frame']['room_id'];
$roles = $this->getContentCreatableRoomRoles($roomId);
debug($roles);
// リンク集の権限をblock_role_permissionsから取り出す(不要?)
$blockRolePermission = $this->getBlockRolePermissionByFrame($frame);
$ret = array();
foreach($roles as $role){
$roleKey = $role['Role']['key'];
$var = array(
'name' => $role['Role']['name'],
'permission' => false, // MyTodo BlockRolePermissionからセット
);
$ret['add_link'][$roleKey] = $var;
}
unset($ret['add_link']['room_administrator']); //ルーム管理者は制限しないので取り除く
$this->set('roles', $ret);
$this->set('_serialize', 'roles');
return $this->render(false);
}
/**
* ルーム管理者以外でリンク作成可能(contetn_creatable)なルームロールを返す
*
*/
protected function getContentCreatableRoomRoles($roomId) {
// ルームから roles_rooms からルームのロールを読み出し
// role_keyに対応した言語をrolesテーブルから読み出したいのでbindModel モデルで定義しときたいなぁ
$this->RolesRoom->bindModel(
array(
'hasMany' => array(
'RoomRolePermission' => array(
'className' => 'Rooms.RoomRolePermission'
)
),
'belongsTo' => array(
'Role' => array(
'className' => 'Roles.Role',
'foreignKey' => false,
'conditions' => array('RolesRoom.role_key = Role.key'),
),
),
)
);
$conditions = array(
'room_id' => $roomId,
'RoomRolePermission.permission' => 'content_creatable', //ε( v ゚ω゚) < conttent_creatableはマジックナンバー?
'RoomRolePermission.value' => 1, //ε( v ゚ω゚) <マジックナンバー?
'role_key !=' => 'room_administrator' // ルーム管理者は除外する
);
$roles = $this->RolesRoom->find('all', array(
'conditions' => $conditions
));
return $roles;
}
protected function saveBlockRolePermission($frameId, $data) {
// MyTodo 現在のBlockRolePermissionを削除
$frame = $this->Frame->findById($frameId);
$roomId = $frame['Frame']['room_id'];
// block_key
$blockKey = $frame['Block']['key'];
foreach($data['BlockRolePermission'] as $permission => $roles){
foreach($roles as $roleKey => $value){
$saveData = array(
'roles_room_id' => '', // MyTodo
'block_key' => $blockKey,
'permission' => $permission,
'value' => $value,
);
$this->BlockRolePermission->create(); // MyTodo 既にある場合は?
$this->BlockRolePermission->save($saveData);
}
}
// roles_room_id <- roke_key AND room_id から
// permisssion
// value
}
protected function getBlockRolePermissionByFrame($frame) {
$blockKey = $frame['Block']['key'];
$blockRolePermission = $this->BlockRolePermission->findByBlockKey($blockKey);
return $blockRolePermission;
}
}