首页 > 解决方案 > 允许门户用户访问功能或导航到 odoo 12 页面

问题描述

我在 odoo 12 中创建了一个模块,它允许门户用户管理他们的时间表。
从控制器模块的所有可用功能,我使用sudo(),以便门户用户不会得到任何访问权限问题。
当创建一个新的时间表控制器直接调用该create()函数时,当删除它时调用unlink()但是当用户想要编辑时间表时,我将用户重定向到另一个页面,在该页面上,有一个编辑表单,但它显示了 403 禁止错误门户用户导航到该页面时的消息。
仅当我创建一个新的门户用户时才会出现此问题,它允许已经在 odoo 中的 Joel Willis。
sudo()也在那个编辑时间表模板中添加了,但它不起作用。
像这样..

class EditTimesheet(http.Controller):

    @http.route(['/edit_timesheet/<model("account.analytic.line"):timesheet>'], type='http', auth="public", website=True)
    def _edit_timesheet(self, timesheet, category='', search='', **kwargs):
        self.sudo().edit_timesheet(timesheet, category='', search='', **kwargs)

    def edit_timesheet(self, timesheet, category='', search='', **kwargs):
        return request.render("timesheet_module.edit_timesheet",{'timesheet':timesheet.sudo()})

记录器中的错误。
Traceback (most recent call last):
  File "/home/milan/workspace/odoo/odoo12/odoo/api.py", line 1049, in get
    value = self._data[key][field][record._ids[0]]
KeyError: 6

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/milan/workspace/odoo/odoo12/odoo/fields.py", line 1012, in __get__
    value = record.env.cache.get(record, self)
  File "/home/milan/workspace/odoo/odoo12/odoo/api.py", line 1051, in get
    raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('account.analytic.line(6,).display_name', None)

odoo.exceptions.AccessError: ('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: Analytic Line, Operation: read) - (Records: [6], User: 8)', None)

标签: python-3.xodoo-12

解决方案


当您<model("account.analytic.line"):timesheet>在路线中使用时,我相信它会在路线被击中时检查模型/登录用户的权限。因此,它甚至在您进行 sudo 调用之前就抛出了错误。我建议改为使用 accout.analytic.line id(确保你只传入 id )并将你的 2 条路由组合成 1 条这样的......

@http.route(['/edit_timesheet/<int:timesheet_id>'], type='http', auth="public", website=True)
    def edit_timesheet(self, timesheet_id, category='', search='', **kwargs):
        timsheet = env['account.analytic.line'].sudo().browse(timesheet_id)
        return request.render("timesheet_module.edit_timesheet",{'timesheet':timesheet})

推荐阅读