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