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