@auth.requires for functions

78 views
Skip to first unread message

Andrea Fae'

unread,
Apr 19, 2017, 8:14:53 AM4/19/17
to web2py-users
For example I have this type of functions:

# sono autorizzati i vari manager a seconda della sede
@auth.requires(auth.has_membership('Total-manager') | auth.has_membership('Conegliano-studente') | auth.has_membership('Pordenone-manager') | auth.has_membership('Pordenone-studente') | auth.has_membership('Udine-studente'))
def appuntamenti_studente():
     # recupero lo studente selezionato
    studente = request.args(0)
    # recupero il gruppo
    # query per recuperare il nome  dello studente
    query_nome_studente = db.auth_user.id == studente
    # recupero nome e cognome dello studente
    row = db(query_nome_studente).select(db.auth_user.first_name,db.auth_user.last_name).first()
    #seleziono gli eventi dello studente
    query = (db.evento.studenti.contains(studente))
    eventi_stud = db(query).select()
    # imposto la grid per far vedere gli eventi dello studente
    exportcls = dict(csv_with_hidden_cols=False, html=False, json=False, tsv_with_hidden_cols=False, tsv=False)
    # solo se lo studente corrisponde a chi si è loggato
    if (str(auth.user.id) == studente):
        #print 'sono al form normale'
        form = SQLFORM.grid(query, args=[studente], fields=[db.evento.titolo, db.evento.inizio,db.evento.fine,db.evento.risorsa, db.evento.materia,db.evento.docente],create=False, details=False, editable=False, deletable=False, maxtextlength=60, exportclasses = exportcls)
    else:
        redirect(URL('default','index'))
    return locals()

I'm testing different "fixed" values in the @auth.requires, but they can be more or less depending on how many roles are in the db. I'd like to do this dynamically from the db, extracting and compising this @auth.requires line in dynamic way. Is it possible? Do you have any examples? Thank you

Anthony

unread,
Apr 19, 2017, 11:30:06 AM4/19/17
to web2py-users
First, when using auth.requires(), it is best to put the test inside a lambda (or standard function) so it will not be evaluated until the decorated function is actually called (otherwise, the test will be evaluated whenever the controller file is accessed).

Anyway, you can do something like this:

list_of_roles = ['role1', 'role2', 'role3'] # You could get this list via a database query

@auth.requires(lambda: any(auth.has_membership(role) for role in list_of_roles))
def myfunc():
   
...

Anthony

Andrea Fae'

unread,
Apr 20, 2017, 8:36:46 AM4/20/17
to web2py-users
But... "any"? I need some more information about lambda I think....where to find this syntax with any?
where is written what you told about evaluation?
Thank you very much

Marvi Benedet

unread,
Apr 20, 2017, 9:40:33 AM4/20/17
to web...@googlegroups.com
https://docs.python.org/2/library/functions.html#any
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

so any() return true if one (or more) of the elements is True.

this:

auth.has_membership(role) for role in list_of_roles
try all the roles in the list.

so if one of the auth.has_membership() return true, the lamba function return true.



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrea Fae'

unread,
Apr 20, 2017, 9:49:25 AM4/20/17
to web2py-users
Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Anthony

unread,
Apr 20, 2017, 10:12:59 AM4/20/17
to web2py-users
On Thursday, April 20, 2017 at 8:36:46 AM UTC-4, Andrea Fae' wrote:
But... "any"? I need some more information about lambda I think....where to find this syntax with any?
where is written what you told about evaluation?

Remember, web2py is a Python framework -- many of the things you see are simply Python (both "any" and "lambda" are plain Python constructs). Note, it doesn't have to be a lambda function -- the point is simply that if you pass a function of any kind to auth.requires(), it will call that function later (when the function it decorates is actually called) rather than immediately (when the decorator itself is defined -- which happens whenever the controller file is executed). So, by putting the test inside a function, you defer the execution of the test until it is actually needed. Because the auth.has_membership() calls each require a database select, you don't want to run those calls unless absolutely necessary. This recommendation is mentioned here: http://web2py.com/books/default/chapter/29/09/access-control#Decorators.

Anthony

Andrea Fae'

unread,
Apr 20, 2017, 10:31:31 AM4/20/17
to web2py-users
Hello, if I have to get the list from database, where I need to put the code? At the start of default.py where I use the functions? Thanks


Il giorno mercoledì 19 aprile 2017 17:30:06 UTC+2, Anthony ha scritto:

Andrea Fae'

unread,
Apr 23, 2017, 12:59:27 PM4/23/17
to web2py-users
Reply all
Reply to author
Forward
0 new messages