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