Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Can you set a viewable permission to a collection

Solved by Dean Bateman View solution

Started by Dean Bateman 3 years ago · 5 replies · 315 views
3 years ago

if I have a homepage that displays 5 slides

can I set each slide to be visible if you are a member of a group?

-slide 1 - group 1
-slide 2 - group 2
-slide 3 - group 1 and 2
etc

currently i display my slides with this code on the home page

TWIG
{% for child in collection %}
        {% include 'slide.html.twig' with {'blog': page, 'page': child, 'truncate': true} %}
{% endfor %}

and have 5 subpages slide.md containing

YAML
access:
   site.paid: true

this doesn't work, but not sure if it is possible and how to go about it, any hints would be great

last edited 02/24/23 by Dean Bateman
3 years ago

@dean_007, Grav's permissions does not provide the granularity on a image by image basis. A user either has or has no permission to see the page.

Having said that... Inside your loop, I suspect you can check the group of the user using grav.user.groups and only include the slide when the user belongs to the correct group.

See https://learn.getgrav.org/17/themes/theme-vars#user-object

last edited 02/24/23 by pamtbaau
3 years ago

Ok i can see where you are going with this

So if i were to create a variable like this

YAML
allowedGroups: 
  - group1
  - group2
TWIG
var display = false;

{% for child in collection %}
  {% if config.plugins.login.enabled and grav.user.username %}
      {% for g in allowedGroups  %}
        {% if g == grav.user.group  %}
          display =true
        {% else %}
          display = false
        {% endif %}
      {% endfor %}
    {% endif %}
  {% if display == true or allowedGroups is empty  %}
        {% include 'slide.html.twig' with {'blog': page, 'page': child, 'truncate': true} %}
  {% endif %}
  display = false
{% endfor %}

This was typed on my phone so might not be absolutely correct, but ill give it a go.

Thanks for the idea, ill report back with how i go.

last edited 02/24/23 by Dean Bateman
3 years ago

@dean_007, Small correction, its grav.user.groups which is an array.

3 years ago Solution

How I ended up creating the code was to create a home page

TWIG
{% for child in collection %}
    {% include 'slide.html.twig' with {'blog': page, 'page': child, 'truncate': true} %}
{% endfor %}

with slide child pages that contain within the front matter the following

TXT
allowedGroups:
   - project1

then create a user with a group of project1

if the slide group equals a user group it displays the slide else does nothing

TWIG
{% if config.plugins.login.enabled and grav.user.username %}
    {% for slideGroup in page.header.allowedGroups %}
    {# <p>slideGroup: {{ slideGroup }} </p> #}
        {% for userGroup in grav.user.groups  %}
        {# <p>userGroup: {{ userGroup }} </p> #}
            {% if userGroup == slideGroup %}
                {% include 'partials/slide-item.html.twig' %}
            {% else %}
                {# <p> FALSE </p> #}
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endif %}

i also added that if a user is not logged in it will display demo slides instead
these have no allowed groups added to the slide page

TWIG
{% if config.plugins.login.enabled and not grav.user.username %}
    {% if page.header.allowedGroups is empty %}
        {% include 'partials/slide-item.html.twig' %}
    {% endif %}
{% endif %}

hope it helps someone, Thanks for the help!

last edited 03/27/23 by Dean Bateman
3 years ago

@dean_007, Maybe I don't exactly understand your problem and solution, but it seems you are creating child pages for the home page solely to add a user group to its frontmatter and show an image based on that user group.

Instead of looping through a collection of child pages containing the user group inside their frontmatter, why not add the user group to the metafile of each image and loop to the images of the home page and read the user group in their metafile?

See Metafiles

last edited 03/27/23 by pamtbaau

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago