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 . time());
45:
46: $item = $this->createNewItem($primaryKeyValue);
47: if (!is_object($item)) {
48: return false;
49: }
50:
51: $groupname = cApiGroup::prefixedGroupName($groupname);
52:
53: $item->set('groupname', $groupname);
54: $item->set('perms', $perms);
55: $item->set('description', $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: public function getGroupId() {
253: return $this->get('group_id');
254: }
255:
256: 257: 258: 259: 260: 261:
262: public function getGroupName($removePrefix = false) {
263: $groupname = $this->get('groupname');
264: return (false === $removePrefix) ? $groupname : self::getUnprefixedGroupName($groupname);
265: }
266:
267: 268: 269: 270: 271: 272:
273: public static function getUnprefixedGroupName($groupname) {
274: return substr($groupname, strlen(self::PREFIX));
275: }
276:
277: 278: 279: 280: 281: 282:
283: public static function prefixedGroupName($groupname) {
284: if (substr($groupname, 0, strlen(cApiGroup::PREFIX)) != cApiGroup::PREFIX) {
285: return cApiGroup::PREFIX . $groupname;
286: }
287: return $groupname;
288: }
289:
290: 291: 292: 293: 294: 295: 296:
297: public function getGroupProperty($type, $name) {
298: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
299: $groupProp = $groupPropColl->fetchByGroupIdTypeName($type, $name);
300: return ($groupProp) ? $groupProp->get('value') : false;
301: }
302:
303: 304: 305: 306: 307: 308: 309: 310:
311: public function getGroupProperties() {
312: $props = array();
313:
314: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
315: $groupProps = $groupPropColl->fetchByGroupId();
316: foreach ($groupProps as $groupProp) {
317: $props[$groupProp->get('idgroupprop')] = array(
318: 'name' => $groupProp->get('name'),
319: 'type' => $groupProp->get('type'),
320: 'value' => $groupProp->get('value')
321: );
322: }
323:
324: return $props;
325: }
326:
327: 328: 329: 330: 331: 332: 333: 334: 335:
336: public function setGroupProperty($type, $name, $value) {
337: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
338: return $groupPropColl->setValueByTypeName($type, $name, $value);
339: }
340:
341: 342: 343: 344: 345: 346: 347:
348: public function deleteGroupProperty($type, $name) {
349: $groupPropColl = new cApiGroupPropertyCollection($this->values['group_id']);
350: return $groupPropColl->deleteByGroupIdTypeName($type, $name);
351: }
352: }
353: