Skip to content

Commit

Permalink
Merge pull request #13 from nliautaud/pico-2.0
Browse files Browse the repository at this point in the history
Pico 2.0 support
  • Loading branch information
nliautaud authored Jul 4, 2018
2 parents 62cfe21 + 4d79375 commit dec2391
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 54 deletions.
52 changes: 26 additions & 26 deletions 45-PicoUsers/PicoUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/
require_once('password.php');
/**
* A hierarchical users and rights system plugin for Pico.
* A hierarchical users and rights system plugin for Pico 2.
*
* @author Nicolas Liautaud
* @link https://github.com/nliautaud/pico-users
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
* @version 0.2.3
*/
class PicoUsers extends AbstractPicoPlugin
{
const API_VERSION = 2;

private $user;
private $users;
private $rights;
Expand Down Expand Up @@ -52,51 +53,50 @@ public function onRequestUrl(&$url)
/**
* Hide 403 and unauthorized pages.
*
* Triggered after Pico has read all known pages
* See {@link DummyPlugin::onSinglePageLoaded()} for details about the
* structure of the page data.
* Triggered after Pico has discovered all known pages
*
* @see Pico::getPages()
* @see Pico::getCurrentPage()
* @see Pico::getPreviousPage()
* @see Pico::getNextPage()
* @param array[] &$pages data of all known pages
* @param array|null &$currentPage data of the page being served
* @param array|null &$previousPage data of the previous page
* @param array|null &$nextPage data of the next page
* @see DummyPlugin::onPagesLoading()
* @see DummyPlugin::onPagesLoaded()
* @param array[] &$pages list of all known pages
* @return void
*/
public function onPagesLoaded(
array &$pages,
array &$currentPage = null,
array &$previousPage = null,
array &$nextPage = null
) {
public function onPagesDiscovered(array &$pages) {
foreach ($pages as $id => $page ) {
if ($id == '403' || !$this->hasRight($page['url'], true)) {
unset($pages[$id]);
}
}
}
/**
* Add various twig variables.
*
* Triggered before Pico renders the page
*
* @see Pico::getTwig()
* @see DummyPlugin::onPageRendered()
* @param Twig_Environment &$twig twig template engine
* @param array &$twigVariables template variables
* @param string &$templateName file name of the template
* @see DummyPlugin::onPageRendered()
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
* @return void
*/
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
public function onPageRendering(&$templateName, array &$twigVariables)
{
$twigVariables['login_form'] = $this->html_form();
if ($this->user) {
$twigVariables['user'] = $this->user;
$twigVariables['username'] = basename($this->user);
$twigVariables['usergroup'] = dirname($this->user);
}
// {{ user_has_right('rule') }}
}
/**
* Add {{ user_has_right('rule') }} twig function.
*
* Triggered when Pico registers the twig template engine
*
* @see Pico::getTwig()
* @param Twig_Environment &$twig Twig instance
* @return void
*/
public function onTwigRegistered(Twig_Environment &$twig)
{
$twig->addFunction(new Twig_SimpleFunction('user_has_right', array($this, 'hasRight')));
}

Expand Down
53 changes: 25 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,31 @@ Features login and logout system, unlimited users and hierarchical user groups,

## Installation

Copy the `PicoUsers` directory to the `plugins/` directory of your Pico Project.
Copy the `45-PicoUsers` directory to the `plugins/` directory of your Pico Project.

*Note that the directory numerical prefix is needed to ensure the Pico plugins loading order.*

*Note that the directory numerical prefix is needed to ensure the Pico plugins loading order.*

## Settings

Users, rights and others settings should be stored in Pico `config/config.php` file.

```php
$config['users'] = array(
'john' => '$2a$08$kA7StQeZgyuEJnIrvypwEeuyjSqxrvavOBSf33n4yWSJFhbQAkO1W',
'editors' => array(
'marc' => '$2a$08$V/En.8vnZFWGOwXvDvFYsO8PTq.KSA5eYTehICnErFnd3V.zzsj.K',
'admins' => array(
'john' => '$2a$08$bCVTtxqH/VxWuHqrZQ/QiOEcvvbVjl9UD3mTf.7AnXhS90DXj5IZ6'
)
),
'family' => array(
'mum' => '$2a$08$qYtklDGOy/cCK1K0Zh8qROkFW3/V7gFgve.0GQv/sPmLYHm0jEiTi',
'dad' => '$2a$08$Eu7aKmOLz1Jme4iReWp6r.TfI2K3V3DyeRDV8oBS6gMtDPessqqru'
)
);
$config['rights'] = array(
'family-things' => 'family',
'secret/infos' => 'editors',
'secret/infos/' => 'editors/admins',
'just-for-john' => 'john'
);
Users, rights and others settings can be defined in Pico config file.

```yml
users:
john: $2a$08$kA7StQeZgyuEJnIrvypwEeuyjSqxrvavOBSf33n4yWSJFhbQAkO1W
editors:
marc: $2a$08$V/En.8vnZFWGOwXvDvFYsO8PTq.KSA5eYTehICnErFnd3V.zzsj.K
admins:
john: $2a$08$bCVTtxqH/VxWuHqrZQ/QiOEcvvbVjl9UD3mTf.7AnXhS90DXj5IZ6
family:
mum: $2a$08$qYtklDGOy/cCK1K0Zh8qROkFW3/V7gFgve.0GQv/sPmLYHm0jEiTi
dad: $2a$08$Eu7aKmOLz1Jme4iReWp6r.TfI2K3V3DyeRDV8oBS6gMtDPessqqru
rights:
family-things: family
secret/infos: editors
secret/infos/: editors/admins
just-for-john: john
```
### Users and groups
Expand All @@ -58,12 +55,12 @@ The setting `rights` is a flat list of rules, associating a rule to a user or a

> Note that you can target a specific path or all sub-paths by using or not a trailing slash.

PicoUsers will use these rules as *rights to view a given page*, but other plugins may define other meanings. Edits rights in [Pico Content Editor](https://github.com/nliautaud/pico-content-editor) for example :
PicoUsers will use these rules as *rights to view pages*, but other plugins may define other meanings, like editing in [Pico Content Editor](https://github.com/nliautaud/pico-content-editor).

```php
$config['rights'] = array(
'PicoContentEditor/save' => 'admins'
);
```yml
rights:
some/path/page: editors
PicoContentEditor/save: admins
```

You can check for a specific right in your theme with the following Twig function :
Expand Down

0 comments on commit dec2391

Please sign in to comment.