Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
CheckProfil.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Checking document's profil
8  * @class CheckProfil
9  * @brief Check profil when importing definition
10  * @see ErrorCodePRFL
11  */
12 class CheckProfil extends CheckData
13 {
14  /**
15  * profil name
16  * @var string
17  */
18  private $prfName = '';
19  /**
20  * doc name
21  * @var string
22  */
23  private $docName = '';
24  /**
25  * profil doccument
26  * @var Doc
27  */
28  private $profil = '';
29  /**
30  * dynamic reference
31  * @var Doc
32  */
33  private $dynDoc = null;
34  /**
35  * access control list
36  * @var array
37  */
38  private $acls = array();
39  /**
40  * modifier
41  * @var string
42  */
43  private $modifier = '';
44 
45  private $defaultAccountType = '';
46  /**
47  * @var array
48  */
49  private $availablesModifier = array(
50  'reset',
51  'add',
52  'delete',
53  'set'
54  );
55 
56  private $availablesDefaultType = array(
57  ':useAccount',
58  ':useDocument',
59  ':useAttribute'
60  );
61  private $userIds = [];
62  /**
63  * @param array $data
64  * @return CheckProfil
65  */
66  function check(array $data, &$extra = null)
67  {
68 
69  if (!empty($data[2]) && !in_array($data[2], $this->availablesDefaultType)) {
70  $this->prfName = $data[2];
71  $this->docName = $data[1];
72  } else {
73  $this->defaultAccountType = isset($data[2]) ? trim($data[2]) : null;
74  $this->prfName = isset($data[1]) ? $data[1] : null;
75  for ($i = 4; $i < count($data); $i++) {
76  $this->acls[] = $data[$i];
77  }
78  }
79  if (isset($data[3])) $this->modifier = strtolower($data[3]);
80  $this->checkUnknow();
81  if (!$this->hasErrors()) {
82  $this->checkModifier();
83  $this->checkIsACompatibleProfil();
84  $this->checkAcls();
85  }
86 
87  return $this;
88  }
89 
90  private function checkUnknow()
91  {
92  if ($this->prfName) {
93  clearCacheDoc();
94  $this->profil = new_doc(getDbAccess() , $this->prfName);
95  if (!$this->profil->isAlive()) {
96  $this->addError(ErrorCode::getError('PRFL0002', $this->prfName));
97  }
98  } else {
99  $this->addError(ErrorCode::getError('PRFL0001'));
100  }
101  }
102 
103  private function checkIsACompatibleProfil()
104  {
105  if ($this->docName) {
106  $doc = new_doc(getDbAccess() , $this->docName);
107  if (!$doc->isAlive()) {
108  $this->addError(ErrorCode::getError('PRFL0003', $this->docName));
109  } else {
110  if ($doc->acls != $this->profil->acls) {
111  $this->addError(ErrorCode::getError('PRFL0004', $this->prfName, $this->docName));
112  }
113  }
114  }
115  }
116 
117  private function checkModifier()
118  {
119  if ($this->modifier) {
120  if (!in_array($this->modifier, $this->availablesModifier)) {
121  $this->addError(ErrorCode::getError('PRFL0005', $this->modifier, implode(', ', $this->availablesModifier)));
122  }
123  }
124  }
125 
126  private function checkAcls()
127  {
128  if (!$this->docName) {
129  $profAcls = $this->profil->acls;
130  $profAcls["viewacl"] = "viewacl"; // common special acl
131  $profAcls["modifyacl"] = "modifyacl";
132  foreach ($this->acls as $acl) {
133  if ($acl) {
134  if (preg_match("/([^=]+)=(.+)/", $acl, $reg)) {
135  $aclId = $reg[1];
136  $userId = $reg[2];
137  if (!in_array($aclId, $profAcls)) {
138 
139  $this->addError(ErrorCode::getError('PRFL0101', $aclId, $this->prfName, implode(',', $profAcls)));
140  }
141  $this->checkUsers(explode(',', $userId));
142  } else {
143  $this->addError(ErrorCode::getError('PRFL0100', $acl, $this->prfName));
144  }
145  }
146  }
147  }
148  }
149 
150  private function checkUsers(array $uids)
151  {
152  foreach ($uids as $uid) {
153  $uid = trim($uid);
154  if ($uid) {
155  if ($this->profil->getRawValue("dpdoc_famid")) {
156  if (!$this->checkAccount($uid)) {
157  $this->checkAttribute($uid);
158  }
159  } else {
160  if (!$this->checkAccount($uid)) {
161  $this->addError(ErrorCode::getError('PRFL0103', $uid, $this->prfName));
162  }
163  }
164  } else {
165  $this->addError(ErrorCode::getError('PRFL0102', $this->prfName));
166  }
167  }
168  }
169 
170  private function checkAccount($reference)
171  {
172  $findUser = false;
173  $this->extractAccount($reference, $type, $value);
174  switch ($type) {
175  case ':useAccount':
176  $findUser = $this->getUserIdFromLogin($value);
177  if (!$findUser) {
178  $this->addError(ErrorCode::getError('PRFL0104', $value, $this->prfName));
179  }
180  break;
181 
182  case ':useDocument':
183  $tu = getTDoc(getDbAccess() , $value);
184  if ($tu) {
185  $findUser = ($tu["us_whatid"] != '');
186  }
187  break;
188 
189  case ':useAttribute':
190  $this->checkAttribute($value);
191  $findUser = true;
192  break;
193 
194  default:
195  if (ctype_digit($reference)) {
196  $findUser = Account::getDisplayName($reference);
197  } else {
198  // search document
199  $tu = getTDoc(getDbAccess() , $reference);
200  if ($tu) {
201  $findUser = ($tu["us_whatid"] != '');
202  }
203  }
204  }
205 
206  return $findUser;
207  }
208 
209  private function extractAccount($reference, &$type, &$value)
210  {
211  if (preg_match('/^attribute\((.*)\)$/', $reference, $reg)) {
212  $type = ":useAttribute";
213  $value = strtolower(trim($reg[1]));
214  } elseif (preg_match('/^account\((.*)\)$/', $reference, $reg)) {
215  $type = ":useAccount";
216  $value = mb_strtolower(trim($reg[1]));
217  } elseif (preg_match('/^document\((.*)\)$/', $reference, $reg)) {
218  $type = ":useDocument";
219  $value = trim($reg[1]);
220  } else {
221  $value = $reference;
222  $type = $this->defaultAccountType;
223  }
224  }
225 
226  private function getUserIdFromLogin($login)
227  {
228  $login = mb_strtolower($login);
229  if (!isset($this->userIds[$login])) {
230  simpleQuery("", sprintf("select login from users where login='%s'", pg_escape_string($login)) , $uid, true, true);
231  $this->userIds[$uid] = $uid;
232  }
233  return $this->userIds[$login];
234  }
235  private function checkAttribute($aid)
236  {
237  $dynName = $this->profil->getRawValue("dpdoc_famid");
238  if (!$this->dynDoc) {
239  $this->dynDoc = new_doc(getDbAccess() , $dynName);
240  }
241  if (!$this->dynDoc->isAlive()) {
242  $this->addError(ErrorCode::getError('PRFL0203', $dynName, $this->prfName));
243  } else {
244  $aids = array_keys($this->dynDoc->getNormalAttributes());
245  $adocids = array();
246  foreach ($aids as $naid) {
247  $aType = $this->dynDoc->getAttribute($naid)->type;
248  $isuserOption = $this->dynDoc->getAttribute($naid)->getOption("isuser");
249  if (($aType == "docid" && $isuserOption == "yes") || ($aType == "account")) {
250  $adocids[] = $naid;
251  }
252  }
253  if (!in_array(strtolower($aid) , $aids)) {
254  $this->addError(ErrorCode::getError('PRFL0200', $aid, $this->prfName, implode(', ', $adocids)));
255  } else {
256  if (!in_array(strtolower($aid) , $adocids)) {
257  $this->addError(ErrorCode::getError('PRFL0201', $aid, $this->prfName, implode(', ', $adocids)));
258  }
259  }
260  }
261  }
262 }
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
Check profil when importing definition.
Definition: CheckProfil.php:12
clearCacheDoc($id=0)
check(array $data, &$extra=null)
Definition: CheckProfil.php:66
static getError($code, $args=null)
Definition: ErrorCode.php:27
$login
Definition: dav.php:40
static getDisplayName($uid)
addError($msg)
Definition: CheckData.php:29
getDbAccess()
Definition: Lib.Common.php:368
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
$value
$data
← centre documentaire © anakeen