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