PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
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
← All changes | src/UserGroup/UserGroup.php +133 -56 2.3.19 → 2.1.11 View file →
@@ -1,57 +1,123 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * UserGroup.php
4 + *
5 + * The UserGroup class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
5 15 namespace UserAccessManager\UserGroup;
6 16
7 -use Exception;
8 17 use UserAccessManager\Config\MainConfig;
9 18 use UserAccessManager\Database\Database;
10 19 use UserAccessManager\Object\ObjectHandler;
20 +use UserAccessManager\Util\Util;
21 +use UserAccessManager\Wrapper\Php;
11 22 use UserAccessManager\Wrapper\Wordpress;
12 23
24 +/**
25 + * Class UserGroup
26 + *
27 + * @package UserAccessManager\UserGroup
28 + */
13 29 class UserGroup extends AbstractUserGroup
14 30 {
15 31 const USER_GROUP_TYPE = 'UserGroup';
16 32
17 - protected ?string $type = self::USER_GROUP_TYPE;
18 - protected ?string $ipRange = null;
33 + /**
34 + * @var string
35 + */
36 + protected $type = self::USER_GROUP_TYPE;
19 37
20 38 /**
39 + * @var string
40 + */
41 + protected $ipRange = null;
42 +
43 + /**
44 + * UserGroup constructor.
45 + *
46 + * @param Php $php
47 + * @param Wordpress $wordpress
48 + * @param Database $database
49 + * @param MainConfig $config
50 + * @param Util $util
51 + * @param ObjectHandler $objectHandler
52 + * @param AssignmentInformationFactory $assignmentInformationFactory
53 + * @param null|string $id
54 + *
21 55 * @throws UserGroupTypeException
22 56 */
23 57 public function __construct(
58 + Php $php,
24 59 Wordpress $wordpress,
25 60 Database $database,
26 61 MainConfig $config,
62 + Util $util,
27 63 ObjectHandler $objectHandler,
28 - AssignedObjectsLoader $assignedObjectsLoader,
29 - int|string|null $id = null
64 + AssignmentInformationFactory $assignmentInformationFactory,
65 + $id = null
30 66 ) {
31 - parent::__construct($wordpress, $database, $config, $objectHandler, $assignedObjectsLoader);
67 + parent::__construct(
68 + $php,
69 + $wordpress,
70 + $database,
71 + $config,
72 + $util,
73 + $objectHandler,
74 + $assignmentInformationFactory
75 + );
32 76
33 77 if ($id !== null) {
34 - $this->load($id);
78 + $this->load((int)$id);
35 79 }
36 80 }
37 81
38 - public function getIpRange(): ?string
82 + /**
83 + * Returns the ip range.
84 + *
85 + * @return array|string
86 + */
87 + public function getIpRange()
39 88 {
40 89 return $this->ipRange;
41 90 }
42 91
43 - public function getIpRangeArray(): array
92 + /**
93 + * Returns the ip range as array
94 + *
95 + * @return array
96 + */
97 + public function getIpRangeArray()
44 98 {
45 - return explode(';', (string) $this->ipRange);
99 + return explode(';', $this->ipRange);
46 100 }
47 101
48 - public function setIpRange(array|string $ipRange): void
102 + /**
103 + * Sets the ip range.
104 + *
105 + * @param string|array $ipRange The new ip range.
106 + */
107 + public function setIpRange($ipRange)
49 108 {
50 109 $this->ipRange = (is_array($ipRange) === true) ? implode(';', $ipRange) : $ipRange;
51 110 }
52 111
53 - public function load(int|string $id): bool
112 + /**
113 + * Loads the user group.
114 + *
115 + * @param string $id
116 + *
117 + * @return bool
118 + */
119 + public function load($id)
54 120 {
55 121 $query = $this->database->prepare(
56 122 "SELECT *
57 123 FROM {$this->database->getUserGroupTable()}
@@ -59,71 +125,82 @@
59 125 LIMIT 1",
60 126 $id
61 127 );
62 128
63 - $databaseUserGroup = $this->database->getRow($query);
129 + $dbUserGroup = $this->database->getRow($query);
64 130
65 - if ($databaseUserGroup === null) {
66 - return false;
131 + if ($dbUserGroup !== null) {
132 + $this->id = $id;
133 + $this->name = $dbUserGroup->groupname;
134 + $this->description = $dbUserGroup->groupdesc;
135 + $this->readAccess = $dbUserGroup->read_access;
136 + $this->writeAccess = $dbUserGroup->write_access;
137 + $this->ipRange = $dbUserGroup->ip_range;
138 +
139 + return true;
67 140 }
68 141
69 - $this->assignDatabaseValues($databaseUserGroup);
70 -
71 - return true;
142 + return false;
72 143 }
73 144
74 - public function assignDatabaseValues(object $databaseUserGroup): void
145 + /**
146 + * Saves the user group.
147 + *
148 + * @return bool
149 + */
150 + public function save()
75 151 {
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 - }
152 + if ($this->id === null) {
153 + $return = $this->database->insert(
154 + $this->database->getUserGroupTable(),
155 + [
156 + 'groupname' => $this->name,
157 + 'groupdesc' => $this->description,
158 + 'read_access' => $this->readAccess,
159 + 'write_access' => $this->writeAccess,
160 + 'ip_range' => $this->ipRange
161 + ]
162 + );
83 163
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(
164 + if ($return !== false) {
165 + $this->id = (string)$this->database->getLastInsertId();
166 + }
167 + } else {
168 + $return = $this->database->update(
96 169 $this->database->getUserGroupTable(),
97 - $columns,
170 + [
171 + 'groupname' => $this->name,
172 + 'groupdesc' => $this->description,
173 + 'read_access' => $this->readAccess,
174 + 'write_access' => $this->writeAccess,
175 + 'ip_range' => $this->ipRange
176 + ],
98 177 ['ID' => $this->id]
99 - ) !== false;
178 + );
100 179 }
101 180
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;
181 + return ($return !== false);
111 182 }
112 183
113 184 /**
114 - * @throws Exception
185 + * Deletes the user group.
186 + *
187 + * @return bool
115 188 */
116 - public function delete(): bool
189 + public function delete()
117 190 {
118 191 if ($this->id === null) {
119 192 return false;
120 193 }
121 194
122 - $deleted = $this->database->delete(
195 + $success = $this->database->delete(
123 196 $this->database->getUserGroupTable(),
124 197 ['ID' => $this->id]
125 198 );
126 199
127 - return ($deleted !== false) && parent::delete();
200 + if ($success !== false) {
201 + $success = parent::delete();
202 + }
203 +
204 + return $success;
128 205 }
129 206 }