Skip to content

Commit

Permalink
[FIX] dms_field: Avoid auto-generate multiple access groups + Refacto…
Browse files Browse the repository at this point in the history
…ring

Now the process will be as follows:
- The auto-generated access group will be created.
- The user will be defined in explicit_user_ids of the group
created in the previous step.

In the directory created (from the corresponding record) the auto-generated
group (for the user to have access) + the access groups that the template
had will be used.
  • Loading branch information
victoralmau authored and CarlosRoca13 committed Jun 19, 2024
1 parent 29971c4 commit 3db3da9
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 57 deletions.
2 changes: 1 addition & 1 deletion dms_field/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DMS Field
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ed7854681f73bbc617938529c1da77fcf81cba2bf46a8ae9387d8f20b360a53b
!! source digest: sha256:32b5522bce9f7a8e1a758183d74db8c1775e5b42d9ceef41a55724d1a4e62681
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
1 change: 1 addition & 0 deletions dms_field/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"website": "https://github.com/OCA/dms",
"depends": ["dms"],
"data": [
"views/dms_access_group_views.xml",
"views/dms_directory.xml",
"views/dms_field_template_views.xml",
"views/dms_storage.xml",
Expand Down
47 changes: 36 additions & 11 deletions dms_field/i18n/dms_field.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-16 11:00+0000\n"
"PO-Revision-Date: 2024-04-16 11:00+0000\n"
"POT-Creation-Date: 2024-06-13 06:26+0000\n"
"PO-Revision-Date: 2024-06-13 06:26+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -59,7 +59,7 @@ msgstr ""
#. odoo-python
#: code:addons/dms_field/models/dms_field_template.py:0
#, python-format
msgid "Autogenerate group from %(model)s (%(name)s) #%(id)s"
msgid "Autogenerated group from %(model)s (%(name)s) #%(id)s"
msgstr ""

#. module: dms_field
Expand Down Expand Up @@ -161,6 +161,9 @@ msgstr ""
#: model:ir.model.fields,field_description:dms_field.field_dms_tag__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_fetchmail_server__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_format_address_mixin__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_iap_account__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_iap_autocomplete_api__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_iap_enrich_api__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_image_mixin__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_ir_actions_act_url__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_ir_actions_act_window__dms_directory_ids
Expand Down Expand Up @@ -309,6 +312,19 @@ msgstr ""
#: model:ir.model.fields,field_description:dms_field.field_res_users_settings__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_res_users_settings_volumes__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_reset_view_arch_wizard__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_api__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_cancel__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_composer__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_resend__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_resend_recipient__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_sms__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_template__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_sms_template_preview__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_snailmail_confirm__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_snailmail_letter__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_snailmail_letter_cancel__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_snailmail_letter_format_error__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_snailmail_letter_missing_required_fields__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_web_editor_assets__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_web_editor_converter_test_sub__dms_directory_ids
#: model:ir.model.fields,field_description:dms_field.field_web_tour_tour__dms_directory_ids
Expand All @@ -324,8 +340,14 @@ msgid "DMS Tree"
msgstr ""

#. module: dms_field
#. odoo-javascript
#: code:addons/dms_field/static/src/views/dms_list/dms_list_renderer.esm.js:0
#: model:ir.model.fields,field_description:dms_field.field_dms_access_group__dms_field_ref
msgid "DMS field reference"
msgstr ""

#. module: dms_field
#. openerp-web
#: code:addons/dms_field/static/src/js/base/dms_tree_renderer.js:0
#: code:addons/dms_field/static/src/xml/controls.xml:0
#, python-format
msgid "Delete"
msgstr ""
Expand Down Expand Up @@ -477,8 +499,13 @@ msgid "Paste"
msgstr ""

#. module: dms_field
#. odoo-javascript
#: code:addons/dms_field/static/src/views/dms_list/dms_list_renderer.esm.js:0
#: model:ir.model,name:dms_field.model_dms_access_group
msgid "Record Access Groups"
msgstr ""

#. module: dms_field
#. openerp-web
#: code:addons/dms_field/static/src/xml/controls.xml:0
#, python-format
msgid "Preview"
msgstr ""
Expand Down Expand Up @@ -533,14 +560,12 @@ msgid "There is already a linked directory created."
msgstr ""

#. module: dms_field
#. odoo-python
#: code:addons/dms_field/models/dms_field_template.py:0
#: code:addons/dms_field/models/dms_access_group.py:0
#, python-format
msgid "There is already a template created for this model."
msgid "There is already an access group created for this record."
msgstr ""

#. module: dms_field
#. odoo-python
#: code:addons/dms_field/models/dms_field_template.py:0
#, python-format
msgid "There is no template linked to this model"
Expand Down
2 changes: 1 addition & 1 deletion dms_field/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ msgstr "Se ha producido un error durante la carga"
#. odoo-python
#: code:addons/dms_field/models/dms_field_template.py:0
#, python-format
msgid "Autogenerate group from %(model)s (%(name)s) #%(id)s"
msgid "Autogenerated group from %(model)s (%(name)s) #%(id)s"
msgstr "Grupo autogenerado desde %(model)s (%(name)s) #%(id)s"

#. module: dms_field
Expand Down
2 changes: 1 addition & 1 deletion dms_field/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ msgstr ""
#. module: dms_field
#: code:addons/dms_field/models/dms_field_template.py:0
#, python-format
msgid "Autogenerate group from %(model)s (%(name)s)"
msgid "Autogenerated group from %(model)s (%(name)s) #%(id)s"
msgstr ""

#. module: dms_field
Expand Down
2 changes: 1 addition & 1 deletion dms_field/i18n/it.po
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ msgstr "Aggiungi file: "
#. module: dms_field
#: code:addons/dms_field/models/dms_field_template.py:0
#, python-format
msgid "Autogenerate group from %(model)s (%(name)s)"
msgid "Autogenerated group from %(model)s (%(name)s) #%(id)s"
msgstr ""

#. module: dms_field
Expand Down
23 changes: 20 additions & 3 deletions dms_field/models/dms_access_group.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class DmsAccessGroups(models.Model):
_inherit = "dms.access.group"

model_ref = fields.Reference(
dms_field_ref = fields.Reference(
selection="_selection_reference_value",
string="Default value",
string="DMS field reference",
)

@api.model
Expand All @@ -20,3 +21,19 @@ def _selection_reference_value(self):
.search([("transient", "=", False)], order="name asc")
)
return [(model.model, model.name) for model in models]

def _get_item_from_dms_field_ref(self, record):
return self.env["dms.access.group"].search(
[("dms_field_ref", "=", "%s,%s" % (record._name, record.id))]
)

@api.constrains("dms_field_ref")
def _check_dms_field_ref(self):
for item in self.filtered("dms_field_ref"):
dms_field_ref = "%s,%s" % (item.dms_field_ref._name, item.dms_field_ref.id)
if self.search(
[("dms_field_ref", "=", dms_field_ref), ("id", "!=", item.id)]
):
raise UserError(
_("There is already an access group created for this record.")
)
25 changes: 25 additions & 0 deletions dms_field/models/dms_field_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,28 @@ def create(self, vals_list):
for item in result:
template.with_context(res_id=item.id).create_dms_directory()
return result

def write(self, vals):
"""When modifying a record that has linked directories and changing the
user_id field it is necessary to update the auto-generated access group
(name and explicit_user_ids).
"""
res = super().write(vals)
for item in self.filtered("dms_directory_ids"):
if "user_id" in vals:
template = self.env["dms.field.template"]._get_template_from_model(

Check warning on line 52 in dms_field/models/dms_field_mixin.py

View check run for this annotation

Codecov / codecov/patch

dms_field/models/dms_field_mixin.py#L52

Added line #L52 was not covered by tests
item._name
)
if template:
template._get_autogenerated_group(item)

Check warning on line 56 in dms_field/models/dms_field_mixin.py

View check run for this annotation

Codecov / codecov/patch

dms_field/models/dms_field_mixin.py#L56

Added line #L56 was not covered by tests
return res

def unlink(self):
"""When deleting a record, we also delete the linked directories and the
auto-generated access group.
"""
for record in self.filtered("dms_directory_ids"):
group = self.env["dms.access.group"]._get_item_from_dms_field_ref(record)
record.sudo().dms_directory_ids.unlink()
group.sudo().unlink()
return super().unlink()

Check warning on line 67 in dms_field/models/dms_field_mixin.py

View check run for this annotation

Codecov / codecov/patch

dms_field/models/dms_field_mixin.py#L64-L67

Added lines #L64 - L67 were not covered by tests
72 changes: 40 additions & 32 deletions dms_field/models/dms_field_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,43 @@ def _copy_files_from_directory(self, directory, new_directory):
for file in directory.file_ids:
file.copy({"directory_id": new_directory.id})

Check warning on line 105 in dms_field/models/dms_field_template.py

View check run for this annotation

Codecov / codecov/patch

dms_field/models/dms_field_template.py#L105

Added line #L105 was not covered by tests

def _set_groups_from_directory(self, directory, record):
"""Set groups of directory, if there are already linked groups we use those
to avoid constraint name error."""
group_model = groups = self.env["dms.access.group"]
groups_ref = group_model.search(
[("model_ref", "=", "%s,%s" % (record._name, record.id))]
)
if groups_ref:
return groups_ref
for group in directory.group_ids:
group_name = _("Autogenerate group from %(model)s (%(name)s) #%(id)s") % {
"model": record._description,
"name": record.display_name,
"id": record.id,
}
new_group = group.copy(
{
"name": group_name,
"directory_ids": False,
"model_ref": "%s,%s" % (record._name, record.id),
}
)
# Apply sudo() because the user may not have permissions to access
# ir.model.fields.
user_field = self.sudo().user_field_id
if user_field:
user = record[user_field.name]
if user:
new_group.write({"explicit_user_ids": [(4, user.id)]})
groups += new_group
return groups
def _prepare_autogenerated_group(self, record):
group_name = _("Autogenerated group from %(model)s (%(name)s) #%(id)s") % {
"model": record._description,
"name": record.display_name,
"id": record.id,
}
vals = {
"name": group_name,
# We need to set all the permissions so that the user can manage their
# documents (directories and files)
"perm_create": True,
"perm_write": True,
"perm_unlink": True,
"dms_field_ref": "%s,%s" % (record._name, record.id),
"explicit_user_ids": [(5, 0)],
}
# Apply sudo() because the user may not have permissions to access
# ir.model.fields.
user_field = self.sudo().user_field_id
if user_field:
user = record[user_field.name]

Check warning on line 127 in dms_field/models/dms_field_template.py

View check run for this annotation

Codecov / codecov/patch

dms_field/models/dms_field_template.py#L127

Added line #L127 was not covered by tests
if user:
vals["explicit_user_ids"] += [(4, user.id)]

Check warning on line 129 in dms_field/models/dms_field_template.py

View check run for this annotation

Codecov / codecov/patch

dms_field/models/dms_field_template.py#L129

Added line #L129 was not covered by tests
return vals

def _get_autogenerated_group(self, record):
"""Get the existing auto-generated group or create a new one.
The permissions of the auto-generated group should be changed
to make sure you have the correct data.
"""
group_model = self.env["dms.access.group"]
group_ref = group_model._get_item_from_dms_field_ref(record)
if group_ref:
group_ref.write(self._prepare_autogenerated_group(record))
return group_ref
# Create the autogenerated group linked to the record
return group_model.create(self._prepare_autogenerated_group(record))

def _create_child_directories(self, parent, directory):
# Create child directories (all leves) + files
Expand All @@ -151,7 +157,9 @@ def _create_child_directories(self, parent, directory):
self._create_child_directories(child, child_directory)

def _prepare_directory_vals(self, directory, record):
groups = self._set_groups_from_directory(directory, record)
# Groups of the new directory will be those of the template + auto-generate
groups = directory.group_ids
groups += self._get_autogenerated_group(record)
directory_name = self.env["mail.render.mixin"]._render_template(
self.directory_format_name,
record._name,
Expand Down
2 changes: 1 addition & 1 deletion dms_field/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">DMS Field</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ed7854681f73bbc617938529c1da77fcf81cba2bf46a8ae9387d8f20b360a53b
!! source digest: sha256:32b5522bce9f7a8e1a758183d74db8c1775e5b42d9ceef41a55724d1a4e62681
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/dms/tree/16.0/dms_field"><img alt="OCA/dms" src="https://img.shields.io/badge/github-OCA%2Fdms-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/dms-16-0/dms-16-0-dms_field"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/dms&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This addon creates a new kind of view and allows to define a folder
Expand Down
28 changes: 22 additions & 6 deletions dms_field/tests/test_dms_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import fields
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError
from odoo.tests import TransactionCase, new_test_user
from odoo.tools import mute_logger


class TestDmsField(TransactionCase):
Expand Down Expand Up @@ -100,12 +101,23 @@ def test_js_tree(self):
)
)

