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