1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: namespace Xoops\Core\Kernel\Handlers;
21:
22: use Xoops\Core\Database\Connection;
23: use Xoops\Core\FixedGroups;
24: use Xoops\Core\Kernel\Criteria;
25: use Xoops\Core\Kernel\CriteriaCompo;
26: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42: class XoopsGroupPermHandler extends XoopsPersistableObjectHandler
43: {
44: 45: 46: 47: 48:
49: public function __construct(Connection $db = null)
50: {
51: parent::__construct(
52: $db,
53: 'system_permission',
54: '\Xoops\Core\Kernel\Handlers\XoopsGroupPerm',
55: 'gperm_id',
56: 'gperm_name'
57: );
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67:
68: public function deleteByGroup($gperm_groupid, $gperm_modid = null)
69: {
70: $criteria = new CriteriaCompo(new Criteria('gperm_groupid', (int)($gperm_groupid)));
71: if (isset($gperm_modid)) {
72: $criteria->add(new Criteria('gperm_modid', (int)($gperm_modid)));
73: }
74: return $this->deleteAll($criteria);
75: }
76:
77: 78: 79: 80: 81: 82: 83: 84: 85:
86: public function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null)
87: {
88: $criteria = new CriteriaCompo(new Criteria('gperm_modid', (int)($gperm_modid)));
89: if (isset($gperm_name)) {
90: $criteria->add(new Criteria('gperm_name', (string)($gperm_name)));
91: if (isset($gperm_itemid)) {
92: $criteria->add(new Criteria('gperm_itemid', (int)($gperm_itemid)));
93: }
94: }
95: return $this->deleteAll($criteria);
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1, $trueifadmin = true)
110: {
111: if (empty($gperm_groupid)) {
112: return false;
113: } else {
114: if (is_array($gperm_groupid)) {
115: if (in_array(FixedGroups::ADMIN, $gperm_groupid) && $trueifadmin) {
116: return true;
117: }
118: $criteria_group = new CriteriaCompo();
119: foreach ($gperm_groupid as $gid) {
120: $criteria_group->add(new Criteria('gperm_groupid', (int)($gid)), 'OR');
121: }
122: } else {
123: if (FixedGroups::ADMIN == $gperm_groupid && $trueifadmin) {
124: return true;
125: }
126: $criteria_group = new CriteriaCompo(new Criteria('gperm_groupid', (int)($gperm_groupid)));
127: }
128: }
129: $criteria = new CriteriaCompo(new Criteria('gperm_modid', (int)($gperm_modid)));
130: $criteria->add($criteria_group);
131: $criteria->add(new Criteria('gperm_name', (string)($gperm_name)));
132: $gperm_itemid = (int)($gperm_itemid);
133: if ($gperm_itemid > 0) {
134: $criteria->add(new Criteria('gperm_itemid', (int)($gperm_itemid)));
135: }
136: if ($this->getCount($criteria) > 0) {
137: return true;
138: }
139: return false;
140: }
141:
142: 143: 144: 145: 146: 147: 148: 149: 150: 151:
152: public function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
153: {
154: $perm = $this->create();
155: $perm->setVar('gperm_name', (string)($gperm_name));
156: $perm->setVar('gperm_groupid', (int)($gperm_groupid));
157: $perm->setVar('gperm_itemid', (int)($gperm_itemid));
158: $perm->setVar('gperm_modid', (int)($gperm_modid));
159: return $this->insert($perm);
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169: 170:
171: public function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1)
172: {
173: $ret = array();
174: $criteria = new CriteriaCompo(new Criteria('gperm_name', (string)($gperm_name)));
175: $criteria->add(new Criteria('gperm_modid', (int)($gperm_modid)));
176: if (is_array($gperm_groupid)) {
177: $criteria2 = new CriteriaCompo();
178: foreach ($gperm_groupid as $gid) {
179: $criteria2->add(new Criteria('gperm_groupid', (int)($gid)), 'OR');
180: }
181: $criteria->add($criteria2);
182: } else {
183: $criteria->add(new Criteria('gperm_groupid', (int)($gperm_groupid)));
184: }
185: $perms = $this->getObjects($criteria, true);
186: foreach (array_keys($perms) as $i) {
187: $ret[] = $perms[$i]->getVar('gperm_itemid');
188: }
189: return array_unique($ret);
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199: 200:
201: public function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1)
202: {
203: $ret = array();
204: $criteria = new CriteriaCompo(new Criteria('gperm_name', (string)($gperm_name)));
205: $criteria->add(new Criteria('gperm_itemid', (int)($gperm_itemid)));
206: $criteria->add(new Criteria('gperm_modid', (int)($gperm_modid)));
207: $perms = $this->getObjects($criteria, true);
208: foreach (array_keys($perms) as $i) {
209: $ret[] = $perms[$i]->getVar('gperm_groupid');
210: }
211: return $ret;
212: }
213: }
214: