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

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