def test_dms_access_group_constrains_dms_field_ref(self):
group = self.env["dms.access.group"].create(
{
"name": "Test 1",
"dms_field_ref": "%s,%s" % (self.partner._name, self.partner.id),
}
)
with self.assertRaises(UserError):
group.copy({"name": "Test 2"})

def test_template_directory(self):
self.assertTrue(self.template.dms_directory_ids)
self.assertIn(
self.template.group_ids, self.template.dms_directory_ids.group_ids
)

@mute_logger("odoo.models.unlink")
def test_creation_process_01(self):
self.assertFalse(self.partner.dms_directory_ids)
template = self.env["dms.field.template"].with_context(
Expand All @@ -119,9 +131,13 @@ def test_creation_process_01(self):
self.assertFalse(directory_0.parent_id)
self.assertTrue(directory_0.is_root_directory)
self.assertTrue(directory_0.inherit_group_ids)
self.assertNotIn(self.template.group_ids, directory_0.group_ids)
self.assertIn(self.template.group_ids, directory_0.group_ids)
self.assertIn(self.group, directory_0.group_ids.group_ids)
self.assertEqual(directory_0.group_ids.model_ref, self.partner)
self.assertIn(self.partner, directory_0.mapped("group_ids.dms_field_ref"))
group_custom = directory_0.group_ids.filtered("dms_field_ref")
self.assertTrue(group_custom.perm_create)
self.assertTrue(group_custom.perm_write)
self.assertTrue(group_custom.perm_unlink)
self.assertIn(self.user_b, directory_0.group_ids.explicit_user_ids)
self.assertIn(self.user_a, directory_0.group_ids.users)
self.assertIn(self.user_b, directory_0.group_ids.users)
Expand All @@ -132,9 +148,9 @@ def test_creation_process_01(self):
# Remove folder: El grupo de acceso todavía existe
old_groups = directory_0.group_ids
directory_0.unlink()
model_ref_value = "%s,%s" % (self.partner._name, self.partner.id)
dms_field_ref_value = "%s,%s" % (self.partner._name, self.partner.id)
total = self.env["dms.access.group"].search_count(
[("model_ref", "=", model_ref_value)]
[("dms_field_ref", "=", dms_field_ref_value)]
)
self.assertEqual(total, 1)
# Create directory again (access groups are the same)
Expand All @@ -158,7 +174,7 @@ def test_creation_process_01_with_parent(self):
self.assertEqual(directory_0.parent_id, self.template.parent_directory_id)
self.assertFalse(directory_0.is_root_directory)
self.assertFalse(directory_0.inherit_group_ids)
self.assertNotIn(self.template.group_ids, directory_0.group_ids)
self.assertIn(self.template.group_ids, directory_0.group_ids)
self.assertIn(self.group, directory_0.group_ids.group_ids)
self.assertIn(self.user_b, directory_0.group_ids.explicit_user_ids)
self.assertIn(self.user_a, directory_0.group_ids.users)
Expand Down
Loading

0 comments on commit 3db3da9

Please sign in to comment.