Forum OpenACS Q&A: "Logged In?"-Datasource and Template

Collapse
Posted by Tim Adler on
Hi there everybody!

I'm new to the ACS system and not very into own coding til now. We use the ShareNet implementation of the system. Dunno really what version that is, but I think its something with 3.x together with some 4.x elements. (But plz don't care bout that, cause I think hints for the current version also help)

So what I'm looking for is the following: As seen on this page and the http://sharenet.redhat.de there's a little tab on the portal page, which shows if one is logged in, or it presents a "Log-In"-link. (example can be seen on the left hand side here http://sharenet.redhat.de/adler/portal/)

I'm looking for a datasource and a template that would help me to do so. Can somebody supply me with such code of point me in the direction how to write my own?!

Thx, Tim

Collapse
Posted by Jarkko Laine on
Hi Tim,

I'm not familiar with the old ACS's, but in OpenACS you can find a good sample from the installation default home page:

index.adp:

      <p>
      <if @user_id@ gt 0>
        You are currently logged in as @name@ (<a href="/register/">change
        login</a>).
      </if>
      <else>
        Start by <b>logging in</b> in the box on the right, using the email
        address and password that you have just specified for the
        administrator.
      </else>
      </p>

In the <else> part you could have a link to yoursite.com/register or a form for the registration.

There might be a simple include that would render the login form, but I'm not sure of that. Maybe someone could confirm if this is true?

then, in index.tcl, you have to prepare the data source:

set user_id [ad_get_user_id]
if { $user_id == 0 } {
    set user_id ""
}

If you're not familiar with the adp/tcl file pairs, you might want to read the docs for ACS Templating (https://openacs.org/doc/acs-templating/).

Developer note: should user_id be also in acs_page_contract -properties of index.tcl? In the current implementation it doesn't seem to be.

Collapse
Posted by Tim Adler on
Hey thanks, that already did it. @user_id@ and @user_name@ seem to be set throughout the whole system.

@dirk 😉

Ok, and now for the Basics. I found the setting of user_id and user_name in /acs-subsite/www/master.tcl.
So now I'm eager to also set user_email throughout the system.

[cc_user_name $user_id] sets the user_name. I assume that everything between [] is a functin-call.
So is there a function to get the user's mail@?

master.tcl(/adp) is probably a template. If you use it as site-wide master template, you can set things like @user_id@ and @user_name@ there.

Yes, everything inside [] brackets is a procedure call with the first word being the name of the proc and subsequent ones being the attributes.

I don't know what you mean by user name (the screen name or the real name), but ad_get_user_info gets the basic info (first names, last name, email) for the current user and might thus do the job (see https://openacs.org/api-doc/proc-view?proc=ad_get_user_info&source_p=1).

It sets the $email variable automatically, so if you call it in your site-wide master template's tcl file, you can refer to @email@, @first_names@ and @last_name@ in any adp page which is using that template. I don't know if that's the best practice, though, maybe some wiser people could answer that.

Thanks again for the instant answer. I found that procedure (wow, haven't used that word for a while ;) exactly where u said it would be.

But me being an absolute newbie to TCL, I have no idea how I'm supposed to call it.

I tried just writing [ad_get_user_info] into master.tcl, but that supplies me with Request Error:

invalid command name ""
    while executing
"[ad_get_user_info]"
    ("uplevel" body line 107)
    invoked from within
"uplevel {
How do I do that right? Do I have to give any params or have to do an import of some kind?
Collapse
Posted by Dirk Gomez on

You need to write

set some_variable_name [ad_get_user_info]

Collapse
Posted by Tim Adler on
Thanks Dirk!

Now, I'll go browsing through that procs...see what else I can use

;)
Dirk,

AFAIK you don't have to use set here. ad_get_user_info sets automatically $first_names, $last_name and $email.

I think the problem is that when you are just calling a proc (without doing anything with the return value), you're not supposed to use [ ] brackets around it. So this should be sufficient:

ad_get_user_info
That's it. Nothing more and you get the variables set.
Yep, works also...just tried that.

BTW, as we're all together here again 😉!
What does

ad_return_error
do (Where can I find a reference in the API). Cause it seems that this one is somehow messing up my desired result. Whenever there is no user_info (e.g not logged in) then the server doesn't respond anymore.

I plan to write to proc at the desired position again and leave out the operations in the catch block.

Tim,

Just use the search box in https://openacs.org/api-doc/ and you'll find the explanation for every proc in the system 😊 (in this case, https://openacs.org/api-doc/proc-view?proc=ad%5freturn%5ferror)

In fact that proc doesn't seem to fit too good in this situation, since it returns a http error if the user_id is not defined. You could get around this by doing something like this:

set user_id [ad_get_user_id]
if { $user_id != 0 } {
    ad_get_user_info
}

This blows away part of the handiness of this function, though. In general I don't understand why ad_get_user_info returns an http error in the first place. At least it narrows the use of it a lot. Maybe that's why e.g. the default index page of an oacs installation uses an ad hoc sql query to get the user info. I think there should definitely be an allround proc for this.

Collapse
Posted by Don Baccus on
Tim - also there are usually people hanging out on our IRC channel (#openacs on freenode.net) who are eager to answer questions instantaneously if you don't want to wait for forum responses ...
Collapse
Posted by Tim Adler on
Thanks there again! I'll try that, I never realized that there's something like that API here. Just found it in our implementation.
Perhaps I even visit you guys on the IRC.