Package gluon :: Module tools :: Class Auth
[hide private]
[frames] | no frames]

Class Auth

source code

object --+
         |
        Auth

Instance Methods [hide private]
 
url(self, f=None, args=None, vars=None, scheme=False) source code
 
here(self) source code
 
__init__(self, environment=None, db=None, mailer=True, hmac_key=None, controller='default', function='user', cas_provider=None, signature=True, secure=False, csrf_prevention=True, propagate_extension=None)
auth=Auth(db)
source code
 
get_vars_next(self) source code
 
_get_user_id(self)
accessor for auth.user_id
source code
 
table_user(self) source code
 
table_group(self) source code
 
table_membership(self) source code
 
table_permission(self) source code
 
table_event(self) source code
 
table_cas(self) source code
 
_HTTP(self, *a, **b)
only used in lambda: self._HTTP(404)
source code
 
__call__(self)
usage:
source code
 
navbar(self, prefix='Welcome', action=None, separators=(' [ ', ' | ', ' ] '), user_identifier=<function <lambda> at 0x552ba28>, referrer_actions=<function <lambda> at 0x552ba28>, mode='default')
Navbar with support for more templates This uses some code from the old navbar.
source code
 
__get_migrate(self, tablename, migrate=True) source code
 
enable_record_versioning(self, tables, archive_db=None, archive_names='%(tablename)s_archive', current_record='current_record', current_record_label=None)
to enable full record versioning (including auth tables):
source code
 
define_signature(self) source code
 
define_tables(self, username=None, signature=None, migrate=None, fake_migrate=None)
to be called unless tables are defined manually
source code
 
log_event(self, description, vars=None, origin='auth')
usage:
source code
 
get_or_create_user(self, keys, update_fields=['email'], login=True, get=True)
Used for alternate login methods: If the user exists already then password is updated.
source code
 
basic(self, basic_auth_realm=False)
perform basic login.
source code
 
login_user(self, user)
login the user = db.auth_user(id)
source code
 
_get_login_settings(self) source code
 
login_bare(self, username, password)
logins user as specified by username (or email) and password
source code
 
register_bare(self, **fields)
registers a user as specified by username (or email) and a raw password.
source code
 
cas_login(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>, version=2) source code
 
cas_validate(self, version=2, proxy=False) source code
 
