Alpha Video Training
Results 1 to 13 of 13

Thread: Find and Use Current Logged In User Data?

  1. #1
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Find and Use Current Logged In User Data?

    A5v10 Web Application

    I need the initial value of a form element to contain the logged in user's userid.

    Following whatever I could find, I now have this as the initial value of that field:

    <%A5 ?session.__protected__userid%>

    Of course, this does not work. It displays nothing, or else it just displays the code, depending on how I use it.

    Similarly, I have tested this in a random place on an .a5w page: "ME <%A5 ?session.__protected__userid%>" which also produces nothing. ("ME: ")

    I have been also trying to use the following:

    1) Login redirects to logincheck.a5w
    2) logincheck.a5w contains only the following:
    <%A5
    DIM pUser as p
    a5ws_logged_in_user_values(pUser,request,session)
    if eval_valid("pUser.userid") = .T. then
    session.__protected__userid = pUser.userid
    response.redirect("AfterLoginPage.a5w")
    end if
    %>

    With that setup, I was successfully redirected to the correct page, but I can't seem to grab that session variable anywhere, and trying to simply print it on that same logincheck.a5w page produces nothing, just as the other attempts did.

    Here are my questions:

    1) In which file can I find and modify the post-login stuff? By that I mean whatever A5 normally does with its standard login process ... where is the file that defines the AfterValidate and whatnot? Can I modify it? Searching my hard drive turns up no files that contain "AfterValidate", and neither does A5's internal search process.

    2) What is the everyday procedure used by A5 afficianados to grab the logged in user info and use it? It must happen every day, in almost every web app, right? Where can I lay my hands on this information?

    Thank you for your help.
    Last edited by Stupidscript; 09-27-2010 at 05:52 PM. Reason: typo

  2. #2
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Re: Find and Use Current Logged In User Data?

    Here's the answer, including the details I couldn't find anywhere:

    1) login.a5w -> logincheck.a5w

    2) logincheck.a5w =

    Code:
    <%A5
    'dimension the pUser array
    DIM pUser as p
    'set the array values to the logged in user values
    a5ws_logged_in_user_values(pUser,request,session)
    'check for the existence of a userid (valid login) in the array
    if eval_valid("pUser.userid") = .T. then
    'if found, set the session variable
    session.__protected__userid = pUser.userid
    'then redirect the user to the main page
    response.redirect("AfterLoginPage.a5w")
    end if
    %>
    3) AfterLoginPage.a5w is a LAYOUT with numerous GRIDS. Placing your code to grab the session variable on this page does nothing for the grids, which are each individually rendered containers.

    4) After finding WHICH GRIDS need the variable, edit THOSE GRIDS' FIELD settings for the field you want to populate:

    Code:
    Initial value: session.__protected__userid
    Note the ABSENCE of any A5 script tags in the "Initial value:". Just call the variable and A5 magically assumes it to be a variable name!! Wow and ew ... not good.

    5) If you need to use that variable inline with HTML or directly on an A5W page, you MUST use the A5 script tags and operators:

    Code:
    Here's the userid: <%A5 ?session.__protected__userid%>
    That equals:
    "echo (?) the value of the __protected__userid SESSION variable"

    And results in:
    "Here's the userid: stanley" (or whatever the userid is)

    To sum up, for people new to A5:

    1) Set your Web Security Settings so that the page users go to following login is your a5w page that checks for and saves the userid. I made and used a page called "logincheck.a5w" for this example.

    2) Then use your code on that checking page to redirect the user to the real first page of your application AFTER it sets the session variable. (If they mess up the login, it won't get to the checking page, anyway. It will return to the login page with the login error.)

    3) Once the session variable has been set, use it in subsequent GRIDS either by direct reference (session.__protected__userid) or by echo (<%A5 ?session.__protected__userid%>), depending on whether you are populating a form field (direct) or printing it on a page (echo).

    See here for a teensy bit more about __protected__ variables.

  3. #3
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Re: Find and Use Current Logged In User Data?

    Oops. Spoke too soon.

    It works ... for awhile ... and then, after awhile, it stops displaying the session variable and just displays the string 'session.__protected__userid' instead.

    The login session is still active, or at least there is no need for the user to log in, again to access the pages.

    I'll keep plugging away. If anyone has a solution that doesn't stop working halfway through the day, I'd love to hear it. But, with this session expiration issue, or whatever, this solution is not "the" solution.

  4. #4
    Member tommyrotten's Avatar
    Real Name
    Tommmy Braaten
    Join Date
    Nov 2009
    Location
    In Sunny....NW Washington.
    Posts
    154

    Wink Re: Find and Use Current Logged In User Data?

    I think I remember seeing a setting in the server that sets the timeout for the session. Not sure if this is the answer but its worth a shot.

    BTW... thanks for the post. I have been searching posts for several days looking for what you described. I need to digest it but again good info...

    Maybe alpha can have a video or wiki that talks about this???

  5. #5
    Member tommyrotten's Avatar
    Real Name
    Tommmy Braaten
    Join Date
    Nov 2009
    Location
    In Sunny....NW Washington.
    Posts
    154

    Default Re: Find and Use Current Logged In User Data?

    So after reading your post again it started to look familiar to a blog post about filtering data with the "ulink" that is passed from the log in.

    http://blog.alphasoftware.com/2010/0...d-by-user.html

  6. #6
    "Certified" Alphaholic chadbrown's Avatar
    Real Name
    Chad Brown
    Join Date
    Aug 2007
    Location
    Aurora, Ontario, Canada
    Posts
    1,408

    Default Re: Find and Use Current Logged In User Data?

    Sorry
    Chad Brown

  7. #7
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Re: Find and Use Current Logged In User Data?

    Thanks, Tommy.

    Now we've got it set in the Web Application where the session lasts as long as the browser is open or until the user logs out, and on the server so that the session lasts for 12 hours (43200 seconds). I guess that's the best we can do with this. Our people will need to re-login every 12 hours or so to restart their session. Oh well.

    I would be interested to learn why there is a distinction being made between session settings in the Web Application and in the Application Server. I would think that a single setting should do the trick, and keeping a session active for as long as the browser is open or until the user logs out seems sensible, to me. Timing out a session is not desirable for our application and does not fit with the options we chose to use in the Web Security settings.

    IMHO, if an application's Web Security settings define a session's lifetime, that should override the Application Server, which could not possibly anticipate what types of session requirements would be needed by an applicatin running on it. Having a session lifetime setting at the server limits all users of that server to that one setting, regardless of what choices they make while developing their app. I mean, why bother, right? Just let the Application Server define how long your sessions last, because it will override your choices, anyway.

    Not good.

    And you're right ... 'ulink' is another way of capturing and using various variables. We're looking at it as an alternative to this issue. It seems unnecessarily complicated when simply using the Web Application's session settings should do what we need.
    Last edited by Stupidscript; 09-29-2010 at 02:46 PM. Reason: typo

  8. #8
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Re: Find and Use Current Logged In User Data?

    Using 'ulink' seems to be holding! I'll report back tomorrow if it's fer sher persistent.

    In the meantime, here are my settings for both the Web Application and the Web Application Server:

    Code:
    ### WEB APPLICATION: WEB SECURITY ###
    
    # WEB SECURITY CONFIGURATION: #
    
    Security Policy:
    - Security Active: check
    - Password Required: check
    - Redirect page - login: login.a5w
    - Redirect page - insufficient permission: login.a5w
    
    Login Options:
    - Login expiration policy: Expires when user closes their browser
    - Redirect page after login: HomePage.a5w
    
    User ID Options:
    - User ID configuration: Value
    
    Customize Options:
    - Enable external user identifier field: check
    - Session variable for identifier field: __protected__userid
    
    # USERS and GROUPS: #
    
    Edit User (for example: NormalUser):
    - User ID: NormalUser
    - Password: ********
    - Confirm Password: *********
    - External User ID Value: NormalUser
    
    # PUBLISH WEB SECURITY FILES! #
    
    ### WEB APPLICATION SERVER: ###
    
    # SETTINGS (tab): #
    
    Session Cookie Name: A5wSessionId
    Session Lifetime: 900 (seconds)
    Enable cookies for session tracking: check
    
    # RESTART SERVER! #
    As noted above, in an HTML/A5W page, reference the session variable with a complete script tag to "echo" the variable's value:

    Code:
    <%A5 ?session.__protected__userid%>
    In a Web Component form field's "Initial value", just use the session variable reference directly ... A5 will recognize it as a variable and display its value:

    Code:
    session.__protected__userid
    Thanks a lot for the help of Jerry Brightbill!

    http://blog.alphasoftware.com/2010/0...d-by-user.html

  9. #9
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Re: Find and Use Current Logged In User Data?

    A NOTE ABOUT 'ulink':

    The idea of 'ulink' is to hold a SINGLE session variable.

    By using the 'ulink' mechanism AND by using BROWSER-BASED session cookies (that are ONLY deleted when the browser closes), you create a PERSISTENT container that will regenerate itself even when the Application Server "session lifetime" has ended.

    For example:

    If the Application Server is set with a "session lifetime" of 900 seconds;

    - Normally, all session variables are deleted 900 seconds after the user's last activity, and they need to log in again. (Activity, like clicking a link or reloading, re-starts the "session lifetime".)

    - By using the 'ulink' mechanism, those session variables still get deleted, but 'ulink' grabs the login data from the browser-based cookie and PERFORMS THE LOGIN FOR YOU, using that cookie data ... restarting the session. You don't even see it happening.

    This means that as long as the browser stays open, the user effectively stays logged in, and the session variable stays available.

    In my settings, above, I CHOSE "Session variable for identifier field" to be both "__protected__" and "userid". (I didn't HAVE to make it '__protected__', and I could have called it whatever I wanted instead of 'userid'. Call it something that represents the variable you are storing.)

    I also CHOSE the "External User ID Value" to match the user's NAME ... I could have picked the user's ID, their Status, their Hair Color ... anything in the user's credentials record, but I wanted the NAME, so that's what I used as the "External User ID Value."

    IF I wanted to use this persistent variable for selecting records that matched the logged in user, I probably would have used their ID, instead of their NAME.

    By doing so, I could then use their ID to filter records or even grab their NAME with a quick query ("select NAME from users where ID = session.__protected__userid")(NOT tested!!) ... IF that was the data I entered into the "External User ID Value" field, as Jerry Brightbill did in his example for filtering datasets based on the customer_id.

    He used customer_id's, like '0000003569'.
    I used a name, like 'NormalUser'.
    It depends on what you need.

    My needs are simple ... I just want the name to print in a couple of places so the user doesn't need to choose it from a drop-down list. That's why I CHOSE to use their name as the value of the 'ulink' variable.

    Hope that is helpful.
    Last edited by Stupidscript; 09-30-2010 at 05:20 PM. Reason: typo

  10. #10
    Member tommyrotten's Avatar
    Real Name
    Tommmy Braaten
    Join Date
    Nov 2009
    Location
    In Sunny....NW Washington.
    Posts
    154

    Default Re: Find and Use Current Logged In User Data?

    Good Stuff

  11. #11
    Member koga101's Avatar
    Real Name
    Chris O'Brien
    Join Date
    Mar 2009
    Location
    Los Angeles, CA
    Posts
    105

    Default Re: Find and Use Current Logged In User Data?

    Typically, in Alpha the easiest way is to store some information and relate it to __protected__ulink. This can be through a linked grid or a large flat table and a single grid. Alphapedia shows how to link a DBF (or sql table) to the web security tables to make this simple. The double underscore is for security reasons. Then either in a Tabbed_UI or A5W page the following syntax (copy and paste forever) is used to create that variable. Here I have the organization ("org") related to the user and this would be used to populate whatever grid field.

    session.org = table.external_record_content_get("[PathAlias.ADB_Path]\web_org_link","org","","uuid="+quote(session.__protected__ulink))

    In a grid you need to develop an argument if you wish to filter on the session variable so organization = :org (org = session.org). If you only want it as an initial value then you would type session.org in that field. You would then either hide the field or turn it off with javascript if Organization ="".

    Hope you find this method easier.

  12. #12
    Member
    Real Name
    James Butler
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    49

    Default Re: Find and Use Current Logged In User Data?

    @koga101

    I'm not sure if it's "easier" than setting and retrieving a "normal" session variable, but the 'ulink' mechanism is definitely the way to go with A5 for a SINGLE persistent session variable. See my post, two above yours.

    And, for the record, after over 24 hours with the Application Server set to a 900 second session lifetime, the session variable is DEFINITELY persistent, using the 'ulink' mechanism described, above.

    Thanks for everybody's help. I'm done with this one.

  13. #13
    Member
    Real Name
    Jeff Funk
    Join Date
    Jan 2011
    Posts
    22

    Default Re: Find and Use Current Logged In User Data?

    I've tried everything in this thread and am still having problems... All I'm trying to do is populate the username in a field of a new record when it pops up. A couple of strange things... during all the things I've tried, it worked once... and never again. The wierd thing is, I've put this code in the main grid:

    <%A5 ?session.__protected__userid%>

    and the username does show up... I've tried putting session.__protected__userid in the initial value of the field and that didn't work... I've also tried to do it programatically like this:

    var today = new Date();
    currday = zeroPad(today.getDate(),2);
    currmth = zeroPad(today.getMonth() + 1, 2);
    curryr = today.getFullYear();
    currdate = currmth + '/' + currday + '/' + curryr;
    {grid.Object}.setValue('D','CREATEDDATE',currdate);
    {grid.Object}.setValue('D','CREATEDBY',session.__protected__userid);

    in the afterDetailViewNewRecord event... the date populates just fine, but the username just comes up blank. It doesn't make sense to me... is there something I'm missing??

Similar Threads

  1. Display Current Logged In User on Webpage
    By tommyrotten in forum Application Server Version 10 - Web/Browser Applications
    Replies: 6
    Last Post: 10-02-2010, 07:06 AM
  2. Filter Grid by Logged in User
    By canc in forum Application Server Version 8
    Replies: 3
    Last Post: 06-25-2009, 11:40 PM
  3. List of current users logged on
    By ray camo in forum Alpha Five Version 9 - Desktop Applications
    Replies: 5
    Last Post: 01-18-2009, 01:13 AM
  4. Filter by logged on user
    By RickK in forum Web Application Server v7
    Replies: 1
    Last Post: 09-21-2006, 07:44 AM
  5. How to find what group they are logged in to
    By Walter in forum Alpha Five Version 6
    Replies: 3
    Last Post: 07-12-2005, 10:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •