1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Kernel\XoopsObjectHandler;
13: use Xoops\Core\Kernel\Criteria;
14: use Xoops\Core\Kernel\CriteriaCompo;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28:
29: include_once dirname(__DIR__) . '/include/common.php';
30:
31: class PublisherPermissionHandler extends XoopsObjectHandler
32: {
33: 34: 35: 36:
37: public $publisher = null;
38:
39: 40: 41:
42: public function __construct()
43: {
44: $this->publisher = Publisher::getInstance();
45: $this->db2 = \Xoops::getInstance()->db();
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55:
56: public function getGrantedGroupsById($gperm_name, $id)
57: {
58: static $items;
59: if (isset($items[$gperm_name][$id])) {
60: return $items[$gperm_name][$id];
61: }
62: $groups = array();
63: $criteria = new CriteriaCompo();
64: $criteria->add(new Criteria('gperm_modid', $this->publisher->getModule()->getVar('mid')));
65: $criteria->add(new Criteria('gperm_name', $gperm_name));
66: $criteria->add(new Criteria('gperm_itemid', $id));
67:
68: $qb = $this->db2->createXoopsQueryBuilder();
69: $qb ->select('gperm_groupid')
70: ->fromPrefix('system_permission', '');
71: $criteria->renderQb($qb);
72: $result = $qb->execute();
73:
74: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
75: $groups[$myrow['gperm_groupid']] = $myrow['gperm_groupid'];
76: }
77: $items[$gperm_name][$id] = $groups;
78: return $groups;
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function getGrantedItems($gperm_name)
89: {
90: static $items;
91: if (isset($items[$gperm_name])) {
92: return $items[$gperm_name];
93: }
94: $ret = array();
95:
96: $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
97: $criteria->add(new Criteria('gperm_modid', $this->publisher->getModule()->getVar('mid')));
98:
99:
100: $groups = \Xoops::getInstance()->getUserGroups();
101: $criteria2 = new CriteriaCompo();
102: foreach ($groups as $gid) {
103: $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
104: }
105: $criteria->add($criteria2);
106:
107: $qb = $this->db2->createXoopsQueryBuilder();
108: $qb ->select('gperm_itemid')
109: ->fromPrefix('system_permission', '');
110: $criteria->renderQb($qb);
111: $result = $qb->execute();
112:
113: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
114: $ret[$myrow['gperm_itemid']] = $myrow['gperm_itemid'];
115: }
116: $items[$gperm_name] = $ret;
117: return $ret;
118: }
119:
120: 121: 122: 123: 124: 125: 126: 127:
128: public function isGranted($gperm_name, $id)
129: {
130: if (!$id) {
131: return false;
132: }
133: $permissions = $this->getGrantedItems($gperm_name);
134: if (!empty($permissions) && isset ($permissions[$id])) {
135: return true;
136: } else {
137: return false;
138: }
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152:
153: public function saveItemPermissions($groups, $itemid, $perm_name)
154: {
155: $xoops = Xoops::getInstance();
156: $result = true;
157: $module_id = $this->publisher->getModule()->getVar('mid');
158: $gperm_handler = $xoops->getHandlerGroupPermission();
159:
160: $gperm_handler->deleteByModule($module_id, $perm_name, $itemid);
161:
162: if (count($groups) > 0) {
163: foreach ($groups as $group_id) {
164: echo $group_id . "-";
165: echo $gperm_handler->addRight($perm_name, $itemid, $group_id, $module_id);
166: }
167: }
168: return $result;
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179:
180: public function deletePermissions($itemid, $gperm_name)
181: {
182: $xoops = Xoops::getInstance();
183: $result = true;
184: $gperm_handler = $xoops->getHandlerGroupPermission();
185: $gperm_handler->deleteByModule($this->publisher->getModule()->getVar('mid'), $gperm_name, $itemid);
186: return $result;
187: }
188: }
189: