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 cPermission {
24:
25: 26: 27: 28: 29:
30: public $classname = 'cPermission';
31:
32: 33: 34: 35: 36:
37: public $areacache = array();
38:
39: 40: 41: 42: 43:
44: public $actioncache = array();
45:
46: 47: 48: 49: 50:
51: public $db;
52:
53: 54: 55: 56: 57: 58: 59:
60: public function getGroupsForUser($userId) {
61: $groups = array();
62:
63: $oGroupMemberColl = new cApiGroupMemberCollection();
64: $oGroupMemberColl->select("user_id='" . $oGroupMemberColl->escape($userId) . "'");
65: while (false !== $oItem = $oGroupMemberColl->next()) {
66: $groups[] = $oItem->get('group_id');
67: }
68:
69: return $groups;
70: }
71:
72: 73: 74: 75: 76: 77: 78: 79: 80:
81: public function getIDForArea($area) {
82: if (is_numeric($area)) {
83: return $area;
84: } elseif (isset($this->areacache[$area])) {
85: return $this->areacache[$area];
86: }
87:
88: $oAreaColl = new cApiAreaCollection();
89: $oAreaColl->select("name='" . $oAreaColl->escape($area) . "'");
90: if (false !== $oItem = $oAreaColl->next()) {
91: $this->areacache[$area] = $oItem->get('idarea');
92: $area = $oItem->get('idarea');
93: }
94:
95: return $area;
96: }
97:
98: 99: 100: 101: 102: 103: 104:
105: public function getIDForAction($action) {
106: if (is_numeric($action)) {
107: return $action;
108: } elseif (isset($this->actioncache[$action])) {
109: return $this->actioncache[$action];
110: }
111:
112: $oActionColl = new cApiActionCollection();
113: $oActionColl->select("name='" . $oActionColl->escape($action) . "'");
114: if (false !== $oItem = $oActionColl->next()) {
115: $this->actioncache[$action] = $oItem->get('idaction');
116: $action = $oItem->get('idaction');
117: }
118:
119: return $action;
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132:
133: public function load_permissions($force = false) {
134: global $sess, $area_rights, $item_rights, $auth, $changelang, $changeclient;
135:
136: $return = '1';
137:
138:
139: if (!$this->have_perm()) {
140: $return = isset($area_rights);
141:
142: if (!isset($area_rights) || !isset($item_rights) || isset($changeclient) || isset($changelang) || $force) {
143: $return = '3';
144:
145: $sess->register('area_rights');
146: $sess->register('item_rights');
147: $item_rights = array();
148: $groups = $this->getGroupsForUser($auth->auth['uid']);
149:
150: if (is_array($groups)) {
151: foreach ($groups as $group) {
152: $this->load_permissions_for_user($group);
153: }
154: }
155:
156: $this->load_permissions_for_user($auth->auth['uid']);
157: }
158: }
159:
160: return $return;
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170:
171: public function load_permissions_for_user($user) {
172: global $client, $lang;
173: global $area_rights, $item_rights;
174:
175: $oRightColl = new cApiRightCollection();
176: $sWhere = "user_id='" . $oRightColl->escape($user) . "'";
177: $sWhere .= " AND idcat=0 AND " . "idclient=" . (int) $client;
178: $sWhere .= " AND idlang=" . (int) $lang;
179: $oRightColl->select($sWhere);
180:
181:
182: if (!is_array($area_rights)) {
183: $area_rights = array();
184: }
185: while (false !== $oItem = $oRightColl->next()) {
186: $idarea = $oItem->get('idarea');
187: $idaction = $oItem->get('idaction');
188: $area_rights[$idarea][$idaction] = true;
189: }
190:
191:
192: $oAreaColl = new cApiAreaCollection();
193: $oAreaColl->select();
194: while (false !== $oItem = $oAreaColl->next()) {
195: $idarea = $oItem->get('idarea');
196: $tmp_area[] = $idarea;
197: }
198:
199: $tmp_area_string = implode("','", array_values($tmp_area));
200: $sWhere = "user_id='" . $oRightColl->escape($user) . "'";
201: $sWhere .= " AND idclient=" . (int) $client;
202: $sWhere .= " AND idlang=" . (int) $lang;
203: $sWhere .= " AND idarea IN ('$tmp_area_string')";
204: $sWhere .= "AND idcat != 0";
205: $oRightColl->select($sWhere);
206: while (false !== $oItem = $oRightColl->next()) {
207: $idarea = $oItem->get('idarea');
208: $idaction = $oItem->get('idaction');
209: $idcat = $oItem->get('idcat');
210: $item_rights[$idarea][$idaction][$idcat] = $idcat;
211: }
212: }
213:
214: 215: 216: 217: 218: 219:
220: public function have_perm_area_action_anyitem($area, $action = 0) {
221: global $item_rights;
222:
223: if ($this->have_perm_area_action($area, $action)) {
224: return true;
225: }
226:
227: $oAreaColl = new cApiAreaCollection();
228: $area = $oAreaColl->getAreaID($area);
229:
230: $action = $this->getIDForAction($action);
231:
232: return isset($item_rights[$area][$action]);
233: }
234:
235: 236: 237: 238: 239: 240: 241:
242: public function have_perm_area_action_item($area, $action, $itemid) {
243: global $item_rights, $auth, $client, $lang, $cfg;
244:
245: if ($this->have_perm()) {
246: return true;
247: }
248:
249: $oAreaColl = new cApiAreaCollection();
250: $area = $oAreaColl->getAreaID($area);
251: $action = $this->getIDForAction($action);
252:
253:
254:
255: if ($this->have_perm_area_action($area, $action)) {
256: return true;
257: }
258:
259:
260: if (isset($item_rights[$area][$action][$itemid])) {
261:
262:
263: return true;
264: }
265:
266: if ($item_rights[$area] != 'noright') {
267: $groupsForUser = $this->getGroupsForUser($auth->auth['uid']);
268: $groupsForUser[] = $auth->auth['uid'];
269:
270: $userIdIn = implode("','", $groupsForUser);
271:
272: $oRightsColl = new cApiRightCollection();
273: $where = "user_id IN ('" . $userIdIn . "') AND idclient=" . (int) $client . " AND idlang=" . (int) $lang . " AND idarea=$area AND idcat != 0";
274:
275: if (!$oRightsColl->select($where)) {
276: $item_rights[$area] = 'noright';
277: return false;
278: }
279:
280: while (false !== $oItem = $oRightsColl->next()) {
281: $item_rights[$oItem->get('idarea')][$oItem->get('idaction')][$oItem->get('idcat')] = $oItem->get('idcat');
282: }
283:
284:
285: if (isset($item_rights[$area][$action][$itemid])) {
286:
287:
288: return true;
289: }
290: }
291: return false;
292: }
293:
294: 295: 296: 297: 298: 299: 300: 301: 302: 303:
304: public function getParentAreaId($area) {
305: $oAreaColl = new cApiAreaCollection();
306: return $oAreaColl->getParentAreaID($area);
307: }
308:
309: 310: 311: 312: 313: 314:
315: public function have_perm_area_action($area, $action = 0) {
316: global $area_rights, $client, $lang, $cfg;
317:
318: $oAreaColl = new cApiAreaCollection();
319: $area = $oAreaColl->getAreaID($area);
320: $action = $this->getIDForAction($action);
321:
322: if ($action == 0) {
323: $area = $oAreaColl->getParentAreaID($area);
324: }
325:
326: $area = $oAreaColl->getAreaID($area);
327:
328: if (!$this->have_perm()) {
329: if ($action == 0 && $area_rights[$area]) {
330:
331:
332: return $this->have_perm_client_lang($client, $lang);
333: }
334:
335:
336: if ($area_rights[$area][$action]) {
337:
338:
339: return $this->have_perm_client_lang($client, $lang);
340: }
341:
342: return false;
343: }
344:
345: return true;
346: }
347:
348: 349: 350: 351: 352: 353:
354: public function have_perm_client_lang($client, $lang) {
355: global $auth;
356:
357:
358:
359:
360:
361:
362: if (!isset($auth->auth['perm'])) {
363: $auth->auth['perm'] = '';
364: }
365:
366:
367: $userperm = explode(',', $auth->auth['perm']);
368:
369: if (in_array('sysadmin', $userperm)) {
370: return true;
371: } elseif (in_array("admin[$client]", $userperm)) {
372: return true;
373: } else {
374:
375: $pageperm = explode(',', "client[$client],lang[$lang]");
376: foreach ($pageperm as $value) {
377: if (!in_array($value, $userperm)) {
378: return false;
379: }
380: }
381: }
382: return true;
383: }
384:
385: 386: 387: 388: 389: 390: 391: 392: 393:
394: public function hasClientPermission($iClient = false, $oUser = false) {
395: global $auth, $client;
396:
397: if ($iClient === false) {
398: $iClient = $client;
399: }
400:
401: $oUser = $this->_checkUserObject($oUser);
402:
403: if ($this->isSysadmin($oUser) || $this->isClientAdmin($iClient, $oUser) || $this->isClientUser($iClient, $oUser)) {
404: return true;
405: } else {
406: return false;
407: }
408: 409: 410: 411: 412: 413: 414: 415: 416: 417:
418: }
419:
420: 421: 422: 423: 424: 425: 426: 427: 428:
429: public function isClientUser($iClient, $oUser) {
430: $oUser = $this->_checkUserObject($oUser);
431:
432: $aPermissions = explode(',', $oUser->getEffectiveUserPerms());
433:
434: if (in_array("client[$iClient]", $aPermissions)) {
435: return true;
436: }
437:
438: return false;
439: }
440:
441: 442: 443: 444: 445: 446: 447: 448: 449:
450: public function isClientGroup($iClient, $oGroup) {
451: $aPermissions = explode(',', $oGroup->getField('perms'));
452:
453: if (in_array("client[$iClient]", $aPermissions)) {
454: return true;
455: }
456:
457: return false;
458: }
459:
460: 461: 462: 463: 464: 465: 466: 467: 468:
469: public function isClientAdmin($iClient, $oUser) {
470: $oUser = $this->_checkUserObject($oUser);
471:
472: $aPermissions = explode(',', $oUser->getEffectiveUserPerms());
473:
474: if (in_array("admin[$iClient]", $aPermissions)) {
475: return true;
476: }
477:
478: return false;
479: }
480:
481: 482: 483: 484: 485: 486: 487:
488: public function isSysadmin($oUser) {
489: $oUser = $this->_checkUserObject($oUser);
490:
491: $aPermissions = explode(',', $oUser->getEffectiveUserPerms());
492:
493: if (in_array('sysadmin', $aPermissions)) {
494: return true;
495: }
496:
497: return false;
498: }
499:
500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512:
513: private function _checkUserObject($oUser) {
514: if ($oUser === false) {
515: global $currentuser;
516: $oUser = $currentuser;
517: }
518:
519: if (!is_object($oUser)) {
520: global $auth;
521: $oUser = new cApiUser($auth->auth['uid']);
522: }
523:
524: if (get_class($oUser) != 'cApiUser') {
525: throw new cInvalidArgumentException('oUser parameter is not of type User');
526: }
527:
528: return $oUser;
529: }
530:
531: 532: 533: 534: 535:
536: public function have_perm_client($perm = 'x') {
537: global $auth, $client;
538:
539: if (!isset($auth->auth['perm'])) {
540: $auth->auth['perm'] = '';
541: }
542:
543:
544: $userperm = explode(',', $auth->auth['perm']);
545:
546:
547: if (in_array('sysadmin', $userperm)) {
548: return true;
549: }
550:
551:
552: $pageperm = explode(',', $perm);
553: foreach ($pageperm as $value) {
554: if (!in_array($value, $userperm)) {
555: return false;
556: }
557: }
558: return true;
559: }
560:
561: 562: 563: 564: 565: 566: 567: 568: 569:
570: public function have_perm($perm = 'x') {
571: global $auth, $client;
572:
573: if (!isset($auth->auth['perm'])) {
574: $auth->auth['perm'] = '';
575: }
576:
577:
578: $userperm = explode(',', $auth->auth['perm']);
579:
580:
581: if (in_array('sysadmin', $userperm)) {
582: return true;
583: } elseif (in_array("admin[$client]", $userperm)) {
584: return true;
585:
586: } else {
587:
588: $pageperm = explode(',', $perm);
589: foreach ($pageperm as $value) {
590: if (!in_array($value, $userperm)) {
591: return false;
592: }
593: }
594: }
595: return true;
596: }
597:
598: 599: 600: 601: 602: 603: 604:
605: public function have_perm_item($mainarea, $itemid) {
606: global $cfg, $item_rights, $cfg, $client, $lang, $auth, $area_tree, $sess;
607:
608: $oAreaColl = new cApiAreaCollection();
609: $mainarea = $oAreaColl->getAreaID($mainarea);
610:
611:
612: if ($this->have_perm()) {
613: return true;
614: }
615:
616:
617:
618: if (!is_object($this->db)) {
619: $this->db = cRegistry::getDb();
620: }
621:
622: $this->showareas($mainarea);
623:
624: $flg = false;
625:
626: foreach ($area_tree[$mainarea] as $value) {
627:
628: if ($item_rights[$value] == 'noright') {
629: continue;
630: } elseif (is_array($item_rights[$value])) {
631:
632: foreach ($item_rights[$value] as $value2) {
633: if (in_array($itemid, $value2)) {
634: return true;
635: }
636: }
637: } elseif ($item_rights[$value] != 'noright') {
638: $groupsForUser = $this->getGroupsForUser($auth->auth['uid']);
639: $groupsForUser[] = $auth->auth['uid'];
640:
641:
642: $sql = "SELECT
643: *
644: FROM
645: " . $cfg['tab']['rights'] . "
646: WHERE
647: user_id IN ('" . implode("','", $groupsForUser) . "') AND
648: idclient = " . cSecurity::toInteger($client) . " AND
649: idlang = " . cSecurity::toInteger($lang) . " AND
650: idarea = '$value' AND
651: idcat != 0";
652: $this->db->query($sql);
653:
654:
655: if ($this->db->affectedRows() == 0) {
656: $item_rights[$value] = 'noright';
657: }
658:
659:
660: while ($this->db->nextRecord()) {
661: if ($this->db->f('idcat') == $itemid) {
662: $flg = true;
663: }
664: $item_rights[$this->db->f('idarea')][$this->db->f('idaction')][$this->db->f('idcat')] = $this->db->f('idcat');
665: }
666: }
667: }
668: return $flg;
669: }
670:
671: 672: 673: 674: 675:
676: public function showareas($mainarea) {
677: global $area_tree, $sess, $perm, $cfg;
678:
679: if (!is_object($this->db)) {
680: $this->db = cRegistry::getDb();
681: }
682:
683: $oAreaColl = new cApiAreaCollection();
684: $mainarea = $oAreaColl->getAreaID($mainarea);
685:
686:
687: if (!isset($area_tree[$mainarea])) {
688: $sess->register('area_tree');
689:
690:
691: $sql = "SELECT name FROM " . $cfg['tab']['area'] . " WHERE idarea=$mainarea";
692: $this->db->query($sql);
693: $this->db->nextRecord();
694: $name = $this->db->f('name');
695:
696:
697: $sql = "SELECT idarea FROM " . $cfg['tab']['area'] . " WHERE parent_id='$name' OR idarea=$mainarea";
698: $this->db->query($sql);
699: $area_tree[$mainarea] = array();
700: while ($this->db->nextRecord()) {
701: $area_tree[$mainarea][] = $this->db->f('idarea');
702: }
703: }
704: return $mainarea;
705: }
706: }
707: