source: branches/mvc/Base/Permission.php

Last change on this file was 47, checked in by chronos, 10 years ago
  • Odstraněno: Zbytečná PHP ukončovací značka "?>" z konce všech souborů.
File size: 3.2 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Model.php');
4
5class Permission extends Model
6{
7 var $BuildCache = true;
8
9 function Check($Module, $Action, $Item = '')
10 {
11 // TODO: Use permission check
12 return(true);
13 if($this->BuildCache)
14 {
15 $this->RebuildCache();
16 $this->BuildCache = false;
17 }
18 $Result = false;
19 if($Item != '') $ItemFilter = ' AND (Item='.$Item.')';
20 else $ItemFilter = ' AND (Item IS NULL)';
21
22 // Check global access
23 $DbResult = $this->Database->query('SELECT * FROM `PermissionAssignment` WHERE `ModuleAction`=(SELECT `Id` FROM `ModuleAction` WHERE (`Module` IS NULL) AND (`Name` IS NULL))');
24 if($DbResult->num_rows > 0)
25 {
26 $DbRow = $DbResult->fetch_assoc();
27 $DbResult = $this->Database->query('SELECT * FROM `PermissionAssignmentCache` WHERE (`Group`='.$DbRow['Group'].') AND (`User`='.
28 $this->System->Modules['User']->Data['Id'].')');
29 $Result = $DbResult->num_rows > 0;
30 if($Result) return(true);
31 }
32
33 // Check module-action-item access
34 $DbResult = $this->Database->query('SELECT * FROM `PermissionAssignment` WHERE `ModuleAction`=(SELECT `Id` FROM `ModuleAction` WHERE (`Module`=(SELECT `Id` FROM `Module` WHERE `Name` = "'.$Module.'")) AND (`Name`="'.$Action.'"))'.$ItemFilter);
35 while($DbRow = $DbResult->fetch_assoc())
36 {
37 $DbResult2 = $this->Database->query('SELECT * FROM `PermissionAssignmentCache` WHERE (`Group`='.$DbRow['Group'].') AND (`User`='.
38 $this->System->Modules['User']->Data['Id'].')');
39 if($DbResult2->num_rows > 0) return(true);
40 }
41 return($Result);
42 }
43
44 function AppendFilter($Module, $Action, $Table, $Key)
45 {
46 if($this->Check($Module, 'Show')) return('');
47 else return(' JOIN PermissionAssignment ON (PermissionAssignment.Item='.$Table.'.'.$Key.
48 ') AND (PermissionAssignment.ModuleAction=(SELECT `Id` FROM `ModuleAction` WHERE (`Module`=(SELECT `Id` FROM `Module` WHERE `Name` = "'.$Module.'")) AND (`Name`="'.$Action.'"))) JOIN PermissionAssignmentCache ON PermissionAssignmentCache.Group=PermissionAssignment.Group AND PermissionAssignmentCache.User='.
49 $this->System->Modules['User']->Data['Id']);
50 }
51
52 function RebuildCache()
53 {
54 $this->Database->query('TRUNCATE `PermissionAssignmentCache`');
55 $DbResult = $this->Database->query('SELECT * FROM `PermissionAssignment` WHERE (`User` > 0) AND (`SubGroup` > 0)');
56 while($UserAssignment = $DbResult->fetch_assoc())
57 {
58 $Cache = array($UserAssignment['SubGroup']);
59 $Cache = array_merge($Cache, $this->RebuildCacheGroup($UserAssignment['SubGroup']));
60 foreach($Cache as $Item)
61 {
62 $this->Database->insert('PermissionAssignmentCache', array('Group' => $Item, 'User' => $UserAssignment['User']));
63 }
64 }
65 }
66
67 function RebuildCacheGroup($Id)
68 {
69 $Cache = array();
70 $DbResult = $this->Database->query('SELECT * FROM `PermissionAssignment` WHERE (`Group` = '.$Id.') AND (`SubGroup` > 0)');
71 while($DbRow = $DbResult->fetch_assoc())
72 {
73 $Cache[] = $DbRow['SubGroup'];
74 $Cache = array_merge($Cache, $this->RebuildCacheGroup($DbRow['SubGroup']));
75 }
76 return($Cache);
77 }
78}
Note: See TracBrowser for help on using the repository browser.