-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCalendarPermissionComponent.php
More file actions
136 lines (130 loc) · 3.76 KB
/
CalendarPermissionComponent.php
File metadata and controls
136 lines (130 loc) · 3.76 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
<?php
/**
* CalendarPermission Component
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Allcreator <info@allcreator.net>
* @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');
App::uses('NetCommonsTime', 'NetCommons.Utility');
App::uses('Block', 'Blocks.Model');
App::uses('CalendarPermissiveRooms', 'Calendars.Utility');
/**
* CalendarPermission Component
*
* リクエストされたカレンダー予定へのアクセス許可を、<br>
* 指定されたカレンダー予定の対象空間、共有人物、ステータス、
* および閲覧者の権限から判定します。<br>
*
* @author Allcreator <info@allcreator.net>
* @package Calendars\Calendars\Controller\Component
*/
class CalendarPermissionComponent extends Component {
/**
* 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) {
$this->controller = $controller;
// add -> どこか一つでもcreatableな空間を持っている人なら
// view -> 対象の空間に参加しているなら
// ただし、対象空間がプライベートのときに限り、共有者となっているなら
// edit -> 対象空間での編集権限を持っているか、対象予定の作成者なら
// delete -> 対象空間での編集権限を持っているか、対象予定の作成者なら
switch ($controller->action) {
case 'add':
if ($this->_hasCreatableRoom()) {
return;
}
break;
case 'edit':
case 'delete':
if ($this->_canEditEvent()) {
return;
}
break;
case 'view':
if ($this->_canReadEvent()) {
return;
}
break;
}
// チェックで引っかかってしまったらForbidden
throw new ForbiddenException();
}
/**
* Creatable権限を持っているルームが一つでもあるか
*
* @return bool
*/
protected function _hasCreatableRoom() {
$rooms = CalendarPermissiveRooms::getCreatableRoomIdList();
if (empty($rooms)) {
return false;
}
return true;
}
/**
* 対象のイベントは存在するか
*
* @return bool
*/
protected function _existEvent() {
if (empty($this->controller->eventData)) {
return false;
}
return true;
}
/**
* _canReadEvent
* 対象のイベントルームに参加しているか
*
* @return bool
*/
protected function _canReadEvent() {
if (! $this->_existEvent()) {
return false;
}
//$roomPermRoles = $this->controller->roomPermRoles;
$calendarEv = $this->controller->eventData['CalendarEvent'];
$shareUsersIds = [];
foreach ($this->controller->shareUsers as $shareUser) {
$shareUsersIds[] = $shareUser['CalendarEventShareUser']['share_user'];
}
// ルームに参加している
if (in_array($calendarEv['room_id'], CalendarPermissiveRooms::getAccessibleRoomIdList())) {
return true;
}
// 参加してなくても対象ルームがプライベートで共有者であればOK
if (in_array(Current::read('User.id'), $shareUsersIds)) {
return true;
}
return false;
}
/**
* 対象のイベントに対して編集権限を持っているか
*
* @return bool
*/
protected function _canEditEvent() {
if (! $this->_existEvent()) {
return false;
}
$calendarEv = $this->controller->eventData['CalendarEvent'];
if (CalendarPermissiveRooms::isEditable($calendarEv['room_id'])) {
return true;
}
if (CalendarPermissiveRooms::isCreatable($calendarEv['room_id'])) {
if ($calendarEv['created_user'] == Current::read('User.id')) {
return true;
}
}
return false;
}
}