ManageUsers.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class ManageUsers {
  19. private static $Instance = null;
  20. private FirebaseCon $Connection;
  21. private string $SpecificPath;
  22. private function __construct(FirebaseCon $Connection, string $SpecificPath) {
  23. $this->Connection = $Connection;
  24. $this->SpecificPath = $SpecificPath;
  25. }
  26. public static function GetInstance(FirebaseCon $Connection, string $SpecificPath) {
  27. if (self::$Instance === null) {
  28. self::$Instance = new ManageUsers($Connection, $SpecificPath);
  29. }
  30. return self::$Instance;
  31. }
  32. public function GetUsers() {
  33. $Result = $this->Connection->GetList($this->SpecificPath);
  34. if (isset($Result['error'])) {
  35. return 'General error while requesting';
  36. } else if (count($Result) === 0) {
  37. return 'No user found';
  38. } else {
  39. $UserList = [];
  40. foreach ($Result as $UserData) {
  41. $User = new User($UserData['id'], $UserData['name'],
  42. $UserData['email'], $UserData['date'],
  43. $UserData['privileges']);
  44. array_push($UserList, $User);
  45. }
  46. return $UserList;
  47. }
  48. }
  49. public function GetUser(string $Id) {
  50. $Result = $this->Connection->GetListElement($this->SpecificPath, $Id);
  51. if (isset($Result['error'])) {
  52. return 'General error while requesting';
  53. } else if (count($Result) === 0) {
  54. return 'User does not exist';
  55. } else {
  56. $User = new User($Result['id'], $Result['name'],
  57. $Result['email'], $Result['date'],
  58. $Result['privileges']);
  59. return $User;
  60. }
  61. }
  62. public function AddUser(User $User): string {
  63. $UserArray = [
  64. 'id' => $User->GetId(),
  65. 'name' => $User->GetName(),
  66. 'email' => $User->GetEMail(),
  67. 'date' => $User->GetDate(),
  68. 'privileges' => $User->GetPrivileges()
  69. ];
  70. if ($this->Connection->AddListElement($this->SpecificPath, $UserArray)) {
  71. return 'User added successfully';
  72. } else {
  73. return 'User already exists';
  74. }
  75. }
  76. public function RemoveUser(string $Id): string {
  77. if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
  78. return 'User removed successfully';
  79. } else {
  80. return 'User does not exist';
  81. }
  82. }
  83. }