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