3

I was wondering if someone could take a minute for a newbie to show the template code for a section index page that needs to be private - accessible only to "clients" (pre-registered as users in a private group - I am assuming).

I am confused about the permissions in the users section of the CP as they only show permissions to edit/view assets etc.... not templates.

Someone asked a similar question and the response was "yes it is easy - go look at the User Model" - however I am just starting to figure all this out so need a little help!!

Thank you

1 Answer 1

6

Here is a basic template that requires login, and checks that the currently logged-in user is in the user group 'clients'.

{% extends "_layout" %}
{% requireLogin %}    

{% block content %}

    {% if currentUser.isInGroup( craft.userGroups.getGroupByHandle('clients') ) %}

        {# Only viewable by logged in users that belong to 'clients' user group #}    
        Restricted client content goes here.

    {% else %}

        {# Viewable by logged in users not in the user group 'clients' #}
        Notice to other user groups that this content is restricted to clients only.

    {% endif %}

{% endblock %}

You may also want to check out this question for more examples.

3
  • 1
    Apparently I have to avoid saying "thanks" but hugely appreciated!
    – Liam
    Commented Oct 22, 2014 at 17:36
  • Glad to help ;) Commented Oct 22, 2014 at 17:40
  • Some of this stuff is hard to track down in the docs, like .getGroupByHandle(), and requires you to go to the full class reference documentation to find. Generally things in there can be referenced in twig by the namespace (i.e. craft) followed by the service class (i.e. userGroups), followed by the method (.getGroupsByHandle). Commented Oct 22, 2014 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.