Skip to content

Commit

Permalink
ACL 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Aug 19, 2020
1 parent 67d76d9 commit db99591
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 140 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<a name="1.3.0"></a>
# [1.3.0](https://github.com/flextype-plugins/acl) (2020-08-19)

### Features

* **core** update code base for new Flextype 0.9.10

<a name="1.2.1"></a>
# [1.2.1](https://github.com/flextype-plugins/acl) (2020-08-05)

Expand Down
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 align="center">ACL Plugin for <a href="http://flextype.org/">Flextype</a></h1>

<p align="center">
<a href="https://github.com/flextype-plugins/acl/releases"><img alt="Version" src="https://img.shields.io/github/release/flextype-plugins/acl.svg?label=version&color=black"></a> <a href="https://github.com/flextype-plugins/acl"><img src="https://img.shields.io/badge/license-MIT-blue.svg?color=black" alt="License"></a> <a href="https://github.com/flextype-plugins/acl"><img src="https://img.shields.io/github/downloads/flextype-plugins/acl/total.svg?color=black" alt="Total downloads"></a> <a href="https://github.com/flextype/flextype"><img src="https://img.shields.io/badge/Flextype-0.9.9-green.svg" alt="Flextype"></a> <a href=""><img src="https://img.shields.io/discord/423097982498635778.svg?logo=discord&color=black&label=Discord%20Chat" alt="Discord"></a>
<a href="https://github.com/flextype-plugins/acl/releases"><img alt="Version" src="https://img.shields.io/github/release/flextype-plugins/acl.svg?label=version&color=black"></a> <a href="https://github.com/flextype-plugins/acl"><img src="https://img.shields.io/badge/license-MIT-blue.svg?color=black" alt="License"></a> <a href="https://github.com/flextype-plugins/acl"><img src="https://img.shields.io/github/downloads/flextype-plugins/acl/total.svg?color=black" alt="Total downloads"></a> <a href="https://github.com/flextype/flextype"><img src="https://img.shields.io/badge/Flextype-0.9.10-green.svg" alt="Flextype"></a> <a href=""><img src="https://img.shields.io/discord/423097982498635778.svg?logo=discord&color=black&label=Discord%20Chat" alt="Discord"></a>
</p>

## Features
Expand All @@ -15,7 +15,7 @@ The following dependencies need to be downloaded and installed for ACL Plugin.

