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