Skip to content

Commit

Permalink
[IMP] document_page_access_group_user_role: Users compatibility
Browse files Browse the repository at this point in the history
TT48786
  • Loading branch information
victoralmau committed Jul 24, 2024
1 parent a4bee7d commit 2764fce
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion document_page_access_group_user_role/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Usage
#. Go to `Knowledge / Pages` and create or edit one.
#. Set in the "Roles" tab the one we have just created.
#. Go back to the role, edit it and add any group(s).
#. The role groups will have been added in the "Security" tab.
#. The role users will have been added in the "Security" tab.

Bug Tracker
===========
Expand Down
2 changes: 1 addition & 1 deletion document_page_access_group_user_role/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Document Page Access Group User Role",
"author": "Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/knowledge",
"version": "16.0.1.0.0",
"version": "16.0.1.1.0",
"depends": ["document_page_access_group", "base_user_role"],
"license": "AGPL-3",
"category": "Knowledge",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
"""Pages that had roles should now have the correct users."""
pages = env["document.page"].sudo().search([("role_ids", "!=", [])])
for page in pages:
page.role_ids = False
page._compute_user_ids()
10 changes: 5 additions & 5 deletions document_page_access_group_user_role/models/document_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class DocumentPage(models.Model):
_inherit = "document.page"

groups_id = fields.Many2many(compute="_compute_groups_id", store=True)
user_ids = fields.Many2many(compute="_compute_user_ids", store=True, readonly=False)
role_ids = fields.Many2many(
comodel_name="res.users.role",
relation="document_page_user_roles_rel",
Expand All @@ -16,8 +16,8 @@ class DocumentPage(models.Model):
string="Roles",
)

@api.depends("role_ids", "role_ids.implied_ids")
def _compute_groups_id(self):
"""Create a compute to auto-set all the groups of the related roles."""
@api.depends("role_ids", "role_ids.users")
def _compute_user_ids(self):
"""Create a compute to auto-set all the users of the related roles."""
for item in self:
item.groups_id = item.mapped("role_ids.implied_ids")
item.user_ids += item.mapped("role_ids.users")
2 changes: 1 addition & 1 deletion document_page_access_group_user_role/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#. Go to `Knowledge / Pages` and create or edit one.
#. Set in the "Roles" tab the one we have just created.
#. Go back to the role, edit it and add any group(s).
#. The role groups will have been added in the "Security" tab.
#. The role users will have been added in the "Security" tab.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -391,7 +390,7 @@ <h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<li>Go to <cite>Knowledge / Pages</cite> and create or edit one.</li>
<li>Set in the “Roles” tab the one we have just created.</li>
<li>Go back to the role, edit it and add any group(s).</li>
<li>The role groups will have been added in the “Security” tab.</li>
<li>The role users will have been added in the “Security” tab.</li>
</ol>
</div>
<div class="section" id="bug-tracker">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests.common import users

from odoo.addons.base.tests.common import BaseCommon
from odoo.addons.document_page_access_group.tests.common import (
TestDocumentPageAccessGroupBase,
)


class TestDocumentPageAccessGroupUserRole(BaseCommon):
class TestDocumentPageAccessGroupUserRole(TestDocumentPageAccessGroupBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.page = cls.env["document.page"].create(
{"name": "Page 1", "type": "content"}
)
cls.group_a = cls.env["res.groups"].create({"name": "Test group A"})
cls.group_b = cls.env["res.groups"].create({"name": "Test group B"})
cls.user_role = cls.env["res.users.role"].create(
{"name": "Test role", "implied_ids": [(6, 0, [cls.group_a.id])]}
{
"name": "Test role",
"implied_ids": [(6, 0, [cls.group.id])],
"users": [(6, 0, [cls.manager_user.id])],
}
)
cls.role_page = cls.env["document.page"].create(
{
"name": "Role Page (test role)",
"type": "content",
"role_ids": [(6, 0, [cls.user_role.id])],
}
)

def test_document_page_role(self):
self.assertFalse(self.page.groups_id)
self.page.role_ids = [(4, self.user_role.id)]
self.assertIn(self.group_a, self.page.groups_id)
self.assertNotIn(self.group_b, self.page.groups_id)
self.user_role.implied_ids = [(4, self.group_b.id)]
self.assertIn(self.group_a, self.page.groups_id)
self.assertIn(self.group_b, self.page.groups_id)
self.page.role_ids = [(6, 0, [])]
self.assertNotIn(self.group_a, self.page.groups_id)
self.assertNotIn(self.group_b, self.page.groups_id)
def test_document_page_role_misc(self):
self.assertFalse(self.role_page.groups_id)
self.assertTrue(self.role_page.user_ids)

@users("test-user")
def test_document_page_role_access_01(self):
pages = self.env["document.page"].search([])
self.assertIn(self.public_page, pages)
self.assertNotIn(self.knowledge_page, pages)
self.assertIn(self.user_page, pages)
self.assertNotIn(self.role_page, pages)

@users("test-manager-user")
def test_document_page_role_access_02(self):
pages = self.env["document.page"].search([])
self.assertIn(self.public_page, pages)
self.assertIn(self.knowledge_page, pages)
self.assertNotIn(self.user_page, pages)
self.assertIn(self.role_page, pages)
19 changes: 12 additions & 7 deletions document_page_access_group_user_role/views/document_page_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
ref="document_page_access_group.document_page_access_group_view_wiki_form"
/>
<field name="arch" type="xml">
<page name="security" position="after">
<page name="roles" string="Roles" groups="base.group_erp_manager">
<field name="role_ids">
<group name="users" position="before">
<group
name="roles"
string="Roles"
groups="base.group_erp_manager"
attrs="{'invisible': [('groups_id','!=',[])]}"
>
<field name="role_ids" nolabel="1" colspan="2">
<tree>
<field name="name" />
<field name="comment" />
</tree>
</field>
</page>
</group>
<!-- It is necessary to add the field without groups to be able to apply the readonly after. !-->
<field name="role_ids" invisible="1" groups="!base.group_erp_manager" />
</page>
<field name="role_ids" invisible="1" />
</group>
<!-- Set groups as readonly if there is a defined roles to avoid UX confusion. !-->
<field name="groups_id" position="attributes">
<field name="user_ids" position="attributes">
<attribute
name="attrs"
>{'readonly': [('role_ids', '!=', [])]}</attribute>
Expand Down

0 comments on commit 2764fce

Please sign in to comment.