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