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