DataObjects/ProjectDirectory.php
[Pman.Core] / DataObjects / Group_Rights.php
1 <?php
2 /**
3  * Table Definition for Group_Rights
4  */
5 require_once 'DB/DataObject.php';
6
7  
8 class Pman_Core_DataObjects_Group_Rights extends DB_DataObject 
9 {
10     ###START_AUTOCODE
11     /* the code below is auto generated do not remove the above tag */
12
13     public $__table = 'Group_Rights';                    // table name
14     public $rightname;                       // string(64)  not_null
15     public $group_id;                        // int(11)  not_null
16     public $AccessMask;                      // string(10)  not_null
17     public $id;                              // int(11)  not_null primary_key auto_increment
18
19     
20     /* the code above is auto generated do not remove the tag below */
21     ###END_AUTOCODE
22     
23     
24     var $fullRights = "ADESPIM";
25     
26     function listPermsFromGroupIds($grps, $isAdmin=false) {
27         
28         $t = clone($this);
29         $t->whereAdd('group_id IN ('. implode(',', $grps).')');
30         $t->find();
31         $ret = array();
32         while($t->fetch()) {
33             if (isset($ret[$t->rightname])) {
34                 $ret[$t->rightname] = $this->mergeMask($ret[$t->rightname], $t->AccessMask);
35                 continue;
36             }
37             $ret[$t->rightname] = $t->AccessMask;
38         }
39         // blank out rights that are disabled by the system..
40         $defs = $this->defaultPermData();
41         //echo "<PRE>";print_r($defs);
42         $r = array();
43         foreach($defs as $k=>$v) {
44             if (empty($v[0])) { // delete right if not there..
45                 $r[$k] = '';
46                 continue;
47             }
48             
49             
50             if (isset($ret[$k])) {
51                 if (empty($ret[$k]) && $isAdmin) {
52                     $r[$k] = $v[0];
53                     continue;
54                 }
55                 
56                 $r[$k] = $ret[$k];
57                 continue;
58             }
59             // not set contition...
60             
61             $r[$k] = $isAdmin ? $v[0] : $v[1];
62             
63        
64         }
65         
66         return $r;
67     }
68     function mergeMask($a, $b) 
69     {
70         // default 
71         $ret = '';
72         for($i=0; $i< strlen($this->fullRights) ; $i++) {
73             if ((strpos($a, $this->fullRights[$i]) > -1) ||
74                 (strpos($b, $this->fullRights[$i]) > -1)
75             ) {
76                 $ret .= $this->fullRights[$i];
77             }
78         }
79         return $ret;
80         
81         
82     }
83     
84     
85     function defaultPermData()
86     {
87         
88         // we should do better caching of this... really..
89         
90         
91         
92         
93         // what they mean:
94         // A - add
95         // D - delete
96         // E - edit
97         // S - list
98         // P - print / export
99         // I - import
100         // M????
101         
102         
103         
104         static $Pman_DataObjects_Group_Right = array();
105         if (!empty($Pman_DataObjects_Group_Right)) {
106             return $Pman_DataObjects_Group_Right;
107         }
108         
109         $ff = HTML_FlexyFramework::get();
110         //print_R($ff);
111         $enabled =  array('Core') ;
112         $enabled = explode(',', $ff->enable);
113         $disabled =  explode(',', $ff->disable? $ff->disable: '');
114         $pman = $ff->rootDir . '/Pman/';
115         $ret = array();
116          //echo '<PRE>';print_r($enabled);
117         foreach($enabled as $module) {
118             $fn = $pman. $module.  '/'.$module. '.perms.json';
119             if (!file_exists($fn)) {
120                 continue;
121             }
122             $ar = (array)json_decode(file_get_contents($fn));
123             if (empty($ar)) {
124                 // since these are critical files.. die'ing with error is ok.
125                 die("invalid json file: " . $fn);
126                }
127            // echo '<PRE>';print_r($ar);
128             foreach($ar as $k=> $perm) {
129                 if ($k[0] == '/') {
130                     continue; // it's a comment..
131                 }
132                 if (in_array($module, $disabled) || in_array($module.'.'. $k, $disabled)) {
133                     continue;
134                 }
135                 $ret[$module.'.'. $k ] = $perm;
136             }
137             
138         }
139         $Pman_DataObjects_Group_Right = $ret;
140        // print_r($ret);
141         return $Pman_DataObjects_Group_Right;
142          
143         
144     }
145     
146     function adminRights() // get the admin rights - used when no accounts are available..
147     {
148         $defs = $this->defaultPermData();
149         $ret = array();
150         foreach($defs as $k=>$v) {
151             $ret[$k] = $v[0];
152         
153         }
154         return $ret;
155         
156     }
157     
158     function validate()
159     {
160         // all groups must have the minimum privaligess..
161         // admin group must have all the privaliges
162         $g = DB_DataObject::Factory('Groups');
163         $g->get($this->group_id);
164         $defs = $this->defaultPermData();
165         switch($g->name) {
166             case "Administrators";
167                 $this->AccessMask = $this->mergeMask($this->AccessMask, $defs[$this->rightname][0]);
168                 break;
169                 
170             default:
171                 $this->AccessMask = $this->mergeMask($this->AccessMask, $defs[$this->rightname][1]);
172                 break;
173         
174         }
175         
176     }
177     function genDefault()
178     {
179         // need to create to special groups, admin & DEFAULT.
180         $g = DB_DataObject::Factory('Groups');
181         //$g->name = 'Default';
182         //if (!$g->find(true)) {
183         //    $g->insert();
184         //}
185         $g->id = 0;
186         $this->applyDefs($g, 1);
187     
188         $g = DB_DataObject::Factory('Groups');
189         $g->name = 'Administrators';
190         if (!$g->find(true)) {
191             $g->insert();
192         }
193         $this->applyDefs($g, 0);
194         
195         
196     }
197         
198     function applyDefs($g, $usecol) {
199         
200         $defs = $this->defaultPermData();
201         //$usecol = 1;
202         foreach($defs as $rightname => $defdata) {
203             $gr = DB_DataObject::Factory('Group_Rights');
204             $gr->rightname  = $rightname;
205             $gr->group_id = $g->id;
206             if (!$gr->find(true)) {
207                 $gr->AccessMask = $defdata[$usecol];
208                 $gr->insert();
209                 continue;
210             }
211             $oldgr = clone($gr);
212             $gr->AccessMask = $gr->mergeMask($gr->AccessMask, $defdata[$usecol]);
213             if ($gr->AccesMask == $oldgr->AccesMask) {
214                 continue;
215             }
216             $gr->update($oldgr);
217         }
218         
219     }
220         
221     function checkPerm($lvl, $au) 
222     {
223         return false;
224     }  
225     
226 }