| Item | Version | Download |
|---|---|---|
| [flextype](https://github.com/flextype/flextype) | 0.9.9 | [download](https://github.com/flextype/flextype/releases) |
| [flextype](https://github.com/flextype/flextype) | 0.9.10 | [download](https://github.com/flextype/flextype/releases) |
| [site](https://github.com/flextype-plugins/site) | >=1.0.0 | [download](https://github.com/flextype-plugins/site/releases) |
| [twig](https://github.com/flextype-plugins/twig) | >=1.0.0 | [download](https://github.com/flextype-plugins/twig/releases) |

Expand Down Expand Up @@ -56,7 +56,7 @@ The following dependencies need to be downloaded and installed for ACL Plugin.

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclIsUserLoggedInMiddleware(['container' => $flextype,
'redirect' => 'another.route.name']));
Expand All @@ -74,7 +74,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclAccountsIsUserLoggedInRolesInMiddleware(['container' => $flextype,
'roles' => 'admin, moderator'
Expand All @@ -93,7 +93,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclIsUserLoggedInEmailsInMiddleware(['container' => $flextype,
'emails' => '[email protected], [email protected]'
Expand All @@ -112,7 +112,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclIsUserLoggedInUuidInMiddleware(['container' => $flextype,
'uuids' => 'ea7432a3-b2d5-4b04-b31d-1c5acc7a55e2, d549af27-79a0-44f2-b9b1-e82b47bf87e2'
Expand All @@ -129,7 +129,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclIsUserNotLoggedInMiddleware(['container' => $flextype,
'redirect' => 'another.route.name']));
Expand All @@ -147,7 +147,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclAccountsIsUserLoggedInRolesNotInMiddleware(['container' => $flextype,
'roles' => 'admin, moderator'
Expand All @@ -166,7 +166,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclIsUserLoggedInEmailsNotInMiddleware(['container' => $flextype,
'emails' => '[email protected], [email protected]'
Expand All @@ -185,7 +185,7 @@ $app->get('/my-route', 'MyController:method()')

#### Example
```
$app->get('/my-route', 'MyController:method()')
$flextype->get('/my-route', 'MyController:method()')
->setName('my.route.name')
->add(new AclIsUserLoggedInUuidNotInMiddleware(['container' => $flextype,
'uuids' => 'ea7432a3-b2d5-4b04-b31d-1c5acc7a55e2, d549af27-79a0-44f2-b9b1-e82b47bf87e2'
Expand Down Expand Up @@ -513,54 +513,54 @@ You may restrict access for specific users to your specific code in the PHP.
#### Run private code for logged in users

```php
if ($flextype->acl->isUserLoggedIn()) {
if ($flextype->container('acl')->isUserLoggedIn()) {
// Private code here..
}
```

#### Run private content for users with roles: admin and student

```php
if ($flextype->acl->isUserLoggedInRolesIn('admin, student')) {
if ($flextype->container('acl')->isUserLoggedInRolesIn('admin, student')) {
// Private code here..
}
```

#### Run private code for users with uuids ea7432a3-b2d5-4b04-b31d-1c5acc7a55e2 and d549af27-79a0-44f2-b9b1-e82b47bf87e2

```php
if ($flextype->acl->isUserLoggedInUuidIn('ea7432a3-b2d5-4b04-b31d-1c5acc7a55e2, d549af27-79a0-44f2-b9b1-e82b47bf87e2') {
if ($flextype->container('acl')->isUserLoggedInUuidIn('ea7432a3-b2d5-4b04-b31d-1c5acc7a55e2, d549af27-79a0-44f2-b9b1-e82b47bf87e2') {
// Private content here..
}
```

#### Run private code for users with emails [email protected], [email protected]

```php
if ($flextype->acl->isUserLoggedInEmailIn('[email protected], [email protected]')) {
if ($flextype->container('acl')->isUserLoggedInEmailIn('[email protected], [email protected]')) {
// Private content here..
}
```

#### Show logged in email

```php
echo 'Hello ' . $flextype->acl->getUserLoggedInEmail();
echo 'Hello ' . $flextype->container('acl')->getUserLoggedInEmail();
```

#### Show logged in uuid

```php
echo 'Hello ' . $flextype->acl->getUserLoggedInEmail();
echo 'your uuid: ' . $flextype->acl->getUserLoggedInUuid();
echo 'Hello ' . $flextype->container('acl')->getUserLoggedInEmail();
echo 'your uuid: ' . $flextype->container('acl')->getUserLoggedInUuid();
```

#### Show logged in roles

```php
echo 'Hello ' . $flextype->acl->getUserLoggedInEmail();
echo 'your uuid: ' . $flextype->acl->getUserLoggedInUuid();
echo 'and your roles: ' . $flextype->acl->getUserLoggedInRoles();
echo 'Hello ' . $flextype->container('acl')->getUserLoggedInEmail();
echo 'your uuid: ' . $flextype->container('acl')->getUserLoggedInUuid();
echo 'and your roles: ' . $flextype->container('acl')->getUserLoggedInRoles();
```

## LICENSE
Expand Down
17 changes: 14 additions & 3 deletions app/Models/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@
namespace Flextype\Plugin\Acl\Models;

use Flextype\Component\Session\Session;
use Flextype\App\Foundation\Container;
use function array_intersect;
use function array_map;
use function explode;
use function in_array;


class Acl extends Container
class Acl
{
/**
* Flextype Application
*/
protected $flextype;

/**
* __construct
*/
public function __construct($flextype)
{
$this->flextype = $flextype;
}

/**
* Check is user logged in
*
Expand Down
6 changes: 3 additions & 3 deletions dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
/**
* Add ACL Model to Flextype container
*/
$flextype['acl'] = static function ($container) {
return new Acl($container);
$flextype->container()['acl'] = static function () use ($flextype) {
return new Acl($flextype);
};

/**
* Add ACL Twig Extension to Flextype container
*/
$flextype->twig->addExtension(new AclTwigExtension($flextype));
$flextype->container('twig')->addExtension(new AclTwigExtension($flextype));
16 changes: 8 additions & 8 deletions entries_acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@

use Flextype\Component\Session\Session;

$flextype->emitter->addListener('onEntryAfterInitialized', function() use ($flextype) {
$flextype->container('emitter')->addListener('onEntryAfterInitialized', function() use ($flextype) {

// Get current entry
$entry = $flextype->entries->entry;
$entry = $flextype->container('entries')->entry;

// Set ACL rules based on accounts uuids
if (isset($entry['acl']['accounts']['uuids'])) {
if (!$flextype->acl->isUserLoggedInUuidsIn($entry['acl']['accounts']['uuids'])) {
$flextype->entries->entry = [];
if (!$flextype->container('acl')->isUserLoggedInUuidsIn($entry['acl']['accounts']['uuids'])) {
$flextype->container('entries')->entry = [];
}
}

// Set ACL rules based on accounts emails
if (isset($entry['acl']['accounts']['emails'])) {
if (!$flextype->acl->isUserLoggedInEmailsIn($entry['acl']['accounts']['emails'])) {
$flextype->entries->entry = [];
if (!$flextype->container('acl')->isUserLoggedInEmailsIn($entry['acl']['accounts']['emails'])) {
$flextype->container('entries')->entry = [];
}
}

// Set ACL rules based on accounts roles
if (isset($entry['acl']['accounts']['roles'])) {
if (!$flextype->acl->isUserLoggedInRolesIn($entry['acl']['accounts']['roles'])) {
$flextype->entries->entry = [];
if (!$flextype->container('acl')->isUserLoggedInRolesIn($entry['acl']['accounts']['roles'])) {
$flextype->container('entries')->entry = [];
}
}
});
19 changes: 11 additions & 8 deletions middlewares/AclIsUserLoggedInEmailInMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

namespace Flextype\Plugin\Acl\Middlewares;

use Flextype\App\Foundation\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class AclIsUserLoggedInEmailInMiddleware extends Container
class AclIsUserLoggedInEmailInMiddleware
{
/**
* Flextype Application
*/
protected $flextype;

/**
* Middleware Settings
*/
Expand All @@ -23,11 +27,10 @@ class AclIsUserLoggedInEmailInMiddleware extends Container
/**
* __construct
*/
public function __construct($settings)
public function __construct($flextype, $settings)
{
parent::__construct($settings['container']);

$this->settings = $settings;
$this->flextype = $flextype;
$this->settings = $settings;
}

/**
Expand All @@ -39,10 +42,10 @@ public function __construct($settings)
*/
public function __invoke(Request $request, Response $response, callable $next) : Response
{
if ($this->acl->isUserLoggedInEmailIn($this->settings['emails'])) {
if ($this->flextype->container('acl')->isUserLoggedInEmailIn($this->settings['emails'])) {
$response = $next($request, $response);
} else {
$response = $response->withRedirect($this->router->pathFor($this->settings['redirect']));
$response = $response->withRedirect($this->flextype->container('router')->pathFor($this->settings['redirect']));
}

return $response;
Expand Down
27 changes: 15 additions & 12 deletions middlewares/AclIsUserLoggedInEmailNotInMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@

namespace Flextype\Plugin\Acl\Middlewares;

use Flextype\App\Foundation\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class AclIsUserLoggedInEmailNotInMiddleware extends Container
class AclIsUserLoggedInEmailNotInMiddleware
{
/**
* Middleware Settings
* Flextype Application
*/
protected $settings;
protected $flextype;

/**
* __construct
* Middleware Settings
*/
public function __construct($settings)
{
parent::__construct($settings['container']);
protected $settings;

$this->settings = $settings;
}
/**
* __construct
*/
public function __construct($flextype, $settings)
{
$this->flextype = $flextype;
$this->settings = $settings;
}

/**
* __invoke
Expand All @@ -39,10 +42,10 @@ public function __construct($settings)
*/
public function __invoke(Request $request, Response $response, callable $next) : Response
{
if (!$this->acl->isUserLoggedInEmailIn($this->settings['emails'])) {
if (!$this->flextype->container('acl')->isUserLoggedInEmailIn($this->settings['emails'])) {
$response = $next($request, $response);
} else {
$response = $response->withRedirect($this->router->pathFor($this->settings['redirect']));
$response = $response->withRedirect($this->flextype->container('router')->pathFor($this->settings['redirect']));
}

return $response;
Expand Down
19 changes: 11 additions & 8 deletions middlewares/AclIsUserLoggedInMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

namespace Flextype\Plugin\Acl\Middlewares;

use Flextype\App\Foundation\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class AclIsUserLoggedInMiddleware extends Container
class AclIsUserLoggedInMiddleware
{
/**
* Flextype Application
*/
protected $flextype;

/**
* Middleware Settings
*/
Expand All @@ -23,11 +27,10 @@ class AclIsUserLoggedInMiddleware extends Container
/**
* __construct
*/
public function __construct($settings)
public function __construct($flextype, $settings)
{
parent::__construct($settings['container']);

$this->settings = $settings;
$this->flextype = $flextype;
$this->settings = $settings;
}

/**
Expand All @@ -39,10 +42,10 @@ public function __construct($settings)
*/
public function __invoke(Request $request, Response $response, callable $next) : Response
{
if ($this->acl->isUserLoggedIn()) {
if ($this->flextype->container('acl')->isUserLoggedIn()) {
$response = $next($request, $response);
} else {
$response = $response->withRedirect($this->router->pathFor($this->settings['redirect']));
$response = $response->withRedirect($this->flextype->container('router')->pathFor($this->settings['redirect']));
}

return $response;
Expand Down
Loading

0 comments on commit db99591

Please sign in to comment.