HestiaAuth.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the FileGator package.
  4. *
  5. * (c) Milos Stojanovic <[email protected]>
  6. *
  7. * For the full copyright and license information, please view the LICENSE file
  8. */
  9. namespace Filegator\Services\Auth\Adapters;
  10. use Filegator\Services\Auth\AuthInterface;
  11. use Filegator\Services\Auth\User;
  12. use Filegator\Services\Auth\UsersCollection;
  13. use Filegator\Services\Service;
  14. use function Hestiacp\quoteshellarg\quoteshellarg;
  15. /**
  16. * @codeCoverageIgnore
  17. */
  18. class HestiaAuth implements Service, AuthInterface {
  19. protected $permissions = [];
  20. protected $private_repos = false;
  21. protected $hestia_user = "";
  22. public function init(array $config = []) {
  23. if (isset($_SESSION["user"])) {
  24. $v_user = $_SESSION["user"];
  25. }
  26. if (!empty($_SESSION["look"])) {
  27. if (isset($_SESSION["look"]) && $_SESSION["userContext"] === "admin") {
  28. $v_user = $_SESSION["look"];
  29. }
  30. if (
  31. $_SESSION["look"] == "admin" &&
  32. $_SESSION["POLICY_SYSTEM_PROTECTED_ADMIN"] == "yes"
  33. ) {
  34. // Go away do not login
  35. header("Location: /");
  36. exit();
  37. }
  38. }
  39. $this->hestia_user = $v_user;
  40. $this->permissions = isset($config["permissions"]) ? (array) $config["permissions"] : [];
  41. $this->private_repos = isset($config["private_repos"])
  42. ? (bool) $config["private_repos"]
  43. : false;
  44. }
  45. public function user(): ?User {
  46. $cmd = "/usr/bin/sudo /usr/local/hestia/bin/v-list-user";
  47. exec($cmd . " " . quoteshellarg($this->hestia_user) . " json", $output, $return_var);
  48. if ($return_var == 0) {
  49. $data = json_decode(implode("", $output), true);
  50. $hestia_user_info = $data[$this->hestia_user];
  51. return $this->transformUser($hestia_user_info);
  52. }
  53. return $this->getGuest();
  54. }
  55. public function transformUser($hstuser): User {
  56. $user = new User();
  57. $user->setUsername($this->hestia_user);
  58. $user->setName($this->hestia_user . " (" . $hstuser["NAME"] . ")");
  59. $user->setRole("user");
  60. $user->setPermissions($this->permissions);
  61. $user->setHomedir("/");
  62. return $user;
  63. }
  64. public function authenticate($username, $password): bool {
  65. # Auth is handled by Hestia
  66. return false;
  67. }
  68. public function forget() {
  69. // Logout return to Hestia
  70. return $this->getGuest();
  71. }
  72. public function store(User $user) {
  73. return null; // not used
  74. }
  75. public function update($username, User $user, $password = ""): User {
  76. // Password change is handled by Hestia
  77. return $this->user();
  78. }
  79. public function add(User $user, $password): User {
  80. return new User(); // not used
  81. }
  82. public function delete(User $user) {
  83. return true; // not used
  84. }
  85. public function find($username): ?User {
  86. return null; // not used
  87. }
  88. public function allUsers(): UsersCollection {
  89. return new UsersCollection(); // not used
  90. }
  91. public function getGuest(): User {
  92. $guest = new User();
  93. $guest->setUsername("guest");
  94. $guest->setName("Guest");
  95. $guest->setRole("guest");
  96. $guest->setHomedir("/");
  97. $guest->setPermissions([]);
  98. return $guest;
  99. }
  100. }