123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- /*
- * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- class ManageUsers {
- private static $Instance = null;
- private FirebaseCon $Connection;
- private string $SpecificPath;
- private function __construct(FirebaseCon $Connection, string $SpecificPath) {
- $this->Connection = $Connection;
- $this->SpecificPath = $SpecificPath;
- }
- public static function GetInstance(FirebaseCon $Connection, string $SpecificPath) {
- if (self::$Instance === null) {
- self::$Instance = new ManageUsers($Connection, $SpecificPath);
- }
- return self::$Instance;
- }
- public function GetUsers() {
- $Result = $this->Connection->GetList($this->SpecificPath);
- if (isset($Result['error'])) {
- return 'General error while requesting';
- } else if (count($Result) === 0) {
- return 'No user found';
- } else {
- $UserList = [];
- foreach ($Result as $UserData) {
- $User = new User($UserData['id'], $UserData['name'],
- $UserData['email'], $UserData['date'],
- $UserData['privileges']);
- array_push($UserList, $User);
- }
- return $UserList;
- }
- }
- public function GetUser(string $Id) {
- $Result = $this->Connection->GetListElement($this->SpecificPath, $Id);
- if (isset($Result['error'])) {
- return 'General error while requesting';
- } else if (count($Result) === 0) {
- return 'User does not exist';
- } else {
- $User = new User($Result['id'], $Result['name'],
- $Result['email'], $Result['date'],
- $Result['privileges']);
- return $User;
- }
- }
- public function AddUser(User $User): string {
- $UserArray = [
- 'id' => $User->GetId(),
- 'name' => $User->GetName(),
- 'email' => $User->GetEMail(),
- 'date' => $User->GetDate(),
- 'privileges' => $User->GetPrivileges()
- ];
-
- if ($this->Connection->AddListElement($this->SpecificPath, $UserArray)) {
- return 'User added successfully';
- } else {
- return 'User already exists';
- }
- }
-
- public function RemoveUser(string $Id): string {
- if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
- return 'User removed successfully';
- } else {
- return 'User does not exist';
- }
- }
- }
|