Odoo Help

1

How to know if the current user is in a certain group

By
Luis Miguel Valenzuela
on 5/11/15, 7:27 PM 5,015 views

I'm building a custom module in which I inherited res.partner. I want certain functionalities of my inherit res.partner  to be available for certain user group, how in partner.py, might I find to which groups the current user is part of ?

I've tried to use a field with a Many2one ('res.groups', domain = ('user', 'in', ??????)) but don't know how to use in this domain the current user.

Thanks in advance

5

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
4002
| 5 4 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 5/12/15, 1:07 AM

If you need to control the method based on the group, then you can try this

flag = self.pool.get('res.users').has_group(cr, uid, 'base.group_sale_manager') 

If user has this group access then flag is TRUE else FALSE

Here,
uid --> User ID that needs to be checked
base.group_sale_manager --> XML id of the group that needs to be checked
-1
Vivek
On 9/18/15, 3:04 PM

I think you need something like : You have created one many2one field for Groups. In which, you want to list number of groups only that assign with current login user. Right? If yes, then I can suggest one way to you

First, you need to make one function field like group_ids in your current model. Then you have to set all groups of current login user in that field. Then you can use that field in domain.

Here is the example for function field (.py file)

def _get_user_groups(self,cr,uid,ids,fields,arg,context={}):

    res = dict.fromkeys(ids,[])

    user = self.pool.get('res.users').browse(cr,uid,uid)

    for partner in self.browse(cr,uid,ids):

        user_group_ids = map(int,user.groups_id)

        res[partner.id] = user_group_ids

    return res

_columns={

                    'group_ids':fields.function(_get_user_groups,type='many2many',relation='res.groups',

                        string='Groups'),

                    }

def _get_default_user_groups(self,cr,uid,context={}):

    user = self.pool.get('res.users').browse(cr,uid,uid)

    user_group_ids = map(int,user.groups_id)

    return user_group_ids

_defaults={

                    'group_ids':_get_default_user_groups,

                }

You must use _defaults because function field value set after record is save. So, at a time to create new record, if you don't use _defaults for group_ids field, there will not any group is set in this field.

 

In xml file you need to add field like :

<field name="group_ids" invisible="1" />

<field name="group_id" string="Group" domain="[('id','in',group_ids[0][2])]">

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 5/11/15, 7:27 PM
Seen: 5015 times
Last updated: 3/2/17, 3:13 AM