[web2py] Adding user to a group after registration

1,122 views
Skip to first unread message

Chandrakant Kumar

unread,
Dec 21, 2011, 3:00:20 PM12/21/11
to web...@googlegroups.com

I'm attempting to add a user to a group , depending on the role he chose
from a dropdown list.

for e.g., In the register form user is presented an option of choosing
user type 1. individual 2. organisation

how do i add the user to the appropriate group depending on his selection?
(i went through the group archives, and one solution was to use
request.args, but i want to use single register link)

Anthony

unread,
Dec 21, 2011, 3:22:50 PM12/21/11
to web...@googlegroups.com
You can use an auth.settings.register_onaccept function, which could use auth.add_membership() to add the appropriate membership for the new user.

Anthony

Chandrakant Kumar

unread,
Dec 21, 2011, 4:18:58 PM12/21/11
to web...@googlegroups.com
I didn't even know that existed, but that worked exactly like i wanted,
thanks.

I thought i should post the code, if anybody in future needs it:

#controllers/default.py
def __add_user_membership(form):
group =
db(db.auth_group.role==form.vars.user_type).select().first()
user_id = form.vars.id
auth.add_membership(group.id,user_id)
def user():
auth.settings.register_onaccept = __add_user_membership
# already existing code

Chandrakant Kumar

unread,
Dec 21, 2011, 5:03:56 PM12/21/11
to web...@googlegroups.com
better version :

def __add_user_membership(form):
group_id = auth.id_group(role=form.vars.user_type)
user_id = form.vars.id
auth.add_membership(group.id,user_id)

On Thu, 22 Dec 2011 01:52:50 +0530, Anthony <abas...@gmail.com> wrote:

Joe Lwe

unread,
Nov 27, 2016, 3:04:57 PM11/27/16
to web2py-users, k.03c...@gmail.com
Hi Kumar,
am at the wall with the same problem you hard, am not having success
below is my code, its from the account type table that i have defined account type
Any assistance is so much appreciated.
Thank you


/dbypy file
db.define_table('account_type',
  Field('name', 'string', length=20, notnull=True, label='Account Type'),
  migrate=True,
  format='%(name)s'
  )



#adding extra fields required on registration

auth.settings.extra_fields['auth_user']= [
    Field('country', 'reference country'),
    Field('district', 'reference district'),
    Field('account_type', 'reference account_type', label='Account Type')
    ]




default.py
def __add_user_membership(form):
    group_id = auth.id_group(role=form.vars.account_type)
    user_id = form.vars.id
    auth.add_membership(group_id, user_id)

   

def user()
  """
    auth.settings.register_onaccept = __add_user_membership

    return dict(form=auth())

Anthony

unread,
Nov 28, 2016, 7:16:18 AM11/28/16
to web2py-users
On Sunday, November 27, 2016 at 3:04:57 PM UTC-5, Joe Lwe wrote:
Hi Kumar,
am at the wall with the same problem you hard, am not having success

First, please always explain exactly what problem you are having -- what do you expect, and what is happening instead? If you get an error, show the traceback. It can be very difficult to diagnose vague problems like "not having success".
 
default.py
def __add_user_membership(form):
    group_id = auth.id_group(role=form.vars.account_type)

auth.id_group takes the name (not id) of a role stored in the db.auth_group table and returns the id of that role. It appears you are instead passing in the id of a record from your own db.account_type table -- it is not clear what you are expecting to get back.
 
    user_id = form.vars.id
    auth.add_membership(group_id, user_id)

As you are not using the db.auth_group table to assign roles, auth.add_membership will be of no use to you.

Anthony

Joe Lwe

unread,
Nov 29, 2016, 4:52:15 AM11/29/16
to web2py-users, k.03c...@gmail.com


Thanks Anthony,

 
 auth.id_group takes the name (not id) of a role stored in the db.auth_group table and returns the id of that role. was so useful.

This worked for me

def add_user_membership(form):
    group_id=form.vars.group_name
    user_id=form.vars.id
    auth.add_membership(group_id,user_id)
    

def user():
    """
     auth.settings.register_onaccept= add_user_membership
Reply all
Reply to author
Forward
0 new messages