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         if (!$this->hasPerm( 'Core.Groups','S')) { // listing groups..
38             $this->jerr("PERMISSION DENIED");
39         }
40         
41         $g = DB_DataObject::Factory('core_group');
42         if (!$g->get($_GET['group_id'])) {
43             $this->jerr("group is invalid");
44         }
45         //print_r($g);
46          //   DB_DataObject::debugLevel(1);
47         $p = DB_DataObject::factory('core_group_right');
48         $p->group_id = (int)$_GET['group_id'];
49         $p->find();
50         $cur = array();
51     
52         while ($p->fetch()) {
53             $cur[$p->rightname] = clone($p);
54         }
55         
56 //        print_r($cur);exit;
57         
58         $e = -1;
59         $ar = array();
60         // echo "<PRE>"; print_r($p->defaultPermData() );
61         foreach($p->defaultPermData() as $k => $defdata) {
62             
63             if (empty($defdata[0])) { // no admin data available..
64                 continue;
65             }
66             if (!isset($cur[$k])) {
67                 // then there is no current access right for it..
68                 //DB_DataObject::debugLevel(1);
69                 $gr = DB_DataObject::factory('core_group_right');
70                 $gr->group_id = (int)$_GET['group_id'];
71                 $gr->rightname = $k;
72                 $gr->accessmask = $g->type == 2 ? '' : $defdata[1]; // set to defaults.. unless it's a contact group.
73                 $gr->insert();
74                 $cur[$k] = clone($gr);
75             }
76             
77             
78             $ar[] = array(
79                 'id' => $cur[$k]->id * 1, //
80                 'rightname' => $k,
81                 'descript' => isset($defdata[2]) ? $defdata[2] : '' ,
82                 'accessmask' => $cur[$k]->accessmask,
83                 'FullMask' => $defdata[0],
84                 'group_id' => (int)$_GET['group_id']
85             );
86                 
87         }
88         $this->jdata($ar);
89         
90          
91     }
92     
93     
94     // post.. 
95     function post($v)
96     {
97         if (!isset($_POST['group_id']) || (int)$_POST['group_id'] < 0) {
98             $this->jerr("NO GROUP");
99         }
100         if (!$this->hasPerm( 'Core.Groups','E')) { // editing groups..
101             $this->jerr("PERMISSION DENIED");
102         }
103         
104         
105             
106         
107         
108         // add or update..
109         if (!empty($_POST['dataUpdate'])) {
110             foreach($_POST['dataUpdate'] as $id => $ac) {
111                 $id  = (int)$id;
112                 $p = DB_DataObject::factory('core_group_right');
113                 $p->group_id = (int)$_POST['group_id'];
114                 if (!$p->get($id)) {
115                     $this->jerr("could not find gid:{$p->group_id} and $id");
116                     continue; // errro cond.
117                 }
118                 $po = clone($p);
119                 $p->accessmask = $ac;
120                 $p->validate(); // ensure that the basic perms can not be removed
121                 $p->update($po);
122             }
123         }
124         if (!empty($_POST['dataAdd'])) {
125             foreach($_POST['dataAdd'] as $perm => $ac) {
126                 $p = DB_DataObject::factory('core_group_right');
127                 $p->group_id = (int)$_POST['group_id'];
128                 $p->rightname = $perm;
129                 $p->accessmask = $ac;
130                 $p->validate(); // ensure that the basic perms can not be removed
131                 $p->insert();
132             }
133         }
134         $this->jok("done");
135         
136         
137         
138     }
139      
140     
141     
142     
143     
144 }