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

Detect if admin is logged in?

Started by Muut Archive 10 years ago · 6 replies · 1387 views
10 years ago

How can I detect is the admin logged in and put it as if to my twig file like:

TWIG
{% if admin.login %}
   // do something here
{% endif %}
10 years ago

{% if grav.user.title == "Administrator" %}
{{ "I ain't sayin she a gold-digger" }}
{% endif %}

Does this work? (assuming you set your title to Administrator when you made your admin account, which you can see in the admin panel in your user info.)

10 years ago

Or something like this?

TWIG
{% if grav.user.access.admin %}
{{ "Hello Admin" }}
{% endif %}
TWIG

{% if grav.user.access.admin.super == true %}
{{ "Hello Admin" }}
{% endif %}
---
10 years ago

@davidalanbruce thanks for ideas but none of these works. I also tried to follow login page from admin plugin you can find plugins/admin/themes/grav/templates/login.html.twigand reuse

TWIG
{% if notAuthorized %}
// do something
{% else %}
// do something else
{% endif %}

but these don't work as well 😕

10 years ago

There were a couple restrictions in the above for me.

  1. I had to be logged in (on the website) - The admin panel seemed to be separate, so I enabled a login for the page.

  2. I had to clear the cache, I've just realised that could be problematic if you viewed the page not logged in, then log in (as the changes wouldn't show unless the cache was cleared)

You'd have to find a better way or get around the above somehow. I'm not very familiar with how the caching works. Sorry I couldn't help more.

Edit: You could try cache_enable: false in the header frontmatter, which worked for me, at the cost of caching the page?

9 years ago

According to the source of GRAV the generated basename of the session is always the same, and the only extension that an administrator login gets is '-admin'. So this works:

INI
$adminCookie = session_name() . '-admin';

if (isset($_COOKIE[$adminCookie]) === false) {
   return;
}

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1332 9 years ago
Archive · by Muut Archive, 9 years ago
2 923 9 years ago
Archive · by Muut Archive, 9 years ago
2 4052 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1109 9 years ago