Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base_user_role - Missing External IDs for User Role Groups #307

Open
COSTLAND opened this issue Sep 5, 2024 · 0 comments
Open

base_user_role - Missing External IDs for User Role Groups #307

COSTLAND opened this issue Sep 5, 2024 · 0 comments
Labels

Comments

@COSTLAND
Copy link

COSTLAND commented Sep 5, 2024

Module

base_user_role

Describe the bug

The base_user_role module creates user role groups without automatically assigning external IDs (XML IDs). This causes issues in two critical areas:

  1. When trying to assign these groups to views using Odoo Studio, as it requires groups to have external IDs.
  2. When manually editing XML views, as the XML IDs are needed to include these user groups in view definitions.
    These limitations significantly hinder the module's functionality and integration with Odoo's view management system.

To Reproduce

Affected versions: Odoo 16.0

Steps to reproduce the behavior:

  1. Install and configure the base_user_role module

  2. Create a new user role (e.g., "Finance Manager")

  3. Attempt to assign the newly created user role group to a view using Odoo Studio

    • Observe the error message: "Only groups with an external ID can be used here. Please choose another group or assign manually an external ID to this group."
    • image
  4. Alternatively, try to manually edit an XML view to include the new user role group

    • Observe that there's no valid XML ID to reference the group

Expected behavior
User role groups should be automatically created with unique, properly formatted external IDs, allowing them to be:

  1. Directly assignable in view definitions through Odoo Studio without errors.
  2. Easily referenceable when manually editing XML views.

Additional context

  • Current workaround: Manually assigning external IDs to the groups (records in res.groups) associated with user roles resolves the issue for both Odoo Studio and manual XML editing.
  • Once external IDs are manually assigned, the functionality works correctly.
  • This manual process is time-consuming, prone to oversight, and needs to be repeated for each role, significantly impacting development and maintenance efficiency.
  • The module would be much more user-friendly if it automatically assigned properly formatted external IDs to the groups upon creation.
  • The desired format for external IDs is "user_roles.{role_name_in_lowercase_with_spaces_replaced_by_underscores}" (e.g., "user_roles.finance_manager").
  • The solution should handle Unicode characters in role names, converting them to ASCII for the external ID.
  • All characters (including special characters) should be kept intact in the external ID, with only spaces being replaced by underscores and the string converted to lowercase.
  • The lack of automatic external ID assignment affects not just the ease of use with Odoo Studio, but also the ability to efficiently manage view access control through manual XML editing.
  • This issue fundamentally limits the integration of the base_user_role module with Odoo's view management system, both through the UI and in code.

Proposed solution

Modify the create method in the ResUsersRole model to automatically generate appropriate external IDs for the associated groups. Here's the proposed implementation:

import unicodedata
from odoo import api, models

def slugify(value):
    """
    Convert to ASCII. Convert spaces to underscores. Convert to lowercase.
    Also strip leading and trailing whitespace.
    """
    value = str(value)
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = value.strip().lower()
    return value.replace(' ', '_')

class ResUsersRole(models.Model):
    _name = "res.users.role"
    _inherits = {"res.groups": "group_id"}
    _description = "User role"

    @api.model_create_multi
    def create(self, vals_list):
        roles = super().create(vals_list)
        for role in roles:
            # Generate the external ID
            ext_id = f"user_roles.{slugify(role.name)}"
            
            # Check if this external ID already exists
            existing = self.env['ir.model.data'].search([
                ('module', '=', 'user_roles'),
                ('name', '=', slugify(role.name))
            ])
            
            # If it exists, append a number to make it unique
            if existing:
                count = 1
                while self.env['ir.model.data'].search([
                    ('module', '=', 'user_roles'),
                    ('name', '=', f"{slugify(role.name)}_{count}")
                ]):
                    count += 1
                ext_id = f"user_roles.{slugify(role.name)}_{count}"

            # Create the external ID
            self.env['ir.model.data'].create({
                'name': ext_id.split('.')[1],
                'module': 'user_roles',
                'model': 'res.groups',
                'res_id': role.group_id.id,
            })
        return roles

This solution ensures that:

  1. Each group associated with a user role has a unique external ID automatically assigned.
  2. The external IDs follow the format "user_roles.{role_name_in_lowercase_with_spaces_replaced_by_underscores}".
  3. Unicode characters in role names are properly handled and converted to ASCII for the external ID.
  4. Only spaces are replaced with underscores; all other characters (including special characters) are kept intact.
  5. The entire string is converted to lowercase.
  6. Leading and trailing whitespace is removed.
  7. Potential duplicates are handled by appending a number if necessary.

Implementing this solution will:

  • Eliminate the need for manual intervention in assigning external IDs.
  • Improve overall usability of the module for both Odoo Studio users and developers working with XML views.
  • Ensure seamless compatibility with Odoo's view management system, both through the UI and in code.
  • Streamline the process of assigning user role groups to views, enhancing the module's integration with Odoo's core functionality.
@COSTLAND COSTLAND added the bug label Sep 5, 2024
SiesslPhillip pushed a commit to grueneerde/OCA-server-backend that referenced this issue Nov 20, 2024
Syncing from upstream OCA/server-backend (16.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant