| 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 |
namespace UserAccessManager\UserGroup; |
| 16 |
|
| 17 |
use UserAccessManager\Config\MainConfig; |
| 18 |
use UserAccessManager\Database\Database; |
| 19 |
use UserAccessManager\Object\ObjectHandler; |
| 20 |
use UserAccessManager\Util\Util; |
| 21 |
use UserAccessManager\Wrapper\Php; |
| 22 |
use UserAccessManager\Wrapper\Wordpress; |
| 23 |
|
| 24 |
/** |
| 25 |
* Class UserGroup |
| 26 |
* |
| 27 |
* @package UserAccessManager\UserGroup |
| 28 |
*/ |
| 29 |
class UserGroup extends AbstractUserGroup |
| 30 |
{ |
| 31 |
const USER_GROUP_TYPE = 'UserGroup'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $type = self::USER_GROUP_TYPE; |
| 37 |
|
| 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 |
* |
| 55 |
* @throws UserGroupTypeException |
| 56 |
*/ |
| 57 |
public function __construct( |
| 58 |
Php $php, |
| 59 |
Wordpress $wordpress, |
| 60 |
Database $database, |
| 61 |
MainConfig $config, |
| 62 |
Util $util, |
| 63 |
ObjectHandler $objectHandler, |
| 64 |
AssignmentInformationFactory $assignmentInformationFactory, |
| 65 |
$id = null |
| 66 |
) { |
| 67 |
parent::__construct( |
| 68 |
$php, |
| 69 |
$wordpress, |
| 70 |
$database, |
| 71 |
$config, |
| 72 |
$util, |
| 73 |
$objectHandler, |
| 74 |
$assignmentInformationFactory |
| 75 |
); |
| 76 |
|
| 77 |
if ($id !== null) { |
| 78 |
$this->load((int)$id); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the ip range. |
| 84 |
* |
| 85 |
* @return array|string |
| 86 |
*/ |
| 87 |
public function getIpRange() |
| 88 |
{ |
| 89 |
return $this->ipRange; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the ip range as array |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function getIpRangeArray() |
| 98 |
{ |
| 99 |
return explode(';', $this->ipRange); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Sets the ip range. |
| 104 |
* |
| 105 |
* @param string|array $ipRange The new ip range. |
| 106 |
*/ |
| 107 |
public function setIpRange($ipRange) |
| 108 |
{ |
| 109 |
$this->ipRange = (is_array($ipRange) === true) ? implode(';', $ipRange) : $ipRange; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Loads the user group. |
| 114 |
* |
| 115 |
* @param string $id |
| 116 |
* |
| 117 |
* @return bool |
| 118 |
*/ |
| 119 |
public function load($id) |
| 120 |
{ |
| 121 |
$query = $this->database->prepare( |
| 122 |
"SELECT * |
| 123 |
FROM {$this->database->getUserGroupTable()} |
| 124 |
WHERE ID = %d |
| 125 |
LIMIT 1", |
| 126 |
$id |
| 127 |
); |
| 128 |
|
| 129 |
$dbUserGroup = $this->database->getRow($query); |
| 130 |
|
| 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; |
| 140 |
} |
| 141 |
|
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Saves the user group. |
| 147 |
* |
| 148 |
* @return bool |
| 149 |
*/ |
| 150 |
public function save() |
| 151 |
{ |
| 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 |
); |
| 163 |
|
| 164 |
if ($return !== false) { |
| 165 |
$this->id = (string)$this->database->getLastInsertId(); |
| 166 |
} |
| 167 |
} else { |
| 168 |
$return = $this->database->update( |
| 169 |
$this->database->getUserGroupTable(), |
| 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 |
], |
| 177 |
['ID' => $this->id] |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
return ($return !== false); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Deletes the user group. |
| 186 |
* |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
public function delete() |
| 190 |
{ |
| 191 |
if ($this->id === null) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
$success = $this->database->delete( |
| 196 |
$this->database->getUserGroupTable(), |
| 197 |
['ID' => $this->id] |
| 198 |
); |
| 199 |
|
| 200 |
if ($success !== false) { |
| 201 |
$success = parent::delete(); |
| 202 |
} |
| 203 |
|
| 204 |
return $success; |
| 205 |
} |
| 206 |
} |
| 207 |
|