On Wed, 26 Sep 2007 14:40:22 -0000
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> 
> Hello,
> 
> Maybe I am missing something obvious, but I am using Pylons with the
> AuthKit middleware and mako templates and I am curious what the best
> method for controlling what elements are displayed to the user based
> on their authentication is .. should the logic exist in the template?
> Should the controller push down the element to the template? An
> example would be changing the login form to an action menu once the
> user is authorized.
> 
> Right now I am pushing c.user_menu to the template (a list of menu
> items based on permissions) to the template. This is an empty value if
> they have no authentication, for an empty list I display the login
> form else I display the users action menu.
> 
> Best practices here? Alternative methods for accomplishing this? Or is
> this the "right" way?
> 
> Thank you in advance,
> 
> Wayne
> Piece of Py
> http://www.pieceofpy.com

I am not sure about 'best practice' but here is one simple way to
display a standard menu for regualar users, and if user is
authenticated, additional items.  Is that your question?

Normally, I advocate fat controllers and skinny templates.  But if the
work to be done is simply to present html, then it goes in a template.
And if that work to be done is in more than one template, break it out
as a 'component' template that is callable. (not a rule, just personal
guideline)

The controller can 'control' what is called, but does not need to set
the user_menu html.  The templates that generate the html (here, the nav
bits are common to all pages and are broken out from standard page
templates; but concept applies) can eval as needed.

In the component template; check for the variable assigned when user is
authenticated. If true, then add items to the menu, else ignore.  

In each page template there is a call to the navigation template asking
for a complete menu.  Each page template just blindly presents what was
passed by this call.

Some of the logic in navigation menu template:

    # determine auth status
    remote_user = request.environ.get('REMOTE_USER')

    # normal menu items
    menu_list = [('Home','home'), 
                ('About','about'), 
                ('Blog','blog'),
                ]

    # priviledged menu items
    priv_list = [('Admin', 'admin'),
                ('Secret', 'secret'),
                ]

    # if authenticated, add priv items     [1]
    if remote_user:
        for extra in priv_list:
            menu_list.append( extra )


    # create navigational menu
    <div id="nav">
        <ul>
        % for display_name, url in menu_list:
          <li> <a href="/${url}">${display_name}</a></li>
        % endfor
        </ul>
    </div>


[1] More may be needed if you go beyond REMOTE_USER (groups, etc).. but
concept still applies.... of course, if that was the actual question.

-- 

michael



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to