Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.ObjectPermission.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Generated Header (not documented yet)
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.ObjectPermission.php,v 1.13 2003/08/18 15:46:42 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 // $Id: Class.ObjectPermission.php,v 1.13 2003/08/18 15:46:42 eric Exp $
19 // $Source: /home/cvsroot/anakeen/freedom/core/Class/Appmng/Class.ObjectPermission.php,v $
20 // ---------------------------------------------------------------
21 $CLASS_OBJECTPERMISSION_PHP = '$Id: Class.ObjectPermission.php,v 1.13 2003/08/18 15:46:42 eric Exp $';
22 include_once ('Class.DbObj.php');
23 include_once ('Class.QueryDb.php');
24 include_once ('Class.Acl.php');
25 include_once ('Class.ControlObject.php');
26 class ObjectPermission extends DbObj
27 {
28  var $fields = array(
29  "id_user",
30  "id_obj",
31  "id_class",
32  "ids_acl"
33  );
34 
35  var $id_fields = array(
36  "id_user",
37  "id_obj",
38  "id_class"
39  );
40 
41  var $dbtable = "operm";
42 
43  var $sqlcreate = '
44 create table operm (id_user int ,
45  id_obj int not null,
46  id_class int not null,
47  ids_acl int[] );
48 create unique index i_operm on operm (id_user, id_obj, id_class); ';
49 
50  var $classid = 0; // if 0 not a controlled object
51  var $description = "";
52  var $coid = array();
53  var $privileges = array(); // default privilege from permission table
54  var $iscomplete = false; // indicate if all privileges are computed
55  function ObjectPermission($dbaccess = '', $id = '', $res = '', $dbid = 0)
56  {
57  if (is_array($id)) {
58  $this->Affect(array(
59  "id_user" => $id[0],
60  "id_obj" => $id[1],
61  "id_class" => $id[2]
62  ));
63  }
64  // change DB for permission : see 'dboperm' session var
65  global $action;
66  $dbaccess = $action->Read("dboperm", $dbaccess);
67 
68  if (!$this->DbObj($dbaccess, $id, $res, $dbid)) {
69 
70  if (is_array($id)) {
71  $this->GetPrivileges();
72  }
73  }
74  }
75 
76  function PreSelect($id)
77  {
78  if (is_array($id)) {
79  $this->Affect(array(
80  "id_user" => $id[0],
81  "id_obj" => $id[1],
82  "id_class" => $id[2]
83  ));
84  }
85  }
86  function GetDescription()
87  {
88  $octrl = new ControlObject($this->dbaccess, array(
89  $this->id_obj,
90  $this->id_class
91  ));
92 
93  if ($octrl->IsAffected()) {
94  $this->classid = $octrl->id_class;
95  $this->description = $octrl->description;
96  }
97  return $this->description;
98  }
99  // function PostSelect($id)
100  // {
101  // // init privileges
102  // $this-> GetPrivileges();
103  // }
104  // function PostDelete()
105  // {
106  // // update privileges
107  // $this-> GetPrivileges();
108  // }
109  // function PostUpdate()
110  // {
111  // // update privileges
112  // $this-> GetPrivileges();
113  // }
114 
115  // return ACL list for a user and a oid
116  // function GetPrivilegesOld() {
117  // if ( $this->iscomplete) return ($this->privileges);
118  // $this->privileges= array();
119  // $this->upprivileges= array();// privileges array for a user (not including group) in an application
120  // $this->unprivileges= array();// specifific NO privileges array for a user in an application
121  // if (true){
122  // // add groups privilege
123  // $ugroup = new Group($this->dbaccess,
124  // $this->id_user);
125  // while (list($k,$gid) = each($ugroup->groups)) {
126  // $gperm = new ObjectPermission($this->dbaccess,
127  // array($gid,
128  // $this->id_obj,
129  // $this->id_class));
130  // // add group
131  // while (list($k2,$gacl) = each($gperm->privileges)) {
132  // if (! in_array($gacl, $this->privileges)) {
133  // $this->gprivileges[]= $gacl;
134  // $this->privileges[]= $gacl;
135  // }
136  // }
137  // }
138  // }
139  // if (is_array($this->ids_acl) ) {
140  // while (list($k,$v) = each($this->ids_acl)) {
141  // if ($v->id_acl > 0) {
142  // // add privilege
143  // $this->upprivileges[]= $v->id_acl;
144  // if (! in_array($v->id_acl, $this->privileges)) {
145  // $this->privileges[]= $v->id_acl;
146  // }
147  // }else {
148  // // suppress privilege
149  // $this->unprivileges[]= -($v->id_acl);
150  // $nk=array_search(-($v->id_acl), $this->privileges, false);
151  // if (is_integer($nk)) {
152  // unset($this->privileges[$nk]);
153  // }
154  // }
155  // }
156  // }
157  // // $this->AddDefaultPrivileges();
158  // $this->iscomplete= true; // to avoid another computing
159  // return($this->privileges);
160  // }
161  // return ACL list for a user and a oid
163  {
164  if (isset($this->gprivileges)) return ($this->gprivileges);
165 
166  $this->gprivileges = array(); // group privilege
167  $result = pg_exec($this->init_dbid() , "select getprivilege({$this->id_user},{$this->id_obj},{$this->id_class},true)");
168  if (pg_numrows($result) > 0) {
169  $arr = pg_fetch_array($result, 0);
170  $this->gprivileges = explode(",", substr($arr[0], 1, -1));
171  }
172  }
173  function GetPrivileges()
174  {
175 
176  if ($this->iscomplete) return ($this->privileges);
177 
178  $this->privileges = array();
179  $this->upprivileges = array(); // privileges array for a user (not including group) in an application
180  $this->unprivileges = array(); // specifific NO privileges array for a user in an application
181 
182  $acls = explode(",", substr($this->ids_acl, 1, -1));
183 
184  while (list($k, $v) = each($acls)) {
185  if ($v > 0) $this->upprivileges[] = $v;
186  else $this->unprivileges[] = - $v;
187  }
188 
189  $result = pg_exec($this->init_dbid() , "select getprivilege({$this->id_user},{$this->id_obj},{$this->id_class},false)");
190  // print "select getprivilege({$this->id_user},{$this->id_obj},{$this->id_class},false)";
191  //print $result."<HR>";
192  if (pg_numrows($result) > 0) {
193  $arr = pg_fetch_array($result, 0);
194  $this->privileges = array_unique(explode(",", substr($arr[0], 1, -1)));
195  }
196  // print_r( $this->privileges);
197 
198  $this->iscomplete = true; // to avoid another computing
199  return ($this->privileges);
200  }
201  // recompute privilege with default permission
203  {
204  // Add uperm->privileges in $this
205  $this->privileges = array_unique(array_merge($this->dprivileges, $this->privileges));
206  // Remove this->unprivileges
207  $this->privileges = array_diff($this->privileges, $this->unprivileges);
208  }
209 
210  function AddAcl($idacl)
211  {
212  if ($this->ids_acl == "") $this->ids_acl = "{" . $idacl . "}";
213  else $this->ids_acl = str_replace("}", ",$idacl}", $this->ids_acl);
214  }
215 
216  function HasPrivilege($idacl)
217  {
218  return (($this->id_user == 1) || // admin user
219  (in_array($idacl, $this->privileges)));
220  }
221 
222  function PostDelete()
223  {
224  $this->ids_acl = "";
225  }
226  function Control($method)
227  {
228  // return "" if the current user can apply method on object
229  // else return string error
230  $this->GetPrivileges();
231  $err = $this->ControlOid($this->id_class, $method);
232  //print "Control : $this->id_user, $object->oid, $this->id_obj, $object->classid, $method : $err<BR>";
233  //print "<BR>up<BR>";print_r($this->upprivileges);
234  //print "<BR>un<BR>";print_r($this->unprivileges);
235  //print "<BR>d<BR>";print_r($this->dprivileges);
236  //print "<BR>g<BR>";print_r($this->gprivileges);
237  //print "<BR>r<BR>";print_r($this->privileges);
238  return ($err);
239  }
240 
241  function ControlOid($idclassapp, $method)
242  {
243 
244  if ($this->id_user == 1) return ""; // Admin can control everything
245  // case already computed
246  if (isset($this->coid[$method])) return $this->coid[$method];
247  // print "ControlOid : $this->oid, $this->id_user, $this->id_obj, $idclassapp, $method <BR>";
248  //$this->AddDefaultPrivileges();
249  // now determine all privileges for current user and for oid parameter
250  if ($this->id_user == "") {
251  return "Object Permission : current user not found";
252  }
253 
254  $acl = new Acl();
255  if (!$acl->Set($method, $idclassapp)) {
256  $this->log->warning("Acl $method not available for App $idclassapp ");
257  $err = "Acl $method not available for App $idclassapp ";
258  $this->coid[$method] = $err; // memo for optimization (no new computing)
259  return $err;
260  }
261 
262  if (!$this->HasPrivilege($acl->id)) {
263  $err = sprintf(_("Object Permission : permission %s needed (%s) - # %d") , _($acl->description) , $this->GetDescription() , $this->id_obj);
264 
265  $this->coid[$method] = $err; // memo for optimization (no new computing)
266  return $err;
267  }
268 
269  $this->coid[$method] = "";
270  return ""; // OK
271 
272  }
273 }
274 ?>
← centre documentaire © anakeen - published under CC License - Dynacase