ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilExternalAuthUserAttributeMapping.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected ilDBInterface $db;
24  protected string $authMode;
25  protected int $authSourceId;
27  protected array $mapping = [];
28 
29  public function __construct(string $authMode, int $authSourceId = 0)
30  {
31  global $DIC;
32  $this->db = $DIC->database();
33 
34  $this->setAuthMode($authMode);
35  $this->setAuthSourceId($authSourceId);
36 
37  $this->read();
38  }
39 
40  public function getAuthSourceId(): int
41  {
42  return $this->authSourceId;
43  }
44 
45  public function setAuthSourceId(int $authSourceId): void
46  {
47  $this->authSourceId = $authSourceId;
48  }
49 
50  public function getAuthMode(): string
51  {
52  return $this->authMode;
53  }
54 
55  public function setAuthMode(string $authMode): void
56  {
57  $this->authMode = $authMode;
58  }
59 
61  {
63  }
64 
65  public function offsetExists($offset): bool
66  {
67  return isset($this->mapping[$offset]);
68  }
69 
71  {
72  return $this->offsetExists($offset) ? $this->mapping[$offset] : null;
73  }
74 
75  public function offsetSet($offset, $value): void
76  {
77  if ($offset === null) {
78  $this->mapping[] = $value;
79  } else {
80  $this->mapping[$offset] = $value;
81  }
82  }
83 
84  public function offsetUnset($offset): void
85  {
86  unset($this->mapping[$offset]);
87  }
88 
89  public function count(): int
90  {
91  return count($this->mapping);
92  }
93 
95  {
96  return current($this->mapping);
97  }
98 
99  public function next(): void
100  {
101  next($this->mapping);
102  }
103 
104  public function key(): string
105  {
106  return key($this->mapping);
107  }
108 
109  public function valid(): bool
110  {
111  return current($this->mapping) instanceof ilExternalAuthUserAttributeMappingRule;
112  }
113 
114  public function rewind(): void
115  {
116  reset($this->mapping);
117  }
118 
119  protected function read(): void
120  {
121  $this->mapping = [];
122 
123  $res = $this->db->queryF(
124  'SELECT * FROM auth_ext_attr_mapping WHERE auth_mode = %s AND auth_src_id = %s',
125  ['text', 'integer'],
126  [$this->getAuthMode(), $this->getAuthSourceId()]
127  );
128  while ($row = $this->db->fetchAssoc($res)) {
129  $rule = $this->getEmptyRule();
130  $rule->setAttribute($row['attribute']);
131  $rule->setExternalAttribute($row['ext_attribute']);
132  $rule->updateAutomatically((bool) $row['update_automatically']);
133 
134  $this->mapping[$rule->getAttribute()] = $rule;
135  }
136  }
137 
138  public function save(): void
139  {
140  foreach ($this->mapping as $rule) {
141  $this->db->replace(
142  'auth_ext_attr_mapping',
143  [
144  'auth_mode' => ['text', $this->getAuthMode()],
145  'auth_src_id' => ['integer', $this->getAuthSourceId()],
146  'attribute' => ['text', $rule->getAttribute()]
147  ],
148  [
149  'ext_attribute' => ['text', $rule->getExternalAttribute()],
150  'update_automatically' => ['integer', (int) $rule->isAutomaticallyUpdated()]
151  ]
152  );
153  }
154  }
155 
156  public function delete(): void
157  {
158  $this->mapping = [];
159  $this->db->manipulateF(
160  'DELETE FROM auth_ext_attr_mapping WHERE auth_mode = %s AND auth_src_id = %s',
161  ['text', 'integer'],
162  [$this->getAuthMode(), $this->getAuthSourceId()]
163  );
164  }
165 }
$res
Definition: ltiservices.php:66
__construct(string $authMode, int $authSourceId=0)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26