GroupRights.php
[Pman.Admin] / GroupRights.php
1 <?php
2 /**
3  * 
4  * Part of Core..
5  * 
6  */
7 require_once 'Pman.php';
8
9 class Pman_Admin_GroupRights extends Pman
10 {
11     function getAuth() {
12         parent::getAuth(); // load company!
13         $au = $this->getAuthUser();
14         if (!$au) {
15             $this->jerr("Not authenticated", array('authFailure' => true));
16         }
17         
18         if ($au->company()->comptype !='OWNER') {
19             $this->jerr("Error", "only company owners can manage groups");
20         }
21         
22         $this->authUser = $au;
23         return true;
24     }
25     
26     // perms - any table that can be modified by the user should be listed here..
27     // without it, our perms manager should deny writing via the web interface...
28     
29     // FOR PERMS - SEE THE DATAOBJECT!
30     
31     function get($v, $opts = Array())
32     {
33         // must recieve a group..
34         if (!isset($_GET['group_id']) || (int)$_GET['group_id'] < 0) {
35             $this->jerr("NO GROUP");
36         }
37         
38         if (!$this->checkPerm('E')) { // editing groups..
39             $this->jerr("PERMISSION DENIED");
40         }
41         
42         $g = DB_DataObject::Factory('core_group');
43         if (!$g->get($_GET['group_id'])) {
44             $this->jerr("group is invalid");
45         }
46         //print_r($g);
47          //   DB_DataObject::debugLevel(1);
48         $p = DB_DataObject::factory('core_group_right');
49         $p->group_id = (int)$_GET['group_id'];
50         $p->find();
51         $cur = array();
52     
53         while ($p->fetch()) {
54             $cur[$p->rightname] = clone($p);
55         }
56         
57 //        print_r($cur);exit;
58         
59         $e = -1;
60         $ar = array();
61         // echo "<PRE>"; print_r($p->defaultPermData() );
62         foreach($p->defaultPermData() as $k => $defdata) {
63             
64             if (empty($defdata[0])) { // no admin data available..
65                 continue;
66             }
67             if (!isset($cur[$k])) {
68                 // then there is no current access right for it..
69                 //DB_DataObject::debugLevel(1);
70                 $gr = DB_DataObject::factory('core_group_right');
71                 $gr->group_id = (int)$_GET['group_id'];
72                 $gr->rightname = $k;
73                 $gr->accessmask = $g->type == 2 ? '' : $defdata[1]; // set to defaults.. unless it's a contact group.
74                 $gr->insert();
75                 $cur[$k] = clone($gr);
76             }
77             
78             $short = explode('.',$k);
79             $ar[] = array(
80                 'id' => $cur[$k]->id * 1, //
81                 'rightname' => $k,
82                 'descript' => isset($defdata[2]) ? $defdata[2] : '' ,
83                 'accessmask' => $cur[$k]->accessmask,
84                 'FullMask' => $defdata[0],
85                 'group_id' => (int)$_GET['group_id'],
86                 'shortname' => $short[0]
87             );
88                 
89         }
90
91         foreach ($ar as $key => $row) {
92             $shortname[$key]  = $row['shortname'];
93             $descript[$key] = $row['descript'];
94         }
95
96         array_multisort($shortname, SORT_ASC, $descript, SORT_ASC, $ar);        
97         
98         $this->jdata($ar);
99         
100          
101     }
102     
103     
104     // post.. 
105     function post($v)
106     {
107         if (!isset($_POST['group_id']) || (int)$_POST['group_id'] < 0) {
108             $this->jerr("NO GROUP");
109         }
110         
111         if (!$this->checkPerm('E')) { // editing groups..
112             $this->jerr("PERMISSION DENIED");
113         }
114         
115         // add or update..
116         if (!empty($_POST['dataUpdate'])) {
117             foreach($_POST['dataUpdate'] as $id => $ac) {
118                 $id  = (int)$id;
119                 $p = DB_DataObject::factory('core_group_right');
120                 $p->group_id = (int)$_POST['group_id'];
121                 if (!$p->get($id)) {
122                     $this->jerr("could not find gid:{$p->group_id} and $id");
123                     continue; // errro cond.
124                 }
125                 $po = clone($p);
126                 $p->accessmask = $ac;
127                 $p->validate(); // ensure that the basic perms can not be removed
128                 $p->update($po);
129             }
130         }
131         if (!empty($_POST['dataAdd'])) {
132             foreach($_POST['dataAdd'] as $perm => $ac) {
133                 $p = DB_DataObject::factory('core_group_right');
134                 $p->group_id = (int)$_POST['group_id'];
135                 $p->rightname = $perm;
136                 $p->accessmask = $ac;
137                 $p->validate(); // ensure that the basic perms can not be removed
138                 $p->insert();
139             }
140         }
141         $this->jok("done");
142         
143         
144         
145     }
146     
147     function checkPerm($lvl)
148     {
149         return $this->hasPerm('Core.Groups', $lvl);
150     }
151     
152 }