Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pico 2.0 support #13

Merged
merged 3 commits into from
Jul 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,32 +14,29 @@ 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.*

## 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 @@ -56,12 +53,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