Friday, 27 October 2017

How to check login user group in odoo

    In this post you will learn how to check login user group. The need of this post is, to sometime we need to visible invisible some filed on the basis of login user group or we want to perform some action on the basis of login user. To do so we need to get login user group. There are two ways


to get login user group. We can use these lines of code on a "button click, onchange, computed filed etc".

    Method 1:
    
    user = self.env['res.users'].sudo().search(
       [('login','=',self.env.user.login)])
    desired_group_user = self.env['res.groups'].sudo().search( 
       [('name','=','desired_group_name')])
    query = "select gid from res_groups_users_rel where 
       gid ={} and uid={}".format(desired_group_user.id,user.id)  
    self.env.cr.execute(query)
    is_desired_group = self.env.cr.fetchone()
    desired_user_gr = self.env['res.groups'].sudo().search(
      [('id','=',is_desired_group)])
    
   
Method 2:
    desired_group_name = self.env['res.groups'].search(
      [('name','=','desired_group_name')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids

2 comments: