DataObjects/core.sql
[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         if (!empty($Pman_DataObjects_Group_Right)) {
117             return $Pman_DataObjects_Group_Right;
118         }
119         
120         $ff = HTML_FlexyFramework::get();
121         //print_R($ff);
122         $enabled =  array('Core') ;
123         $enabled = explode(',', $ff->enable);
124         $disabled =  explode(',', $ff->disable? $ff->disable: '');
125         $pman = $ff->rootDir . '/Pman/';
126         $ret = array();
127          //echo '<PRE>';print_r($enabled);
128         foreach($enabled as $module) {
129             $fn = $pman. $module.  '/'.$module. '.perms.json';
130             if (!file_exists($fn)) {
131                 continue;
132             }
133             $ar = (array)json_decode(file_get_contents($fn));
134             if (empty($ar)) {
135                 // since these are critical files.. die'ing with error is ok.
136                 die("invalid json file: " . $fn);
137                }
138            // echo '<PRE>';print_r($ar);
139             foreach($ar as $k=> $perm) {
140                 if ($k[0] == '/') {
141                     continue; // it's a comment..
142                 }
143                 if (in_array($module, $disabled) || in_array($module.'.'. $k, $disabled)) {
144                     continue;
145                 }
146                 $ret[$module.'.'. $k ] = $perm;
147             }
148             
149         }
150         $Pman_DataObjects_Group_Right = $ret;
151        // print_r($ret);
152         return $Pman_DataObjects_Group_Right;
153          
154         
155     }
156     
157     function adminRights() // get the admin rights - used when no accounts are available..
158     {
159         $defs = $this->defaultPermData();
160         $ret = array();
161         foreach($defs as $k=>$v) {
162             $ret[$k] = $v[0];
163         
164         }
165         return $ret;
166         
167     }
168     
169     function validate()
170     {
171         // all groups must have the minimum privaligess..
172         // admin group must have all the privaliges
173         $g = DB_DataObject::Factory('Groups');
174         $g->get($this->group_id);
175         $defs = $this->defaultPermData();
176         switch($g->name) {
177             case "Administrators";
178                 $this->accessmask = $this->mergeMask($this->accessmask, $defs[$this->rightname][0]);
179                 break;
180                 
181             default:
182                 //$this->accessmask = $this->mergeMask($this->accessmask, $defs[$this->rightname][1]);
183                 break;
184         
185         }
186         
187     }
188     /**
189      * generates the default admin group.
190      * and returns it.
191      */
192     function genDefault()
193     {
194         // need to create to special groups, admin & DEFAULT.
195         $g = DB_DataObject::Factory('Groups');
196         //$g->name = 'Default';
197         //if (!$g->find(true)) {
198         //    $g->insert();
199         //}
200         $g->id = 0;
201         $this->applyDefs($g, 1);
202     
203         $g = DB_DataObject::Factory('Groups');
204         $g->name = 'Administrators';
205         $g->type = 0;
206         if (!$g->find(true)) {
207             $g->insert();
208         }
209         $this->applyDefs($g, 0);
210         return $g;
211         
212     }
213         
214     function applyDefs($g, $usecol) {
215         
216         $defs = $this->defaultPermData();
217         //echo '<PRE>';print_r($defs);
218         //$usecol = 1;
219         foreach($defs as $rightname => $defdata) {
220             $gr = DB_DataObject::Factory('Group_Rights');
221             $gr->rightname  = $rightname;
222             $gr->group_id = $g->id;
223             if (!$gr->find(true)) {
224                 $gr->accessmask = $defdata[$usecol];
225                 $gr->insert();
226                 continue;
227             }
228             $oldgr = clone($gr);
229             $gr->accessmask = $gr->mergeMask($gr->accessmask, $defdata[$usecol]);
230             if ($gr->accessmask == $oldgr->accessmask) {
231                 continue;
232             }
233             $gr->update($oldgr);
234         }
235         
236     }
237         
238     function checkPerm($lvl, $au) 
239     {
240         return false;
241     }  
242     
243 }