1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**
21: * A group permission
22: *
23: * These permissions are managed through a {@link XoopsGroupPermHandler} object
24: *
25: * @package kernel
26: *
27: * @author Kazumi Ono <onokazu@xoops.org>
28: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
29: */
30: class XoopsGroupPerm extends XoopsObject
31: {
32: //PHP 8.2 Dynamic properties deprecated
33: public $gperm_id;
34: public $gperm_groupid;
35: public $gperm_itemid;
36: public $gperm_modid;
37: public $gperm_name;
38:
39: /**
40: * Constructor
41: *
42: */
43: public function __construct()
44: {
45: parent::__construct();
46: $this->initVar('gperm_id', XOBJ_DTYPE_INT, null, false);
47: $this->initVar('gperm_groupid', XOBJ_DTYPE_INT, null, false);
48: $this->initVar('gperm_itemid', XOBJ_DTYPE_INT, null, false);
49: $this->initVar('gperm_modid', XOBJ_DTYPE_INT, 0, false);
50: $this->initVar('gperm_name', XOBJ_DTYPE_OTHER, null, false);
51: }
52:
53: /**
54: * Returns Class Base Variable gperm_id
55: * @param string $format
56: * @return mixed
57: */
58: public function id($format = 'N')
59: {
60: return $this->getVar('gperm_id', $format);
61: }
62:
63: /**
64: * Returns Class Base Variable gperm_id
65: * @param string $format
66: * @return mixed
67: */
68: public function gperm_id($format = '')
69: {
70: return $this->getVar('gperm_id', $format);
71: }
72:
73: /**
74: * Returns Class Base Variable gperm_groupid
75: * @param string $format
76: * @return mixed
77: */
78: public function gperm_groupid($format = '')
79: {
80: return $this->getVar('gperm_groupid', $format);
81: }
82:
83: /**
84: * Returns Class Base Variable gperm_itemid
85: * @param string $format
86: * @return mixed
87: */
88: public function gperm_itemid($format = '')
89: {
90: return $this->getVar('gperm_itemid', $format);
91: }
92:
93: /**
94: * Returns Class Base Variable gperm_modid
95: * @param string $format
96: * @return mixed
97: */
98: public function gperm_modid($format = '')
99: {
100: return $this->getVar('gperm_modid', $format);
101: }
102:
103: /**
104: * Returns Class Base Variable gperm_name
105: * @param string $format
106: * @return mixed
107: */
108: public function gperm_name($format = '')
109: {
110: return $this->getVar('gperm_name', $format);
111: }
112: }
113:
114: /**
115: * XOOPS group permission handler class.
116: *
117: * This class is responsible for providing data access mechanisms to the data source
118: * of XOOPS group permission class objects.
119: * This class is an abstract class to be implemented by child group permission classes.
120: *
121: * @see XoopsGroupPerm
122: * @author Kazumi Ono <onokazu@xoops.org>
123: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
124: */
125: class XoopsGroupPermHandler extends XoopsObjectHandler
126: {
127: /**
128: * This should be here, since this really should be a XoopsPersistableObjectHandler
129: * Here, we fake it for future compatibility
130: *
131: * @var string table name
132: */
133: public $table;
134:
135: public function __construct(XoopsDatabase $db)
136: {
137: parent::__construct($db);
138: $this->table = $this->db->prefix('group_permission');
139: }
140:
141: /**
142: * Create a new {@link XoopsGroupPerm}
143: *
144: * @param bool $isNew Flag the object as "new"?
145: *
146: * @return XoopsGroupPerm
147: */
148: public function create($isNew = true)
149: {
150: $perm = new XoopsGroupPerm();
151: if ($isNew) {
152: $perm->setNew();
153: }
154:
155: return $perm;
156: }
157:
158: /**
159: * Retrieve a group permission
160: *
161: * @param int $id ID
162: *
163: * @return XoopsGroupPerm|false {@link XoopsGroupPerm}, false on fail
164: */
165: public function get($id)
166: {
167: $id = (int)$id;
168: $perm = false;
169: if ($id > 0) {
170: $sql = sprintf('SELECT * FROM %s WHERE gperm_id = %u', $this->db->prefix('group_permission'), $id);
171: $result = $this->db->query($sql);
172: if (!$this->db->isResultSet($result)) {
173: return $perm;
174: }
175: $numrows = $this->db->getRowsNum($result);
176: if ($numrows == 1) {
177: $perm = new XoopsGroupPerm();
178: $perm->assignVars($this->db->fetchArray($result));
179: }
180: }
181:
182: return $perm;
183: }
184:
185: /**
186: * Store a {@link XoopsGroupPerm}
187: *
188: * @param XoopsObject|XoopsGroupPerm $perm a XoopsGroupPerm object
189: *
190: * @return bool true on success, otherwise false
191: */
192: public function insert(XoopsObject $perm)
193: {
194: $className = 'XoopsGroupPerm';
195: if (!($perm instanceof $className)) {
196: return false;
197: }
198: if (!$perm->isDirty()) {
199: return true;
200: }
201: if (!$perm->cleanVars()) {
202: return false;
203: }
204: foreach ($perm->cleanVars as $k => $v) {
205: ${$k} = $v;
206: }
207: if ($perm->isNew()) {
208: $gperm_id = $this->db->genId('group_permission_gperm_id_seq');
209: $sql = sprintf('INSERT INTO %s (gperm_id, gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, %u, %u, %s)', $this->db->prefix('group_permission'), $gperm_id, $gperm_groupid, $gperm_itemid, $gperm_modid, $this->db->quoteString($gperm_name));
210: } else {
211: $sql = sprintf('UPDATE %s SET gperm_groupid = %u, gperm_itemid = %u, gperm_modid = %u WHERE gperm_id = %u', $this->db->prefix('group_permission'), $gperm_groupid, $gperm_itemid, $gperm_modid, $gperm_id);
212: }
213: if (!$result = $this->db->query($sql)) {
214: return false;
215: }
216: if (empty($gperm_id)) {
217: $gperm_id = $this->db->getInsertId();
218: }
219: $perm->assignVar('gperm_id', $gperm_id);
220:
221: return true;
222: }
223:
224: /**
225: * Delete a {@link XoopsGroupPerm}
226: *
227: * @param XoopsObject|XoopsGroupPerm $perm a XoopsGroupPerm object
228: *
229: * @return bool true on success, otherwise false
230: */
231: public function delete(XoopsObject $perm)
232: {
233: $className = 'XoopsGroupPerm';
234: if (!($perm instanceof $className)) {
235: return false;
236: }
237: $sql = sprintf('DELETE FROM %s WHERE gperm_id = %u', $this->db->prefix('group_permission'), $perm->getVar('gperm_id'));
238: if (!$result = $this->db->query($sql)) {
239: return false;
240: }
241:
242: return true;
243: }
244:
245: /**
246: * Retrieve multiple {@link XoopsGroupPerm}s
247: *
248: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
249: * @param bool $id_as_key Use IDs as array keys?
250: *
251: * @return array Array of {@link XoopsGroupPerm}s
252: */
253: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
254: {
255: $ret = array();
256: $limit = $start = 0;
257: $sql = 'SELECT * FROM ' . $this->db->prefix('group_permission');
258: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
259: $sql .= ' ' . $criteria->renderWhere();
260: $limit = $criteria->getLimit();
261: $start = $criteria->getStart();
262: }
263: $result = $this->db->query($sql, $limit, $start);
264: if (!$this->db->isResultSet($result)) {
265: return $ret;
266: }
267: /** @var array $myrow */
268: while (false !== ($myrow = $this->db->fetchArray($result))) {
269: $perm = new XoopsGroupPerm();
270: $perm->assignVars($myrow);
271: if (!$id_as_key) {
272: $ret[] =& $perm;
273: } else {
274: $ret[$myrow['gperm_id']] =& $perm;
275: }
276: unset($perm);
277: }
278:
279: return $ret;
280: }
281:
282: /**
283: * Count some {@link XoopsGroupPerm}s
284: *
285: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
286: *
287: * @return int
288: */
289: public function getCount(CriteriaElement $criteria = null)
290: {
291: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('group_permission');
292: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
293: $sql .= ' ' . $criteria->renderWhere();
294: }
295: $result = $this->db->query($sql);
296: if (!$this->db->isResultSet($result)) {
297: return 0;
298: }
299: list($count) = $this->db->fetchRow($result);
300:
301: return (int)$count;
302: }
303:
304: /**
305: * Delete all permissions by a certain criteria
306: *
307: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
308: *
309: * @return bool TRUE on success
310: */
311: public function deleteAll(CriteriaElement $criteria = null)
312: {
313: $sql = sprintf('DELETE FROM %s', $this->db->prefix('group_permission'));
314: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
315: $sql .= ' ' . $criteria->renderWhere();
316: }
317: if (!$result = $this->db->query($sql)) {
318: return false;
319: }
320:
321: return true;
322: }
323:
324: /**
325: * Delete all module specific permissions assigned for a group
326: *
327: * @param int $gperm_groupid ID of a group
328: * @param int $gperm_modid ID of a module
329: *
330: * @return bool TRUE on success
331: */
332: public function deleteByGroup($gperm_groupid, $gperm_modid = null)
333: {
334: $criteria = new CriteriaCompo(new Criteria('gperm_groupid', (int)$gperm_groupid));
335: if (isset($gperm_modid)) {
336: $criteria->add(new Criteria('gperm_modid', (int)$gperm_modid));
337: }
338:
339: return $this->deleteAll($criteria);
340: }
341:
342: /**
343: * Delete all module specific permissions
344: *
345: * @param int $gperm_modid ID of a module
346: * @param string $gperm_name Name of a module permission
347: * @param int $gperm_itemid ID of a module item
348: *
349: * @return bool TRUE on success
350: */
351: public function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null)
352: {
353: $criteria = new CriteriaCompo(new Criteria('gperm_modid', (int)$gperm_modid));
354: if (isset($gperm_name)) {
355: $criteria->add(new Criteria('gperm_name', $gperm_name));
356: if (isset($gperm_itemid)) {
357: $criteria->add(new Criteria('gperm_itemid', (int)$gperm_itemid));
358: }
359: }
360:
361: return $this->deleteAll($criteria);
362: }
363:
364: /**
365: * Check permission
366: *
367: * @param string $gperm_name Name of permission
368: * @param int $gperm_itemid ID of an item
369: * @param int /array $gperm_groupid A group ID or an array of group IDs
370: * @param int $gperm_modid ID of a module
371: * @param bool $trueifadmin Returns true for admin groups
372: *
373: * @return bool TRUE if permission is enabled
374: */
375: public function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1, $trueifadmin = true)
376: {
377: if (empty($gperm_groupid)) {
378: return false;
379: } elseif (is_array($gperm_groupid)) {
380: if (in_array(XOOPS_GROUP_ADMIN, $gperm_groupid) && $trueifadmin) {
381: return true;
382: }
383: $criteria_group = new CriteriaCompo();
384: foreach ($gperm_groupid as $gid) {
385: $criteria_group->add(new Criteria('gperm_groupid', $gid), 'OR');
386: }
387: } else {
388: if (XOOPS_GROUP_ADMIN == $gperm_groupid && $trueifadmin) {
389: return true;
390: }
391: $criteria_group = new CriteriaCompo(new Criteria('gperm_groupid', $gperm_groupid));
392: }
393: $criteria = new CriteriaCompo(new Criteria('gperm_modid', $gperm_modid));
394: $criteria->add($criteria_group);
395: $criteria->add(new Criteria('gperm_name', $gperm_name));
396: $gperm_itemid = (int)$gperm_itemid;
397: if ($gperm_itemid > 0) {
398: $criteria->add(new Criteria('gperm_itemid', $gperm_itemid));
399: }
400: return $this->getCount($criteria) > 0;
401: }
402:
403: /**
404: * Add a permission
405: *
406: * @param string $gperm_name Name of permission
407: * @param int $gperm_itemid ID of an item
408: * @param int $gperm_groupid ID of a group
409: * @param int $gperm_modid ID of a module
410: *
411: * @return bool TRUE if success
412: */
413: public function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
414: {
415: /** @var XoopsGroupPerm $perm */
416: $perm = $this->create();
417: $perm->setVar('gperm_name', $gperm_name);
418: $perm->setVar('gperm_groupid', $gperm_groupid);
419: $perm->setVar('gperm_itemid', $gperm_itemid);
420: $perm->setVar('gperm_modid', $gperm_modid);
421:
422: return $this->insert($perm);
423: }
424:
425: /**
426: * Get all item IDs that a group is assigned a specific permission
427: *
428: * @param string $gperm_name Name of permission
429: * @param int /array $gperm_groupid A group ID or an array of group IDs
430: * @param int $gperm_modid ID of a module
431: *
432: * @return array array of item IDs
433: */
434: public function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1)
435: {
436: $ret = array();
437: $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
438: $criteria->add(new Criteria('gperm_modid', (int)$gperm_modid));
439: if (is_array($gperm_groupid)) {
440: $criteria2 = new CriteriaCompo();
441: foreach ($gperm_groupid as $gid) {
442: $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
443: }
444: $criteria->add($criteria2);
445: } else {
446: $criteria->add(new Criteria('gperm_groupid', (int)$gperm_groupid));
447: }
448: $perms = $this->getObjects($criteria, true);
449: foreach (array_keys($perms) as $i) {
450: $ret[] = $perms[$i]->getVar('gperm_itemid');
451: }
452:
453: return array_unique($ret);
454: }
455:
456: /**
457: * Get all group IDs assigned a specific permission for a particular item
458: *
459: * @param string $gperm_name Name of permission
460: * @param int $gperm_itemid ID of an item
461: * @param int $gperm_modid ID of a module
462: *
463: * @return array array of group IDs
464: */
465: public function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1)
466: {
467: $ret = array();
468: $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
469: $criteria->add(new Criteria('gperm_itemid', (int)$gperm_itemid));
470: $criteria->add(new Criteria('gperm_modid', (int)$gperm_modid));
471: $perms = $this->getObjects($criteria, true);
472: foreach (array_keys($perms) as $i) {
473: $ret[] = $perms[$i]->getVar('gperm_groupid');
474: }
475:
476: return $ret;
477: }
478: }
479: