Skip to Content
Menu
This question has been flagged
4 Replies
24592 Views

Hello everybody.

    I am trying to obtain the groups which the logged in user belong to; after this what I´m trying to achieve is to compare if the logged in user belongs to a certain group in order to create a boolean flag which later on, I will use un my xml view to conditioned a page to be invisible or not.

I am using odoo 8 and the code I have until now is the following:

'make_invisible':fields.function(get_user, string="Is Invisible")
def get_user(self, cr, uid, ids, context=None): 
    _logger.debug("This is GET_USER method ")
    result = {}
    user = self.pool.get('res.users').browse(cr, uid, user_id, context=context)
    if user.has_group('hr_employee.NewGroup'):
        _logger.debug(":::True:::")
        return False
    else:
        _logger.debug(":::False:::")
        return False

Another question is: with the "has_group" should I use the group name that I wrote through the GUI or the XML ID??

Currently I am getting:

TypeError: get_user() takes at most 5 arguments (7 given)

Avatar
Discard

Define your function like this

` def get_user (self, cr, uid, ids, name, arg, context=None):

//statement

Author Best Answer

Hi Sehrish, Kabeer thanks for the tips, although I think I´m getting closer, I still can´t make it work, I went coding with method 2 (Sehrish) using it inside a computed field but several exceptions have been occurring:

field:

'make_invisible':fields.function(get_user, string="Is Invisible", readonly=0)

XML:

<field name="make_invisible"/>

First try:

def get_user(self, cr, uid, ids, name, arg, context=None):   
    _logger.debug("This is GET_USER method ") 
    desired_group_name = self.env['res.groups'].search([('name','=','GM')]) 
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

desired_group_name = self.env['res.groups'].search([('name','=','GM')]) 
AttributeError: 'hr.employee' object has no attribute 'env'

Second try: (try to use api.multi to avoid previous error)

@api.multi 
def get_user(self, cr, uid, ids, name, arg, context=None):
    _logger.debug("This is GET_USER method ")
    desired_group_name = self.env['res.groups'].search([('name','=','GM')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

File "/opt/odoo/openerp/api.py", line 363, in old_api 
    result = method(recs, *args, **kwargs)
TypeError: get_user() takes at least 6 arguments (4 given)

Third try: (use different function definition with api.multi)

@api.multi 
def get_user(self):
    _logger.debug("This is GET_USER method ")
    desired_group_name = self.env['res.groups'].search([('name','=','GM')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

File "/opt/odoo/openerp/api.py", line 709, in __new__  
    self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context)) 
ValueError: dictionary update sequence element #0 has length 1; 2 is required

Fouth try: (tried to use original function definition with pool.get instead of env)

def get_user(self, cr, uid, ids, name, arg, context=None):  
    _logger.debug("This is GET_USER method ")
    #desired_group_name = self.env['res.groups'].search([('name','=','GM')])
    desired_group_name = self.pool.get('res.groups').search([('name','=','GM')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

File "/opt/odoo/openerp/api.py", line 241, in wrapper  
    return old_api(self, *args, **kwargs) 
TypeError: search() takes at least 4 arguments (2 given)

Please help, dont know what else to do and what am I doing wrong!!!

Avatar
Discard
Best Answer

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

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

For further info about code and description visit: http://learnopenerp.blogspot.com/2017/10/how-to-check-login-user-group-in-odoo.html

This is the answer of your first part of question. If you want to visible invisible fields on some condition read: http://learnopenerp.blogspot.com/2016/10/how-to-visible-and-invisible-fields-in.html

Avatar
Discard
Related Posts Replies Views Activity
2
Feb 24
12689
2
Nov 19
2181
1
Jun 15
2452
0
May 15
2424
5
May 23
11338