kamilpp
kamilpp

Reputation: 117

Web2py, authenticating two types of profile

In one application I need to create two types of accounts, let's say users and tutors. Each of them should be able to register/login/... user/tutor account defined by different tables in db.

e.g.

[app]/user/register should provide a form with fields: username, email, password, hobby [app]/tutor/register should provide a form with fields: username, email, pass, name, surname, telephone

Web2py auth service allows using auth_user table which can be customized. Is there a way to use two tables separately according to controller in one application?

Field auth.settings.table_user contains reference for auth_table but I probably shouldn't use it for this purpose.

Upvotes: 1

Views: 514

Answers (1)

Anthony
Anthony

Reputation: 25536

The Auth system isn't designed to work this way. Instead, it would probably be better to put all the fields for both user types in the single auth_user table and then selectively set the readable and writable attributes of the fields to True or False depending on the context.

In the model file defining Auth:

user_extra_fields = [Field('hobby'), ...]
tutor_extra_fields = [Field('telephone', ...]

auth.settings.extra_fields['auth_user'] = (
    [Field('user_type', requires=IS_IN_SET(['User', 'Tutor']))] +
    user_extra_fields + tutor_extra_fields)

In the controller that manages the Auth functions:

def user():
    if request.args(0) in ['register', 'profile']:
        hidden_fields = (user_extra_fields if request.args(1) == 'tutor'
                         else tutor_extra_fields)
        for field in hidden_fields:
            field.readable = field.writable = False
    return dict(form=auth())

Upvotes: 1

Related Questions