forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrent.php
More file actions
556 lines (511 loc) · 11.1 KB
/
Current.php
File metadata and controls
556 lines (511 loc) · 11.1 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* Current Utility
*
* @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('CurrentSystem', 'NetCommons.Utility');
App::uses('CurrentFrame', 'NetCommons.Utility');
App::uses('CurrentPage', 'NetCommons.Utility');
App::uses('UserRole', 'UserRoles.Model');
App::uses('Plugin', 'PluginManager.Model');
/**
* Current Utility
*
* NetCommonsの機能として必要な情報を保持します。<br>
* [NetCommonsAppController::beforeFilter](./NetCommonsAppController.html#method_beforeFilter)
* で初期処理が呼び出され、値が設定されます。<br>
* 値を取得する時は、[readメソッド](#method_read)を使用します。<br>
* 権限を取得する時は、[permissionメソッド](#method_permission)を使用します。<br>
*
* #### 保持データ
* ```
* Array(
* [User] => Array(
* [id] => 1
* [username] => admin
* [key] => 640f981d6104fd21463d674f18477348
* [avatar] =>
* [avatar_file_id] =>
* [is_avatar_public] =>
* [handlename] => admin
* [is_handlename_public] =>
* [is_name_public] =>
* [email] =>
* [is_email_public] =>
*
* ・・・
*
* [Role] => Array(
* [id] => 1
* [language_id] => 2
* [key] => system_administrator
* [type] => 1
* [name] => システム管理者
* [is_system] => 1
* )
* )
*
* [Language] => Array(
* [id] => 2
* [code] => ja
* [weight] => 2
* [is_active] => 1
* )
*
* [PluginsRole] => Array(
* [13] => Array(
* [id] => 13
* [role_key] => system_administrator
* [plugin_key] => rooms
* )
*
* [20] => Array(
* [id] => 20
* [role_key] => system_administrator
* [plugin_key] => user_manager
* )
*
* )
*
* [Page] => Array(
* [id] => 1
* [room_id] => 1
* [parent_id] =>
* [lft] => 1
* [rght] => 2
* [permalink] =>
* [slug] =>
* [is_published] => 1
* [from] =>
* [to] =>
* [is_container_fluid] =>
* )
*
* [Room] => Array(
* [id] => 1
* [space_id] => 2
* [page_id_top] => 1
* [root_id] =>
* [parent_id] =>
* [lft] => 1
* [rght] => 2
* [active] => 1
* [default_role_key] => visitor
* [need_approval] => 1
* [default_participation] => 1
* [page_layout_permitted] => 1
* )
*
* [ParentPage] => Array(
* ・・・
* )
*
* [RolesRoomsUser] => Array(
* ・・・
* )
*
* [RolesRoom] => Array(
* ・・・
* )
*
* ・・・
*
* [Permission] => Array(
* [page_editable] => Array(
* [id] => 9
* [role_key] => room_administrator
* [type] => room_role
* [permission] => page_editable
* [value] => 1
* [fixed] => 1
* [roles_room_id] => 1
* )
*
* [block_editable] => Array(
* ・・・
* )
*
* [content_readable] => Array(
* ・・・
* )
*
* [content_creatable] => Array(
* ・・・
* )
*
* [content_editable] => Array(
* ・・・
* )
*
* [content_publishable] => Array(
* ・・・
* )
*
* [content_comment_creatable] => Array(
* ・・・
* )
*
* [content_comment_editable] => Array(
* ・・・
* )
*
* [content_comment_publishable] => Array(
* ・・・
* )
*
* [block_permission_editable] => Array(
* ・・・
* )
*
* [html_not_limited] => Array(
* ・・・
* )
*
* )
*
* [Plugin] => Array(
* [id] => 5
* [language_id] => 2
* [key] => announcements
* [name] => お知らせ
* [namespace] => netcommons/announcements
* [weight] =>
* [type] => 1
* [default_action] => announcements/view
* [default_setting_action] => announcement_blocks/index
* )
*
* [Frame] => Array(
* [id] => 1
* [language_id] => 2
* [room_id] => 1
* [box_id] => 3
* [plugin_key] => announcements
* [block_id] => 1
* [key] => frame_1
* [name] => お知らせ
* [header_type] => default
* [translation_engine] =>
* [is_first_auto_translation] =>
* [is_auto_translated] =>
* [weight] => 1
* [is_deleted] =>
* )
*
* [Box] => Array(
* [id] => 3
* [container_id] => 3
* [type] => 4
* [space_id] =>
* [room_id] => 1
* [page_id] => 1
* [weight] => 1
* )
*
* [Block] => Array(
* [id] => 1
* [language_id] => 2
* [room_id] => 1
* [plugin_key] => announcements
* [key] => block_1
* [name] => NetCommons 3! セッティングモードで編集しよう.
* [public_type] => 1
* [from] =>
* [to] =>
* [translation_engine] =>
* [is_auto_translated] =>
* [is_first_auto_translation] =>
* )
* )
* ```
*
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Utility
*/
class CurrentBase {
/**
* Current data
*
* @var array
*/
public static $current = array();
/**
* 指定された$keyの値を返します。
*
* 現在のBlockKeyを取得したい場合
* ```
* Cuurent::read('Block.key')
* ```
*
* @param string|null $key Hashクラスのpath
* @param mixed $default デフォルト値
* @return array|null Current data.
*/
public static function read($key = null, $default = null) {
if (! isset(self::$current)) {
return self::$current;
}
if (! isset($key)) {
return self::$current;
}
return Hash::get(self::$current, $key, $default);
}
/**
* 指定された$keyの値をセットします
*
* 現在のBlockKeyをセットしたい場合
* ```
* Cuurent::write('Block.key', 'block_key)
* ```
*
* @param string|null $key Hashクラスのpath、nullの場合、Hash::mergeする
* @param mixted $value セットする値
* @return void
*/
public static function write($key, $value) {
if (! isset(self::$current)) {
self::$current = array();
}
if (! isset($key)) {
self::$current = Hash::merge(self::$current, $value);
} else {
self::$current = Hash::insert(self::$current, $key, $value);
}
}
/**
* 指定された$keyの値を削除します。
*
* 現在のBlockKeyを削除したい場合
* ```
* Cuurent::remove('Block.key')
* ```
*
* @param string|null $key Hashクラスのpath
* @return array|null Current data.
*/
public static function remove($key = null) {
if (! isset(self::$current) || ! isset($key)) {
self::$current = array();
}
self::$current = Hash::remove(self::$current, $key);
}
/**
* 指定された$key(権限名文字列)の値を返します。
*
* ```
* Current::permission('content_publishable')
* ```
*
* @param string $key Hashクラスのpath
* @return bool permission value
*/
public static function permission($key) {
if (! isset(self::$current)) {
return false;
}
$path = 'Permission.' . $key . '.value';
return (bool)Hash::get(self::$current, $path);
}
}
/**
* Current Utilityチェック
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Utility
*/
class Current extends CurrentBase {
/**
* Constant setting mode value
*/
const SETTING_MODE_WORD = 'setting';
/**
* Usersプラグイン名の定数
*/
const PLUGIN_USERS = 'users';
/**
* Groupsプラグイン名の定数
*/
const PLUGIN_GROUPS = 'groups';
/**
* Wysiwygプラグイン名の定数
*/
const PLUGIN_WYSIWYG = 'wysiwyg';
/**
* Pagesプラグイン名の定数
*/
const PLUGIN_PAGES = 'pages';
/**
* is setting mode true
*
* @var bool
*/
protected static $_isSettingMode = null;
/**
* Request object
*
* @var mixed
*/
public static $request;
/**
* layout
*
* @var string
*/
public static $layout;
/**
* Instance object
*
* @var mixed
*/
protected static $_instance;
/**
* M17n data
*
* @var array
*/
public static $m17n = array();
/**
* setup current data
*
* @param Controller $controller コントローラ
* @return void
*/
public static function initialize(Controller $controller) {
if (! self::$_instance) {
self::$_instance = new Current();
}
self::$request = clone $controller->request;
self::$layout = $controller->layout;
self::$current['User'] = AuthComponent::user();
(new CurrentSystem())->initialize();
if (! self::isControlPanel()) {
(new CurrentFrame())->initialize();
}
}
/**
* 多言語のデータ取得
*
* @param string|null $languageId 言語ID
* @param string|null $model モデル名
* @param string|null $field フィールド名
* @return mixed Currentデータ
*/
public static function readM17n($languageId, $model = null, $field = null) {
if (! isset(self::$m17n)) {
return self::$m17n;
}
if (! isset($model)) {
return self::$m17n;
}
if (! isset(self::$m17n[$model])) {
return null;
}
if (! isset($languageId)) {
return self::$m17n[$model];
}
$pathKey = $model . '.{n}.' . $model . '[language_id=' . $languageId . ']';
$result = Hash::extract(self::$m17n, $pathKey);
if (! $result) {
return null;
}
if (! isset($field)) {
return array($model => $result[0]);
} else {
return $result[0][$field];
}
}
/**
* ログインチェック
*
* @return bool
*/
public static function isLogin() {
return (bool)AuthComponent::user('id');
}
/**
* セッティングモードチェック
*
* @param bool|null $settingMode セッティングモードの状態変更
* @return bool
*/
public static function isSettingMode($settingMode = null) {
if (isset($settingMode)) {
self::$_isSettingMode = $settingMode;
}
if (isset(self::$_isSettingMode)) {
return self::$_isSettingMode;
}
$pattern = preg_quote('/' . self::SETTING_MODE_WORD . '/', '/');
if (preg_match('/' . $pattern . '/', Router::url())) {
self::$_isSettingMode = true;
} else {
self::$_isSettingMode = false;
}
return self::$_isSettingMode;
}
/**
* セッティングモードの有無
*
* @return bool
*/
public static function hasSettingMode() {
return self::permission('page_editable');
}
/**
* コントロールパネルチェック
*
* @return bool
*/
public static function isControlPanel() {
if (! isset(self::$request)) {
return false;
}
if (self::$request->params['plugin'] === CurrentSystem::PLUGIN_CONTROL_PANEL) {
return true;
}
if (! isset(self::$current['Plugin'])) {
return false;
}
$controlPanelKeys = array(
Plugin::PLUGIN_TYPE_FOR_SITE_MANAGER, Plugin::PLUGIN_TYPE_FOR_SYSTEM_MANGER
);
if (in_array(self::$current['Plugin']['type'], $controlPanelKeys, true)) {
return true;
} else {
return false;
}
}
/**
* コントロールパネルの有無
*
* @return bool
*/
public static function hasControlPanel() {
if (! isset(self::$current['PluginsRole'])) {
return false;
} else {
return (bool)count(self::$current['PluginsRole']);
}
}
/**
* 管理系プラグインの許可
*
* @param string $pluginKey プラグインkey
* @return bool
*/
public static function allowSystemPlugin($pluginKey) {
if (! isset(self::$current['PluginsRole'])) {
return false;
}
//if (self::read('User.role_key') === UserRole::USER_ROLE_KEY_SYSTEM_ADMINISTRATOR) {
// return true;
//}
return Hash::check(Current::$current['PluginsRole'], '{n}[plugin_key=' . $pluginKey . ']');
}
}