Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.GroupAccount.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Group account
8  */
9 namespace Dcp\Core;
10 
11 use Dcp\AttributeIdentifiers\Igroup as MyAttributes;
12 /**
13  * Class GroupAccount
14  *
15  * @method \Account getAccount($a=false)
16  * @method array getSystemIds($a)
17  * @method string setGroups
18  */
20 {
21  public $wuser;
22 
23  var $cviews = array(
24  "FUSERS:FUSERS_IGROUP"
25  );
26  var $eviews = array(
27  "USERCARD:CHOOSEGROUP"
28  );
29  var $exportLdap = array(
30  // posixGroup
31  "gidNumber" => "GRP_GIDNUMBER",
32  // "mail" => "GRP_MAIL", // not in schema but used in mailing client application
33  "description" => "GRP_DESC"
34  );
35  var $ldapobjectclass = "posixGroup";
36  function preRefresh()
37  {
38  // $err=$this->ComputeGroup();
39  $err = "";
40  $this->AddParamRefresh("US_WHATID", "GRP_MAIL,US_LOGIN");
41  // refresh MEID itself
42  $iduser = $this->getRawValue("US_WHATID");
43  if ($iduser > 0) {
44  $user = $this->getAccount();
45  if (!$user) return sprintf(_("group #%d does not exist") , $iduser);
46  } else {
47  return _("group has not identificator");
48  }
49 
50  if ($this->getRawValue("grp_isrefreshed") == "0") $err.= _("this groups must be refreshed");
51  return $err;
52  }
53  public function preUndelete()
54  {
55  return _("group cannot be revived");
56  }
57  /**
58  * test if the document can be set in LDAP
59  */
60  function canUpdateLdapCard()
61  {
62  return true;
63  }
64  /**
65  * get LDAP title for group
66  */
67  function getLDAPTitle()
68  {
69  return sprintf(_("%s group") , $this->title);
70  }
71  /**
72  * get LDAP array of members
73  * @return array
74  */
75  function getLDAPMember()
76  {
77  $g = $this->getAccount();
78  $members = $g->getAllMembers();
79  $tdn = array();
80  foreach ($members as $k => $v) {
81  $du = getTDoc($this->dbaccess, $v["fid"]);
82  $tdnu = explode("\n", $du["ldapdn"]);
83  if (count($tdnu) > 0) {
84  $dnu = $tdnu[0];
85  if ($dnu) $tdn[] = $dnu;
86  }
87  }
88  if (count($tdn) == 0) $tdn = "cn=nobody,dc=users," . $this->racine;
89  return $tdn;
90  }
91  /**
92  * recompute only parent group
93  * call {@see ComputeGroup()}
94  * @apiExpose
95  *
96  * @return string error message, if no error empty string
97  */
98  function RefreshGroup()
99  {
100  //if ($this->norefreshggroup) return '';
101  include_once ("FDL/Lib.Usercard.php");
102  // $err=_GROUP::RefreshGroup();
103  $err = $this->RefreshDocUser();
104  //$err.=$this->refreshMembers();
105  // refreshGroups(array($this->getRawValue("us_whatid")));
106  $err.= $this->insertGroups();
107  $err.= $this->Modify();
108  //AddWarningMsg(sprintf("RefreshGroup %d %s",$this->id, $this->title));
109  if ($err == "") {
110  refreshGroups(array(
111  $this->getRawValue("us_whatid")
112  ) , true);
113  /*$this->setValue("grp_isrefreshed","1");
114  $this->modify(true,array("grp_isrefreshed"),true);*/
115  }
116  return $err;
117  }
118  /**
119  * Refresh folder parent containt
120  */
121  function refreshParentGroup()
122  {
123  $tgid = $this->getMultipleRawValues("GRP_IDPGROUP");
124  foreach ($tgid as $gid) {
125  /**
126  * @var \Dcp\Family\Igroup $gdoc
127  */
128  $gdoc = new_Doc($this->dbaccess, $gid);
129  if ($gdoc->isAlive()) {
130  $gdoc->insertGroups();
131  }
132  }
133  }
134  public function postStore()
135  {
136  return $this->synchronizeSystemGroup();
137  }
138  /**
139  * @deprecated use postStore() instead
140  * @return string
141  */
142  public function postModify()
143  {
145  return self::postStore();
146  }
147  public function synchronizeSystemGroup()
148  {
149  $gname = $this->getRawValue("GRP_NAME");
150  $login = $this->getRawValue("US_LOGIN");
151  $roles = $this->getMultipleRawValues("grp_roles");
152 
153  $fid = $this->id;
154  /**
155  * @var \Account $user
156  */
157  $user = $this->getAccount();
158  if (!$user) {
159  $user = new \Account(""); // create new user
160  $this->wuser = & $user;
161  }
162  // get system role ids
163  $roleIds = $this->getSystemIds($roles);
164  $err = $user->SetGroups($fid, $gname, $login, $roleIds);
165  if ($err == "") {
166  $this->setValue(MyAttributes::us_whatid, $user->id);
167  $this->setValue(MyAttributes::us_meid, $this->id);
168  $this->modify(false, array(
169  MyAttributes::us_whatid,
170  MyAttributes::us_meid
171  ));
172  if ($user) {
173  $this->setGroups();
174  }
175  // get members
176  //$this->RefreshGroup(); // in postinsert
177  // $this->refreshParentGroup();
178  $wrg = $this->RefreshLdapCard();
179  if ($wrg) AddWarningMsg($wrg);
180  // add in default folder root groups : usefull for import
181  $tgid = $this->getMultipleRawValues("GRP_IDPGROUP");
182  $fdoc = $this->getFamilyDocument();
183  $dfldid = $fdoc->dfldid;
184  if ($dfldid != "") {
185  /**
186  * @var \Dir $dfld
187  */
188  $dfld = new_doc($this->dbaccess, $dfldid);
189  if ($dfld->isAlive()) {
190  if (count($tgid) == 0) $dfld->insertDocument($this->initid);
191  else $dfld->removeDocument($this->initid);
192  }
193  }
194 
195  $err = $this->refreshMailMembersOnChange();
196  }
197 
198  if ($err == "") $err = "-"; // don't do modify after because it is must be set by USER::setGroups
199  return $err;
200  }
201  /**
202  * compute the mail of the group
203  * concatenation of each user mail and group member mail
204  *
205  * @param bool $nomail if true no mail will be computed
206  * @return string error message, if no error empty string
207  */
208  public function setGroupMail($nomail = false)
209  {
210  if (!$nomail) $nomail = ($this->getRawValue("grp_hasmail") == "no");
211  if (!$nomail) {
212  $this->setValue("grp_mail", $this->getMail());
213  } else {
214  $this->clearValue('grp_mail');
215  }
216  }
217  /**
218  * return concatenation of mail addresses
219  * @param bool $rawmail if true only raw address will be returned else complete address with firstname and lastname are returned
220  * @return string
221  */
222  public function getMail($rawmail = false)
223  {
224  $wu = $this->getAccount();
225  if ($wu->isAffected()) {
226  return $wu->getMail($rawmail);
227  }
228  return '';
229  }
230  /**
231  * update LDAP menbers after imodification of containt
232  */
233  function specPostInsert()
234  {
235  return $this->RefreshLdapCard();
236  }
237  /**
238  * update groups table in USER database
239  * @param int $docid
240  * @param bool $multiple
241  * @return string error message
242  */
243  function postInsertDocument($docid, $multiple = false)
244  {
245  $err = "";
246  if ($multiple == false) {
247  $gid = $this->getRawValue("US_WHATID");
248  if ($gid > 0) {
249  /**
250  * @var \Dcp\Family\Iuser $du
251  */
252  $du = new_Doc($this->dbaccess, $docid);
253  $uid = $du->getRawValue("us_whatid");
254  if ($uid > 0) {
255  $g = new \Group("", $uid);
256  $g->iduser = $uid;
257  $g->idgroup = $gid;
258  $err = $g->Add();
259  if ($err == "OK") $err = "";
260  if ($err == "") {
261  $du->disableEditControl();
262  $du->RefreshDocUser(); // to refresh group of user attributes
263  $du->enableEditControl();
264  $this->RefreshGroup();
265  }
266  }
267  }
268  }
269  return $err;
270  }
271  /**
272  * update groups table in USER database
273  * @param array $tdocid
274  * @return string error message
275  */
276  function postInsertMultipleDocuments($tdocid)
277  {
278 
279  $err = "";
280 
281  $gid = $this->getRawValue("US_WHATID");
282  if ($gid > 0) {
283 
284  $g = new \Group("");
285  foreach ($tdocid as $k => $docid) {
286  /**
287  * @var \Dcp\Family\Iuser $du
288  */
289  $du = new_Doc($this->dbaccess, $docid);
290  $uid = $du->getRawValue("us_whatid");
291  if ($uid > 0) {
292  $g->iduser = $uid;
293  $g->idgroup = $gid;
294  $err = $g->Add();
295  if ($err == "") {
296  $du->disableEditControl();
297  $du->RefreshDocUser();
298  $du->enableEditControl();
299  }
300  }
301  }
302 
303  $this->RefreshGroup();
304  }
305  return $err;
306  }
307  /**
308  * update groups table in USER database before suppress
309  * @param int $docid
310  * @param bool $multiple
311  * @return string error message
312  */
313  function postRemoveDocument($docid, $multiple = false)
314  {
315 
316  $err = "";
317  $gid = $this->getRawValue("US_WHATID");
318  if ($gid > 0) {
319  /**
320  * @var \Dcp\Family\Iuser $du
321  */
322  $du = new_Doc($this->dbaccess, $docid);
323  $uid = $du->getRawValue("us_whatid");
324  if ($uid > 0) {
325  $g = new \Group("", $gid);
326  $g->iduser = $gid;
327  $err = $g->SuppressUser($uid);
328  if ($err == "") {
329  $du->disableEditControl();
330  $du->RefreshDocUser();
331  $du->enableEditControl();
332  $this->RefreshGroup();
333  }
334  }
335  }
336  return $err;
337  }
338  function postDelete()
339  {
340 
341  $gAccount = $this->getAccount();
342  if ($gAccount) $gAccount->Delete();
343  }
344  /**
345  * (re)insert members of the group in folder from USER databasee
346  *
347  * @return string error message, if no error empty string
348  */
349  function insertGroups()
350  {
351  $gAccount = $this->getAccount();
352  $err = "";
353  // get members
354  $tu = $gAccount->GetUsersGroupList($gAccount->id);
355 
356  if (is_array($tu)) {
357  parent::Clear();
358  $tfid = array();
359  foreach ($tu as $k => $v) {
360  // if ($v["fid"]>0) $err.=$this->AddFile($v["fid"]);
361  if ($v["fid"] > 0) $tfid[] = $v["fid"];
362  }
363  $err = $this->QuickInsertMSDocId($tfid); // without postInsert
364  $this->specPostInsert();
365  }
366  return $err;
367  }
368  /**
369  * insert members in a group in folder
370  * it does not modify anakeen database (use only when anakeen database if updated)
371  * must be use after a group add in anakeen database (use only for optimization in ::setGroups
372  *
373  * @param int $docid user doc parameter
374  * @return string error message, if no error empty string
375  */
377  {
378  $err = $this->insertDocument($docid, "latest", true); // without postInsert
379  $this->setValue("grp_isrefreshed", "0");
380  $this->modify(true, array(
381  "grp_isrefreshed"
382  ) , true);
383 
384  return $err;
385  }
386  /**
387  * suppress members of the group in folder
388  * it does not modify anakeen database (use only when anakeen database if updated)
389  * must be use after a group add in anakeen database (use only for optimization in ::setGroups
390  *
391  * @param int $docid user doc parameter
392  * @return string error message, if no error empty string
393  */
395  {
396  $err = $this->removeDocument($docid, true); // without postInsert
397  $this->setValue("grp_isrefreshed", "0");
398  $this->modify(true, array(
399  "grp_isrefreshed"
400  ) , true);
401 
402  return $err;
403  }
404  /**
405  * recompute intranet values from USER database
406  */
407  function refreshDocUser()
408  {
409  $err = "";
410  $wid = $this->getRawValue("us_whatid");
411  if ($wid > 0) {
412  $wuser = $this->getAccount(true);
413  if ($wuser->isAffected()) {
414  $this->setValue("US_WHATID", $wuser->id);
415  $this->setValue("GRP_NAME", $wuser->lastname);
416  // $this->setValue("US_FNAME",$wuser->firstname);
417  $this->setValue("US_LOGIN", $wuser->login);
418 
419  $this->setValue("US_MEID", $this->id);
420  // search group of the group
421  $g = new \Group("", $wid);
422  $tglogin = $tgid = array();
423  if (count($g->groups) > 0) {
424  foreach ($g->groups as $gid) {
425  $gt = new \Account("", $gid);
426  $tgid[$gid] = $gt->fid;
427  $tglogin[$gid] = $this->getTitle($gt->fid);
428  }
429  $this->setValue("GRP_IDPGROUP", $tgid);
430  } else {
431  $this->setValue("GRP_IDPGROUP", " ");
432  }
433  $this->setValue("grp_roles", $wuser->getRoles(false));
434  $err = $this->modify(true, array(
435  "us_whatid",
436  "grp_name",
437  "grp_roles",
438  "us_login",
439  "us_meid",
440  "grp_idgroup"
441  ));
442  } else {
443  $err = sprintf(_("group %d does not exist") , $wid);
444  }
445  }
446  return $err;
447  }
448  /**
449  * refresh members of the group from USER database
450  */
451  function refreshMembers()
452  {
453  $err = '';
454 
455  $wid = $this->getRawValue("us_whatid");
456  if ($wid > 0) {
457  $u = $this->getAccount(true);
458 
459  $tu = $u->GetUsersGroupList($wid, true);
460  $tglogin = '';
461  if (count($tu) > 0) {
462 
463  foreach ($tu as $uid => $tvu) {
464  if ($tvu["accounttype"] == \Account::GROUP_TYPE) {
465  $tgid[$uid] = $tvu["fid"];
466  // $tglogin[$uid]=$this->getTitle($tvu["fid"]);
467  $tglogin[$tvu["fid"]] = $tvu["lastname"];
468  }
469  }
470  }
471 
472  if (is_array($tglogin)) {
473  uasort($tglogin, "strcasecmp");
474  $this->setValue("GRP_IDGROUP", array_keys($tglogin));
475  } else {
476  $this->clearValue("GRP_IDGROUP");
477  }
478 
479  $err = $this->modify();
480  }
481  return $err;
482  }
483  /**
484  * Flush/empty group's content
485  */
486  function clear()
487  {
488  $err = '';
489  $content = $this->getContent(false);
490  if (is_array($content)) {
491  foreach ($content as $tdoc) {
492  $err.= $this->removeDocument($tdoc['id']);
493  }
494  }
495  return $err;
496  }
497 }
$tdoc
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
postInsertDocument($docid, $multiple=false)
getFamilyDocument()
Definition: Class.Doc.php:1482
clearValue($attrid)
Definition: Class.Doc.php:4409
count($onlyprimary=false)
Definition: Class.Dir.php:916
refreshGroups($groupIdList, $refresh=false, &$currentPath=array(), &$groupDepth=array())
const GROUP_TYPE
QuickInsertMSDocId($tdocids)
Definition: Class.Dir.php:584
getContent($controlview=true, array $filter=array(), $famid="", $qtype="TABLE", $trash="")
Definition: Class.Dir.php:895
modify($nopost=false, $sfields="", $nopre=false)
removeDocument($docid, $noprepost=false, $nocontrol=false)
Definition: Class.Dir.php:673
$docid
Definition: cleanFamily.php:13
$tgid
setValue($attrid, $value, $index=-1, &$kvalue=null)
Definition: Class.Doc.php:3528
getMultipleRawValues($idAttr, $def="", $index=-1)
Definition: Class.Doc.php:3240
$login
Definition: dav.php:40
getTitle($id="-1", $def="", $latest=false)
Definition: Class.Doc.php:8715
deprecatedFunction($msg= '')
Definition: Lib.Common.php:86
new_Doc($dbaccess, $id= '', $latest=false)
insertDocument($docid, $mode="latest", $noprepost=false, $forcerestrict=false, $nocontrol=false)
Definition: Class.Dir.php:315
if($file) if($subject==""&&$file) if($subject=="") $err
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
postRemoveDocument($docid, $multiple=false)
← centre documentaire © anakeen