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