This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAccess.php
281 lines (274 loc) · 11.4 KB
/
Access.php
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
<?php
/**
* Clip
*
* @copyright (c) Clip Team
* @link http://github.com/zikula-modules/clip/
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @package Clip
* @subpackage Lib
*/
namespace Matheo\Clip;
use SecurityUtil;
use ZLanguage;
use DataUtil;
use LogUtil;
use UserUtil;
use Matheo\Clip\Util;
use Matheo_Clip_Model_Pubtype;
use Matheo\Clip\Workflow;
use Exception;
use ModUtil;
use DBUtil;
use Matheo\Clip\Doctrine\PubdataDoctrine;
class Access
{
/**
* Checks if the current user has access to Clip.
*
* @param integer $permlvl Required permission level to have (default: ACCESS_OVERVIEW).
* @param string $instance Instance to check (default: '::').
*
* @return boolean True if allowed, false otherwise.
*/
public static function toClip($permlvl = null, $instance = null)
{
// fill default values if needed
$permlvl = $permlvl ? $permlvl : ACCESS_OVERVIEW;
$instance = $instance ? $instance : '::';
// evaluate the access
return SecurityUtil::checkPermission('Clip::', $instance, $permlvl);
}
/**
* Checks if the current user has access to a grouptype.
*
* @param integer $gid Grouptype ID.
*
* @return boolean True if allowed, false otherwise.
*/
public static function toGrouptype($gid)
{
$dom = ZLanguage::getModuleDomain('Clip');
if (!$gid) {
return LogUtil::registerError(__f('%1$s: Invalid grouptype ID passed [%2$s].', array('Access::toGrouptype', DataUtil::formatForDisplay($gid)), $dom));
}
// evaluate the access
return SecurityUtil::checkPermission("Clip:g{$gid}:", '::', ACCESS_OVERVIEW);
}
/**
* Checks if the current user has access to a pubtype.
*
* @param integer $pubtype Publication type instance or ID.
* @param string $context Context to check the access for (default: admin).
* @param string $tplid Template ID required for permission check on main/list (default: '').
* @param integer $uid User ID of the user to check permission for (default: current user).
*
* @throws Exception If an invalid context was passed.
*
* @return boolean True if allowed, false otherwise.
*/
public static function toPubtype(
$pubtype = null,
$context = null,
$tplid = null,
$uid = null
) {
$dom = ZLanguage::getModuleDomain('Clip');
// fill default values if needed
$context = $context ? strtolower($context) : 'admin';
$tplid = $tplid ? $tplid : '';
$uid = $uid ? $uid : (int) UserUtil::getVar('uid');
$component = 'Clip::';
$instance = '::';
$permlvl = ACCESS_ADMIN;
// be sure to have a Matheo_Clip_Model_Pubtype instance
if ($pubtype) {
if (!$pubtype instanceof PubtypeModel) {
if (!Util::validateTid($pubtype)) {
return LogUtil::registerError(__f('%1$s: Invalid publication type ID passed [%2$s].', array('Access::toPubtype', DataUtil::formatForDisplay($pubtype)), $dom));
}
$pubtype = Util::getPubType($pubtype);
}
$component = "Clip:{$pubtype->tid}:";
} else {
if (in_array($context, array('editor', 'submit'))) {
return false;
}
}
// evaluate the access depending of the required context
switch ($context) {
case 'access':
$permlvl = ACCESS_OVERVIEW;
break;
case 'main':
case 'list':
case 'edit':
$component .= $context;
$instance .= $tplid;
$permlvl = ACCESS_OVERVIEW;
break;
case 'editor':
// panel
if ($pubtype['enableeditown'] != 1) {
$component .= 'edit';
// TODO consider edit.own and not for pubs submitted by guests
$workflow = new Workflow($pubtype);
// assumes level 1 as the first moderator permission
$permlvl = $workflow->getPermissionLevel(1, 'initial');
if (!$permlvl) {
// if there isn't a level 1, assumes the level 0 as editor
$permlvl = $workflow->getPermissionLevel(0, 'initial');
}
break;
}
case 'submit':
// submit new content
$component .= 'edit';
$instance = ':initial:';
$workflow = new Workflow($pubtype);
// assumes level 0 as the basic submit permission
$permlvl = $workflow->getPermissionLevel(0, 'initial');
break;
case 'admin':
break;
case 'anyeditor':
$permlvl = ACCESS_EDITOR;
case 'anyadmin':
//$component = "Clip::";
//$instance = 'ANY';
// TODO Use a reverse method to detect the available permissions rules
break;
default:
throw new Exception(__f('%1$s: Invalid context passed [%2$s].', array('Access::toPubtype', DataUtil::formatForDisplay($context)), $dom));
}
// check the cached results
static $cache = array();
if (!isset($cache[$context][$uid][$component][$instance])) {
$cache[$context][$uid][$component][$instance] = $permlvl ? SecurityUtil::checkPermission($component, $instance, $permlvl, $uid) : false;
}
return $cache[$context][$uid][$component][$instance];
}
/**
* Checks if the current user has access to a publication.
*
* @param integer $pubtype Publication type instance or ID.
* @param integer $pub Publication instance or ID.
* @param integer $id Publication revision ID (optional if $pub is a instance) (default: null).
* @param string $context Context to check the access for (default: access).
* @param string $tplid Template ID required for permission check on display (default: null).
* @param integer $permlvl Required permission level to have, used on edit context only (default: null).
* @param integer $uid User ID of the user to check permission for (default: current user).
* @param string $action Action ID required for permission check on exec (default: null).
*
* @return boolean True if allowed, false otherwise.
*/
public static function toPub(
$pubtype,
$pub,
$id = null,
$context = null,
$tplid = null,
$permlvl = null,
$uid = null,
$action = null
) {
$dom = ZLanguage::getModuleDomain('Clip');
// fill default values if needed
$context = $context ? strtolower($context) : 'edit';
$tplid = $tplid ? $tplid : '';
$state = '';
$action = $action ? $action : '';
// be sure to have a Clip_Model_Pubtype instance
if (!$pubtype instanceof Matheo_Clip_Model_Pubtype) {
if (!Util::validateTid($pubtype)) {
return LogUtil::registerError(__f('%1$s: Invalid publication type ID passed [%2$s].', array('Access::toPub', DataUtil::formatForDisplay($pubtype)), $dom));
}
$pubtype = Util::getPubType($pubtype);
}
// when it's an instance we can do a complete check
if ($pub instanceof PubdataDoctrine) {
// check an already stored record
if ($pub->exists()) {
$pid = $pub['core_pid'];
} else {
// the user may wants to save a new record
$pid = '';
}
// check if the state is needed
if (self::isPubStateNeeded($context)) {
$state = $pub->clipWorkflow('state');
}
} else {
if (is_numeric($pub)) {
// direct pid
$pid = (int) $pub;
} else {
if (is_array($pub) && isset($pub['core_pid'])) {
// pub as array
$pid = (int) $pub['core_pid'];
} else {
return false;
}
}
// check if we need additional information
if (self::isPubStateNeeded($context)) {
// gets the online/latest revision state, if $id not specified
if (!$id) {
$id = (int) ModUtil::apiFunc('Clip', 'user', 'getId', array('tid' => $pubtype->tid, 'pid' => $pid, 'lastrev' => true));
}
// query for the state
$dbtables = DBUtil::getTables();
$wfcolumn = $dbtables['workflows_column'];
$where = "WHERE {$wfcolumn['module']} = 'Clip' AND {$wfcolumn['obj_table']} = '{$pubtype->getTableName()}' AND {$wfcolumn['obj_idcolumn']} = 'id' AND {$wfcolumn['obj_id']} = '" . DataUtil::formatForStore($id) . '\'';
$state = (string) DBUtil::selectField('workflows', 'state', $where);
}
}
// evaluate the access depending of the required context
$allowed = false;
switch ($context) {
case 'access':
$permlvl = $permlvl ? $permlvl : ACCESS_READ;
// TODO adjust with display/edit changes
$allowed = SecurityUtil::checkPermission("Clip:{$pubtype->tid}:edit", "{$pid}:{$state}:{$tplid}", $permlvl, $uid);
if (!$allowed) {
$allowed = SecurityUtil::checkPermission("Clip:{$pubtype->tid}:display", "{$pid}::{$tplid}", $permlvl, $uid);
}
break;
case 'display':
$permlvl = $permlvl ? $permlvl : ACCESS_READ;
// TODO check core_online + normal user = false (relations check, etc)
$allowed = SecurityUtil::checkPermission("Clip:{$pubtype->tid}:display", "{$pid}::{$tplid}", $permlvl, $uid);
break;
case 'form':
$permlvl = $permlvl ? $permlvl : ACCESS_READ;
$allowed = SecurityUtil::checkPermission("Clip:{$pubtype->tid}:edit", "{$pid}:{$state}:{$tplid}", $permlvl, $uid);
break;
case 'edit':
if (!SecurityUtil::checkPermission("Clip:{$pubtype->tid}:edit", "{$pid}:{$state}:{$tplid}", ACCESS_READ, $uid)) {
// direct discard if do not have permission to the form
break;
}
case 'exec':
case 'execinline':
if (!$permlvl) {
// gets the minimum state permission level
$workflow = $pub instanceof PubdataDoctrine ? new Workflow($pubtype, $pub) : new Workflow($pubtype);
$mode = $context == 'execinline' ? Workflow::ACTIONS_ALL : Workflow::ACTIONS_FORM;
$permlvl = $workflow->getPermissionLevel(
0,
$state,
$mode
);
}
if ($permlvl) {
$allowed = SecurityUtil::checkPermission("Clip:{$pubtype->tid}:exec", "{$pid}:{$state}:{$action}", $permlvl, $uid);
}
break;
}
return $allowed;
}
public static function isPubStateNeeded($context)
{
return in_array($context, array('access', 'exec')) || strpos($context, 'edit') === 0;
}
}