Published on

深入探究 Odoo 权限管理:模型和表解析

Authors
  • avatar
    Name
    NoOne
    Twitter

在企业管理软件中,权限管理是一项至关重要的功能,它确保不同用户和用户组能够安全、合理地访问和操作其被授权的资源和数据。在 Odoo 中,权限管理机制巧妙地融合了多个模型和表格,以实现细粒度的权限控制。本文将为您详细介绍 Odoo 权限管理相关的模型和表,并提供一个示例来帮助理解其用法。

权限管理相关的主要模型和表

在 Odoo 中,权限管理是确保不同用户和用户组只能访问和操作其被授权的资源和数据的核心机制。相关模型和表格包括以下几种:

res.users

   - 表名:res_users    - 描述:这个模型代表系统中的用户。每个用户都有一个唯一的ID、用户名、密码等信息。

 CREATE TABLE res_users ( 
 id integer DEFAULT nextval('res_users_id_seq'::regclass) not null,
 company_id integer  not null,
 partner_id integer  not null,
 active boolean DEFAULT true null,
 create_date timestamp without time zone  null,
 login character varying  not null,
 password character varying  null,
 action_id integer  null,
 create_uid integer  null,
 write_uid integer  null,
 signature text  null,
 share boolean  null,
 write_date timestamp without time zone  null,
 totp_secret character varying  null,
 notification_type character varying  not null,
 odoobot_state character varying  null,
 odoobot_failed boolean  null,
 sale_team_id integer  null,
 ); 

res.groups

   - 表名:res_groups    - 描述:用户组模型。它定义了一组用户,并且可以为这些用户赋予特定的访问权限。用户组可以被分配到特定的访问权限(ir.rule 和 ir.model.access)。

 CREATE TABLE res_groups ( 
 id integer DEFAULT nextval('res_groups_id_seq'::regclass) not null,
 name jsonb  not null,
 category_id integer  null,
 color integer  null,
 create_uid integer  null,
 write_uid integer  null,
 comment jsonb  null,
 share boolean  null,
 create_date timestamp without time zone  null,
 write_date timestamp without time zone  null,
 ); 
  • ir_act_window_group_rel: 定义了动作和组的关系
  • ir_model_fields_group_rel: 定义了模型字段和组的关系
  • ir_ui_menu_group_rel: 定义了菜单和组的关系
  • ir_ui_view_group_rel: 定义了视图和组的关系
  • res_groups_users_rel: 定义了用户和组的关系

ir.model.access

   - 表名:ir_model_access    - 描述:访问控制模型,这个模型定义了特定用户组对特定模型的访问权限,比如读、写、创建、删除等权限。

 CREATE TABLE ir_model_access ( 
 id integer DEFAULT nextval('ir_model_access_id_seq'::regclass) not null,
 model_id integer  not null,
 group_id integer  null,
 create_uid integer  null,
 write_uid integer  null,
 name character varying  not null,
 active boolean  null,
 perm_read boolean  null,
 perm_write boolean  null,
 perm_create boolean  null,
 perm_unlink boolean  null,
 create_date timestamp without time zone  null,
 write_date timestamp without time zone  null,
 ); 

ir.rule

   - 表名:ir_rule    - 描述:记录规则模型,用于定义基于记录级别的访问权限。可以在特定条件下限制某些记录仅供特定用户或用户组访问。

 CREATE TABLE ir_rule ( 
 id integer DEFAULT nextval('ir_rule_id_seq'::regclass) not null,
 model_id integer  not null,
 create_uid integer  null,
 write_uid integer  null,
 name character varying  null,
 domain_force text  null,
 active boolean  null,
 perm_read boolean  null,
 perm_write boolean  null,
 perm_create boolean  null,
 perm_unlink boolean  null,
 global boolean  null,
 create_date timestamp without time zone  null,
 write_date timestamp without time zone  null,
 ); 

权限管理示例

假设有以下需求:某个用户组Sales Team只能访问和修改sales.order模型中的记录,但是不能删除记录。

1. 创建用户组

res.groups中创建名为Sales Team的用户组。

2. 配置访问控制

ir.model.access中,添加以下记录:

  • Model: sales.order
  • Group: Sales Team
  • Read Access: 是
  • Write Access: 是
  • Create Access: 是
  • Delete Access: 否

3. 添加用户到用户组

将相应用户添加到刚创建的Sales Team用户组。

通过上述步骤,我们可以实现对 Sales Team 用户组的权限配置,使其只能够读取、创建和修改 sales.order 模型中的记录,而不能删除这些记录。进一步的复杂权限管理可以通过配置 ir.rule 来实现,例如,限制特定条件下的记录访问权限等。

代码判断是否有权限示例

应用中判断用户有无对应 group 权限示例:

if not self.env.user.has_group('account.group_account_manager'):
	raise UserError(_('The Journal Entry sequence is not conform to the current format. Only the Accountant can change it.'))

具体下钻到 has_group 方法的具体实现,本质上是查询用户在 res_groups_users_rel 表里面有没有记录。

@api.model
@tools.ormcache('self._uid', 'group_ext_id')
def _has_group(self, group_ext_id):
	"""Checks whether user belongs to given group.

	:param str group_ext_id: external ID (XML ID) of the group.
	   Must be provided in fully-qualified form (``module.ext_id``), as there
	   is no implicit module to use..
	:return: True if the current user is a member of the group with the
	   given external ID (XML ID), else False.
	"""
	assert group_ext_id and '.' in group_ext_id, "External ID '%s' must be fully qualified" % group_ext_id
	module, ext_id = group_ext_id.split('.')
	self._cr.execute("""SELECT 1 FROM res_groups_users_rel WHERE uid=%s AND gid IN
						(SELECT res_id FROM ir_model_data WHERE module=%s AND name=%s AND model='res.groups')""",
					 (self._uid, module, ext_id))
	return bool(self._cr.fetchone())

总结

Odoo 的权限管理机制十分灵活,通过组合使用 res.groups, ir.model.accessir.rule 等模型和表,我们可以实现非常细致和安全的权限控制。这不仅提高了系统的安全性,还能有效地支持各种业务需求。

希望本文对您理解 Odoo 的权限管理有所帮助。持续关注我们的博客,获取更多关于 Odoo 和企业管理软件的深入解析!

Share this content