initial commit

This commit is contained in:
Milos Stojanovic
2019-06-13 18:52:40 +02:00
commit 261607e1d3
160 changed files with 41704 additions and 0 deletions

View 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;
}
}

View 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);
}
}

View File

@@ -0,0 +1,34 @@
<?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;
interface AuthInterface
{
public function user(): ?User;
public function authenticate($username, $password): bool;
public function forget();
public function find($username): ?User;
public function store(User $user);
public function update($username, User $user, $password = ''): User;
public function add(User $user, $password): User;
public function delete(User $user);
public function getGuest(): User;
public function allUsers(): UsersCollection;
}

View File

@@ -0,0 +1,158 @@
<?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;
class User implements \JsonSerializable
{
protected $role = 'guest';
protected $permissions = [];
protected $username = '';
protected $homedir = '';
protected $name = '';
protected $available_roles = ['guest', 'user', 'admin'];
protected $available_permissions = ['read', 'write', 'upload', 'download', 'batchdownload', 'zip'];
public function __construct()
{
}
public function isGuest(): bool
{
return 'guest' == $this->role;
}
public function isUser(): bool
{
return 'user' == $this->role;
}
public function isAdmin(): bool
{
return 'admin' == $this->role;
}
public function hasRole($check): bool
{
if (is_array($check)) {
return in_array($this->getRole(), $check);
}
return $this->getRole() == $check;
}
public function getRole(): string
{
return $this->role;
}
public function hasPermissions($check): bool
{
if (empty($check)) {
return true;
}
if (is_array($check)) {
return count(array_intersect($check, $this->getPermissions())) == count($check);
}
return in_array($check, $this->getPermissions());
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function setUsername(string $username)
{
$this->username = $username;
}
public function getUsername(): string
{
return $this->username;
}
public function setHomedir(string $homedir)
{
$this->homedir = $homedir;
}
public function getHomeDir(): string
{
return $this->homedir;
}
public function setRole(string $role)
{
$this->checkValidRole($role);
$this->role = $role;
}
public function setPermissions($permissions, $encoded = false)
{
if ($encoded) {
$permissions = explode('|', $permissions);
}
$this->checkValidPermissions($permissions);
$this->permissions = $permissions;
}
public function getPermissions($encoded = false)
{
return $encoded ? implode('|', $this->permissions) : $this->permissions;
}
public function jsonSerialize()
{
return [
'role' => $this->getRole(),
'permissions' => $this->getPermissions(),
'homedir' => $this->getHomeDir(),
'username' => $this->getUsername(),
'name' => $this->getName(),
];
}
protected function checkValidRole($role)
{
if (! in_array($role, $this->available_roles)) {
throw new \Exception("User role {$role} does not exists.");
}
return true;
}
protected function checkValidPermissions(array $permissions)
{
foreach ($permissions as $permission) {
if ($permission && ! in_array($permission, $this->available_permissions)) {
throw new \Exception("Permission {$permission} does not exists.");
}
}
return true;
}
}

View File

@@ -0,0 +1,23 @@
<?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;
use Filegator\Utils\Collection;
class UsersCollection implements \JsonSerializable
{
use Collection;
public function addUser(User $user)
{
$this->add($user);
}
}