This question has been flagged
4 Replies
62036 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

Avatar
Discard
Best Answer

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
Avatar
Discard

how to get the value of cr?

Best Answer

Hello

for version 11, you can use below code.

to check if the use has a group_base try this :

if self.env.user.has_group('base.group_user') :

Hope this help you.

Best Thanks,

Ankit H Gandhi.

Avatar
Discard
Best Answer

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])]">

Avatar
Discard
Best Answer

here is something more recent wich work for me :

ack_workflows = name of my module
group_manager = my group name
generate_project_task() = my method name
@api.multi
def generate_task_action(self):
# On test l'appartenance au groupe
flag = self.env['res.users'].has_group('ack_workflows.group_manager')
if flag:
order.generate_project_task()
else:
raise UserError('Vous n\'avez pas les droits pour réaliser cette action')

very good link for the security : 
 https://www.odoo.yenthevg.com/creating-security-groups-odoo/
import for raise UserError :
from odoo.exceptions import UserError
I hope it useful

Avatar
Discard