Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.SearchAccount.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Search Account : User / Group / Role
8  *
9  * @author Anakeen
10  * @package FDL
11  */
12 
13 include_once ("FDL/Lib.Dir.php");
14 /**
15  * @class SearchAccount
16  * @code
17  $s = new SearchAccount();
18  $s->addRoleFilter($s->getLoginFromDocName('TST_ROLEWRITTER'));
19  $s->addGroupFilter("all");
20  $s->addFilter("mail ~ '%s'", "test");
21  $al = $s->search();
22  foreach ($al as $account) {
23  printf("%s => %s\n ", $account->login, $account->mail);
24  }
25  * @endcode
26  */
28 {
29  /**
30  * user type filter
31  */
32  const userType = 0x01;
33  /**
34  * group type filter
35  */
36  const groupType = 0x02;
37  /**
38  * role type filter
39  */
40  const roleType = 0x04;
41  /**
42  * AccountList type return
43  */
44  const returnAccount = 1;
45  /**
46  * DocumentList type return
47  */
48  const returnDocument = 2;
49 
50  private $returnType = self::returnAccount;
51  private $roleFilters = array();
52  private $groupFilters = array();
53  private $searchResult = array();
54  private $dbaccess;
55  private $filters = array();
56  private $order = 'login';
57  private $slice = 'ALL';
58  private $start = 0;
59  private $familyFilter = null;
60 
61  private $returnUser = true;
62  private $returnGroup = true;
63  private $returnRole = true;
64  private $viewControl = false;
65 
66  public function __construct()
67  {
68  $this->dbaccess = getDbAccess();
69  }
70  /**
71  * add role filter appartenance
72  * @api add role filter appartenance
73  * @param string $role role reference (login)
74  * @throws Dcp\Sacc\Exception
75  */
76  public function addRoleFilter($role)
77  {
78  $roles = explode(' ', $role);
79  foreach ($roles as $aRole) {
80  $aRole = trim($aRole);
81  if ($aRole) {
82  $sql = sprintf("select id from users where accounttype='R' and login='%s'", pg_escape_string(mb_strtolower($aRole)));
83  simpleQuery($this->dbaccess, $sql, $result, true, true);
84  if (!$result) {
85  throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0002", $aRole));
86  }
87  $this->roleFilters[] = $result;
88  }
89  }
90  }
91  /**
92  * add group filter appartenance
93  * @api add group filter appartenance
94  * @param string $group group name (login)
95  * @throws Dcp\Sacc\Exception
96  */
97  public function addGroupFilter($group)
98  {
99  $groups = explode(' ', $group);
100  foreach ($groups as $aGroup) {
101  $aGroup = trim($aGroup);
102  if ($aGroup) {
103  $sql = sprintf("select id from users where accounttype='G' and login='%s'", pg_escape_string(mb_strtolower($aGroup)));
104  simpleQuery($this->dbaccess, $sql, $result, true, true);
105  if (!$result) {
106  throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0005", $aGroup));
107  }
108  $this->groupFilters[] = $result;
109  }
110  }
111  }
112  /**
113  * set account type filter (only matching accounts will be returned)
114  * @api set account type filter (only matching accounts will be returned)
115  * @code
116  * $s->setTypeFilter($s::userType | $s::groupType);
117  * @endcode
118  * @param int $type can be bitmask of SearchAccount::userType, SearchAccount::groupType,SearchAccount::roleType
119  */
120  public function setTypeFilter($type)
121  {
122 
123  $this->returnUser = ($type & self::userType) == self::userType;
124  $this->returnGroup = ($type & self::groupType) == self::groupType;
125  $this->returnRole = ($type & self::roleType) == self::roleType;
126  }
127  /**
128  * add sql filter about Account properties
129  * @api add sql filter about Account properties
130  * @code
131  * $s->addFilter("mail ~ '%s'", $mailExpr);
132  * @endcode
133  * @param string $filter sql filter
134  * @param string $arg optional arguments
135  */
136  public function addFilter($filter, $arg = null)
137  {
138  if ($filter != "") {
139  $args = func_get_args();
140  if (count($args) > 1) {
141  $fs[0] = $args[0];
142  for ($i = 1; $i < count($args); $i++) {
143  $fs[] = pg_escape_string($args[$i]);
144  }
145  $filter = call_user_func_array("sprintf", $fs);
146  }
147 
148  $this->filters[] = $filter;
149  }
150  }
151  /**
152  * set order can be login, mail, id, firstname,… each Account properties
153  * @api set order can be login, mail, id, firstname,… each Account properties
154  * @param string $order
155  */
156  public function setOrder($order)
157  {
158  $this->order = $order;
159  }
160  /**
161  * set slice limit / "all" for no limit
162  * @api set slice limit / "all" for no limit
163  * @param int|string $slice
164  * @throws Dcp\Sacc\Exception
165  */
166  public function setSlice($slice)
167  {
168  if (((!is_numeric($slice)) && (strtolower($slice) != 'all')) || ($slice < 0)) {
169  throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0003", $slice));
170  }
171  if (is_numeric($slice)) $this->slice = intval($slice);
172  else $this->slice = $slice;
173  }
174  /**
175  * set start offset
176  * @api set start offset
177  * @param int $start
178  * @throws Dcp\Sacc\Exception
179  */
180  public function setStart($start)
181  {
182  if ((!is_numeric($start)) || ($start < 0)) {
183  throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0004", $start));
184  }
185  $this->start = intval($start);
186  }
187 
188  /**
189  * set if use view control document's privilege to filter account
190  *
191  * @deprecated use {@link SearchAccount::overrideViewControl} instead. Be carefull: this is the opposite!
192  * @see SearchAccount::overrideViewControl
193  *
194  * @param bool $control
195  */
196  public function useViewControl($control = true)
197  {
199  $this->overrideViewControl(!$control);
200  }
201 
202  /**
203  * include accounts the user cannot view
204  * @api include accounts the user cannot view
205  * @param bool $override
206  */
207  public function overrideViewControl($override = true)
208  {
209  $this->viewControl = !$override;
210  }
211 
212  /**
213  * set object type return by ::search method
214  *
215  * @deprecated use {@link SearchAccount::setReturnType} instead
216  * @see SearchAccount::setReturnType
217  *
218  * @param string $type self::returnDocument or self::returnAccount
219  * @throws Dcp\Sacc\Exception
220  */
221  public function setObjectReturn($type)
222  {
223  if ($type != self::returnAccount && $type != self::returnDocument) {
224  throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0001", $type));
225  }
226  $this->returnType = $type;
227  }
228  /**
229  * set object type return by ::search method
230  *
231  * @api set object type return by ::search method
232  *
233  * @param string $type self::returnDocument or self::returnAccount
234  * @throws Dcp\Sacc\Exception
235  */
236  public function setReturnType($type)
237  {
238  if ($type != self::returnAccount && $type != self::returnDocument) {
239  throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0001", $type));
240  }
241  $this->returnType = $type;
242  }
243 
244  /**
245  * convert logical name document to login account
246  *
247  * @static
248  * @deprecated use {@link SearchAccount::getLoginFromDocName} instead
249  * @see SearchAccount::getLoginFromDocName
250  *
251  * @param string $name lolgical name
252  * @return string login , null if not found
253  */
254  public static function docName2login($name)
255  {
257  return self::getLoginFromDocName($name);
258  }
259  /**
260  * get login account from logical name document
261  *
262  * @static
263  * @api get login account from logical name document
264  *
265  * @param string $name logical name
266  * @return string|bool login , false if not found
267  */
268  public static function getLoginFromDocName($name)
269  {
270  $sql = sprintf("select login from docname, users where docname.id = users.fid and docname.name='%s'", pg_escape_string($name));
271  simpleQuery('', $sql, $login, true, true);
272  return $login;
273  }
274 
275  /**
276  * @param string $family
277  * @throws Dcp\Sacc\Exception if $family is not a valid family name
278  */
279  public function filterFamily($family)
280  {
281  if (!is_numeric($family)) {
282  $famId = getFamIdFromName($this->dbaccess, $family);
283  if (!$famId) throw new Dcp\Sacc\Exception(ErrorCode::getError("SACC0006", $family));
284  $this->familyFilter = $famId;
285  } else {
286  $this->familyFilter = $family;
287  }
288  }
289  /**
290  * send search of account's object
291  * @api send search of account's object
292  * @return DocumentList|AccountList
293  */
294  public function search()
295  {
296  simpleQuery($this->dbaccess, $this->getQuery() , $this->searchResult);
297  if ($this->returnType == self::returnAccount) {
298  $al = new AccountList($this->searchResult);
299  return $al;
300  } else {
301  $ids = array();
302  foreach ($this->searchResult as $account) {
303  if ($account["fid"]) $ids[] = $account["fid"];
304  }
305  $dl = new DocumentList();
306 
307  $dl->addDocumentIdentifiers($ids);
308  return $dl;
309  }
310  }
311  /**
312  * get sql par to filter group or role
313  * @return string
314  */
315  private function getgroupRoleFilter()
316  {
317  $rids = array_merge($this->roleFilters, $this->groupFilters);
318  if ($rids) {
319  $filter = sprintf("memberof && '{%s}'", implode(',', $rids));
320  return $filter;
321  } else {
322  return "true";
323  }
324  }
325  /**
326  * get final query to search accounts
327  * @return string
328  */
329  public function getQuery()
330  {
331 
332  $groupRoleFilter = $this->getgroupRoleFilter();
333 
334  $u = getCurrentUser();
335  if ($this->viewControl && $u->id != 1) {
336  $viewVector = SearchDoc::getUserViewVector($u->id);
337  if ($this->familyFilter) {
338  $table = "doc" . $this->familyFilter;
339  $sql = sprintf("select users.* from users, $table where users.fid = $table.id and $table.views && '%s' and %s ", $viewVector, $groupRoleFilter);
340  } else {
341  $sql = sprintf("select users.* from users, docread where users.fid = docread.id and docread.views && '%s' and %s ", $viewVector, $groupRoleFilter);
342  }
343  } else {
344  if ($this->familyFilter) {
345  $table = "doc" . $this->familyFilter;
346  $sql = sprintf("select users.* from users, $table where users.fid = $table.id and %s ", $groupRoleFilter);
347  } else {
348  $sql = sprintf("select * from users where %s ", $groupRoleFilter);
349  }
350  }
351  foreach ($this->filters as $aFilter) {
352  $sql.= sprintf(" and (%s) ", $aFilter);
353  }
354 
355  if ((!$this->returnUser) || (!$this->returnGroup) || (!$this->returnRole)) {
356  $fa = array();
357  if ($this->returnUser) $fa[] = "accounttype='U'";
358  if ($this->returnGroup) $fa[] = "accounttype='G'";
359  if ($this->returnRole) $fa[] = "accounttype='R'";
360  if ($fa) $sql.= sprintf(" and (%s)", implode(' or ', $fa));
361  }
362 
363  if ($this->order) $sql.= sprintf(" order by %s", pg_escape_string($this->order));
364  $sql.= sprintf(" offset %d limit %s", $this->start, pg_escape_string($this->slice));
365 
366  return $sql;
367  }
368 }
369 ?>
static docName2login($name)
if(substr($wsh, 0, 1)!= '/') $args
$s slice
addFilter($filter, $arg=null)
static getError($code, $args=null)
Definition: ErrorCode.php:27
useViewControl($control=true)
overrideViewControl($override=true)
$login
Definition: dav.php:40
getFamIdFromName($dbaccess, $name)
deprecatedFunction($msg= '')
Definition: Lib.Common.php:86
getCurrentUser()
Definition: Lib.Common.php:250
$s start
getDbAccess()
Definition: Lib.Common.php:368
$account
Definition: guest.php:36
static getUserViewVector($uid)
static getLoginFromDocName($name)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
← centre documentaire © anakeen