ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.shibUser.php
Go to the documentation of this file.
1 <?php
2 
20 
26 class shibUser extends ilObjUser
27 {
29 
35  public static function buildInstance(shibServerData $shibServerData): shibUser
36  {
37  $shib_user = new self();
38  $shib_user->setLastPasswordChangeToNow();
39  $shib_user->shibServerData = $shibServerData;
40  $ext_id = $shib_user->shibServerData->getLogin();
41  $shib_user->setExternalAccount($ext_id);
42  $existing_usr_id = self::getUsrIdByExtId($ext_id);
43  if ($existing_usr_id !== null) {
44  $shib_user->setId($existing_usr_id);
45  $shib_user->read();
46  }
47  $shib_user->setAuthMode('shibboleth');
48 
49  return $shib_user;
50  }
51 
52  public function updateFields(): void
53  {
54  $shibConfig = shibConfig::getInstance();
55  if ($shibConfig->getUpdateFirstname()) {
56  $this->setFirstname($this->shibServerData->getFirstname());
57  }
58  if ($shibConfig->getUpdateLastname()) {
59  $this->setLastname($this->shibServerData->getLastname());
60  }
61  if ($shibConfig->getUpdateGender()) {
62  $this->setGender($this->shibServerData->getGender());
63  }
64  if ($shibConfig->getUpdateTitle()) {
65  $this->setUTitle($this->shibServerData->getTitle());
66  }
67  if ($shibConfig->getUpdateInstitution()) {
69  }
70  if ($shibConfig->getUpdateDepartment()) {
71  $this->setDepartment($this->shibServerData->getDepartment());
72  }
73  if ($shibConfig->getUpdateStreet()) {
74  $this->setStreet($this->shibServerData->getStreet());
75  }
76  if ($shibConfig->getUpdateZipcode()) {
77  $this->setZipcode($this->shibServerData->getZipcode());
78  }
79  if ($shibConfig->getUpdateCountry()) {
80  $this->setCountry($this->shibServerData->getCountry());
81  }
82  if ($shibConfig->getUpdatePhoneOffice()) {
84  }
85  if ($shibConfig->getUpdatePhoneHome()) {
86  $this->setPhoneHome($this->shibServerData->getPhoneHome());
87  }
88  if ($shibConfig->getUpdatePhoneMobile()) {
90  }
91  if ($shibConfig->getUpdateFax()) {
92  $this->setFax($this->shibServerData->getFax());
93  }
94  if ($shibConfig->getUpdateMatriculation()) {
96  }
97  if ($shibConfig->getUpdateEmail()) {
98  $this->setEmail($this->shibServerData->getEmail());
99  }
100  if ($shibConfig->getUpdateHobby()) {
101  $this->setHobby($this->shibServerData->getHobby());
102  }
103  if ($shibConfig->getUpdateLanguage()) {
104  $this->setLanguage($this->shibServerData->getLanguage());
105  }
106  $this->setDescription($this->getEmail());
107  }
108 
109  public function createFields(): void
110  {
111  $this->setFirstname($this->shibServerData->getFirstname());
112  $this->setLastname($this->shibServerData->getLastname());
113  $this->setLogin($this->returnNewLoginName());
115  $this->setPasswd(md5((string) end($array)), ilObjUser::PASSWD_CRYPTED);
116  $this->setGender($this->shibServerData->getGender());
117  $this->setExternalAccount($this->shibServerData->getLogin());
118  $this->setUTitle($this->shibServerData->getTitle());
119  $this->setInstitution($this->shibServerData->getInstitution());
120  $this->setDepartment($this->shibServerData->getDepartment());
121  $this->setStreet($this->shibServerData->getStreet());
122  $this->setZipcode($this->shibServerData->getZipcode());
123  $this->setCountry($this->shibServerData->getCountry());
124  $this->setPhoneOffice($this->shibServerData->getPhoneOffice());
125  $this->setPhoneHome($this->shibServerData->getPhoneHome());
126  $this->setPhoneMobile($this->shibServerData->getPhoneMobile());
127  $this->setFax($this->shibServerData->getFax());
129  $this->setEmail($this->shibServerData->getEmail());
130  $this->setHobby($this->shibServerData->getHobby());
131  $this->setTitle($this->getFullname());
132  $this->setDescription($this->getEmail());
133  $this->setLanguage($this->shibServerData->getLanguage());
134  $this->setTimeLimitOwner(7);
135  $this->setTimeLimitUnlimited(1);
136  $this->setTimeLimitFrom(time());
137  $this->setTimeLimitUntil(time());
138  $this->setActive(true);
139  }
140 
144  public function create(): int
145  {
147  $registration_settings = new ilRegistrationSettings();
148  $recipients = array_filter($registration_settings->getApproveRecipients(), static fn($v): bool => is_int($v));
149  if ($recipients !== [] && $c->isActivateNew()) {
150  $this->setActive(false);
151  $mail = new ilRegistrationMailNotification();
153  $mail->setRecipients($registration_settings->getApproveRecipients());
154  $mail->setAdditionalInformation(['usr' => $this]);
155  $mail->send();
156  }
157 
158  if ($this->getLogin() !== '' && $this->getLogin() !== '.') {
159  return parent::create();
160  }
161 
162  throw new ilUserException('No Login-name created');
163  }
164 
165  protected function returnNewLoginName(): ?string
166  {
167  $login = substr($this->cleanName($this->getFirstname()), 0, 1) . '.' . $this->cleanName($this->getLastname());
168  //remove whitespaces see mantis 0023123: https://www.ilias.de/mantis/view.php?id=23123
169  $login = preg_replace('/\s+/', '', $login);
170  $appendix = null;
171  $login_tmp = $login;
172  while ($this->loginExists($login, $this->getId())) {
173  $login = $login_tmp . $appendix;
174  $appendix++;
175  }
176 
177  return $login;
178  }
179 
180  public function isNew(): bool
181  {
182  return $this->getId() === 0;
183  }
184 
185  protected function cleanName(string $name): string
186  {
187  $umlaut_map = [
188  'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue',
189  'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue',
190  'ß' => 'ss'
191  ];
192  $name = strtr($name, $umlaut_map);
193 
194  $form_d = new UTFNormal();
195  $name = $form_d->formD()->transform($name);
196  $name = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', (string) $name);
197 
198  return strtolower((string) preg_replace('/[^a-zA-Z0-9\s]/', '', $name));
199  }
200 
201  private function loginExists(string $login, int $usr_id): bool
202  {
203  global $DIC;
204 
205  $db = $DIC->database();
206 
207  $query = 'SELECT usr_id FROM usr_data WHERE login = ' . $db->quote($login, 'text');
208  $query .= ' AND usr_id != ' . $db->quote($usr_id, 'integer');
209 
210  return $db->numRows($db->query($query)) > 0;
211  }
212 
213  protected static function getUsrIdByExtId(string $ext_id): ?int
214  {
215  global $DIC;
216 
217  $db = $DIC->database();
218 
219  $query = 'SELECT usr_id FROM usr_data WHERE ext_account = ' . $db->quote($ext_id, 'text');
220  $a_set = $db->query($query);
221  if ($db->numRows($a_set) === 0) {
222  return null;
223  }
224 
225  $usr = $db->fetchObject($a_set);
226 
227  return ($usr !== null && isset($usr->usr_id)) ? (int) $usr->usr_id : null;
228  }
229 }
setUTitle(string $a_str)
set user title (note: don&#39;t mix up this method with setTitle() that is derived from ilObject and sets...
setTimeLimitUnlimited(bool $a_unlimited)
numRows(ilDBStatement $statement)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setMatriculation(string $a_str)
setPhoneOffice(string $a_str)
loginExists(string $login, int $usr_id)
shibServerData $shibServerData
getFullname(int $a_max_strlen=0)
quote($value, string $type)
setTitle(string $title)
$c
Definition: deliver.php:9
setTimeLimitUntil(?int $a_until)
setInstitution(string $a_str)
static getInstance()
setPasswd(string $a_str, string $a_type=ilObjUser::PASSWD_PLAIN)
setGender(string $a_str)
setExternalAccount(string $a_str)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setLogin(string $a_str)
setActive(bool $a_active, int $a_owner=0)
set user active state and updates system fields appropriately
setDepartment(string $a_str)
Class shibUser.
setPhoneHome(string $a_str)
ilDBInterface $db
global $DIC
Definition: shib_login.php:25
fetchObject(ilDBStatement $query_result)
query(string $query)
Run a (read-only) Query on the database.
setHobby(string $a_str)
static buildInstance(shibServerData $shibServerData)
static getUsrIdByExtId(string $ext_id)
setCountry(string $a_str)
setEmail(string $a_str)
setFax(string $a_str)
const PASSWD_CRYPTED
static generatePasswords(int $a_number)
Generate a number of passwords.
setPhoneMobile(string $a_str)
setLastname(string $a_str)
setStreet(string $a_str)
Class ilObjAuthSettingsGUI.
setLanguage(string $a_str)
setTimeLimitOwner(int $a_owner)
setLastPasswordChangeToNow()
setZipcode(string $a_str)
setTimeLimitFrom(?int $a_from)
setFirstname(string $a_str)
cleanName(string $name)
setDescription(string $description)