Core  3.2
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  * @package FDL
5 */
6 /**
7  * User Group Definition
8  *
9  * @author Anakeen
10  * @version $Id: Class.Group.php,v 1.22 2007/03/12 08:25:55 eric Exp $
11  * @package FDL
12  * @subpackage CORE
13  */
14 /**
15  */
16 
17 include_once ('Class.DbObj.php');
18 include_once ('Class.QueryDb.php');
19 include_once ('Class.Log.php');
20 include_once ('Class.Application.php');
21 
22 class Group extends DbObj
23 {
24  var $fields = array(
25  "iduser",
26  "idgroup"
27  );
28 
29  var $id_fields = array(
30  "iduser"
31  );
32 
33  var $dbtable = "groups";
34 
35  var $sqlcreate = "
36 create table groups ( iduser int not null,
37  idgroup int not null);
38 create index groups_idx1 on groups(iduser);
39 create unique index groups_idx2 on groups(iduser,idgroup);
40 create trigger t_nogrouploop before insert or update on groups for each row execute procedure nogrouploop();";
41 
42  var $groups = array(); // user groups
43  public $iduser;
44  public $idgroup;
45 
46  protected $syncAccount = true;
47 
48  private $allgroups;
49  private $levgid;
50  /**
51  * get groups of a user
52  * set groups attribute. This attribute containt id of group of a user
53  * @return bool true if at least one group
54  */
55  function GetGroups()
56  {
57  $query = new QueryDb($this->dbaccess, "Group");
58 
59  $query->AddQuery("iduser='{$this->iduser}'");
60  $sql = sprintf("SELECT groups.idgroup as gid from groups, users where groups.idgroup=users.id and users.accounttype!='R' and groups.iduser=%d order by accounttype, lastname", $this->iduser);
61  simpleQuery($this->dbaccess, $sql, $groupIds, true, false);
62  $this->groups = $groupIds;
63 
64  return (count($groupIds) > 0);
65  }
66  /**
67  * suppress a user from the group
68  *
69  * @param int $uid user identifier to suppress
70  * @param bool $nopost set to to true to not perform postDelete methods
71  * @return string error message
72  */
73  function SuppressUser($uid, $nopost = false)
74  {
75  $err = "";
76 
77  if (($this->iduser > 0) && ($uid > 0)) {
78  $err = $this->exec_query("delete from groups where idgroup=" . $this->iduser . " and iduser=$uid");
79  $err.= $this->exec_query("delete from sessions where userid=$uid");
80 
81  $dbf = $this->dbaccess;
82  $g = new Group($dbf);
83  $err.= $g->exec_query("delete from groups where idgroup=" . $this->iduser . " and iduser=$uid");
84 
85  if (!$nopost) $this->PostDelete($uid);
86  }
87  return $err;
88  }
89  /**
90  * initialise groups for a user
91  */
92  function PostSelect($id)
93  {
94  $this->GetGroups();
95  }
96 
97  function preInsert()
98  {
99  // verify is exists
100  $err = $this->exec_query(sprintf("select * from groups where idgroup=%s and iduser=%s", $this->idgroup, $this->iduser));
101  if ($this->numrows() > 0) {
102  $err = "OK"; // just to say it is not a real error
103 
104  }
105  return $err;
106  }
107 
108  function PostDelete($uid = 0)
109  {
110  if ($uid) $u = new Account("", $uid);
111  else $u = new Account("", $this->iduser);
112  $u->updateMemberOf();
113  if ($u->accounttype != Account::USER_TYPE) {
114  // recompute all doc profil
115  $this->resetAccountMemberOf();
116  } else {
117  $dbf = $this->dbaccess;
118  $g = new Group($dbf);
119  $g->iduser = $this->iduser;
120  $g->idgroup = $this->idgroup;
121  $err = $g->exec_query("delete from groups where idgroup=" . $this->iduser . " and iduser=" . $u->id);
122  if ($err == "") {
123  // if it is a user (not a group)
124  $g->exec_query("delete from permission where computed");
125 
126  $p = new Permission($this->dbaccess);
127  $p->deletePermission($g->iduser, null, null, true);
128  }
129  }
130  }
131 
132  function PostInsert()
133  {
134  $err = $this->exec_query(sprintf("delete from sessions where userid=%d", $this->iduser));
135  // $this->FreedomCopyGroup();
136  $u = new Account("", $this->iduser);
137 
138  $u->updateMemberOf();
139 
140  if ($u->accounttype != Account::USER_TYPE) {
141  // recompute all doc profil
142  $this->resetAccountMemberOf();
143  } else {
144  $dbf = $this->dbaccess;
145  $g = new Group($dbf);
146  $g->iduser = $this->iduser;
147  $g->idgroup = $this->idgroup;
148  $err = $g->Add(true);
149  if ($err == "" || $err == "OK") {
150  // if it is a user (not a group)
151  $g->exec_query("delete from permission where computed");
152 
153  $p = new Permission($this->dbaccess);
154  $p->deletePermission($g->iduser, null, null, true);
155  $err = "";
156  }
157  }
158 
159  return $err;
160  }
161  /**
162  * @param boolean $syncAccount
163  */
164  public function setSyncAccount($syncAccount)
165  {
166  $this->syncAccount = $syncAccount;
167  }
168  /**
169  * recompute all memberof properties of user accounts
170  */
171  public function resetAccountMemberOf($synchro = false)
172  {
173  if ($this->syncAccount) {
174  $this->exec_query(sprintf("delete from sessions where userid=%d", $this->iduser));
175  $this->exec_query("delete from permission where computed");
176 
177  if ($synchro) {
178  simpleQuery($this->dbaccess, "select * from users order by id", $tusers);
179  $u = new Account($this->dbaccess);
180  foreach ($tusers as $tu) {
181  $u->affect($tu);
182  $u->updateMemberOf();
183  }
184  } else {
185  $wsh = getWshCmd();
186  $cmd = $wsh . " --api=initViewPrivileges --reset-account=yes";
187 
188  bgexec(array(
189  $cmd
190  ) , $result, $err);
191  }
192  }
193  }
194  /**
195  * get ascendant direct group and group of group
196  */
197  function GetAllGroups()
198  {
199  $allg = $this->groups;
200  foreach ($this->groups as $k => $gid) {
201  $og = new Group($this->dbaccess, $gid);
202  $allg = array_merge($allg, $og->GetAllGroups());
203  }
204  $allg = array_unique($allg);
205 
206  return $allg;
207  }
208  /**
209  * get all child (descendant) group of this group
210  * @return array id
211  */
212  function getChildsGroupId($pgid)
213  {
214  $this->_initAllGroup();
215 
216  $groupsid = array();
217 
218  if ($this->allgroups) {
219  foreach ($this->allgroups as $k => $v) {
220  if ($v["idgroup"] == $pgid) {
221  $uid = $v["iduser"];
222  $groupsid[$uid] = $uid;
223  // $groupsid=array_merge($groupsid, $this->getChildsGroup($v["iduser"]));
224  $groupsid+= $this->getChildsGroupId($uid);
225  }
226  }
227  }
228  return $groupsid;
229  }
230  /**
231  * get all parent (ascendant) group of this group
232  * @return array id
233  */
234  function getParentsGroupId($pgid, $level = 0)
235  {
236  $this->_initAllGroup();
237 
238  $groupsid = array();
239 
240  if ($this->allgroups) {
241  foreach ($this->allgroups as $k => $v) {
242  if ($v["iduser"] == $pgid) {
243  $gid = $v["idgroup"];
244  $groupsid[$gid] = $gid;
245  if (isset($this->levgid[$gid])) $this->levgid[$gid] = max($level, $this->levgid[$gid]);
246  else $this->levgid[$gid] = $level;
247 
248  $groupsid+= $this->getParentsGroupId($gid, $level + 1);
249  }
250  }
251  }
252  return $groupsid;
253  }
254  /**
255  * get all parent (ascendant) group of this group
256  * @return array id
257  */
258  function getDirectParentsGroupId($pgid = "", &$uasid)
259  {
260  $this->levgid = array();
261  $this->getParentsGroupId($pgid);
262  //print_r2($this->levgid);
263  $groupsid = array();
264  asort($this->levgid);
265  foreach ($this->levgid as $k => $v) {
266  if ($v == 0) $groupsid[$k] = $k;
267  else $uasid[$k] = $k;
268  }
269  return $groupsid;
270  }
271 
272  private function _initAllGroup()
273  {
274  if (!isset($this->allgroups)) {
275  $query = new QueryDb($this->dbaccess, "Group");
276  $list = $query->Query(0, 0, "TABLE", "select * from groups where iduser in (select id from users where accounttype='G')");
277  if ($list) {
278  foreach ($list as $v) {
279  $this->allgroups[] = $v;
280  }
281  }
282  }
283  }
284 }
exec_query($sql, $lvl=0, $prepare=false)
getParentsGroupId($pgid, $level=0)
preInsert()
Definition: Class.Group.php:97
$syncAccount
Definition: Class.Group.php:46
setSyncAccount($syncAccount)
SuppressUser($uid, $nopost=false)
Definition: Class.Group.php:73
resetAccountMemberOf($synchro=false)
GetGroups()
Definition: Class.Group.php:55
foreach($argv as $arg) $cmd
getChildsGroupId($pgid)
PostInsert()
PostDelete($uid=0)
getWshCmd($nice=false, $userid=0, $sudo=false)
Definition: Lib.Common.php:594
const USER_TYPE
PostSelect($id)
Definition: Class.Group.php:92
GetAllGroups()
getDirectParentsGroupId($pgid="", &$uasid)
if(($docid!==0)&&(!is_numeric($docid))) $query
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
bgexec($tcmd, &$result, &$err)
Definition: Lib.Common.php:621
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen