This question has been flagged
1 Reply
6115 Views

I need to assign the groups to user through programatically rather than directly selecting the groups from Access Rights screen.

I have created a field called "user_roles" which will give the list of roles such as [Sales, Purchase,so on..].

based on these roles i need to assign groups to user, and to clear other standard groups assigned to user.

Avatar
Discard
Best Answer

You can do something like this

user_model = self.pool.get('res.users')
external_id_model = self.pool.get('ir.model.data')
portal_group_id = external_id_model.get_object_reference(cr,uid,'portal','group_portal')[1]
res_id = False

username = "login id of user"

res_id = user_model.search(cr, uid, [('login','=', username)]))[0].id

user_model.write(cr,uid,[res_id],{'groups_id': [(6, 0, [portal_group_id])]})

this code removes all previous groups then adds the portal group.

if you need more reference on adding to many2many fields:

http://stackoverflow.com/questions/9377402/insert-into-many-to-many-openerp/9387447#9387447

Avatar
Discard