1: <?php
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: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35:
36: define("GROUPPERM_VAL_MODREAD", "module_read");
37: define("GROUPPERM_VAL_MODADMIN", "module_admin");
38: define("GROUPPERM_VAL_BLOCKREAD", "block_read");
39:
40: 41: 42: 43: 44: 45: 46: 47:
48:
49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: class XoopsGroupPerm extends XoopsObject
60: {
61:
62: 63: 64: 65:
66: function XoopsGroupPerm()
67: {
68: static $initVars;
69: if (isset($initVars)) {
70: $this->vars = $initVars;
71: return;
72: }
73: $this->XoopsObject();
74: $this->initVar('gperm_id', XOBJ_DTYPE_INT, null, false);
75: $this->initVar('gperm_groupid', XOBJ_DTYPE_INT, null, false);
76: $this->initVar('gperm_itemid', XOBJ_DTYPE_INT, null, false);
77: $this->initVar('gperm_modid', XOBJ_DTYPE_INT, 0, false);
78: $this->initVar('gperm_name', XOBJ_DTYPE_OTHER, null, false);
79: $initVars = $this->vars;
80: }
81:
82: function cleanVars()
83: {
84: if (!parent::cleanVars()) {
85: return false;
86: }
87:
88:
89:
90:
91:
92: $gHandler = xoops_gethandler('group');
93: $group =& $gHandler->get($this->get('gperm_groupid'));
94: if (!is_object($group)) {
95: return false;
96: }
97:
98: $mHandler = xoops_gethandler('module');
99:
100: if ($this->get('gperm_modid') != 1) {
101: $module =& $mHandler->get($this->get('gperm_modid'));
102: if (!is_object($module)) {
103: return false;
104: }
105: }
106:
107: if ($this->get('gperm_name') == GROUPPERM_VAL_MODREAD
108: || $this->get('gperm_name') == GROUPPERM_VAL_MODADMIN)
109: {
110: $mHandler = xoops_gethandler('module');
111: $module =& $mHandler->get($this->get('gperm_itemid'));
112: if (!is_object($module)) {
113: return false;
114: }
115: }
116: else if ($this->get('gperm_name') == GROUPPERM_VAL_BLOCKREAD) {
117: $bHandler = xoops_gethandler('block');
118: $block =& $bHandler->get($this->get('gperm_itemid'));
119: if (!is_object($block)) {
120: return false;
121: }
122: }
123:
124: return true;
125: }
126: }
127:
128:
129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139:
140: class XoopsGroupPermHandler extends XoopsObjectHandler
141: {
142:
143: 144: 145: 146: 147:
148: function &create($isNew = true)
149: {
150: $perm =new XoopsGroupPerm();
151: if ($isNew) {
152: $perm->setNew();
153: }
154: return $perm;
155: }
156:
157: 158: 159: 160: 161: 162: 163:
164: function &get($id)
165: {
166: $ret = false;
167: if ((int)$id > 0) {
168: $db = &$this->db;
169: $sql = sprintf('SELECT * FROM %s WHERE gperm_id = %u', $db->prefix('group_permission'), $id);
170: if ($result = $db->query($sql)) {
171: $numrows = $db->getRowsNum($result);
172: if ( $numrows == 1 ) {
173: $perm =new XoopsGroupPerm();
174: $perm->assignVars($db->fetchArray($result));
175: $ret =& $perm;
176: }
177: }
178: }
179: return $ret;
180: }
181:
182: 183: 184: 185: 186: 187: 188:
189: function insert(&$perm)
190: {
191: if ( strtolower(get_class($perm)) != 'xoopsgroupperm' ) {
192: return false;
193: }
194: if ( !$perm->isDirty() ) {
195: return true;
196: }
197: if (!$perm->cleanVars()) {
198: return false;
199: }
200:
201: foreach ($perm->cleanVars as $k => $v) {
202: ${$k} = $v;
203: }
204: $db = &$this->db;
205: if ($perm->isNew()) {
206: $gperm_id = $db->genId('group_permission_gperm_id_seq');
207: $sql = sprintf('INSERT INTO %s (gperm_id, gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, %u, %u, %s)', $db->prefix('group_permission'), $gperm_id, $gperm_groupid, $gperm_itemid, $gperm_modid, $db->quoteString($gperm_name));
208: } else {
209: $sql = sprintf('UPDATE %s SET gperm_groupid = %u, gperm_itemid = %u, gperm_modid = %u WHERE gperm_id = %u', $db->prefix('group_permission'), $gperm_groupid, $gperm_itemid, $gperm_modid, $gperm_id);
210: }
211: if (!$result = $db->query($sql)) return false;
212: if (empty($gperm_id)) $gperm_id = $this->db->getInsertId();
213: $perm->assignVar('gperm_id', $gperm_id);
214: return true;
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: function delete(&$perm)
225: {
226: if (strtolower(get_class($perm)) != 'xoopsgroupperm') {
227: return false;
228: }
229: $sql = sprintf("DELETE FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $perm->getVar('gperm_id'));
230: if (!$result = $this->db->query($sql)) {
231: return false;
232: }
233: return true;
234: }
235:
236: 237: 238: 239: 240: 241: 242: 243:
244: function &getObjects($criteria = null, $id_as_key = false)
245: {
246: $ret = array();
247: $limit = $start = 0;
248: $db = &$this->db;
249: $sql = 'SELECT * FROM '.$db->prefix('group_permission');
250: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
251: $sql .= ' '.$criteria->renderWhere();
252: $limit = $criteria->getLimit();
253: $start = $criteria->getStart();
254: }
255: $result = $db->query($sql, $limit, $start);
256: if (!$result) {
257: return $ret;
258: }
259: while ($myrow = $db->fetchArray($result)) {
260: $perm =new XoopsGroupPerm();
261: $perm->assignVars($myrow);
262: if (!$id_as_key) {
263: $ret[] =& $perm;
264: } else {
265: $ret[$myrow['gperm_id']] =& $perm;
266: }
267: unset($perm);
268: }
269: return $ret;
270: }
271:
272: 273: 274: 275: 276: 277: 278:
279: function getCount($criteria = null)
280: {
281: $db = &$this->db;
282: $sql = 'SELECT COUNT(*) FROM '.$db->prefix('group_permission');
283: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
284: $sql .= ' '.$criteria->renderWhere();
285: }
286: $result = $db->query($sql);
287: if (!$result) {
288: return 0;
289: }
290: list($count) = $db->fetchRow($result);
291: return $count;
292: }
293:
294: 295: 296: 297: 298: 299: 300:
301: function deleteAll($criteria = null)
302: {
303: $sql = sprintf("DELETE FROM %s", $this->db->prefix('group_permission')); if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
304: $sql .= ' '.$criteria->renderWhere();
305: }
306: if (!$result = $this->db->query($sql)) {
307: return false;
308: }
309: return true;
310: }
311:
312: 313: 314: 315: 316: 317: 318: 319:
320: function deleteByGroup($gperm_groupid, $gperm_modid = null)
321: {
322: $criteria = new CriteriaCompo(new Criteria('gperm_groupid', (int)$gperm_groupid));
323: if (isset($gperm_modid)) {
324: $criteria->add(new Criteria('gperm_modid', (int)$gperm_modid));
325: }
326: return $this->deleteAll($criteria);
327: }
328:
329: 330: 331: 332: 333: 334: 335: 336: 337:
338: function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null)
339: {
340: $criteria = new CriteriaCompo(new Criteria('gperm_modid', (int)$gperm_modid));
341: if (isset($gperm_name)) {
342: $criteria->add(new Criteria('gperm_name', $gperm_name));
343: if (isset($gperm_itemid)) {
344: $criteria->add(new Criteria('gperm_itemid', (int)$gperm_itemid));
345: }
346: }
347: return $this->deleteAll($criteria);
348: }
349:
350:
351: 352: 353:
354: function deleteBasicPermission($gperm_groupid)
355: {
356: $criteria = new CriteriaCompo(new Criteria('gperm_groupid', $gperm_groupid));
357: $criteria->add(new Criteria('gperm_modid', 1));
358: $criteria2 = new CriteriaCompo(new Criteria('gperm_name', 'system_admin'));
359: $criteria2->add(new Criteria('gperm_name', 'module_admin'), 'OR');
360: $criteria2->add(new Criteria('gperm_name', 'module_read'), 'OR');
361: $criteria2->add(new Criteria('gperm_name', 'block_read'), 'OR');
362: $criteria->add($criteria2);
363: $this->deleteAll($criteria);
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376:
377: function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1, $bypass_admincheck = false)
378: {
379: if (empty($gperm_groupid)) {
380: return false;
381: }
382:
383: if (!$bypass_admincheck &&
384: (is_array($gperm_groupid)?in_array(XOOPS_GROUP_ADMIN, $gperm_groupid):(XOOPS_GROUP_ADMIN == $gperm_groupid))) {
385: return true;
386: }
387:
388: $criteria =& $this->getCriteria($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid);
389: if ($this->getCount($criteria) > 0) {
390: return true;
391: }
392: return false;
393: }
394:
395: 396: 397: 398: 399: 400: 401: 402: 403: 404:
405: function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
406: {
407: $criteria =& $this->getCriteria($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid);
408: $count = $this->getCount($criteria);
409: if ($count == 1) {
410: return true;
411: } else if ($count > 1) {
412:
413:
414: $this->removeRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid);
415: }
416:
417: $perm =& $this->create();
418: $perm->setVar('gperm_name', $gperm_name);
419: $perm->setVar('gperm_groupid', $gperm_groupid);
420: $perm->setVar('gperm_itemid', $gperm_itemid);
421: $perm->setVar('gperm_modid', $gperm_modid);
422: return $this->insert($perm);
423: }
424:
425: 426: 427: 428: 429: 430: 431: 432: 433: 434:
435: function removeRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
436: {
437: $criteria =& $this->getCriteria($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid);
438: return $this->deleteAll($criteria);
439: }
440:
441: 442: 443: 444: 445: 446: 447: 448: 449:
450: function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1)
451: {
452: $ret = array();
453:
454: $criteria =& $this->getCriteria($gperm_name, 0, $gperm_groupid, $gperm_modid);
455:
456: $perms =& $this->getObjects($criteria, true);
457: foreach (array_keys($perms) as $i) {
458: $ret[] = $perms[$i]->getVar('gperm_itemid');
459: }
460: return array_unique($ret);
461: }
462:
463: 464: 465: 466: 467: 468: 469: 470: 471:
472: function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1)
473: {
474: $ret = array();
475:
476: $criteria =& $this->getCriteria($gperm_name, $gperm_itemid, array(), $gperm_modid);
477:
478: $perms =& $this->getObjects($criteria, true);
479: foreach (array_keys($perms) as $i) {
480: $ret[] = $perms[$i]->getVar('gperm_groupid');
481: }
482: return $ret;
483: }
484:
485: 486: 487: 488: 489: 490: 491: 492: 493: 494:
495: function &getCriteria($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
496: {
497: $criteria = new CriteriaCompo(new Criteria('gperm_modid', (int)$gperm_modid));
498: $criteria->add(new Criteria('gperm_name', $gperm_name));
499: $gperm_itemid = (int)$gperm_itemid;
500: if ($gperm_itemid > 0) {
501: $criteria->add(new Criteria('gperm_itemid', $gperm_itemid));
502: }
503: if (is_array($gperm_groupid)) {
504: if (count($gperm_groupid) > 0) {
505: $criteria2 = new CriteriaCompo();
506: foreach ($gperm_groupid as $gid) {
507: $criteria2->add(new Criteria('gperm_groupid', (int)$gid), 'OR');
508: }
509: $criteria->add($criteria2);
510: }
511: } else {
512: $criteria->add(new Criteria('gperm_groupid', (int)$gperm_groupid));
513: }
514: return $criteria;
515: }
516: }
517: ?>
518: