mirror of
https://github.com/filegator/filegator.git
synced 2025-08-30 05:09:49 +02:00
initial commit
This commit is contained in:
185
backend/Services/Auth/Adapters/Database.php
Normal file
185
backend/Services/Auth/Adapters/Database.php
Normal file
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Filegator\Services\Auth\Adapters;
|
||||
|
||||
use Dibi\Connection;
|
||||
use Dibi\Row;
|
||||
use Filegator\Services\Auth\AuthInterface;
|
||||
use Filegator\Services\Auth\User;
|
||||
use Filegator\Services\Auth\UsersCollection;
|
||||
use Filegator\Services\Service;
|
||||
use Filegator\Services\Session\SessionStorageInterface as Session;
|
||||
use Filegator\Utils\PasswordHash;
|
||||
|
||||
class Database implements Service, AuthInterface
|
||||
{
|
||||
use PasswordHash;
|
||||
|
||||
const SESSION_KEY = 'database_auth';
|
||||
|
||||
const GUEST_USERNAME = 'guest';
|
||||
|
||||
protected $session;
|
||||
|
||||
public function __construct(Session $session)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function init(array $config = [])
|
||||
{
|
||||
$this->conn = new Connection($config);
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
public function user(): ?User
|
||||
{
|
||||
return $this->session ? $this->session->get(self::SESSION_KEY, null) : null;
|
||||
}
|
||||
|
||||
public function authenticate($username, $password): bool
|
||||
{
|
||||
$ret = $this->getConnection()
|
||||
->fetch('SELECT * FROM users WHERE username = ?', $username)
|
||||
;
|
||||
|
||||
if ($ret && $this->verifyPassword($password, $ret->password)) {
|
||||
$user = $this->mapToUserObject($ret);
|
||||
$this->store($user);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function forget()
|
||||
{
|
||||
return $this->session->invalidate();
|
||||
}
|
||||
|
||||
public function store(User $user)
|
||||
{
|
||||
return $this->session->set(self::SESSION_KEY, $user);
|
||||
}
|
||||
|
||||
public function update($username, User $user, $password = ''): User
|
||||
{
|
||||
if (! $this->find($username)) {
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
if ($username != $user->getUsername() && $this->find($user->getUsername())) {
|
||||
throw new \Exception('Username already taken');
|
||||
}
|
||||
|
||||
$this->getConnection()->query('UPDATE users SET', [
|
||||
'username' => $user->getUsername(),
|
||||
'name' => $user->getName(),
|
||||
'homedir' => $user->getHomeDir(),
|
||||
'permissions' => $user->getPermissions(true),
|
||||
'role' => $user->getRole(),
|
||||
], 'WHERE username = ?', $username);
|
||||
|
||||
if ($password) {
|
||||
$this->getConnection()->query('UPDATE users SET', [
|
||||
'password' => $this->hashPassword($password),
|
||||
], 'WHERE username = ?', $username);
|
||||
}
|
||||
|
||||
return $this->find($user->getUsername());
|
||||
}
|
||||
|
||||
public function add(User $user, $password): User
|
||||
{
|
||||
if ($this->find($user->getUsername())) {
|
||||
throw new \Exception('Username already taken');
|
||||
}
|
||||
|
||||
$this->getConnection()->query('INSERT INTO users', [
|
||||
'username' => $user->getUsername(),
|
||||
'name' => $user->getName(),
|
||||
'role' => $user->getRole(),
|
||||
'homedir' => $user->getHomeDir(),
|
||||
'permissions' => $user->getPermissions(true),
|
||||
'password' => $this->hashPassword($password),
|
||||
]);
|
||||
|
||||
return $this->find($user->getUsername());
|
||||
}
|
||||
|
||||
public function delete(User $user)
|
||||
{
|
||||
if (! $this->find($user->getUsername())) {
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
$this->getConnection()->query('DELETE FROM users WHERE username = ?', $user->getUsername());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function find($username): ?User
|
||||
{
|
||||
$row = $this->getConnection()
|
||||
->fetch('SELECT * FROM users WHERE username = ?', $username)
|
||||
;
|
||||
|
||||
if ($row) {
|
||||
return $this->mapToUserObject($row);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getGuest(): User
|
||||
{
|
||||
$guest = $this->find(self::GUEST_USERNAME);
|
||||
|
||||
if (! $guest || ! $guest->isGuest()) {
|
||||
throw new \Exception('No guest account');
|
||||
}
|
||||
|
||||
return $guest;
|
||||
}
|
||||
|
||||
public function allUsers(): UsersCollection
|
||||
{
|
||||
$users = new UsersCollection();
|
||||
|
||||
$rows = $this->getConnection()
|
||||
->fetchAll('SELECT * FROM users')
|
||||
;
|
||||
|
||||
foreach ($rows as $user) {
|
||||
$users->addUser($this->mapToUserObject($user));
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
protected function mapToUserObject(Row $user): User
|
||||
{
|
||||
$new = new User();
|
||||
|
||||
$new->setRole($user->role);
|
||||
$new->setHomedir($user->homedir);
|
||||
$new->setPermissions($user->permissions, true);
|
||||
$new->setUsername($user->username);
|
||||
$new->setName($user->name);
|
||||
|
||||
return $new;
|
||||
}
|
||||
}
|
201
backend/Services/Auth/Adapters/JsonFile.php
Normal file
201
backend/Services/Auth/Adapters/JsonFile.php
Normal file
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the FileGator package.
|
||||
*
|
||||
* (c) Milos Stojanovic <alcalbg@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
*/
|
||||
|
||||
namespace Filegator\Services\Auth\Adapters;
|
||||
|
||||
use Filegator\Services\Auth\AuthInterface;
|
||||
use Filegator\Services\Auth\User;
|
||||
use Filegator\Services\Auth\UsersCollection;
|
||||
use Filegator\Services\Service;
|
||||
use Filegator\Services\Session\SessionStorageInterface as Session;
|
||||
use Filegator\Utils\PasswordHash;
|
||||
|
||||
class JsonFile implements Service, AuthInterface
|
||||
{
|
||||
use PasswordHash;
|
||||
|
||||
const SESSION_KEY = 'json_auth';
|
||||
|
||||
const GUEST_USERNAME = 'guest';
|
||||
|
||||
protected $session;
|
||||
|
||||
protected $file;
|
||||
|
||||
public function __construct(Session $session)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
public function init(array $config = [])
|
||||
{
|
||||
if (! file_exists($config['file'])) {
|
||||
copy($config['file'].'.blank', $config['file']);
|
||||
}
|
||||
|
||||
$this->file = $config['file'];
|
||||
}
|
||||
|
||||
public function user(): ?User
|
||||
{
|
||||
return $this->session ? $this->session->get(self::SESSION_KEY, null) : null;
|
||||
}
|
||||
|
||||
public function authenticate($username, $password): bool
|
||||
{
|
||||
$all_users = $this->getUsers();
|
||||
|
||||
foreach ($all_users as &$u) {
|
||||
if ($u['username'] == $username && $this->verifyPassword($password, $u['password'])) {
|
||||
$user = $this->mapToUserObject($u);
|
||||
$this->store($user);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function forget()
|
||||
{
|
||||
return $this->session->invalidate();
|
||||
}
|
||||
|
||||
public function store(User $user)
|
||||
{
|
||||
return $this->session->set(self::SESSION_KEY, $user);
|
||||
}
|
||||
|
||||
public function update($username, User $user, $password = ''): User
|
||||
{
|
||||
$all_users = $this->getUsers();
|
||||
|
||||
if ($username != $user->getUsername() && $this->find($user->getUsername())) {
|
||||
throw new \Exception('Username already taken');
|
||||
}
|
||||
|
||||
foreach ($all_users as &$u) {
|
||||
if ($u['username'] == $username) {
|
||||
$u['username'] = $user->getUsername();
|
||||
$u['name'] = $user->getName();
|
||||
$u['role'] = $user->getRole();
|
||||
$u['homedir'] = $user->getHomeDir();
|
||||
$u['permissions'] = $user->getPermissions(true);
|
||||
|
||||
if ($password) {
|
||||
$u['password'] = $this->hashPassword($password);
|
||||
}
|
||||
|
||||
$this->saveUsers($all_users);
|
||||
|
||||
return $this->find($user->getUsername());
|
||||
}
|
||||
}
|
||||
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
public function add(User $user, $password): User
|
||||
{
|
||||
if ($this->find($user->getUsername())) {
|
||||
throw new \Exception('Username already taken');
|
||||
}
|
||||
|
||||
$all_users = $this->getUsers();
|
||||
|
||||
$all_users[] = [
|
||||
'username' => $user->getUsername(),
|
||||
'name' => $user->getName(),
|
||||
'role' => $user->getRole(),
|
||||
'homedir' => $user->getHomeDir(),
|
||||
'permissions' => $user->getPermissions(true),
|
||||
'password' => $this->hashPassword($password),
|
||||
];
|
||||
|
||||
$this->saveUsers($all_users);
|
||||
|
||||
return $this->find($user->getUsername());
|
||||
}
|
||||
|
||||
public function delete(User $user)
|
||||
{
|
||||
$all_users = $this->getUsers();
|
||||
|
||||
foreach ($all_users as $key => $u) {
|
||||
if ($u['username'] == $user->getUsername()) {
|
||||
unset($all_users[$key]);
|
||||
$this->saveUsers($all_users);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
public function find($username): ?User
|
||||
{
|
||||
foreach ($this->getUsers() as $user) {
|
||||
if ($user['username'] == $username) {
|
||||
return $this->mapToUserObject($user);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getGuest(): User
|
||||
{
|
||||
$guest = $this->find(self::GUEST_USERNAME);
|
||||
|
||||
if (! $guest || ! $guest->isGuest()) {
|
||||
throw new \Exception('No guest account');
|
||||
}
|
||||
|
||||
return $guest;
|
||||
}
|
||||
|
||||
public function allUsers(): UsersCollection
|
||||
{
|
||||
$users = new UsersCollection();
|
||||
|
||||
foreach ($this->getUsers() as $user) {
|
||||
$users->addUser($this->mapToUserObject($user));
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
protected function mapToUserObject(array $user): User
|
||||
{
|
||||
$new = new User();
|
||||
|
||||
$new->setUsername($user['username']);
|
||||
$new->setName($user['name']);
|
||||
$new->setRole($user['role']);
|
||||
$new->setHomedir($user['homedir']);
|
||||
$new->setPermissions($user['permissions'], true);
|
||||
|
||||
return $new;
|
||||
}
|
||||
|
||||
protected function getUsers(): array
|
||||
{
|
||||
$users = json_decode(file_get_contents($this->file), true);
|
||||
|
||||
return is_array($users) ? $users : [];
|
||||
}
|
||||
|
||||
protected function saveUsers(array $users)
|
||||
{
|
||||
return file_put_contents($this->file, json_encode($users), LOCK_EX);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user