How to know if user is logged in from inside template code

Author Message

Michael Scofield

Tuesday 15 July 2008 9:08:39 pm

Hi all,

Sorry for the simple question, but I've googled a lot and couldn't find how to do it.

Inside a template, what is the best and faster way to know if a user is logged in at the website?

I want to limit some functionallity of my website to logged in users.

In a website made without eZ Publish we probably could check it using $_SESSION variables. What about with eZ Publish?

Btw, I found a fetch function that appears to do that:

{if fetch( 'content', 'access', hash( ...) ) }

{/if}

Should I use that or there is a better solution?

Thank you
Michael Scofield

Bård Farstad

Tuesday 15 July 2008 11:29:19 pm

You can try this template code:

{if $current_user.is_logged_in}
LOGGED IN
{else}
NOT LOGGED IN
{/if}

Also remember to not have cache blocks around this code, or if you do remember to use the user_id part of the cache key.

-bård

Documentation: http://ez.no/doc

André R.

Wednesday 16 July 2008 1:29:41 am

Or if you only need to know if the user is logged in (no need for user id or user name), then use $current_user.is_logged_in as cache key.

Also note that inside node/system templates you'll need to fetch the user first, so use something like this first*:

{if is_unset( $current_user )}
    {def $current_user = fetch('user', 'current_user') }
{/if}

But if you do want to check if a user has access to a certain module / function, then fetch('content', 'access') is a cleaner solution.

* node templates use content view cache, witch is unique pr user rights, but not user id. So you have to disable view cache (search for cache_ttl) if you need to use name / id in a node template.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 02:59:08
Script start
Timing: Jan 18 2025 02:59:08
Module start 'layout'
Timing: Jan 18 2025 02:59:08
Module start 'content'
Timing: Jan 18 2025 02:59:08
Module end 'content'
Timing: Jan 18 2025 02:59:08
Script end

Main resources:

Total runtime0.6453 sec
Peak memory usage4,096.0000 KB
Database Queries59

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 589.2891152.6563
Module start 'layout' 0.00620.0033 741.945339.5078
Module start 'content' 0.00940.6347 781.4531580.2188
Module end 'content' 0.64410.0012 1,361.671912.0938
Script end 0.6453  1,373.7656 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.5251160.0002
Check MTime0.00130.2027160.0001
Mysql Total
Database connection0.00080.129510.0008
Mysqli_queries0.594792.1637590.0101
Looping result0.00050.0745570.0000
Template Total0.611194.720.3055
Template load0.00220.337820.0011
Template processing0.608994.353120.3044
Template load and register function0.00010.021710.0001
states
state_id_array0.00140.221010.0014
state_identifier_array0.00070.113620.0004
Override
Cache load0.00190.2893310.0001
Sytem overhead
Fetch class attribute can translate value0.00050.078930.0002
Fetch class attribute name0.00110.174550.0002
XML
Image XML parsing0.00110.168830.0004
class_abstraction
Instantiating content class attribute0.00000.001250.0000
General
dbfile0.00170.2668240.0001
String conversion0.00000.001340.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 16
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs