1 | <?php |
||
2 | /****************************************************************************** |
||
3 | * Wikipedia Account Creation Assistance tool * |
||
4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
5 | * * |
||
6 | * This is free and unencumbered software released into the public domain. * |
||
7 | * Please see LICENSE.md for the full licencing statement. * |
||
8 | ******************************************************************************/ |
||
9 | |||
10 | namespace Waca\DataObjects; |
||
11 | |||
12 | use Exception; |
||
13 | use PDO; |
||
14 | use Waca\DataObject; |
||
15 | use Waca\PdoDatabase; |
||
16 | |||
17 | class UserRole extends DataObject |
||
18 | { |
||
19 | /** @var int */ |
||
20 | private $user; |
||
21 | /** @var string */ |
||
22 | private $role; |
||
23 | private ?int $domain; |
||
24 | |||
25 | /** |
||
26 | * @param int $user |
||
27 | * @param PdoDatabase $database |
||
28 | * @param int $domain |
||
29 | * |
||
30 | * @return UserRole[] |
||
31 | */ |
||
32 | public static function getForUser(int $user, PdoDatabase $database, int $domain) |
||
33 | { |
||
34 | $sql = 'SELECT * FROM userrole WHERE user = :user AND (domain IS NULL OR domain = :domain)'; |
||
35 | $statement = $database->prepare($sql); |
||
36 | $statement->bindValue(':user', $user); |
||
37 | $statement->bindValue(':domain', $domain); |
||
38 | |||
39 | $statement->execute(); |
||
40 | |||
41 | $result = array(); |
||
42 | |||
43 | /** @var Ban $v */ |
||
44 | foreach ($statement->fetchAll(PDO::FETCH_CLASS, get_called_class()) as $v) { |
||
45 | $v->setDatabase($database); |
||
46 | $result[] = $v; |
||
47 | } |
||
48 | |||
49 | return $result; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Saves a data object to the database, either updating or inserting a record. |
||
54 | * |
||
55 | * @throws Exception |
||
56 | */ |
||
57 | public function save() |
||
58 | { |
||
59 | if ($this->isNew()) { |
||
60 | // insert |
||
61 | $statement = $this->dbObject->prepare('INSERT INTO `userrole` (user, role, domain) VALUES (:user, :role, :domain);' |
||
62 | ); |
||
63 | $statement->bindValue(":user", $this->user); |
||
64 | $statement->bindValue(":role", $this->role); |
||
65 | $statement->bindValue(":domain", $this->domain); |
||
66 | |||
67 | if ($statement->execute()) { |
||
68 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
69 | } |
||
70 | else { |
||
71 | throw new Exception($statement->errorInfo()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
72 | } |
||
73 | } |
||
74 | else { |
||
75 | // update |
||
76 | throw new Exception('Updating roles is not available'); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
83 | public function getUser() |
||
84 | { |
||
85 | return $this->user; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param int $user |
||
90 | */ |
||
91 | public function setUser($user) |
||
92 | { |
||
93 | $this->user = $user; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getRole() |
||
100 | { |
||
101 | return $this->role; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $role |
||
106 | */ |
||
107 | public function setRole($role) |
||
108 | { |
||
109 | $this->role = $role; |
||
110 | } |
||
111 | |||
112 | public function getDomain(): ?int |
||
113 | { |
||
114 | return $this->domain; |
||
115 | } |
||
116 | |||
117 | public function setDomain(?int $domain): void |
||
118 | { |
||
119 | $this->domain = $domain; |
||
120 | } |
||
121 | } |
||
122 |