[web2py] Re: Preventing login after Auth.register; Adding groups to the register form
I managed to get both of those problems sorted. Thank you for your input! Attached below is the rough code I used: @auth.requires_membership('administrators') def create(): # set up groups to be put into groups = db().select(db.auth_group.ALL) group_fields = [] group_lookup = {} # create the fields and a lookup table for the ids of the groups if we want to add them for g in groups: name = "is_in_group_%s" % g.role group_fields.append(Field(name, "boolean", label=g.role.capitalize())) group_lookup[name] = g.id # create the form form = SQLFORM.factory(db.auth_user, Field("password_two", "password", requires=IS_EXPR('value==%s' % repr(request.vars.get("password", None)), error_message=auth.messages.mismatched_password), label="Verify Password"), *group_fields) # if all good if form.accepts(request.vars): response.flash = 'User "%s" added successfully!' % request.vars["username"] # create an ignore list of input names to seperate the user data from the rest ignore_list = group_lookup.keys() + ["password_two", "_formname"] # seperate the user_data user_data = {} for k in request.vars: if k not in ignore_list: user_data[k] = request.vars[k] # create the new user new_user = db.auth_user.insert(**user_data) # process the membership information for g in group_lookup.keys(): if request.vars.get(g, None).lower() == "on": auth.add_membership(group_lookup[g], new_user) elif form.errors: response.flash = 'An error has occurred.' else: response.flash = 'Please fill out the form.' return dict(form=form) On Apr 22, 3:40 pm, mdipierro wrote: > auth.resiter() form is not thought for person A to register person B > but for person A to register itself. > > You can use appadmin to register another person or you have to create > your own interface for example via > > form=crud.create(db.auth_user) > > You can also make your form more complex > > form=SQLFORM.factory(db.auth_user,Field('group1'),Field('group2')): > if form.accepts(request.vars): > insert form.vars into db.auth_user > if form.vars.group1. auth.add_membership() > if form.vars.group2. auth.add_membership() > > On Apr 21, 10:00 pm, rohfle wrote: > > > When I said "logging on", I meant the process of login, not logging > > events. > > > Elaborating on the original questions: > > 1) For example, I login as an administrator and I register a user name > > named bob. When I click submit to register bob, the currently logged- > > in user gets switched from administrator to "bob" after being added to > > the database. Can I stop this switch from occurring? > > > 2) I want to extend the register form from looking in HTML like: > > [username] > > [firstname] > > [lastname] > > [password] > > [verify password] > > > > to: > > [username] > > [firstname] > > [lastname] > > [password] > > [verify password] > > > [x] group1 > > [ ] group2 > > > > where group1 and group2 come from the table auth_group. > > > Sorry I should have made these things clearer. > > > On Apr 22, 2:13 pm, mdipierro wrote: > > > > 1) Yes. Look at the bottom of this page: > > > >http://www.web2py.com/book/default/section/8/2 > > > > If you set a log message to None, the log is not performed. > > > > 2) Not sure I understand. You can do > > > > auth.settings.register_onaccept=lambda form: do_something_with(form) > > > > On Apr 21, 8:14 pm, rohfle wrote: > > > > > Couple of questions about web2py Auth: > > > > > 1) Can I stop Auth.register from logging on as the newly created user > > > > after registration? > > > > 2) Is there a way to add group membership to form returned by > > > > Auth.register? > > > > > -- > > > > Subscription > > > > settings:http://groups.google.com/group/web2py/subscribe?hl=en
[web2py] Re: Preventing login after Auth.register; Adding groups to the register form
When I said "logging on", I meant the process of login, not logging events. Elaborating on the original questions: 1) For example, I login as an administrator and I register a user name named bob. When I click submit to register bob, the currently logged- in user gets switched from administrator to "bob" after being added to the database. Can I stop this switch from occurring? 2) I want to extend the register form from looking in HTML like: [username] [firstname] [lastname] [password] [verify password] to: [username] [firstname] [lastname] [password] [verify password] [x] group1 [ ] group2 where group1 and group2 come from the table auth_group. Sorry I should have made these things clearer. On Apr 22, 2:13 pm, mdipierro wrote: > 1) Yes. Look at the bottom of this page: > > http://www.web2py.com/book/default/section/8/2 > > If you set a log message to None, the log is not performed. > > 2) Not sure I understand. You can do > > auth.settings.register_onaccept=lambda form: do_something_with(form) > > On Apr 21, 8:14 pm, rohfle wrote: > > > Couple of questions about web2py Auth: > > > 1) Can I stop Auth.register from logging on as the newly created user > > after registration? > > 2) Is there a way to add group membership to form returned by > > Auth.register? > > > -- > > Subscription settings:http://groups.google.com/group/web2py/subscribe?hl=en
[web2py] Re: Auto Creating Users/Groups/Roles
You probably need to 'crypt' the password before inserting it into the database. This can be done using something like: pw_hasher = IS_CRYPT() ticket_master = db.auth_user.insert(first_name=fname, last_name=lname, email=maile, password=pw_hasher(passwd)) The form based user registration does this automatically as part of validation, but since you are inserting a record manually, these validation steps are not executed to the best of my knowledge. Regards, R On Apr 22, 9:32 am, Patrick wrote: > I have been learning web2py and decided to create a simple ticket > system. Thus far I've been able to read the book/documentation or look > at others code and been able to hack my way through most issues. > However in my situation I need to have a default user created for > default administrative purposes, such as adding other admin users (I > know this isn't the best way to accomplish this but I'm learning... > advice, suggestions always welcome). > > I've tried something similar to this post "http://groups.google.com/ > group/web2py/browse_thread/thread/39f2d63f7024bbfb/2da45ae0132fe8fc? > lnk=gst&q=auto+users#2da45ae0132fe8fc", but I can't login as the user > because it says invalid login. > > I believe this is due to when I set the default password. If I login > appadmin and change the password and then try to login it works. So > I'm messing up on the password creation part. Here is the code for my > z_defuser.py: > > fname='ticket' > lname='master' > maile='tmas...@example.com' > passwd='tmaster09!' > > # Check to see if the user exists first. > # If the user does do nothing, else create the new user. > rows = db(db.auth_user.email == maile).select() > if rows: > pass > else: > ticket_master = db.auth_user.insert(first_name=fname, > last_name=lname, > email=maile, > password=passwd) > > P.S. > Any suggestions/advice are welcome. Thank you.
[web2py] Preventing login after Auth.register; Adding groups to the register form
Couple of questions about web2py Auth: 1) Can I stop Auth.register from logging on as the newly created user after registration? 2) Is there a way to add group membership to form returned by Auth.register? -- Subscription settings: http://groups.google.com/group/web2py/subscribe?hl=en