ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRegistrationRoleAssignments.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 public const IL_REG_MISSING_DOMAIN = 1;
28 public const IL_REG_MISSING_ROLE = 2;
29
30 public array $assignments = [];
31 public int $default_role = 0;
32
33 protected ilDBInterface $db;
35
36 public function __construct()
37 {
38 global $DIC;
39
40 $this->db = $DIC->database();
41 $this->settings = $DIC->settings();
42 $this->read();
43 }
44
45 public function getRoleByEmail(string $a_email): int
46 {
47 foreach ($this->assignments as $assignment) {
48 if (!$assignment['domain'] || !$assignment['role']) {
49 continue;
50 }
51 if (stripos($a_email, $assignment['domain']) !== false) {
52 // check if role exists
53 if (!ilObject::_lookupType($assignment['role'])) {
54 continue;
55 }
56 return $assignment['role'];
57 }
58 }
59 // return default
60 return $this->getDefaultRole();
61 }
62
63 public function getDomainsByRole(int $role_id): array
64 {
65 $query = $this->db->query("SELECT * FROM reg_er_assignments " .
66 "WHERE role = " . $this->db->quote($role_id, 'integer'));
67
68 $res = [];
69 while ($row = $this->db->fetchAssoc($query)) {
70 $res[] = $row["domain"];
71 }
72 return $res;
73 }
74
75 public function getAssignments(): array
76 {
77 return $this->assignments;
78 }
79
80 public function setDomain(int $id, string $a_domain): void
81 {
82 $this->assignments[$id]['domain'] = $a_domain;
83 }
84
85 public function setRole(int $id, int $a_role): void
86 {
87 $this->assignments[$id]['role'] = $a_role;
88 }
89
90 public function getDefaultRole(): int
91 {
93 }
94
95 public function setDefaultRole(int $a_role_id): void
96 {
97 $this->default_role = $a_role_id;
98 }
99
100 public function deleteAll(): bool
101 {
102 $query = "DELETE FROM reg_er_assignments ";
103 $this->db->manipulate($query);
104 $this->read();
105 return true;
106 }
107
108 public function save(): bool
109 {
110 // Save default role
111 $this->settings->set('reg_default_role', (string) $this->getDefaultRole());
112
113 foreach ($this->assignments as $assignment) {
114 if (empty($assignment['id'])) {
115 $next_id = $this->db->nextId('reg_er_assignments');
116 $query = "INSERT INTO reg_er_assignments (assignment_id,domain,role) " .
117 "VALUES( " .
118 $this->db->quote($next_id, 'integer') . ', ' .
119 $this->db->quote($assignment['domain'], 'text') . ", " .
120 $this->db->quote($assignment['role'], 'integer') .
121 ")";
122 $res = $this->db->manipulate($query);
123 }
124 }
125 $this->read();
126 return true;
127 }
128
129 public function read(): bool
130 {
131 $query = "SELECT * FROM reg_er_assignments ";
132 $res = $this->db->query($query);
133
134 $this->assignments = [];
135 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
136 $this->assignments[$row->assignment_id]['id'] = (int) $row->assignment_id;
137 $this->assignments[$row->assignment_id]['role'] = (int) $row->role;
138 $this->assignments[$row->assignment_id]['domain'] = $row->domain;
139 }
140 $this->default_role = (int) $this->settings->get('reg_default_role', '0');
141 return true;
142 }
143}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _lookupType(int $id, bool $reference=false)
Class class.ilregistrationEmailRoleAssignments.
ILIAS Setting Class.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26