Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Acl.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  * Access Control for application
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Acl.php,v 1.8 2005/10/27 14:26:05 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 
19 include_once ('Class.DbObj.php');
20 include_once ('Class.QueryDb.php');
21 include_once ('Class.Application.php');
22 include_once ('Class.User.php');
23 
24 class Acl extends DbObj
25 {
26  var $fields = array(
27  "id",
28  "id_application",
29  "name",
30  "grant_level",
31  "description",
32  "group_default"
33  );
34 
35  var $id_fields = array(
36  "id"
37  );
38 
39  var $dbtable = "acl";
40 
41  var $sqlcreate = '
42 create table acl (id int not null,
43  id_application int not null,
44  name varchar(30) not null,
45  grant_level int not null,
46  description varchar(100),
47  group_default varchar(1));
48 create index acl_idx1 on acl(id);
49 create index acl_idx2 on acl(id_application);
50 create index acl_idx3 on acl(name);
51 create sequence SEQ_ID_ACL;
52  ';
53 
54  function Set($name, $id_app)
55  {
56  $query = new QueryDb($this->dbaccess, "Acl");
57  $query->basic_elem->sup_where = array(
58  "name='$name'",
59  "id_application=$id_app"
60  );
61  $query->Query(0, 0, "TABLE");
62 
63  if ($query->nb > 0) {
64  $this->Affect($query->list[0]);
65  } else {
66  return false;
67  }
68  return true;
69  }
70 
71  function Complete()
72  {
73  }
74 
75  function PreInsert()
76  {
77  if ($this->Exists($this->name, $this->id_application)) return "Acl {$this->name} already exists...";
78  $msg_res = $this->exec_query("select nextval ('seq_id_acl')");
79  $arr = $this->fetch_array(0);
80  $this->id = $arr["nextval"];
81  }
82  function PreUpdate()
83  {
84  if ($this->dbid == - 1) return FALSE;
85  }
86 
87  function Exists($name, $id_app)
88  {
89  $query = new QueryDb($this->dbaccess, "Acl");
90  $query->basic_elem->sup_where = array(
91  "name='$name'",
92  "id_application=$id_app"
93  );
94  $query->Query(0, 0, "TABLE");
95  return ($query->nb > 0);
96  }
97 
98  function DelAppAcl($id)
99  {
100  $query = new QueryDb($this->dbaccess, "Acl");
101  $query->basic_elem->sup_where = array(
102  "id_application=$id"
103  );
104  $list = $query->Query();
105  if ($query->nb > 0) {
106  while (list($k, $v) = each($list)) {
107  $v->Delete();
108  }
109  }
110  // Remove Permission
111  $permission = new Permission($this->dbaccess);
112  $permission->DelAppPerm($id);
113  }
114 
115  function Init($app, $app_acl, $update = FALSE)
116  {
117  if (sizeof($app_acl) == 0) {
118  $this->log->debug("No acl available");
119  return ("");
120  }
121 
122  $default_grant_level_found = false; // indicate user default set explicitly
123  if (isset($app_acl[0]["grant_level"])) $oldacl = true; // for old ACL description (for compatibility with old application)
124  else $oldacl = false;
125  // read init file
126  $default_user_acl = array(); // default acl ids
127  $default_acl = false; // to update default acl id
128  while (list($k, $tab) = each($app_acl)) {
129  $acl = new Acl($this->dbaccess);
130  if ($acl->Exists($tab["name"], $app->id)) {
131  $acl->Set($tab["name"], $app->id);
132  }
133  $acl->id_application = $app->id;
134  $acl->name = $tab["name"];
135  if (isset($tab["description"])) {
136  $acl->description = $tab["description"];
137  }
138  if (isset($tab["grant_level"])) {
139  $acl->grant_level = $tab["grant_level"];
140  } else {
141  $acl->grant_level = 1;
142  }
143  // initialise grant level default
144  if ((isset($tab["group_default"])) && ($tab["group_default"] == "Y")) {
145  if ($oldacl) {
146  $default_grant_level = $tab["grant_level"];
147  $default_grant_level_found = true;
148  }
149  $acl->group_default = "Y";
150  $default_acl = true;
151  } else {
152  $acl->group_default = "N";
153 
154  if ($oldacl) {
155  if ((!$default_grant_level_found) && ((!isset($smalestgrant)) || ($tab["grant_level"] < $smalestgrant)) && (!((isset($tab["admin"]) && $tab["admin"])))) {
156  // default acl admin must be specified explicitly
157  $smalestgrant = $tab["grant_level"];
158  }
159  }
160  }
161 
162  if ($acl->Exists($acl->name, $acl->id_application)) {
163  $this->log->info("Acl Modify : {$acl->name}, {$acl->description}");
164  $acl->Modify();
165  } else {
166  $this->log->info("Acl Add : {$acl->name}, {$acl->description}");
167  $acl->Add();
168  }
169  if (isset($tab["admin"]) && $tab["admin"]) {
170  $permission = new Permission($this->dbaccess);
171  $permission->id_user = 1;
172  $permission->id_application = $app->id;
173  $permission->id_acl = $acl->id;
174  if ($permission->Exists($permission->id_user, $app->id, $permission->id_acl)) {
175  $this->log->info("Modify admin permission : {$acl->name}");
176  $permission->Modify();
177  } else {
178  $this->log->info("Create admin permission : {$acl->name}");
179  $permission->Add();
180  }
181  }
182  if ($default_acl) {
183  $default_user_acl[] = $acl->id;
184  $default_acl = false;
185  }
186  }
187  // default privilige is the smallest if no definition (for old old application)
188  if (count($default_user_acl) == 0) {
189  if (isset($smalestgrant)) {
190  $default_user_acl[] = $smalestgrant;
191  $default_grant_level = $smalestgrant;
192  }
193  }
194 
195  if ($oldacl) {
196  // ----------------------------------------------
197  // for old acl form definition (with grant_level)
198  // set default acl for grant level under the default
199  if (isset($default_grant_level)) {
200  $query = new QueryDb($this->dbaccess, "Acl");
201  $query->AddQuery("id_application = " . $app->id);
202  $query->AddQuery("grant_level < $default_grant_level");
203  if ($qacl = $query->Query()) {
204  while (list($k2, $acl) = each($qacl)) {
205  if (!in_array($acl->id, $default_user_acl)) {
206  $default_user_acl[] = $acl->id;
207  }
208  }
209  }
210  }
211  }
212  // create default permission
213  reset($default_user_acl);
214  while (list($ka, $aclid) = each($default_user_acl)) {
215  // set the default user access
216  $defaultacl = new Acl($this->dbaccess, $aclid);
217  $defaultacl->group_default = "Y";
218  $defaultacl->Modify();
219 
220  if (!$update) {
221  // set default access to 'all' group only
222  $permission = new Permission($this->dbaccess);
223  $permission->id_user = 2;
224  $permission->id_application = $app->id;
225  $permission->id_acl = $aclid;
226  if (!$permission->Exists($permission->id_user, $app->id, $permission->id_acl)) {
227  $permission->Add();
228  }
229  }
230  }
231  // Remove unused Acl in case of update
232  // if ($update) {
233  // $query=new QueryDb($this->dbaccess,"Acl");
234  // $query->basic_elem->sup_where=array ("id_application = {$app->id}");
235  // $list=$query->Query();
236  // while (list($k,$v)=each($list)) {
237  // // Check if the ACL still exists
238  // $find=FALSE;
239  // reset($app_acl);
240  // while ( (list($k2,$v2) = each($app_acl)) && ($find==FALSE) ) {
241  // $find=( $v2["name"] == $v->name );
242  // }
243  // if (!$find) {
244  // // remove the ACL and all associated permissions
245  // $this->log->info("Removing the {$v->name} ACL");
246  // $query2 = new QueryDb($this->dbaccess,"Permission");
247  // $query2->basic_elem->sup_where=array("id_application= {$app->id}",
248  // "id_acl = {$v->id}");
249  // $list_perm = $query2->Query();
250  // if ($query2->nb>0) {
251  // while (list($k2,$p) = each ($list_perm)) {
252  // $p->Delete();
253  // }
254  // }
255  // $v->Delete();
256  // }
257  // }
258  // }
259 
260 
261  }
262  // get default ACL for an application
263  function getDefaultAcls($idapp)
264  {
265 
266  $aclids = array();
267  $query = new QueryDb($this->dbaccess, "Acl");
268  $query->AddQuery("id_application = $idapp");
269  $query->AddQuery("group_default = 'Y'");
270  if ($qacl = $query->Query()) {
271  while (list($k2, $acl) = each($qacl)) {
272  $aclids[] = $acl->id;
273  }
274  }
275  return $aclids;
276  }
277 
278  function getAclApplication($idapp)
279  {
280 
281  $query = new QueryDb($this->dbaccess, "Acl");
282  $query->AddQuery("id_application = $idapp");
283  if ($qacl = $query->Query()) return $qacl;
284  return 0;
285  }
286 }
287 ?>
← centre documentaire © anakeen - published under CC License - Dynacase