1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
19:
20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: function checkLangInClients($aClients, $iLang, $aCfg, $oDb) {
36: $oClientLanguageCollection = new cApiClientLanguageCollection();
37: return $oClientLanguageCollection->hasLanguageInClients($iLang, $aClients);
38: }
39:
40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54:
55: function copyRightsForElement($area, $iditem, $newiditem, $idlang = false) {
56: global $perm, $auth, $area_tree;
57:
58: if (!is_object($perm)) {
59: return false;
60: }
61: if (!is_object($auth)) {
62: return false;
63: }
64:
65: $oDestRightCol = new cApiRightCollection();
66: $oSourceRighsColl = new cApiRightCollection();
67: $whereUsers = array();
68: $whereAreaActions = array();
69:
70:
71: $userIDContainer = $perm->getGroupsForUser($auth->auth['uid']);
72:
73:
74: $userIDContainer[] = $auth->auth['uid'];
75: foreach ($userIDContainer as $key) {
76: $whereUsers[] = "user_id = '" . $oDestRightCol->escape($key) . "'";
77: }
78: $whereUsers = '(' . implode(' OR ', $whereUsers) . ')';
79:
80:
81:
82: $areaContainer = $area_tree[$perm->showareas($area)];
83:
84:
85: $oActionColl = new cApiActionCollection();
86: $oActionColl->select('idarea IN (' . implode(',', $areaContainer) . ')');
87: while (($oItem = $oActionColl->next()) !== false) {
88: $whereAreaActions[] = '(idarea = ' . (int) $oItem->get('idarea') . ' AND idaction = ' . (int) $oItem->get('idaction') . ')';
89: }
90: $whereAreaActions = '(' . implode(' OR ', $whereAreaActions) . ')';
91:
92:
93:
94:
95:
96:
97: $sWhere = "{$whereAreaActions} AND {$whereUsers} AND idcat = {$iditem}";
98: if ($idlang) {
99: $sWhere .= ' AND idlang=' . (int) $idlang;
100: }
101:
102: $oSourceRighsColl->select($sWhere);
103: while (($oItem = $oSourceRighsColl->next()) !== false) {
104: $rs = $oItem->toObject();
105: $oDestRightCol->create($rs->user_id, $rs->idarea, $rs->idaction, $newiditem, $rs->idclient, $rs->idlang, $rs->type);
106: }
107:
108:
109: $perm->load_permissions(true);
110:
111: return true;
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126:
127: function createRightsForElement($area, $iditem, $idlang = false) {
128: global $perm, $auth, $area_tree, $client;
129:
130: if (!is_object($perm)) {
131: return false;
132: }
133: if (!is_object($auth)) {
134: return false;
135: }
136:
137: $oDestRightCol = new cApiRightCollection();
138: $oSourceRighsColl = new cApiRightCollection();
139: $whereUsers = array();
140: $rightsCache = array();
141:
142:
143: $userIDContainer = $perm->getGroupsForUser($auth->auth['uid']);
144:
145:
146: $userIDContainer[] = $auth->auth['uid'];
147: foreach ($userIDContainer as $key) {
148: $whereUsers[] = "user_id = '" . $oDestRightCol->escape($key) . "'";
149: }
150: $whereUsers = '(' . implode(' OR ', $whereUsers) . ')';
151:
152:
153:
154: $areaContainer = $area_tree[$perm->showareas($area)];
155:
156:
157:
158: $sWhere = 'idclient=' . (int) $client . ' AND idarea IN (' . implode(',', $areaContainer) . ')' . ' AND idcat != 0 AND idaction != 0 AND ' . $whereUsers;
159: if ($idlang) {
160: $sWhere .= ' AND idlang=' . (int) $idlang;
161: }
162:
163: $oSourceRighsColl->select($sWhere);
164: while (($oItem = $oSourceRighsColl->next()) !== false) {
165: $rs = $oItem->toObject();
166:
167:
168: $key = $rs->user_id . '-' . $rs->idarea . '-' . $rs->idaction . '-' . $iditem . '-' . $rs->idclient . '-' . $rs->idlang . '-' . $rs->type;
169: if (isset($rightsCache[$key])) {
170: continue;
171: }
172:
173:
174: $oDestRightCol->create($rs->user_id, $rs->idarea, $rs->idaction, $iditem, $rs->idclient, $rs->idlang, $rs->type);
175:
176: $rightsCache[$key] = true;
177: }
178:
179:
180: $perm->load_permissions(true);
181:
182: return true;
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192: 193: 194:
195: function deleteRightsForElement($area, $iditem, $idlang = false) {
196: global $perm, $area_tree, $client;
197:
198:
199: $areaContainer = $area_tree[$perm->showareas($area)];
200:
201: $sWhere = "idcat=" . (int) $iditem . " AND idclient=" . (int) $client . " AND idarea IN (" . implode(',', $areaContainer) . ")";
202: if ($idlang) {
203: $sWhere .= " AND idlang=" . (int) $idlang;
204: }
205:
206: $oRightColl = new cApiRightCollection();
207: $oRightColl->deleteByWhereClause($sWhere);
208:
209:
210: $perm->load_permissions(true);
211: }
212:
213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223:
224: function buildUserOrGroupPermsFromRequest($bAddUserToClient = false) {
225: global $cfg, $msysadmin, $madmin, $mclient, $mlang, $auth, $client;
226:
227: $aPerms = array();
228:
229:
230:
231: $bSysadmin = (isset($msysadmin) && $msysadmin);
232:
233: $aAdmin = (isset($madmin) && is_array($madmin)) ? $madmin : array();
234: foreach ($aAdmin as $p => $value) {
235: if (!is_numeric($value)) {
236: unset($aAdmin[$p]);
237: }
238: }
239:
240: $aClient = (isset($mclient) && is_array($mclient)) ? $mclient : array();
241: foreach ($aClient as $p => $value) {
242: if (!is_numeric($value)) {
243: unset($aClient[$p]);
244: }
245: }
246:
247: $aLang = (isset($mlang) && is_array($mlang)) ? $mlang : array();
248: foreach ($aLang as $p => $value) {
249: if (!is_numeric($value)) {
250: unset($aLang[$p]);
251: }
252: }
253:
254:
255:
256: if ($bSysadmin) {
257: $aPerms[] = 'sysadmin';
258: }
259:
260: foreach ($aAdmin as $value) {
261: $aPerms[] = sprintf('admin[%s]', $value);
262: }
263:
264: foreach ($aClient as $value) {
265: $aPerms[] = sprintf('client[%s]', $value);
266: }
267:
268: if (count($aClient) == 0 && $bAddUserToClient) {
269:
270:
271:
272:
273: $aUserPerm = explode(',', $auth->auth['perm']);
274: if (!in_array('sysadmin', $aUserPerm)) {
275: $aPerms[] = sprintf('client[%s]', $client);
276: }
277: }
278:
279: if (count($aLang) > 0 && count($aClient) > 0) {
280:
281:
282: $db = cRegistry::getDb();
283: foreach ($aLang as $value) {
284: if (checkLangInClients($aClient, $value, $cfg, $db)) {
285: $aPerms[] = sprintf('lang[%s]', $value);
286: }
287: }
288: }
289:
290: return $aPerms;
291: }
292:
293: 294: 295: 296:
297: function saveRights() {
298: global $perm, $notification, $db, $userid;
299: global $rights_list, $rights_list_old, $rights_client, $rights_lang;
300:
301:
302: if (!is_array($rights_list)) {
303: $rights_list = array();
304: }
305:
306:
307: $arraydel = array_diff(array_keys($rights_list_old), array_keys($rights_list));
308:
309:
310: $arraysave = array_diff(array_keys($rights_list), array_keys($rights_list_old));
311: $oAreaColl = new cApiAreaCollection();
312:
313: if (is_array($arraydel)) {
314: foreach ($arraydel as $value) {
315: $data = explode('|', $value);
316: $data[0] = $oAreaColl->getAreaID($data[0]);
317: $data[1] = $perm->getIDForAction($data[1]);
318:
319: $where = "user_id = '" . $db->escape($userid) . "' AND idclient = " . (int) $rights_client . " AND idlang = " . (int) $rights_lang . " AND idarea = " . (int) $data[0] . " AND idcat = " . (int) $data[2] . " AND idaction = " . (int) $data[1] . " AND type = 0";
320: $oRightColl = new cApiRightCollection();
321: $oRightColl->deleteByWhereClause($where);
322: }
323: }
324:
325: unset($data);
326:
327:
328: if (is_array($arraysave)) {
329: foreach ($arraysave as $value) {
330:
331: $data = explode('|', $value);
332:
333:
334:
335:
336: $data[0] = $oAreaColl->getAreaID($data[0]);
337: $data[1] = $perm->getIDForAction($data[1]);
338:
339: if (!isset($data[1])) {
340: $data[1] = 0;
341: }
342:
343:
344: $oRightColl = new cApiRightCollection();
345: $oRightColl->create($userid, $data[0], $data[1], $data[2], $rights_client, $rights_lang, 0);
346: }
347: }
348:
349: $rights_list_old = $rights_list;
350:
351: return true;
352:
353: }
354:
355: 356: 357: 358:
359: function saveGroupRights() {
360: global $perm, $notification, $db, $groupid;
361: global $rights_list, $rights_list_old, $rights_client, $rights_lang;
362:
363:
364: if (!is_array($rights_list)) {
365: $rights_list = array();
366: }
367:
368:
369: $arraydel = array_diff(array_keys($rights_list_old), array_keys($rights_list));
370:
371:
372: $arraysave = array_diff(array_keys($rights_list), array_keys($rights_list_old));
373:
374: $oAreaColl = new cApiAreaCollection();
375:
376: if (is_array($arraydel)) {
377: foreach ($arraydel as $value) {
378: $data = explode('|', $value);
379: $data[0] = $oAreaColl->getAreaID($data[0]);
380: $data[1] = $perm->getIDForAction($data[1]);
381:
382: $where = "user_id = '" . $db->escape($groupid) . "' AND idclient = " . (int) $rights_client . " AND idlang = " . (int) $rights_lang . " AND idarea = " . (int) $data[0] . " AND idcat = " . (int) $data[2] . " AND idaction = " . (int) $data[1] . " AND type = 1";
383: $oRightColl = new cApiRightCollection();
384: $oRightColl->deleteByWhereClause($where);
385: }
386: }
387:
388: unset($data);
389:
390:
391: if (is_array($arraysave)) {
392: foreach ($arraysave as $value) {
393:
394: $data = explode('|', $value);
395:
396:
397:
398:
399: $data[0] = $oAreaColl->getAreaID($data[0]);
400: $data[1] = $perm->getIDForAction($data[1]);
401:
402: if (!isset($data[1])) {
403: $data[1] = 0;
404: }
405:
406:
407: $oRightColl = new cApiRightCollection();
408: $oRightColl->create($groupid, $data[0], $data[1], $data[2], $rights_client, $rights_lang, 1);
409: }
410: }
411:
412: $rights_list_old = $rights_list;
413: return true;
414: }
415: