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