1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
19: |
|
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: |
|
30: | class XoopsGroupPerm extends XoopsObject
|
31: | {
|
32: |
|
33: | public $gperm_id;
|
34: | public $gperm_groupid;
|
35: | public $gperm_itemid;
|
36: | public $gperm_modid;
|
37: | public $gperm_name;
|
38: |
|
39: | |
40: | |
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: | |
55: | |
56: | |
57: |
|
58: | public function id($format = 'N')
|
59: | {
|
60: | return $this->getVar('gperm_id', $format);
|
61: | }
|
62: |
|
63: | |
64: | |
65: | |
66: | |
67: |
|
68: | public function gperm_id($format = '')
|
69: | {
|
70: | return $this->getVar('gperm_id', $format);
|
71: | }
|
72: |
|
73: | |
74: | |
75: | |
76: | |
77: |
|
78: | public function gperm_groupid($format = '')
|
79: | {
|
80: | return $this->getVar('gperm_groupid', $format);
|
81: | }
|
82: |
|
83: | |
84: | |
85: | |
86: | |
87: |
|
88: | public function gperm_itemid($format = '')
|
89: | {
|
90: | return $this->getVar('gperm_itemid', $format);
|
91: | }
|
92: |
|
93: | |
94: | |
95: | |
96: | |
97: |
|
98: | public function gperm_modid($format = '')
|
99: | {
|
100: | return $this->getVar('gperm_modid', $format);
|
101: | }
|
102: |
|
103: | |
104: | |
105: | |
106: | |
107: |
|
108: | public function gperm_name($format = '')
|
109: | {
|
110: | return $this->getVar('gperm_name', $format);
|
111: | }
|
112: | }
|
113: |
|
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: |
|
125: | class XoopsGroupPermHandler extends XoopsObjectHandler
|
126: | {
|
127: | |
128: | |
129: | |
130: | |
131: | |
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: | |
143: | |
144: | |
145: | |
146: | |
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: | |
160: | |
161: | |
162: | |
163: | |
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: | |
187: | |
188: | |
189: | |
190: | |
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: | |
226: | |
227: | |
228: | |
229: | |
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: | |
247: | |
248: | |
249: | |
250: | |
251: | |
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: |
|
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: | |
284: | |
285: | |
286: | |
287: | |
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: | |
306: | |
307: | |
308: | |
309: | |
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: | |
326: | |
327: | |
328: | |
329: | |
330: | |
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: | |
344: | |
345: | |
346: | |
347: | |
348: | |
349: | |
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: | |
366: | |
367: | |
368: | |
369: | |
370: | |
371: | |
372: | |
373: | |
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: | |
405: | |
406: | |
407: | |
408: | |
409: | |
410: | |
411: | |
412: |
|
413: | public function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
|
414: | {
|
415: |
|
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: | |
427: | |
428: | |
429: | |
430: | |
431: | |
432: | |
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: | |
458: | |
459: | |
460: | |
461: | |
462: | |
463: | |
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: | |