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