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