Each tenant has a different domain of the form
tenant.example.com.
I create an auth_user record for the tenant's administrator and make his request_tenant field the same as his domain.
Then when the administrator creates other users, the request tenant default makes the new user's request_tenant same as the administrator's.
I have two other tables something like this.
db.define_table('tenant', Field('name', ....), Field('domain', ...), ...)
db.define_table('tenant_group', Field('tenant', db.tenant,), Field('group_id'), Field('is_default', 'boolean', default=False)...)
db.tenant_group.tenant.requires = IS_IN_DB(db, 'tenant.domain', '%(name)s')
I need the tenant group table because some tenants have different access rights.
The is_default field identifies which access rights for all the users related to the tenant.
Then the controller is something like this:
def add():
form = SQLFORM(db.auth_user, ...)
if form.process().accepted:
query = ((db.tenant_group.tenant==session.auth.user.request_tenant) &
(db.tenant_group.is_default==True))
rows = db(query).select(db.tenant_group.group_id)
for r in rows:
# this would be faster with a bulk insert!
db.auth_membership[0] = dict(user_id=
form.vars.id, group_id=r.group_id)
session.flash = blah
redirect(URL('edit')) # This is so the admin can further refine the user's access privileges.