ILIAS  trunk Revision v11.0_alpha-1749-g1a06bdef097
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
User.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use ilObjUser;
28 use Closure;
31 use ilAuthUtils;
34 
35 class User
36 {
38  private readonly Closure $matching_document;
39 
40  public function __construct(
41  private readonly ilObjUser $user,
42  private readonly Settings $settings,
43  private readonly UserSettings $user_settings,
44  private readonly Provide $legal_documents,
45  private readonly Clock $clock
46  ) {
47  $this->matching_document = $this->lazy(fn() => $this->legal_documents->document()->chooseDocumentFor($this->user));
48  }
49 
50  public function isLoggedIn(): bool
51  {
52  return !in_array($this->user->getId(), [ANONYMOUS_USER_ID, 0], true);
53  }
54 
55  public function cannotAgree(): bool
56  {
57  return in_array($this->user->getId(), [ANONYMOUS_USER_ID, SYSTEM_USER_ID, 0], true);
58  }
59 
60  public function neverAgreed(): bool
61  {
62  return null === $this->agreeDate()->value();
63  }
64 
68  public function withdrawalRequested(): Setting
69  {
70  return $this->user_settings->withdrawalRequested();
71  }
72 
76  public function agreeDate(): Setting
77  {
78  return $this->user_settings->agreeDate();
79  }
80 
81  public function didNotAcceptCurrentVersion(): bool
82  {
83  $false = fn() => new Ok(false);
84  return $this->settings->validateOnLogin()->value() && $this->matchingDocument()->map($this->didNotAccept(...))->except($false)->value();
85  }
86 
87  public function needsToAcceptNewDocument(): bool
88  {
89  if ($this->neverAgreed()) {
90  return true;
91  }
92 
93  $true = fn() => new Ok(true);
94  $db = $this->legal_documents->history();
95 
96  return $this->settings->validateOnLogin()->value()
97  && $db->currentDocumentOfAcceptedVersion($this->user)->map($this->doesntMatch(...))->except($true)->value();
98  }
99 
100  public function doesntMatch(Document $document): bool
101  {
102  return !$this->legal_documents->document()->documentMatches($document, $this->user);
103  }
104 
105  public function matchingDocument(): Result
106  {
107  return ($this->matching_document)();
108  }
109 
110  public function acceptedVersion(): Result
111  {
112  return $this->cannotAgree() || $this->neverAgreed() ?
113  new Error('User never agreed.') :
114  $this->legal_documents->history()->acceptedVersion($this->user);
115  }
116 
117  public function acceptMatchingDocument(): void
118  {
119  $this->legal_documents->history()->acceptDocument(
120  $this->user,
121  $this->matchingDocument()->value()
122  );
123  $this->agreeDate()->update($this->clock->now());
124  }
125 
126  public function acceptAnyDocument(): void
127  {
128  $this->legal_documents->history()->acceptDocument(
129  $this->user,
130  current($this->legal_documents->document()->repository()->all())
131  );
132  }
133 
134  public function isLDAPUser(): bool
135  {
136  return $this->authMode() === (string) ilAuthUtils::AUTH_LDAP;
137  }
138 
139  public function isExternalAccount(): bool
140  {
141  return in_array((int) $this->authMode(), [ilAuthUtils::AUTH_PROVIDER_LTI, ilAuthUtils::AUTH_ECS], true);
142  }
143 
144  public function format(string $format_string): string
145  {
146  return str_ireplace('[BR]', "\n", sprintf(
147  $format_string,
148  $this->user->getFullname(),
149  $this->user->getLogin(),
150  $this->user->getExternalAccount()
151  ));
152  }
153 
154  public function raw(): ilObjUser
155  {
156  return $this->user;
157  }
158 
159  private function authMode(): string
160  {
161  $auth_mode = $this->user->getAuthMode();
162  return $auth_mode === 'default' ?
163  $this->settings->authMode()->value() :
164  $auth_mode;
165  }
166 
167  private function didNotAccept(Document $document): bool
168  {
169  return !$this->legal_documents->history()->alreadyAccepted($this->user, $document);
170  }
171 
177  private function lazy(callable $create_value): Closure
178  {
179  $proc = function () use (&$proc, $create_value) {
180  $value = $create_value();
181  $proc = fn() => $value;
182  return $value;
183  };
184  return function () use (&$proc) {
185  return $proc();
186  };
187  }
188 }
__construct(private readonly ilObjUser $user, private readonly Settings $settings, private readonly UserSettings $user_settings, private readonly Provide $legal_documents, private readonly Clock $clock)
Definition: User.php:40
const ANONYMOUS_USER_ID
Definition: constants.php:27
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
didNotAccept(Document $document)
Definition: User.php:167
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:30
lazy(callable $create_value)
A
Definition: User.php:177
format(string $format_string)
Definition: User.php:144