ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilRoleMailboxSearch.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  protected ilDBInterface $db;
30 
31  public function __construct(
32  protected ilMailRfc822AddressParserFactory $parserFactory,
33  ?ilDBInterface $db = null
34  ) {
35  global $DIC;
36 
37  if (null === $db) {
38  $db = $DIC->database();
39  }
40  $this->db = $db;
41  }
42 
85  public function searchRoleIdsByAddressString(string $a_address_list): array
86  {
87  $parser = $this->parserFactory->getParser($a_address_list);
88  $parsedList = $parser->parse();
89 
90  $role_ids = [];
91  foreach ($parsedList as $address) {
92  $local_part = $address->getMailbox();
93  if (!str_starts_with($local_part, '#') && !($local_part[0] === '"' && $local_part[1] === "#")) {
94  // A local-part which doesn't start with a '#' doesn't denote a role.
95  // Therefore we can skip it.
96  continue;
97  }
98 
99  $local_part = substr($local_part, 1);
100 
101  /* If role contains spaces, eg. 'foo role', double quotes are added which have to be removed here.*/
102  if ($local_part[0] === '#' && $local_part[strlen($local_part) - 1] === '"') {
103  $local_part = substr($local_part, 1);
104  $local_part = substr($local_part, 0, -1);
105  }
106 
107  if (str_starts_with($local_part, 'il_role_')) {
108  $role_id = substr($local_part, 8);
109  $query = "SELECT t.tree " .
110  "FROM rbac_fa fa " .
111  "JOIN tree t ON t.child = fa.parent " .
112  "WHERE fa.rol_id = " . $this->db->quote($role_id, 'integer') . " " .
113  "AND fa.assign = 'y' " .
114  "AND t.tree = 1";
115  $res = $this->db->query($query);
116  if ($this->db->numRows($res) > 0) {
117  $role_ids[] = (int) $role_id;
118  }
119  continue;
120  }
121 
122  $domain = $address->getHost();
123  if (str_starts_with($domain, '[') && strrpos($domain, ']')) {
124  $domain = substr($domain, 1, -1);
125  }
126  if ($local_part === '') {
127  $local_part = $domain;
128  $address->setHost(ilMail::ILIAS_HOST);
129  $domain = ilMail::ILIAS_HOST;
130  }
131 
132  if (strtolower($address->getHost()) === ilMail::ILIAS_HOST) {
133  // Search for roles = local-part in the whole repository
134  $query = "SELECT dat.obj_id " .
135  "FROM object_data dat " .
136  "JOIN rbac_fa fa ON fa.rol_id = dat.obj_id " .
137  "JOIN tree t ON t.child = fa.parent " .
138  "WHERE dat.title =" . $this->db->quote($local_part, 'text') . " " .
139  "AND dat.type = 'role' " .
140  "AND fa.assign = 'y' " .
141  "AND t.tree = 1";
142  } else {
143  // Search for roles like local-part in objects = host
144  $query = "SELECT rdat.obj_id " .
145  "FROM object_data odat " .
146  "JOIN object_reference oref ON oref.obj_id = odat.obj_id " .
147  "JOIN tree otree ON otree.child = oref.ref_id " .
148  "JOIN rbac_fa rfa ON rfa.parent = otree.child " .
149  "JOIN object_data rdat ON rdat.obj_id = rfa.rol_id " .
150  "WHERE odat.title = " . $this->db->quote($domain, 'text') . " " .
151  "AND otree.tree = 1 " .
152  "AND rfa.assign = 'y' " .
153  "AND rdat.title LIKE " .
154  $this->db->quote(
155  '%' . preg_replace('/([_%])/', '\\\\$1', $local_part) . '%',
156  'text'
157  );
158  }
159  $res = $this->db->query($query);
160 
161  $count = 0;
162  while ($row = $this->db->fetchAssoc($res)) {
163  $role_ids[] = (int) $row['obj_id'];
164 
165  $count++;
166  }
167 
168  // Nothing found?
169  // In this case, we search for roles = host.
170  if ($count === 0 && strtolower($address->getHost()) === ilMail::ILIAS_HOST) {
171  $q = "SELECT dat.obj_id " .
172  "FROM object_data dat " .
173  "JOIN object_reference ref ON ref.obj_id = dat.obj_id " .
174  "JOIN tree t ON t.child = ref.ref_id " .
175  "WHERE dat.title = " . $this->db->quote($domain, 'text') . " " .
176  "AND dat.type = 'role' " .
177  "AND t.tree = 1 ";
178  $res = $this->db->query($q);
179 
180  while ($row = $this->db->fetchAssoc($res)) {
181  $role_ids[] = (int) $row['obj_id'];
182  }
183  }
184  }
185 
186  return $role_ids;
187  }
188 }
$res
Definition: ltiservices.php:66
const ILIAS_HOST
Class ilRoleMailboxSearch.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Class ilMailRfc822AddressParserFactory.
global $DIC
Definition: shib_login.php:22
searchRoleIdsByAddressString(string $a_address_list)
Finds all role ids that match the specified user friendly role mailbox address list.
__construct(protected ilMailRfc822AddressParserFactory $parserFactory, ?ilDBInterface $db=null)
$q
Definition: shib_logout.php:21