Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.RoleAccount.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Specials methods for Role family
8  *
9  */
10 namespace Dcp\Core;
12 {
13  /**
14  * @var \Account system role
15  */
16  protected $sysRole = null;
17 
18  public function PreCreated()
19  {
20  $this->lowerLogin();
21  $err = $this->userSynchronize();
22  return $err;
23  }
24 
25  public function PreUpdate()
26  {
27  parent::PreUpdate();
28  if ($this->isChanged()) {
29  $this->lowerLogin();
30  }
31  }
32  public function PostDelete()
33  {
34 
35  $role = $this->getAccount();
36  if ($role) $role->Delete();
37  }
38  public function preUndelete()
39  {
40  return _("role cannot be revived");
41  }
42  /**
43  * return concatenation of mail addresses
44  * @param bool $rawmail if true get mail address only else get mail address with name
45  * @return string
46  */
47  public function getMail($rawmail = false)
48  {
49  $wu = $this->getAccount();
50  if ($wu->isAffected()) {
51  return $wu->getMail($rawmail);
52  }
53  return '';
54  }
55  private function lowerLogin()
56  {
57  $login = $this->getRawValue("role_login");
58  if (mb_strtolower($login) != $login) {
59 
60  $this->setValue("role_login", mb_strtolower($login));
61  }
62  }
63  /**
64  * synchro with User table
65  *
66  * @return string error message, if no error empty string
67  */
68  public function postStore()
69  {
70  $err = $this->userSynchronize();
71  return $err;
72  }
73  /**
74  * update/create system role from document role
75  * @return string error message
76  */
77  public function userSynchronize()
78  {
79  $err = '';
80  if ($this->isAffected()) {
81  $sR = $this->getAccount();
82 
83  if (!$sR) {
84  // try create it
85  $sR = new \Account();
86  $sR->login = $this->getRawValue('role_login');
87  $sR->lastname = $this->getRawValue('role_name');
88  $sR->fid = $this->initid;
89  $sR->accounttype = \Account::ROLE_TYPE;
90  $sR->password_new = uniqid("role");
91  /** @noinspection PhpDeprecationInspection */
92  $sR->isgroup = 'N';
93  $err = $sR->add();
94  if ($err == "") {
95  $this->setValue("us_whatid", $sR->id);
96  $this->modify(true, array(
97  "us_whatid"
98  ) , true);
99  $this->refreshDocUser();
100  }
101  } else {
102  // update it
103  $sR->login = $this->getRawValue('role_login');
104  $sR->lastname = $this->getRawValue('role_name');
105  $sR->fid = $this->initid;
106  $err = $sR->modify();
107  }
108  }
109 
110  return $err;
111  }
112  /**
113  * recompute sytstem values from USER database
114  */
115  function refreshDocUser()
116  {
117  $wid = $this->getRawValue("us_whatid");
118  if ($wid > 0) {
119  $wuser = $this->getAccount(true);
120 
121  if ($wuser && $wuser->isAffected()) {
122  $this->SetValue("us_whatid", $wuser->id);
123  $this->SetValue("role_login", $wuser->login);
124  $this->SetValue("role_name", $wuser->lastname);
125  $this->modify(true, "", true);
126  }
127  }
128  }
129  /**
130  * return system user object conform to whatid
131  * @param bool $nocache
132  * @return \Account|null return null if not found
133  */
134  function getAccount($nocache = false)
135  {
136  if ($nocache) {
137  unset($this->sysRole); // needed for reaffect new values
138  $this->sysRole = null;
139  }
140  if (empty($this->sysRole)) {
141  $wid = $this->getRawValue("us_whatid");
142  if ($wid > 0) {
143  $this->sysRole = new \Account("", $wid);
144  }
145  }
146  if (!$this->sysRole) return null;
147  return $this->sysRole;
148  }
149  /**
150  * constraint to detect unique login
151  * @param $login
152  * @return string
153  */
154  public function isUniqueLogin($login)
155  {
156  $err = "";
157  $sql = sprintf("select id from users where login = '%s' and id != %d", mb_strtolower(pg_escape_string($login)) , $this->getRawValue("us_whatid"));
158  simpleQuery('', $sql, $id, true, true);
159 
160  if ($id) $err = sprintf(_("role %s id is already used") , $login);
161 
162  return $err;
163  }
164 }
isAffected()
modify($nopost=false, $sfields="", $nopre=false)
isChanged()
Definition: Class.Doc.php:853
setValue($attrid, $value, $index=-1, &$kvalue=null)
Definition: Class.Doc.php:3528
$login
Definition: dav.php:40
getAccount($nocache=false)
getMail($rawmail=false)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
if($file) if($subject==""&&$file) if($subject=="") $err
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
$initid
Definition: Class.Doc.php:361
const ROLE_TYPE
← centre documentaire © anakeen