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