PluginProbe
User Access Manager / 2.3.17
User Access Manager v2.3.17
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / UserGroup / UserGroup.php

UserGroup.php in User Access Manager 2.3.17, at src/UserGroup/UserGroup.php

151 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\UserGroup;
6
7 use Exception;
8 use UserAccessManager\Config\MainConfig;
9 use UserAccessManager\Database\Database;
10 use UserAccessManager\Object\ObjectHandler;
11 use UserAccessManager\Util\Util;
12 use UserAccessManager\Wrapper\Php;
13 use UserAccessManager\Wrapper\Wordpress;
14
15 class UserGroup extends AbstractUserGroup
16 {
17 const USER_GROUP_TYPE = 'UserGroup';
18
19 protected ?string $type = self::USER_GROUP_TYPE;
20 protected ?string $ipRange = null;
21
22 /**
23 * @throws UserGroupTypeException
24 */
25 public function __construct(
26 Php $php,
27 Wordpress $wordpress,
28 Database $database,
29 MainConfig $config,
30 Util $util,
31 ObjectHandler $objectHandler,
32 AssignedObjectsLoader $assignedObjectsLoader,
33 int|string|null $id = null
34 ) {
35 parent::__construct(
36 $php,
37 $wordpress,
38 $database,
39 $config,
40 $util,
41 $objectHandler,
42 $assignedObjectsLoader
43 );
44
45 if ($id !== null) {
46 $this->load($id);
47 }
48 }
49
50 public function getIpRange(): array|string|null
51 {
52 return $this->ipRange;
53 }
54
55 public function getIpRangeArray(): array
56 {
57 return explode(';', (string) $this->ipRange);
58 }
59
60 public function setIpRange(array|string $ipRange): void
61 {
62 $this->ipRange = (is_array($ipRange) === true) ? implode(';', $ipRange) : $ipRange;
63 }
64
65 public function load(int|string $id): bool
66 {
67 $query = $this->database->prepare(
68 "SELECT *
69 FROM {$this->database->getUserGroupTable()}
70 WHERE ID = %d
71 LIMIT 1",
72 $id
73 );
74
75 $databaseUserGroup = $this->database->getRow($query);
76
77 if ($databaseUserGroup !== null) {
78 $this->assignDatabaseValues($databaseUserGroup);
79
80 return true;
81 }
82
83 return false;
84 }
85
86 public function assignDatabaseValues(object $databaseUserGroup): void
87 {
88 $this->id = $databaseUserGroup->ID;
89 $this->name = $databaseUserGroup->groupname;
90 $this->description = $databaseUserGroup->groupdesc;
91 $this->readAccess = $databaseUserGroup->read_access;
92 $this->writeAccess = $databaseUserGroup->write_access;
93 $this->ipRange = $databaseUserGroup->ip_range;
94 }
95
96 public function save(): bool
97 {
98 if ($this->id === null) {
99 $return = $this->database->insert(
100 $this->database->getUserGroupTable(),
101 [
102 'groupname' => $this->name,
103 'groupdesc' => $this->description,
104 'read_access' => $this->readAccess,
105 'write_access' => $this->writeAccess,
106 'ip_range' => $this->ipRange
107 ]
108 );
109
110 if ($return !== false) {
111 $this->id = (string) $this->database->getLastInsertId();
112 }
113 } else {
114 $return = $this->database->update(
115 $this->database->getUserGroupTable(),
116 [
117 'groupname' => $this->name,
118 'groupdesc' => $this->description,
119 'read_access' => $this->readAccess,
120 'write_access' => $this->writeAccess,
121 'ip_range' => $this->ipRange
122 ],
123 ['ID' => $this->id]
124 );
125 }
126
127 return ($return !== false);
128 }
129
130 /**
131 * @throws Exception
132 */
133 public function delete(): bool
134 {
135 if ($this->id === null) {
136 return false;
137 }
138
139 $success = $this->database->delete(
140 $this->database->getUserGroupTable(),
141 ['ID' => $this->id]
142 );
143
144 if ($success !== false) {
145 $success = parent::delete();
146 }
147
148 return $success;
149 }
150 }
151