WordPress adapter included in the main repo

This commit is contained in:
Milos Stojanovic 2020-05-09 21:04:13 +02:00
parent e2199c812f
commit 15c386fe08
5 changed files with 170 additions and 6 deletions

View File

@ -2,9 +2,11 @@
## Upcoming...
* WordPress Auth adapter is now included in the main repo
* New config: 'guest_redirection' (useful for external auth adapters)
* More css classes so the elements can be easily hidden (e.g. add_to_head style)
* Integrated https://github.com/filegator/filegator/pull/74
* Updated docs
## 7.3.5 - 2020-04-18

View File

@ -82,7 +82,7 @@ This is read-only demo with guest account enabled.
## Features & Goals
- Multiple storage adapters (Local, FTP, Amazon S3, Dropbox, DO Spaces, Azure Blob and many others via [Flysystem](https://github.com/thephpleague/flysystem))
- Multiple auth adapters with roles and permissions (Store users in json file or database)
- Multiple auth adapters with roles and permissions (Store users in json file, database or use WordPress)
- Multiple session adapters (Native File, Pdo, Redis, MongoDB, Memcached and others via [Symfony](https://github.com/symfony/symfony/tree/4.4/src/Symfony/Component/HttpFoundation/Session/Storage/Handler))
- Single page front-end (built with [Vuejs](https://github.com/vuejs/vue), [Bulma](https://github.com/jgthms/bulma) and [Buefy](https://github.com/buefy/buefy))
- Chunked uploads (built with [Resumable.js](https://github.com/23/resumable.js))

View File

@ -0,0 +1,137 @@
<?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;
class WPAuth implements Service, AuthInterface
{
protected $permissions = [];
protected $private_repos = false;
public function init(array $config = [])
{
define('WP_USE_THEMES', false);
require_once(rtrim($config['wp_dir'], '/').'/wp-blog-header.php');
$this->permissions = isset($config['permissions']) ? (array)$config['permissions'] : [];
$this->private_repos = isset($config['private_repos']) ? (bool)$config['private_repos'] : false;
}
public function user(): ?User
{
$wpuser = wp_get_current_user();
if ($wpuser->exists()) {
return $this->transformUser($wpuser);
}
return $this->getGuest();
}
public function transformUser($wpuser): User
{
$user = new User();
$user->setUsername($wpuser->data->user_login);
$user->setName($wpuser->data->display_name);
$user->setRole('user');
$user->setPermissions($this->permissions);
$user->setHomedir('/');
// private repositories for each user?
if ($this->private_repos) {
$user->setHomedir('/'.$wpuser->data->user_login);
}
// ...but not for wp admins
if (in_array('administrator', (array)$wpuser->roles)) {
$user->setHomedir('/');
}
return $user;
}
public function authenticate($username, $password): bool
{
$creds = array(
'user_login' => $username,
'user_password' => $password,
'remember' => true
);
$wpuser = wp_signon($creds, false);
if (!is_wp_error($wpuser)) {
wp_set_current_user($wpuser->data->ID);
$this->transformUser($wpuser);
return true;
}
return false;
}
public function forget()
{
wp_logout();
}
public function store(User $user)
{
return null; // not used
}
public function update($username, User $user, $password = ''): User
{
if ($password && get_current_user_id()) {
wp_set_password($password, get_current_user_id());
}
return new User(); // not used
}
public function add(User $user, $password): User
{
return new User(); // not used
}
public function delete(User $user)
{
return true; // not used
}
public function find($username): ?User
{
return null; // not used
}
public function allUsers(): UsersCollection
{
return new UsersCollection(); // not used
}
public function getGuest(): User
{
$guest = new User();
$guest->setUsername('guest');
$guest->setName('Guest');
$guest->setRole('guest');
$guest->setHomedir('/');
$guest->setPermissions([]);
return $guest;
}
}

View File

@ -58,13 +58,38 @@ At the end, open `configuration.php` and update AuthInterface handler to reflect
],
```
## Custom Authentication using 3rd party (WordPress or similar)
## Configuring Auth service to use WordPress
Replace your current Auth handler in `configuration.php` file like this:
```
'Filegator\Services\Auth\AuthInterface' => [
'handler' => '\Filegator\Services\Auth\Adapters\WPAuth',
'config' => [
'wp_dir' => '/var/www/my_wordpress_site/',
'permissions' => ['read', 'write', 'upload', 'download', 'batchdownload', 'zip'],
'private_repos' => false,
],
],
```
Adjust in the config above:
- `wp_dir` should be the directory path of your wordpress installation
- `permissions` is the array of permissions given to each user
- `private_repos` each user will have its own sub folder, admin will see everything (false/true)
Note: With more recent versions of FileGator you can set `guest_redirection` in your `configuration.php` to redirect logged-out users back to your WP site:
```
'frontend_config' => [
...
'guest_redirection' => 'http://example.com/wp-admin/',
...
]
```
## Custom Authentication using 3rd party
If you want to use FileGator as a part of another application, you probably already have users stored somewhere else. What you need in this case is to build a new custom Auth adapter that matches the [AuthInterface](https://github.com/filegator/filegator/blob/master/backend/Services/Auth/AuthInterface.php) to connect those two. This new adapter will try to authenticate users in your application and translate each user into filegator [User](https://github.com/filegator/filegator/blob/master/backend/Services/Auth/User.php) object.
You can look at this simple [WordPress auth adapter](https://github.com/filegator/wp_auth_adapter) to see how all this works.
## API authentication
Front-end will use session based authentication to authenticate and consume the back-end.

View File

@ -23,7 +23,7 @@ File upload supports drag&drop, progress bar, pause and resume. Upload is chunke
## Features & Goals
- Multiple storage adapters (Local, FTP, Amazon S3, Dropbox, DO Spaces, Azure Blob and many others via [Flysystem](https://github.com/thephpleague/flysystem))
- Multiple auth adapters with roles and permissions (Store users in json file or database)
- Multiple auth adapters with roles and permissions (Store users in json file, database or use WordPress)
- Multiple session adapters (Native File, Pdo, Redis, MongoDB, Memcached and others via [Symfony](https://github.com/symfony/symfony/tree/4.4/src/Symfony/Component/HttpFoundation/Session/Storage/Handler))
- Single page front-end (built with [Vuejs](https://github.com/vuejs/vue), [Bulma](https://github.com/jgthms/bulma) and [Buefy](https://github.com/buefy/buefy))
- Chunked uploads (built with [Resumable.js](https://github.com/23/resumable.js))