Odoo Help

Welcome!

This community 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.

0

How to now if current user is in certain group

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

I'm building a custom module in which I inherit res.partner. I want certain functinalities of my inherit res.partner  to be abailable for certain user gruop, 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

4

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3918
| 5 3 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])]">

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 5/11/15, 7:27 PM
Seen: 3752 times
Last updated: 8/19/16, 4:07 PM