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