Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Group.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  * User Group Definition
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Group.php,v 1.22 2007/03/12 08:25:55 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.Log.php');
22 include_once ('Class.Application.php');
23 
24 class Group extends DbObj
25 {
26  var $fields = array(
27  "iduser",
28  "idgroup"
29  );
30 
31  var $id_fields = array(
32  "iduser"
33  );
34 
35  var $dbtable = "groups";
36 
37  var $sqlcreate = "
38 create table groups ( iduser int not null,
39  idgroup int not null);
40 create index groups_idx1 on groups(iduser);
41 create unique index groups_idx2 on groups(iduser,idgroup);
42 create trigger t_nogrouploop before insert or update on groups for each row execute procedure nogrouploop();";
43 
44  var $groups = array(); // user groups
45 
46 
47  /**
48  * get groups of a user
49  * set groups attribute. This attribute containt id of group of a user
50  * @return bool true if at least one group
51  */
52  function GetGroups()
53  {
54  $query = new QueryDb($this->dbaccess, "Group");
55 
56  $query->AddQuery("iduser='{$this->iduser}'");
57 
58  $list = $query->Query();
59  if ($query->nb > 0) {
60  while (list($k, $v) = each($list)) {
61  $this->groups[] = $v->idgroup;
62  }
63  } else {
64  return false;
65  }
66 
67  return true;
68  }
69  /**
70  * suppress a user from the group
71  *
72  * @param int $uid user identificator to suppress
73  * @return string error message
74  */
75  function SuppressUser($uid, $nopost = false)
76  {
77  $err = "";
78 
79  if (($this->iduser > 0) && ($uid > 0)) {
80  $err = $this->exec_query("delete from groups where idgroup=" . $this->iduser . " and iduser=$uid");
81  $err = $this->exec_query("delete from sessions where userid=$uid");
82 
83  if (usefreedomuser()) {
84  $dbf = getParam("FREEDOM_DB");
85  $g = new Group($dbf);
86  $err = $g->exec_query("delete from groups where idgroup=" . $this->iduser . " and iduser=$uid");
87  }
88  if (!$nopost) $this->PostDelete($uid);
89  }
90  return $err;
91  }
92  /**
93  * initialise groups for a user
94  */
95  function PostSelect()
96  {
97  $this->GetGroups();
98  }
99 
100  function preInsert()
101  {
102  // verify is exists
103  $err = $this->exec_query(sprintf("select * from groups where idgroup=%s and iduser=%s", $this->idgroup, $this->iduser));
104  if ($this->numrows() > 0) {
105  $err = "OK"; // just to say it is not a real error
106 
107  }
108  return $err;
109  }
110 
111  function PostDelete($uid = 0)
112  {
113  if (usefreedomuser()) {
114  if ($uid) $u = new User("", $uid);
115  else $u = new User("", $this->iduser);
116  if ($u->isgroup == "Y") {
117  // recompute all doc profil
118  $this->FreedomCopyGroup();
119  } else {
120  $dbf = getParam("FREEDOM_DB");
121  $g = new Group($dbf);
122  $g->iduser = $this->iduser;
123  $g->idgroup = $this->idgroup;
124  $err = $g->exec_query("delete from groups where idgroup=" . $this->iduser . " and iduser=" . $u->id);
125  if ($err == "") {
126  // if it is a user (not a group)
127  $g->exec_query("delete from docperm where upacl=0 and unacl=0 and userid=" . $g->iduser);
128  $g->exec_query("update docperm set cacl=0 where cacl != 0 and userid=" . $g->iduser);
129  $g->exec_query("delete from permission where computed");
130 
131  $p = new Permission($this->dbaccess);
132  $p->deletePermission($g->iduser, null, null, true);
133  }
134  }
135  }
136  }
137 
138  function PostInsert()
139  {
140  $err = $this->exec_query("delete from sessions where userid=" . $this->iduser);
141  // $this->FreedomCopyGroup();
142  if (usefreedomuser()) {
143  $u = new User("", $this->iduser);
144  if ($u->isgroup == "Y") {
145  // recompute all doc profil
146  $this->FreedomCopyGroup();
147  } else {
148  $dbf = getParam("FREEDOM_DB");
149  $g = new Group($dbf);
150  $g->iduser = $this->iduser;
151  $g->idgroup = $this->idgroup;
152  $err = $g->Add(true);
153  if ($err == "" || $err == "OK") {
154  // if it is a user (not a group)
155  $g->exec_query("delete from docperm where upacl=0 and unacl=0 and userid=" . $g->iduser);
156  $g->exec_query("update docperm set cacl=0 where cacl != 0 and userid=" . $g->iduser);
157  $g->exec_query("delete from permission where computed");
158 
159  $p = new Permission($this->dbaccess);
160  $p->deletePermission($g->iduser, null, null, true);
161  }
162  }
163  }
164  return $err;
165  }
166 
167  function FreedomCopyGroup()
168  {
169  $err = $this->exec_query("delete from sessions where userid=" . $this->iduser);
170 
171  if (usefreedomuser()) {
172  $wsh = getWshCmd();
173  $cmd = $wsh . " --api=freedom_groups";
174 
175  exec($cmd);
176  // $wsh = "nice -n 1 ".GetParam("CORE_PUBDIR")."/wsh.php";
177  // $cmd = $wsh . " --api=usercard_iuser >/dev/null 2>&1 &";
178  // exec($cmd);
179 
180  }
181  }
182  /**
183  * get ascendant direct group and group of group
184  */
185  function GetAllGroups()
186  {
187  $allg = $this->groups;
188  while (list($k, $gid) = each($this->groups)) {
189  $og = new Group($this->dbaccess, $gid);
190  $allg = array_merge($allg, $og->GetAllGroups());
191  }
192  $allg = array_unique($allg);
193 
194  return $allg;
195  }
196  /**
197  * get all child (descendant) group of this group
198  * @return array id
199  */
200  function getChildsGroupId($pgid = "")
201  {
202  if ($pgid == "") $pgid = $this->id;
203  $this->_initAllGroup();
204 
205  $groupsid = array();
206 
207  if ($this->allgroups) {
208  foreach ($this->allgroups as $k => $v) {
209  if ($v["idgroup"] == $pgid) {
210  $uid = $v["iduser"];
211  $groupsid[$uid] = $uid;
212  // $groupsid=array_merge($groupsid, $this->getChildsGroup($v["iduser"]));
213  $groupsid+= $this->getChildsGroupId($uid);
214  }
215  }
216  }
217  return $groupsid;
218  }
219  /**
220  * get all parent (ascendant) group of this group
221  * @return array id
222  */
223  function getParentsGroupId($pgid = "", $level = 0)
224  {
225  if ($pgid == "") $pgid = $this->id;
226  $this->_initAllGroup();
227 
228  $groupsid = array();
229 
230  if ($this->allgroups) {
231  foreach ($this->allgroups as $k => $v) {
232  if ($v["iduser"] == $pgid) {
233  $gid = $v["idgroup"];
234  $groupsid[$gid] = $gid;
235  $this->levgid[$gid] = max($level, $this->levgid[$gid]);
236 
237  $groupsid+= $this->getParentsGroupId($gid, $level + 1);
238  }
239  }
240  }
241  return $groupsid;
242  }
243  /**
244  * get all parent (ascendant) group of this group
245  * @return array id
246  */
247  function getDirectParentsGroupId($pgid = "", &$uasid)
248  {
249  $this->levgid = array();
250  $this->getParentsGroupId($pgid);
251  //print_r2($this->levgid);
252  $groupsid = array();
253  asort($this->levgid);
254  foreach ($this->levgid as $k => $v) {
255  if ($v == 0) $groupsid[$k] = $k;
256  else $uasid[$k] = $k;
257  }
258  return $groupsid;
259  }
260  function _initAllGroup()
261  {
262  if (!isset($this->allgroups)) {
263  /* alone groups : not needed
264  $query = new QueryDb($this->dbaccess, "User");
265  $query->AddQuery("isgroup='Y'");
266  $list= $query->Query(0,0,"TABLE");
267 
268  foreach ($list as $v) {
269  $this->allgroups[$v["id"]]="";
270  }
271  */
272  $query = new QueryDb($this->dbaccess, "Group");
273  $list = $query->Query(0, 0, "TABLE", "select * from groups where iduser in (select id from users where isgroup='Y')");
274  if ($list) {
275  foreach ($list as $v) {
276  $this->allgroups[] = $v;
277  }
278  }
279  }
280  }
281 }
282 ?>
← centre documentaire © anakeen - published under CC License - Dynacase