Giter Club home page Giter Club logo

members's Introduction

Membership Extension for Bolt

Members provides functionality to enable restricted access to parts of Bolt's front end.

There are a couple of things you need to take care of in order to set up Members and membership access:

  • Enable at least one provider (can be "Local" email address & password, or remote OAuth2 provider)
  • Edit your templates to secure sections of your site (there are a number of defaults templates you can use included)

Twig Templates

Authentication & Registration

The fastest way to get moving is to implement parent templates that use Twig blocks. The included partials will render their content in these blocks.

To do this, edit app/config/extensions/members.bolt.yml to specify relative paths to these files in your theme directory, e.g.:

templates:
    profile:
        parent: profile.twig
    authentication:
        parent: authentication.twig

For profile registration, editing, and viewing, the parent template (shown as profile.twig above) just needs the following block where you would like to render content:

    {% block members %}
    {% endblock members %}

For login, logout, and password recovery (shown as authentication.twig above) just needs the following block where you would like to render content:

    {% block login %}
    {% endblock login %}

    {% block logout %}
    {% endblock logout %}

    {% block recovery %}
    {% endblock recovery %}

For more precise control over partials, the following Twig functions give you shortcuts you can use for much of the functionality:

  • {{ member_providers() }}
  • {{ members_auth_switcher([true | false]) }}
  • {{ members_auth_associate() }}
  • {{ members_auth_login([true | false]) }}
  • {{ members_auth_logout([true | false]) }}
  • {{ members_profile_edit() }}
  • {{ members_profile_register() }}

Member Only Site Access

The following Twig functions are available:

  • {{ is_member() }} — Returns true if the broswer session is logged in
  • {{ member() }} — Returns an entity array of the member's account information
  • {{ member_meta() }} — Returns an entity array of the member's meta data (if any)
  • {{ member_has_role('role_name') }} — Returns true if the logged in

For example:

    {% set member = member() %}
    {% if is_member() and member_has_role('participant') %}
        Hello, {{ member.displayname }}. You were last seen at {{ member.lastseen }},
        logging on from {{ member.lastip }}, and your email is {{ member.email }}
    {% endif %}

Roles

You can define roles to members in app/config/extensions/members.bolt.yml like so:

roles:
    member:
        admin: Administrator
        participant: Participant

Other extensions can also add roles relevant to their functionality via events (see below).

Extending Members

Members is event driven and provides a number of dispatcher that your own extension can listen for, and affect the behaviour of different parts of the Members extension.

There is an example repository called Members Addon Example that shows some of the available behaviour.

Adding Meta Fields

use Bolt\Extension\Bolt\Members\Event\MembersEvents;
use Bolt\Extension\Bolt\Members\Event\MembersProfileEvent;
    protected function subscribe(EventDispatcherInterface $dispatcher)
    {
        $dispatcher->addListener(MembersEvents::MEMBER_PROFILE_PRE_SAVE, [$this, 'onProfileSave']);
    }

    /**
     * @param MembersProfileEvent $event
     */
    public function onProfileSave(MembersProfileEvent $event)
    {
        $fields = [
            'website',
            'address_street',
            'address_street_meta',
            'address_city',
            'address_state',
            'address_country',
            'phone_number',
        ];
        $event->addMetaFieldNames($fields);
    }

Adding Roles

To add roles to Member via your extension, you can add a listener to the MembersEvents::MEMBER_ROLE event.

You will need the following use statements at the top of your PHP class file:

use Bolt\Extension\Bolt\Members\AccessControl\Role;
use Bolt\Extension\Bolt\Members\Event\MembersEvents;
use Bolt\Extension\Bolt\Members\Event\MembersRolesEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

In your extension's subscribe() function you can define the listener and pass in the callback that will be called when the event is triggered, e.g.

    protected function subscribe(EventDispatcherInterface $dispatcher)
    {
        $dispatcher->addListener(MembersEvents::MEMBER_ROLE, [$this, 'myCallbackRunction']);
    }

The callback function will be passed a MembersRolesEvent object, you can then use the events addRole() function to add \Bolt\Extension\Bolt\Members\AccessControl\Role objects, e.g.

    public function myCallbackRunction(MembersRolesEvent $event)
    {
        $event->addRole(new Role('koala', 'Friendly Koalas'));
        $event->addRole(new Role('dropbear', 'Deady Drop Bears'));
    }

The Role class takes two parameters, programmatic name and a display name that will be used in the members admin pages in Bolt's backend admin section.

members's People

Contributors

gwendolenlynch avatar jadwigo avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.