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