init
[Pman.Core] / DataObjects / Group_Rights.php
1 <?php
2 /**
3  * Table Definition for Group_Rights
4  */
5 require_once 'DB/DataObject.php';
6
7  
8 class Pman_Core_DataObjects_Group_Rights extends DB_DataObject 
9 {
10     ###START_AUTOCODE
11     /* the code below is auto generated do not remove the above tag */
12
13     public $__table = 'Group_Rights';                    // table name
14     public $rightname;                       // string(64)  not_null
15     public $group_id;                        // int(11)  not_null
16     public $AccessMask;                      // string(10)  not_null
17     public $id;                              // int(11)  not_null primary_key auto_increment
18
19     
20     /* the code above is auto generated do not remove the tag below */
21     ###END_AUTOCODE
22     
23     
24     var $fullRights = "ADESPIM";
25     
26     function listPermsFromGroupIds($grps, $isAdmin=false) {
27         
28         $t = clone($this);
29         $t->whereAdd('group_id IN ('. implode(',', $grps).')');
30         $t->find();
31         $ret = array();
32         while($t->fetch()) {
33             if (isset($ret[$t->rightname])) {
34                 $ret[$t->rightname] = $this->mergeMask($ret[$t->rightname], $t->AccessMask);
35                 continue;
36             }
37             $ret[$t->rightname] = $t->AccessMask;
38         }
39         // blank out rights that are disabled by the system..
40         $defs = $this->defaultPermData();
41         //echo "<PRE>";print_r($defs);
42         $r = array();
43         foreach($defs as $k=>$v) {
44             if (empty($v[0])) { // delete right if not there..
45                 $r[$k] = '';
46                 continue;
47             }
48             
49             
50             if (isset($ret[$k])) {
51                 if (empty($ret[$k]) && $isAdmin) {
52                     $r[$k] = $v[0];
53                     continue;
54                 }
55                 
56                 $r[$k] = $ret[$k];
57                 continue;
58             }
59             // not set contition...
60             
61             $r[$k] = $isAdmin ? $v[0] : $v[1];
62             
63        
64         }
65         
66         return $r;
67     }
68     function mergeMask($a, $b) 
69     {
70         // default 
71         $ret = '';
72         for($i=0; $i< strlen($this->fullRights) ; $i++) {
73             if ((strpos($a, $this->fullRights[$i]) > -1) ||
74                 (strpos($b, $this->fullRights[$i]) > -1)
75             ) {
76                 $ret .= $this->fullRights[$i];
77             }
78         }
79         return $ret;
80         
81         
82     }
83     
84     
85     function defaultPermData()
86     {
87         
88         // we should do better caching of this... really..
89         
90         
91         
92         
93         // what they mean:
94         // A - add
95         // D - delete
96         // E - edit
97         // S - list
98         // P - print / export
99         // I - import
100         // M????
101         
102         
103         
104         static $Pman_DataObjects_Group_Right = array();
105         if (!empty($Pman_DataObjects_Group_Right)) {
106             return $Pman_DataObjects_Group_Right;
107         }
108         
109         $ff = HTML_FlexyFramework::get();
110         //print_R($ff);
111         $enabled =  array('Core') ;
112         $enabled = explode(',', $ff->enable);
113         $disabled =  explode(',', $ff->disable? $ff->disable: '');
114         $pman = dirname(__FILE__).'/../../';
115         $ret = array();
116          //echo '<PRE>';print_r($enabled);
117         foreach($enabled as $module) {
118             $fn = $pman. $module.  '/'.$module. '.perms.json';
119             if (!file_exists($fn)) {
120                 continue;
121             }
122             $ar = (array)json_decode(file_get_contents($fn));
123            // echo '<PRE>';print_r($ar);
124             foreach($ar as $k=> $perm) {
125                 if ($k[0] == '/') {
126                     continue; // it's a comment..
127                 }
128                 if (in_array($module, $disabled) || in_array($module.'.'. $k, $disabled)) {
129                     continue;
130                 }
131                 $ret[$module.'.'. $k ] = $perm;
132             }
133             
134         }
135         $Pman_DataObjects_Group_Right = $ret;
136        // print_r($ret);
137         return $Pman_DataObjects_Group_Right;
138          
139         
140     }
141     
142     function adminRights() // get the admin rights - used when no accounts are available..
143     {
144         $defs = $this->defaultPermData();
145         $ret = array();
146         foreach($defs as $k=>$v) {
147             $ret[$k] = $v[0];
148         
149         }
150         return $ret;
151         
152     }
153     
154     function validate()
155     {
156         // all groups must have the minimum privaligess..
157         // admin group must have all the privaliges
158         $g = DB_DataObject::Factory('Groups');
159         $g->get($this->group_id);
160         $defs = $this->defaultPermData();
161         switch($g->name) {
162             case "Administrators";
163                 $this->AccessMask = $this->mergeMask($this->AccessMask, $defs[$this->rightname][0]);
164                 break;
165                 
166             default:
167                 $this->AccessMask = $this->mergeMask($this->AccessMask, $defs[$this->rightname][1]);
168                 break;
169         
170         }
171         
172     }
173     function genDefault()
174     {
175         // need to create to special groups, admin & DEFAULT.
176         $g = DB_DataObject::Factory('Groups');
177         //$g->name = 'Default';
178         //if (!$g->find(true)) {
179         //    $g->insert();
180         //}
181         $g->id = 0;
182         $this->applyDefs($g, 1);
183     
184         $g = DB_DataObject::Factory('Groups');
185         $g->name = 'Administrators';
186         if (!$g->find(true)) {
187             $g->insert();
188         }
189         $this->applyDefs($g, 0);
190         
191         
192     }
193         
194     function applyDefs($g, $usecol) {
195         
196         $defs = $this->defaultPermData();
197         //$usecol = 1;
198         foreach($defs as $rightname => $defdata) {
199             $gr = DB_DataObject::Factory('Group_Rights');
200             $gr->rightname  = $rightname;
201             $gr->group_id = $g->id;
202             if (!$gr->find(true)) {
203                 $gr->AccessMask = $defdata[$usecol];
204                 $gr->insert();
205                 continue;
206             }
207             $oldgr = clone($gr);
208             $gr->AccessMask = $gr->mergeMask($gr->AccessMask, $defdata[$usecol]);
209             if ($gr->AccesMask == $oldgr->AccesMask) {
210                 continue;
211             }
212             $gr->update($oldgr);
213         }
214         
215     }
216         
217     function checkPerm($lvl, $au) 
218     {
219         return false;
220     }  
221     
222 }