forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermissionComponent.php
More file actions
233 lines (216 loc) · 6.28 KB
/
PermissionComponent.php
File metadata and controls
233 lines (216 loc) · 6.28 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
<?php
/**
* Permission 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');
/**
* Permission Component
*
* リクエストされたController、もしくは、actionのアクセス許可を、<br>
* [Currentオブジェクト](https://github.com/NetCommons3/NetCommons3Docs/blob/master/phpdocMd/NetCommons/Current.md#current)
* の権限から判定します。<br>
* チェックタイプと許可アクションリストを指定してください。
*
* [チェックタイプ](#type)<br>
* [許可アクションリスト](#allow)
*
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Controller\Component
*/
class PermissionComponent extends Component {
/**
* コンテンツReadableの定数
*
* @var string
*/
const READABLE_PERMISSION = 'content_readable';
/**
* チェックタイプの定数
*
* @var string
*/
const CHECK_TYEP_GENERAL_PLUGIN = 'general_plugin',
CHECK_TYEP_CONTROL_PANEL = 'control_panel',
CHECK_TYEP_SYSTEM_PLUGIN = 'system_plugin';
/**
* チェックタイプ
*
* * CHECK_TYEP_GENERAL_PLUGIN<br>
* ページに配置するプラグインの場合に指定します。(デフォルト)<br>
* 許可アクションリストに指定された権限から判定します。
*
* * CHECK_TYEP_CONTROL_PANEL<br>
* コントロールパネルを表示する際に指定します。<br>
* コントロールパネルで動作するプラグインの有無で判定します。
*
* * CHECK_TYEP_SYSTEM_PLUGIN<br>
* 管理プラグインを表示・設定する際に指定します。<br>
* ユーザーが使用できる管理プラグインか否かで判定します。
*
* @var string
*/
public $type = self::CHECK_TYEP_GENERAL_PLUGIN;
/**
* 許可アクションリスト
*
* チェックタイプがCHECK_TYEP_GENERAL_PLUGINの場合に使用される判定リストです。<br>
* アクション名 => 権限名の形式で指定してください。<br>
* デフォルトでは、indexアクション、viewアクションを許可しています。
* #### サンプルコード
* ##### Controller
* ```
* public $components = array(
* 'NetCommons.Permission' => array(
* 'allow' => array(
* 'add,edit,delete' => 'content_creatable',
* 'reply' => 'content_comment_creatable',
* 'approve' => 'content_comment_publishable',
* )
* )
* )
* ```
*
* アクション名に'*'を指定するとコントローラ内すべてのアクションが対象になります。
* ```
* public $components = array(
* 'NetCommons.Permission' => array(
* 'allow' => array(
* '*' => 'content_creatable'
* )
* )
* )
* ```
*
* 権限名にnullを指定するとアクセスが許可されます。
* ```
* public $components = array(
* 'NetCommons.Permission' => array(
* 'allow' => array(
* 'add,edit,delete' => 'null'
* )
* )
* )
* ```
*
* @var array
*/
public $allow = array('index,view' => null);
/**
* Called before the Controller::beforeFilter().
*
* @param Controller $controller Instantiating controller
* @return void
*/
public function initialize(Controller $controller) {
foreach ($this->allow as $allow => $permission) {
if (isset($permission) && ! is_array($permission)) {
$permission = array($permission);
}
if ($allow === '*') {
$allow = implode(',', $controller->methods);
}
$actions = explode(',', $allow);
foreach ($actions as $action) {
if (! isset($permission)) {
$allowActions[$action] = $permission;
break;
}
if (! isset($allowActions[$action])) {
$allowActions[$action] = array();
}
$allowActions[$action] = Hash::merge($allowActions[$action], $permission);
if (count($allowActions[$action]) === 0) {
$allowActions[$action] = array(self::READABLE_PERMISSION);
}
}
}
//$allowActionKeys = array_keys($allowActions);
//foreach ($allowActionKeys as $action) {
// if (! isset($allowActions[$action])) {
// break;
// }
// if (count($allowActions[$action]) === 0) {
// $allowActions[$action] = array(self::READABLE_PERMISSION);
// }
//}
$this->allow = $allowActions;
}
/**
* Called after the Controller::beforeFilter() and before the controller action
*
* @param Controller $controller Controller with components to startup
* @return void
* @throws ForbiddenException
*/
public function startup(Controller $controller) {
if (! Configure::read('NetCommons.installed')) {
return;
}
switch ($this->type) {
case self::CHECK_TYEP_SYSTEM_PLUGIN:
if (Current::allowSystemPlugin($controller->params['plugin'])) {
return;
}
break;
case self::CHECK_TYEP_CONTROL_PANEL:
if (Current::hasControlPanel()) {
return;
}
break;
case self::CHECK_TYEP_GENERAL_PLUGIN:
if (! $this->__accessCheck($controller)) {
break;
}
if ($this->__allowAction($controller)) {
return;
}
break;
}
throw new ForbiddenException(__d('net_commons', 'Permission denied'));
}
/**
* アクションの許可チェック
*
* @param Controller $controller Controller with components to startup
* @return bool
*/
private function __allowAction(Controller $controller) {
if (! isset($this->allow[$controller->params['action']])) {
return true;
}
foreach ($this->allow[$controller->params['action']] as $action) {
if (Current::permission($action)) {
return true;
}
}
return false;
}
/**
* アクセスチェック
*
* @param Controller $controller Controller with components to startup
* @return bool
*/
private function __accessCheck(Controller $controller) {
try {
$Room = ClassRegistry::init('Rooms.Room');
$spaces = $Room->getSpaces();
if ($spaces && Current::read('Room')) {
$space = Hash::get($spaces, Hash::get(Current::read('Room'), 'space_id'));
$plugin = Inflector::camelize($space['Space']['plugin_key']);
$this->SpaceComponent = $controller->Components->load($plugin . '.' . $plugin);
return $this->SpaceComponent->accessCheck($controller);
}
} catch (Exception $ex) {
CakeLog::error($ex);
}
return true;
}
}