PluginProbe
User Access Manager / 2.3.18
User Access Manager v2.3.18
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.18, at src/UserGroup/UserGroup.php

130 lines 3.2 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\Wrapper\Wordpress;
12
13 class UserGroup extends AbstractUserGroup
14 {
15 const USER_GROUP_TYPE = 'UserGroup';
16
17 protected ?string $type = self::USER_GROUP_TYPE;
18 protected ?string $ipRange = null;
19
20 /**
21 * @throws UserGroupTypeException
22 */
23 public function __construct(
24 Wordpress $wordpress,
25 Database $database,
26 MainConfig $config,
27 ObjectHandler $objectHandler,
28 AssignedObjectsLoader $assignedObjectsLoader,
29 int|string|null $id = null
30 ) {
31 parent::__construct($wordpress, $database, $config, $objectHandler, $assignedObjectsLoader);
32
33 if ($id !== null) {
34 $this->load($id);
35 }
36 }
37
38 public function getIpRange(): ?string
39 {
40 return $this->ipRange;
41 }
42
43 public function getIpRangeArray(): array
44 {
45 return explode(';', (string) $this->ipRange);
46 }
47
48 public function setIpRange(array|string $ipRange): void
49 {
50 $this->ipRange = (is_array($ipRange) === true) ? implode(';', $ipRange) : $ipRange;
51 }
52
53 public function load(int|string $id): bool
54 {
55 $query = $this->database->prepare(
56 "SELECT *
57 FROM {$this->database->getUserGroupTable()}
58 WHERE ID = %d
59 LIMIT 1",
60 $id
61 );
62
63 $databaseUserGroup = $this->database->getRow($query);
64
65 if ($databaseUserGroup === null) {
66 return false;
67 }
68
69 $this->assignDatabaseValues($databaseUserGroup);
70
71 return true;
72 }
73
74 public function assignDatabaseValues(object $databaseUserGroup): void
75 {
76 $this->id = $databaseUserGroup->ID;
77 $this->name = $databaseUserGroup->groupname;
78 $this->description = $databaseUserGroup->groupdesc;
79 $this->readAccess = $databaseUserGroup->read_access;
80 $this->writeAccess = $databaseUserGroup->write_access;
81 $this->ipRange = $databaseUserGroup->ip_range;
82 }
83
84 public function save(): bool
85 {
86 $columns = [
87 'groupname' => $this->name,
88 'groupdesc' => $this->description,
89 'read_access' => $this->readAccess,
90 'write_access' => $this->writeAccess,
91 'ip_range' => $this->ipRange
92 ];
93
94 if ($this->id !== null) {
95 return $this->database->update(
96 $this->database->getUserGroupTable(),
97 $columns,
98 ['ID' => $this->id]
99 ) !== false;
100 }
101
102 $return = $this->database->insert($this->database->getUserGroupTable(), $columns);
103
104 if ($return === false) {
105 return false;
106 }
107
108 $this->id = (string) $this->database->getLastInsertId();
109
110 return true;
111 }
112
113 /**
114 * @throws Exception
115 */
116 public function delete(): bool
117 {
118 if ($this->id === null) {
119 return false;
120 }
121
122 $deleted = $this->database->delete(
123 $this->database->getUserGroupTable(),
124 ['ID' => $this->id]
125 );
126
127 return ($deleted !== false) && parent::delete();
128 }
129 }
130