How to use session in tags?

Hello,

I would like to create something like that :
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

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

Thank you !
Vincent

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

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

Hi Vincent,

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

Cheers,
Casper

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

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

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

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

-C

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

-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

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

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