# How to use session in tags?

**URL:** <https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882>\
**Category:** Radiant CMS\
**Created:** [August 26, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882 "2008-08-26T14:06:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 26, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/1 "2008-08-26T14:06:54Z")

</div>

Hello,

I would like to create something like that :  
[http://lists.radiantcms.org/pipermail/radiant/2006-September/001695.html](http://lists.radiantcms.org/pipermail/radiant/2006-September/001695.html)  
The goal is to display a ‘login box’ if the current user is not  
connected, else an ‘account box’.

But I can’t access to the ‘session’. I tried to enable the session like  
that :

module CoreExtension::SiteControllerExtensions  
def self.included(base)  
base.class\_eval do  
session :disabled =\> false  
end  
end  
end

(and include this file to SiteController)

It doesn’t work. I found an other discution about :  
[http://lists.radiantcms.org/pipermail/radiant/2007-November/007146.html](http://lists.radiantcms.org/pipermail/radiant/2007-November/007146.html)

But ir doesn’t work too… can you explain me, what is the best way to  
do my feature?

Thank you !  
Vincent

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 26, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/2 "2008-08-26T18:47:20Z")

</div>

Hi Vincent,

I ran into this just the other day myself and I’ve done some digging.  
In theory you could override a method in SiteController and use it to  
pass the current user to @page after find\_page() happens… You would  
have to disable the cache in that case and make sure the page is  
rendered for each user.

Or, you can use javascript to grab the user info from the controller.  
As for tags, you can create a tag that can output the necessary  
javascript. Then the problem is how to make it degrade gracefully…

Personally, I don’t want to disable the cache… but that will mean  
that I won’t be able to display the logged-in status for people  
without javascript.

-C

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 26, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/3 "2008-08-26T22:16:03Z")

</div>

Hey Casper,

Do you just store the name and association\_name in the cookie in plain  
text or is there some facility that you use to encrypt the data…?

-Chris

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 26, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/4 "2008-08-26T19:07:17Z")

</div>

Hi Vincent,

I’ve done with Javascript reading login information from a cookie like  
this:  
[http://pastie.org/260306](http://pastie.org/260306)

Cheers,  
Casper

(Cookie is a util object, btw, it’s not built into javascript)

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 26, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/5 "2008-08-26T22:22:30Z")

</div>

I just store it in plain text. The pages that need the user to be  
logged in is of a special page type, checking the session. Also, the  
controllers in my custom extension checks for login. That way, there  
is no reason to encrypt the cookie data in any way, it is just a  
display thing, not for access control.

Cheers,  
Casper

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 27, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/6 "2008-08-27T12:30:08Z")

</div>

Hello,

The javascript way was my last solution. It could be interessant…

Christopher D., I don’t follow what you mean by create ovveriding a  
method to add the current\_user?

Is there an other way to access to the current\_user var when I’m  
creating radius tags?

Thanks a lot !  
Vincent

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 27, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/7 "2008-08-27T19:32:37Z")

</div>

Vincent,

You have to get SiteController to pass the info into your model,  
that’s the only way to get the data into a tag AFAIK. So you have to  
write an extension for SiteController that adds the function of  
passing the current\_user’s info into your model (e.g. pass in the  
user\_id). Then you have to either add the ability to receive the  
current\_user info into Page or into another type of Page like a  
UserAuthPage…

Basically I mean something like this… (this code is just for  
illustrative purposes and not necessarily the best way to accomplish  
this… )

[http://pastie.org/261093](http://pastie.org/261093)

-C

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 28, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/8 "2008-08-28T12:19:59Z")

</div>

Christopher D. wrote:

> Vincent,
> 
> You have to get SiteController to pass the info into your model,  
> that’s the only way to get the data into a tag AFAIK. So you have to  
> write an extension for SiteController that adds the function of  
> passing the current\_user’s info into your model (e.g. pass in the  
> user\_id). Then you have to either add the ability to receive the  
> current\_user info into Page or into another type of Page like a  
> UserAuthPage…
> 
> Basically I mean something like this… (this code is just for  
> illustrative purposes and not necessarily the best way to accomplish  
> this… )
> 
> [http://pastie.org/261093](http://pastie.org/261093)
> 
> -C

Hello,

Thanks for your answer, I’m understanding what you meant.

I created the follwing site controller extension (which is include in my  
core extension) :

module CoreExtension::SiteControllerExtensions  
def self.included(base)  
base.class\_eval do  
# This reenables the session for the SiteController - session  
:on does not work!  
session :disabled =\> false  
alias\_method\_chain :find\_page, :current\_front\_user  
end  
end

def find\_page\_with\_current\_front\_user(url)  
found = find\_page\_without\_current\_front\_user(url)  
unless found.nil?  
found.current\_front\_user = current\_user  
end  
found  
end  
end

Then, I added the following line in my app/models/page.rb

attr\_accessor :current\_front\_user

And finally, this is my extensions tags file :

module MembersTags  
include Radiant::Taggable

tag “current\_front\_user”, :for =\> current\_front\_user

tag “if\_current\_front\_user” do |tag|  
if current\_front\_user  
tag.expand  
end  
end

tag “unless\_current\_front\_user” do |tag|  
unless current\_front\_user  
tag.expand  
end  
end  
end

When I try to run the server, I got the following error :  
“undefined local variable or method current\_front\_user for MembersTags”

I think it’s a basic ruby error, can you give me the way to access to my  
attribute?

Thank you !  
Vincent

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 29, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/9 "2008-08-29T16:13:53Z")

</div>

Hello,

I overrided the process\_page as you advised me, and tried to recover the  
current\_user by tag.locals.page.

Finally I get the follwing error :

Error : ‘tag : wrong number of arguments (0 to 1)’

Here :

module MembersTags  
include Radiant::Taggable

current\_user = tag.locals.page.current\_front\_user

tag “current\_user”, :for =\> current\_user

tag “if\_current\_user” do |tag|  
if current\_user  
tag.expand  
end  
end

tag “unless\_current\_user” do |tag|  
unless current\_user  
tag.expand  
end  
end

end

Can you explain me what is missing?

Thank you very much !  
Vincent

---

<div class="post-metadata">

**Author:** ![vpredoehl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/67e7ee/32.png) [@vpredoehl](https://www.ruby-forum.com/u/vpredoehl)\
**Post date:** [August 28, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/how-to-use-session-in-tags/146882/10 "2008-08-28T14:09:50Z")

</div>

Actually, unless you need to override whether the page is even visible,  
I would override the process\_page method instead. But that’s just my  
preference.

In your tags, read the current\_front\_user off of tag.globals.page.  
Radius blocks get passed around and the scope can be wonky sometimes.

Sean