login(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a login form
source code
 
logout(self, next=<function <lambda> at 0x552ba28>, onlogout=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
logout and redirects to login
source code
 
register(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a registration form
source code
 
is_logged_in(self)
checks if the user is logged in and returns True/False.
source code
 
verify_email(self, next=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
action user to verify the registration email, XXXXXXXXXXXXXXXX
source code
 
retrieve_username(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a form to retrieve the user username (only if there is a username field)
source code
 
random_password(self) source code
 
reset_password_deprecated(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a form to reset the user password (deprecated)
source code
 
reset_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a form to reset the user password
source code
 
request_reset_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a form to reset the user password
source code
 
email_reset_password(self, user) source code
 
retrieve_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>) source code
 
change_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a form that lets the user change password
source code
 
profile(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)
returns a form that lets the user change his/her profile
source code
 
run_login_onaccept(self) source code
 
is_impersonating(self) source code
 
impersonate(self, user_id=<function <lambda> at 0x552ba28>)
usage: POST TO http://..../impersonate request.post_vars.user_id=<id> set request.post_vars.user_id to 0 to restore original user.
source code
 
update_groups(self) source code
 
groups(self)
displays the groups and their roles for the logged in user
source code
 
not_authorized(self)
you can change the view for this page to make it look as you like
source code
 
requires(self, condition, requires_login=True, otherwise=None)
decorator that prevents access to action if not logged in
source code
 
requires_login(self, otherwise=None)
decorator that prevents access to action if not logged in
source code
 
requires_membership(self, role=None, group_id=None, otherwise=None)
decorator that prevents access to action if not logged in or if user logged in is not a member of group_id.
source code
 
requires_permission(self, name, table_name='', record_id=0, otherwise=None)
decorator that prevents access to action if not logged in or if user logged in is not a member of any group (role) that has 'name' access to 'table_name', 'record_id'.
source code
 
requires_signature(self, otherwise=None, hash_vars=True)
decorator that prevents access to action if not logged in or if user logged in is not a member of group_id.
source code
 
add_group(self, role, description='')
creates a group associated to a role
source code
 
del_group(self, group_id)
deletes a group
source code
 
id_group(self, role)
returns the group_id of the group specified by the role
source code
 
user_group(self, user_id=None)
returns the group_id of the group uniquely associated to this user i.e.
source code
 
user_group_role(self, user_id=None) source code
 
has_membership(self, group_id=None, user_id=None, role=None)
checks if user is member of group_id or role
source code
 
add_membership(self, group_id=None, user_id=None, role=None)
gives user_id membership of group_id or role if user is None than user_id is that of current logged in user
source code
 
del_membership(self, group_id=None, user_id=None, role=None)
revokes membership from group_id to user_id if user_id is None than user_id is that of current logged in user
source code
 
has_permission(self, name='any', table_name='', record_id=0, user_id=None, group_id=None)
checks if user_id or current logged in user is member of a group that has 'name' permission on 'table_name' and 'record_id' if group_id is passed, it checks whether the group has the permission
source code
 
add_permission(self, group_id, name='any', table_name='', record_id=0)
gives group_id 'name' access to 'table_name' and 'record_id'
source code
 
del_permission(self, group_id, name='any', table_name='', record_id=0)
revokes group_id 'name' access to 'table_name' and 'record_id'
source code
 
accessible_query(self, name, table, user_id=None)
returns a query with all accessible records for user_id or the current logged in user this method does not work on GAE because uses JOIN and IN
source code
 
wiki(self, slug=None, env=None, render='markmin', manage_permissions=False, force_prefix='', restrict_search=False, resolve=True, extra=None, menu_groups=None, templates=None, migrate=True, controller=None, function=None, force_render=False, groups=None) source code
 
wikimenu(self)
to be used in menu.py for app wide wiki menus
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
 
get_or_create_key(filename=None, alg='sha512') source code
 
archive(form, archive_table=None, current_record='current_record', archive_current=False, fields=None)
If you have a table (db.mytable) that needs full revision history you can just do:
source code
Class Variables [hide private]
  default_settings = {'allow_basic_login': False, 'allow_basic_l...
  default_messages = {'access_denied': 'Insufficient privileges'...
Class for authentication, authorization, role based access control.
Properties [hide private]
  user_id
user.id or None

Inherited from object: __class__

Method Details [hide private]

__init__(self, environment=None, db=None, mailer=True, hmac_key=None, controller='default', function='user', cas_provider=None, signature=True, secure=False, csrf_prevention=True, propagate_extension=None)
(Constructor)

source code 

auth=Auth(db)

- environment is there for legacy but unused (awful)
- db has to be the database where to create tables for authentication
- mailer=Mail(...) or None (no mailed) or True (make a mailer)
- hmac_key can be a hmac_key or hmac_key=Auth.get_or_create_key()
- controller (where is the user action?)
- cas_provider (delegate authentication to the URL, CAS2)

Overrides: object.__init__

__call__(self)
(Call operator)

source code 

usage:

def authentication(): return dict(form=auth())

navbar(self, prefix='Welcome', action=None, separators=(' [ ', ' | ', ' ] '), user_identifier=<function <lambda> at 0x552ba28>, referrer_actions=<function <lambda> at 0x552ba28>, mode='default')

source code 

Navbar with support for more templates This uses some code from the old navbar.

Keyword arguments: mode -- see options for list of

enable_record_versioning(self, tables, archive_db=None, archive_names='%(tablename)s_archive', current_record='current_record', current_record_label=None)

source code 

to enable full record versioning (including auth tables):

auth = Auth(db) auth.define_tables(signature=True) # define our own tables db.define_table('mything',Field('name'),auth.signature) auth.enable_record_versioning(tables=db)

tables can be the db (all table) or a list of tables. only tables with modified_by and modified_on fiels (as created by auth.signature) will have versioning. Old record versions will be in table 'mything_archive' automatically defined.

when you enable enable_record_versioning, records are never deleted but marked with is_active=False.

enable_record_versioning enables a common_filter for every table that filters out records with is_active = False

Important: If you use auth.enable_record_versioning, do not use auth.archive or you will end up with duplicates. auth.archive does explicitly what enable_record_versioning does automatically.

define_tables(self, username=None, signature=None, migrate=None, fake_migrate=None)

source code 

to be called unless tables are defined manually

usages:

    # defines all needed tables and table files
    # 'myprefix_auth_user.table', ...
    auth.define_tables(migrate='myprefix_')

    # defines all needed tables without migration/table files
    auth.define_tables(migrate=False)

log_event(self, description, vars=None, origin='auth')

source code 

usage:

    auth.log_event(description='this happened', origin='auth')

get_or_create_user(self, keys, update_fields=['email'], login=True, get=True)

source code 

Used for alternate login methods:
    If the user exists already then password is updated.
    If the user doesn't yet exist, then they are created.

basic(self, basic_auth_realm=False)

source code 

perform basic login.

:param basic_auth_realm: optional basic http authentication realm. :type basic_auth_realm: str or unicode or function or callable or boolean.

reads current.request.env.http_authorization and returns basic_allowed,basic_accepted,user.

if basic_auth_realm is defined is a callable it's return value is used to set the basic authentication realm, if it's a string its content is used instead. Otherwise basic authentication realm is set to the application name. If basic_auth_realm is None or False (the default) the behavior is to skip sending any challenge.

login(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a login form

method: Auth.login([next=DEFAULT [, onvalidation=DEFAULT
    [, onaccept=DEFAULT [, log=DEFAULT]]]])

logout(self, next=<function <lambda> at 0x552ba28>, onlogout=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

logout and redirects to login

method: Auth.logout ([next=DEFAULT[, onlogout=DEFAULT[,
    log=DEFAULT]]])

register(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a registration form

method: Auth.register([next=DEFAULT [, onvalidation=DEFAULT
    [, onaccept=DEFAULT [, log=DEFAULT]]]])

is_logged_in(self)

source code 

checks if the user is logged in and returns True/False. if so user is in auth.user as well as in session.auth.user

verify_email(self, next=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

action user to verify the registration email, XXXXXXXXXXXXXXXX

method: Auth.verify_email([next=DEFAULT [, onvalidation=DEFAULT
    [, onaccept=DEFAULT [, log=DEFAULT]]]])

retrieve_username(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a form to retrieve the user username
(only if there is a username field)

method: Auth.retrieve_username([next=DEFAULT
    [, onvalidation=DEFAULT [, onaccept=DEFAULT [, log=DEFAULT]]]])

reset_password_deprecated(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a form to reset the user password (deprecated)

method: Auth.reset_password_deprecated([next=DEFAULT
    [, onvalidation=DEFAULT [, onaccept=DEFAULT [, log=DEFAULT]]]])

reset_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a form to reset the user password

method: Auth.reset_password([next=DEFAULT
    [, onvalidation=DEFAULT [, onaccept=DEFAULT [, log=DEFAULT]]]])

request_reset_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a form to reset the user password

method: Auth.reset_password([next=DEFAULT
    [, onvalidation=DEFAULT [, onaccept=DEFAULT [, log=DEFAULT]]]])

change_password(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a form that lets the user change password

method: Auth.change_password([next=DEFAULT[, onvalidation=DEFAULT[,
    onaccept=DEFAULT[, log=DEFAULT]]]])

profile(self, next=<function <lambda> at 0x552ba28>, onvalidation=<function <lambda> at 0x552ba28>, onaccept=<function <lambda> at 0x552ba28>, log=<function <lambda> at 0x552ba28>)

source code 

returns a form that lets the user change his/her profile

method: Auth.profile([next=DEFAULT [, onvalidation=DEFAULT
    [, onaccept=DEFAULT [, log=DEFAULT]]]])

impersonate(self, user_id=<function <lambda> at 0x552ba28>)

source code 

usage: POST TO http://..../impersonate request.post_vars.user_id=<id> set request.post_vars.user_id to 0 to restore original user.

requires impersonator is logged in and has_permission('impersonate', 'auth_user', user_id)

requires_membership(self, role=None, group_id=None, otherwise=None)

source code 

decorator that prevents access to action if not logged in or if user logged in is not a member of group_id. If role is provided instead of group_id then the group_id is calculated.

requires_signature(self, otherwise=None, hash_vars=True)

source code 

decorator that prevents access to action if not logged in or if user logged in is not a member of group_id. If role is provided instead of group_id then the group_id is calculated.

user_group(self, user_id=None)

source code 

returns the group_id of the group uniquely associated to this user i.e. role=user:[user_id]

accessible_query(self, name, table, user_id=None)

source code 

returns a query with all accessible records for user_id or
the current logged in user
this method does not work on GAE because uses JOIN and IN

example:

   db(auth.accessible_query('read', db.mytable)).select(db.mytable.ALL)

archive(form, archive_table=None, current_record='current_record', archive_current=False, fields=None)
Static Method

source code 

If you have a table (db.mytable) that needs full revision history you can just do:

    form=crud.update(db.mytable,myrecord,onaccept=auth.archive)

or

    form=SQLFORM(db.mytable,myrecord).process(onaccept=auth.archive)

crud.archive will define a new table "mytable_archive" and store
a copy of the current record (if archive_current=True)
or a copy of the previous record (if archive_current=False)
in the newly created table including a reference
to the current record.

fields allows to specify extra fields that need to be archived.

If you want to access such table you need to define it yourself
in a model:

    db.define_table('mytable_archive',
        Field('current_record',db.mytable),
        db.mytable)

Notice such table includes all fields of db.mytable plus one: current_record.
crud.archive does not timestamp the stored record unless your original table
has a fields like:

    db.define_table(...,
        Field('saved_on','datetime',
             default=request.now,update=request.now,writable=False),
        Field('saved_by',auth.user,
             default=auth.user_id,update=auth.user_id,writable=False),

there is nothing special about these fields since they are filled before
the record is archived.

If you want to change the archive table name and the name of the reference field
you can do, for example:

    db.define_table('myhistory',
        Field('parent_record',db.mytable),
        db.mytable)

and use it as:

    form=crud.update(db.mytable,myrecord,
                     onaccept=lambda form:crud.archive(form,
                     archive_table=db.myhistory,
                     current_record='parent_record'))


Class Variable Details [hide private]

default_settings

Value:
{'allow_basic_login': False,
 'allow_basic_login_only': False,
 'allow_delete_accounts': False,
 'alternate_requires_registration': False,
 'auth_manager_role': None,
 'captcha': None,
 'cas_maps': None,
 'client_side': True,
...

default_messages


Class for authentication, authorization, role based access control.

Includes:

- registration and profile
- login and logout
- username and password retrieval
- event logging
- role creation and assignment
- user defined group/role based permission

Authentication Example:

    from gluon.contrib.utils import *
    mail=Mail()
    mail.settings.server='smtp.gmail.com:587'
    mail.settings.sender='you@somewhere.com'
    mail.settings.login='username:password'
    auth=Auth(db)
    auth.settings.mailer=mail
    # auth.settings....=...
    auth.define_tables()
    def authentication():
        return dict(form=auth())

exposes:

- http://.../{application}/{controller}/authentication/login
- http://.../{application}/{controller}/authentication/logout
- http://.../{application}/{controller}/authentication/register
- http://.../{application}/{controller}/authentication/verify_email
- http://.../{application}/{controller}/authentication/retrieve_username
- http://.../{application}/{controller}/authentication/retrieve_password
- http://.../{application}/{controller}/authentication/reset_password
- http://.../{application}/{controller}/authentication/profile
- http://.../{application}/{controller}/authentication/change_password

On registration a group with role=new_user.id is created
and user is given membership of this group.

You can create a group with:

    group_id=auth.add_group('Manager', 'can access the manage action')
    auth.add_permission(group_id, 'access to manage')

Here "access to manage" is just a user defined string.
You can give access to a user:

    auth.add_membership(group_id, user_id)

If user id is omitted, the logged in user is assumed

Then you can decorate any action:

    @auth.requires_permission('access to manage')
    def manage():
        return dict()

You can restrict a permission to a specific table:

    auth.add_permission(group_id, 'edit', db.sometable)
    @auth.requires_permission('edit', db.sometable)

Or to a specific record:

    auth.add_permission(group_id, 'edit', db.sometable, 45)
    @auth.requires_permission('edit', db.sometable, 45)

If authorization is not granted calls:

    auth.settings.on_failed_authorization

Other options:

    auth.settings.mailer=None
    auth.settings.expiration=3600 # seconds

    ...

    ### these are messages that can be customized
    ...

Value:
{'access_denied': 'Insufficient privileges',
 'add_group_log': 'Group %(group_id)s created',
 'add_membership_log': None,
 'add_permission_log': None,
 'change_password_log': 'User %(id)s Password changed',
 'del_group_log': 'Group %(group_id)s deleted',
 'del_membership_log': None,
 'del_permission_log': None,
...

Property Details [hide private]

user_id

user.id or None

Get Method:
_get_user_id(self) - accessor for auth.user_id