Community mailing list archives

community@mail.odoo.com

Re: how to create user using rpc calls

by
Phillip
- 06/26/2016 10:19:58
In my experience I have not been able to create a user using RPC. You will notice the error is saying "You cannot create a new user from here." which means that some type of security is blocking the creation of a user by means of RPC. Unless I am wrong on this what I would do is direct write to the database using postgresql.

Basically you need to determine which fields are written to during the creatiion of a user and then use an sql query to write directly to the database. This can be trickier than it sounds. There are a bunch of fields which you are going to want to have in order to have a functional user. You need to add the user to the appropriate company as well as some messaging related fields and at the same time a partner record needs to be created for the user's partner. Here is a script which I wrote. Could be better but I end up with functional users.

This example I adapted from an odoo module and so I had access to the odoo env database cr. I have not been following your thread so I do not know if you are executing this from outside or inside odoo. If outside you are going to need to use psycopg2 to establish your cursor. You will need to define your own cursor and establish connection to the database. This may also involve configuring the database to allow your computer to connect to it if you are executing these scripts remotely. So instead of self.env.cr you will need to replace this with the cursor you define.

psycopg2

import psycopg2

conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print "Opened database successfully"

cur = conn.cursor()
cur.execute('''SOME SQL QUERY''')

conn.commit()
conn.close()



        #import xmlrpclib

        new_user_name = "John Smith"
        new_user_email = "john@gmail.com"
        partner = self.env['res.partner'].create({'name':new_user_name,'email':new_user_email})
        db_name = "myDbname"
        odoo_user = "Admin"
        odoo_password = "Admin"
        common = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/2/common)
        uid = common.authenticate(db_name, odoo_user, odoo_password, {})
        models = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/2/object')

        self.env.cr.execute("insert into res_users (active,login,company_id,partner_id,create_uid,write_uid,password_crypt,alias_id,display_groups_suggestions,share) values (true,%s,1,%s,1,1,'$pbkdf2-sha512$12000$DgFAqNWa8x5DyJkzxtg7Bw$Zu.Tpdi.BfR2f7NNPEMHkeCIMqSZQnwg3W5X1yP3ygwpElce8jq3I6.3JBYx/Fd5qr8GSk0wuwRO9tQCTDfKyQ',1,true,false)", ( new_user_name, partner,))
        self.env.cr.commit()
       
        user_id = models.execute_kw(db_name,uid,osoo_password,'res.users', 'search', [[['email','=',new_user_email ]]])[0]

        #HERE WE DETERMINE THE ID's OF THE SECURITY GROUPS YOU WISH THE USER TO BE A PART OF
        #YOUR SITUATION MAY REQUIRE DIFFERENT GROUPS

        contact_grp_id = models.execute_kw(db_name,uid,odoo_password,'res.groups', 'search', [[['name','=', 'Contact Creation']]])[0]
        employee_grp_id = models.execute_kw(db_name,uid,odoo_password,'res.groups', 'search', [[['name','=', 'Employee']]])[0]
        comments_grp_id = models.execute_kw(db_name,uid,odoo_password,'res.groups', 'search', [[['name','=', 'Website Comments']]])[0]

        self.env.cr.execute("insert into res_groups_users_rel (gid,uid) values (%s, %s)", (contact_grp_id,user_id,))
        self.env.cr.commit()
        self.env.cr.execute("insert into res_groups_users_rel (gid,uid) values (%s, %s)", (employee_grp_id,user_id,))
        self.env.cr.commit()
        self.env.cr.execute("insert into res_groups_users_rel (gid,uid) values (%s, %s)", (comments_grp_id,user_id,))
        self.env.cr.commit()
        self.env.cr.execute("insert into res_company_users_rel (cid,user_id) values (1,%s)", (user_id,))
        self.env.cr.commit()
        self.env.cr.execute("insert into mail_alias (create_uid, alias_parent_thread_id, alias_defaults, alias_contact, alias_parent_model_id, write_uid, alias_force_thread_id, alias_model_id,alias_user_id) values (1,%s,'{}','everyone',93,%s,%s,93,1)", (user_data,uid,user.id,))
        self.env.cr.commit()
        self.env.cr.execute("select id from mail_alias where alias_parent_thread_id = %s", (user_id,))
        res = self.env.cr.fetchall()
        mail_alias_id = res[0][0]
        self.env.cr.execute("update res_users set alias_id = %s where id = %s", (mail_alias_id,user_id,))
        self.env.cr.commit()
        reset_context = { 'create_user':1 }
        _logger.info("SENDING PASSWORD RESET REQUEST")
        context = self.env.context.copy()
        context.update(reset_context)
        user = self.with_context(context).env['res.users'].sudo().browse([user_id]).action_reset_password()