1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cApiGroupCollection extends ItemCollection {
24:
25: 26: 27:
28: public function __construct() {
29: global $cfg;
30: parent::__construct($cfg['tab']['groups'], 'group_id');
31: $this->_setItemClass('cApiGroup');
32: }
33:
34: 35: 36: 37: 38: 39: 40: 41:
42: public function create($groupname, $perms, $description) {
43: $primaryKeyValue = md5($groupname . time());
44:
45: $item = $this->createNewItem($primaryKeyValue);
46: if (!is_object($item)) {
47: return false;
48: }
49:
50: $groupname = cApiGroup::prefixedGroupName($groupname);
51:
52: $item->set('groupname', $groupname);
53: $item->set('perms', $perms);
54: $item->set('description', $description);
55: $item->store();
56:
57: return $item;
58: }
59:
60: 61: 62: 63: 64: 65: 66:
67: public function fetchByUserID($userid) {
68: global $cfg;
69:
70: $aIds = array();
71: $aGroups = array();
72:
73: $sql = "SELECT a.group_id FROM `%s` AS a, `%s` AS b " . "WHERE (a.group_id = b.group_id) AND (b.user_id = '%s')";
74:
75: $this->db->query($sql, $this->table, $cfg['tab']['groupmembers'], $userid);
76: $this->_lastSQL = $sql;
77:
78: while ($this->db->nextRecord()) {
79: $aIds[] = $this->db->f('group_id');
80: }
81:
82: if (0 === count($aIds)) {
83: return $aGroups;
84: }
85:
86: $where = "group_id IN ('" . implode("', '", $aIds) . "')";
87: $this->select($where);
88: while (($oItem = $this->next()) !== false) {
89: $aGroups[] = clone $oItem;
90: }
91:
92: return $aGroups;
93: }
94:
95: 96: 97: 98: 99: 100: 101: 102:
103: public function deleteGroupByGroupname($groupname) {
104: $groupname = cApiGroup::prefixedGroupName($groupname);
105: $result = $this->deleteBy('groupname', $groupname);
106: return ($result > 0) ? true : false;
107: }
108:
109: 110: 111: 112: 113: 114: 115:
116: public function fetchAccessibleGroups($perms) {
117: $groups = array();
118: $limit = array();
119: $where = '';
120:
121: if (!in_array('sysadmin', $perms)) {
122:
123: $oClientColl = new cApiClientCollection();
124: $allClients = $oClientColl->getAvailableClients();
125: foreach ($allClients as $key => $value) {
126: if (in_array('client[' . $key . ']', $perms) || in_array('admin[' . $key . ']', $perms)) {
127: $limit[] = 'perms LIKE "%client[' . $this->escape($key) . ']%"';
128: }
129: if (in_array('admin[' . $key . ']', $perms)) {
130: $limit[] = 'perms LIKE "%admin[' . $this->escape($key) . ']%"';
131: }
132: }
133:
134: if (count($limit) > 0) {
135: $where = '1 AND ' . implode(' OR ', $limit);
136: }
137: }
138:
139: $this->select($where);
140: while (($oItem = $this->next()) !== false) {
141: $groups[] = clone $oItem;
142: }
143:
144: return $groups;
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160:
161: public function getAccessibleGroups($perms) {
162: $groups = array();
163: $oGroups = $this->fetchAccessibleGroups($perms);
164: foreach ($oGroups as $oItem) {
165: $groups[$oItem->get('group_id')] = array(
166: 'groupname' => $oItem->getGroupName(true),
167: 'description' => $oItem->get('description')
168: );
169: }
170: return $groups;
171: }
172: }
173:
174: 175: 176: 177: 178: 179:
180: class cApiGroup extends Item {
181:
182: 183: 184: 185: 186:
187: const PREFIX = 'grp_';
188:
189: 190: 191: 192: 193: 194:
195: public function __construct($mId = false) {
196: global $cfg;
197: parent::__construct($cfg['tab']['groups'], 'group_id');
198: $this->setFilters(array(), array());
199: if ($mId !== false) {
200: $this->loadByPrimaryKey($mId);
201: }
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211:
212: public function loadGroupByGroupID($groupId) {
213: return $this->loadByPrimaryKey($groupId);
214: }
215:
216: 217: 218: 219: 220: 221: 222: 223:
224: public function loadGroupByGroupname($groupname) {
225: $groupname = cApiGroup::prefixedGroupName($groupname);
226: return $this->loadBy('groupname', $groupname);
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240:
241: public function setField($sField, $mValue, $bSafe = true) {
242: if ('perms' === $sField) {
243: if (is_array($mValue)) {
244: $mValue = implode(',', $mValue);
245: }
246: }
247:
248: return parent::setField($sField, $mValue, $bSafe);
249: }
250:
251: 252: 253: 254: 255:
256: public function getPermsArray() {
257: return explode(',', $this->get('perms'));
258: }
259:
260: 261: 262: 263: 264:
265: public function getGroupId() {
266: return $this->get('group_id');
267: }
268:
269: 270: 271: 272: 273: 274: 275:
276: public function getGroupName($removePrefix = false) {
277: $groupname = $this->get('groupname');
278: return (false === $removePrefix) ? $groupname : self::getUnprefixedGroupName($groupname);
279: }
280:
281: 282: 283: 284: 285: 286:
287: public static function getUnprefixedGroupName($groupname) {
288: return substr($groupname, strlen(self::PREFIX));
289: }
290:
291: 292: 293: 294: 295: 296:
297: public static function prefixedGroupName($groupname) {
298: if (substr($groupname, 0, strlen(cApiGroup::PREFIX)) != cApiGroup::PREFIX) {
299: return cApiGroup::PREFIX . $groupname;
300: }
301: return $groupname;
302: }
303:
304: 305: 306: 307: 308: 309: 310: 311:
312: public function getGroupProperty($type, $name) {
313: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
314: $groupProp = $groupPropColl->fetchByGroupIdTypeName($type, $name);
315: return ($groupProp) ? $groupProp->get('value') : false;
316: }
317:
318: 319: 320: 321: 322: 323: 324: 325: 326:
327: public function getGroupProperties() {
328: $props = array();
329:
330: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
331: $groupProps = $groupPropColl->fetchByGroupId();
332: foreach ($groupProps as $groupProp) {
333: $props[$groupProp->get('idgroupprop')] = array(
334: 'name' => $groupProp->get('name'),
335: 'type' => $groupProp->get('type'),
336: 'value' => $groupProp->get('value')
337: );
338: }
339:
340: return $props;
341: }
342:
343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353:
354: public function setGroupProperty($type, $name, $value) {
355: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
356: return $groupPropColl->setValueByTypeName($type, $name, $value);
357: }
358:
359: 360: 361: 362: 363: 364: 365: 366: 367:
368: public function deleteGroupProperty($type, $name) {
369: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
370: return $groupPropColl->deleteByGroupIdTypeName($type, $name);
371: }
372: }
373: