Odoo - Changing user group id just right after signup (ecommerce)

Viewed 1334

I'm using Odoo 10. After a new user sign up (through localhost:8069/web/signup) i want him to be automatically allocated inside a group i created on my very own custom module (the user will need authentication from an admin later on so he can be converted to a regular portal user; after signup he will receive restricted access).

I have tried many things. My latest effort looks like this:

class RestrictAccessOnSignup(auth_signup_controller.AuthSignupHome):

      def do_signup(self, *args):
        super(RestrictAccessOnSignup, self).do_signup(*args)
        request.env['res.groups'].sudo().write({'groups_id': 'group_unuser'})

Note that I have import odoo.addons.auth_signup.controllers.main as auth_signup_controller so that I can override the auth_signup controller.

I have located that method as the responsible for doing the signup. So I call it in my new method and then try to change the newly created user's group_id.

What i miss is a fundamental understanding of how to overwrite a field's value from another model inside a controller method context. I'm using the 'request' object although i'm not sure of it. I have seen people using 'self.pool['res.users'] (e.g.) for such purposes but i don't understand how to apply it inside my problem's context.

I believe, also, that there is a way to change the default group for a user after it is created (i would like to know), but i also want to understand how to solve the general problem (accessing and overwriting a field's value from another module).

Another weird thing is that the field groups_id does exist in 'res.users' model, but it does not appear as a column in my pgAdmin interface when i click to see the 'res.users' table... Any idea why?

Thanks a lot!

2 Answers

i don't know if after calling :

   super(RestrictAccessOnSignup,self).do_signup(*args)

you will have access to user record in request object but if so just add the group to user like this, if not you have to find where the user record or id is saved after calling do_signup because you need to update that record to ad this group.

   # create env variable i hate typing even i'm typing here ^^
   env = request.env
   env.user.sudo().write({'groups_id': [
                           # in odoo relation field accept a list of commands
                            # command 4 means add the id in the second position must be an integer
                            # ref return an object so we return the id
                            ( 4, env.ref('your_module_name.group_unuser').id),
                              ]
                          })

and if changes are not committed in database you may need to commit them

          request.env.cr.commit()

Note: self.env.ref you must pass the full xmlID.

This is what worked for me:

def do_signup(self, *args):

    super(RestrictAccessOnSignup, self).do_signup(*args)
    group_id = request.env['ir.model.data'].get_object('academy2', 'group_unuser')
    group_id.sudo().write({'users': [(4, request.env.uid)]})

In the get_object i pass as arguments the 'module' and the 'xmlID' of the group i want to fetch.

It is still not clear to me why 'ir.model.data' is the environment used, but this works as a charm. Please note that here we are adding a user to the group, and not a group to the user, and to me that actually makes more sense.

Any further elucidation or parallel solutions are welcome, the methods aren't as clear to me as they should be.

thanks.

Related