1

I know this is a basic question, but i never use Craft this way.

I need to show a section ( list of items ) in the frontend but only available for admin users ( that are already logged in the control panel ).

How can achieve this restriction from the template?

Thanks!

1

One option is to do this:

{% if currentUser.admin ?? false %}

    ...this code can only be seen by admins...

{% endif %}

currentUser is a Global Variable on which you can pass the admin property. ?? is a null-coalescing operator which returns false if the currentUser (if there is one) is not an admin.

Edit:

So if you want to show this section to anyone logged in to the admin or client account in Craft Client, I'd recommend doing the following:

{% if currentUser ?? false %}

    ...this code can only be seen by admins...

{% endif %}

Basically, this says if the current user is logged in, display the contents. Since the only ones with logins are you and the client, this will only display the content to you!


If you'd like for the whole page to only display for certain users (for any unaccepted visitors it will throw a Craft 403 error page), you can add something like this to the top of your template:

{% requirePermission "permissionHandle" %}

(You can find a full list of permissions here.)

I'd recommend the accessCp permission handle if you're wanting to block everyone but the client and admin accounts for Craft Client.

7
  • Thanks for your answer! i will check your solution, and i´m checking right now with requirePermission craftcms.com/docs/templating/requirepermission – Sebastian Mar 13 '18 at 21:35
  • {% requirePermission %} could possibly do the trick for you, yes, but you'd be allowing access based on whether they have some specific permission vs if they are admins or not. Which isn't a bad thing but just wanted to clarify the difference... – Jalen Davenport Mar 13 '18 at 21:38
  • I´m using Craft client license, so i have admin account and client account only. Admin account and client account should have both access. With this in mind, what option whould you recommend? I set {% requirePermission "accessCp" %} as the needed permission, since both account have access to cp. – Sebastian Mar 13 '18 at 21:43
  • @Sebastian I updated my answer, check it out ^ – Jalen Davenport Mar 13 '18 at 21:48
  • 1
    Yes, but the error page it´s not a problem in this case, because the url to access it´s not available to public. I see your solution works great for pieces of content too, like displaying some areas for users only. The {% requirePermission %} blocks the whole thing :) – Sebastian Mar 13 '18 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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