Show part of page in content for access group


#1

Hi
I got problem. I searched here and google and nothing helps. Seems I do step by step like I found but not works as I expect.

I got private area where I login and this works.
I got 2 access groups with:

access:
  site:
    aaa: 'true'
access:
  site:
    bbb: 'true'

My site I set to access to:

access:
site.aaa: true
site.bbb: true

and on this site in content I like to show part of it, so I use twig:

<p>AAA and BBB see this</p>
{% if grav.user.authorize('site.aaa') %}
<p>AAA only</p>
{% endif %}

In theory it should work but it does not and show up all for AAA and BBB.


#2

I gotcha buddy. This was a great question and I thought it would be educational for me to find out too. I had to examine Grav docs, Twig docs, Grav community, Stack Overflow and data dumps of running Grav instances but got there in the end!:

{% for group in grav.user.groups %}
	{% if group == "aaa" %}
    	<p>AAA only</p>
    {% endif %}
    {% if group == "bbb" %}
    	<p>BBB only</p>
	{% endif %}
{% endfor %}

Since we are using dynamic Twig variables we don’t want the processed Twig to be cached (otherwise a BBB user could see cached AAA content). So in the frontmatter of the page with your content, add:

never_cache_twig: true

Thank you @onemhz for helping me help you help me by helping you!

Note that if the user is in both groups, the above code will show content for both. BBB content could actually appear before AAA content if the group was created first. The order follows /user/config/groups.yaml or whatever array manipulation you do.

never_cache_twig: true is also “not compatible with twig_first: true currently because all processing is happening in the one Twig call.”