Skip to content

Commit

Permalink
[Add] improve regex to detect for change API check access right
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Tran committed Nov 18, 2024
1 parent 1734786 commit 81eccb8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
44 changes: 42 additions & 2 deletions odoo_module_migrate/migration_scripts/migrate_170_180.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,51 @@ def replace_access_methods(
files_to_process = tools.get_files(module_path, (".py",))

replaces = {
# Fixed string operations with raise_exception=False
r"\.check_access_rights\(['\"](\w+)['\"],\s*raise_exception=False\)": r".has_access('\1')",
# Fixed string with operation= label
r"\.check_access_rights\(operation=['\"](\w+)['\"],\s*raise_exception=False\)": r".has_access('\1')",
# Variable operations with raise_exception=False
r"\.check_access_rights\((\w+),\s*raise_exception=False\)": r".has_access(\1)",
# Variable with operation= label
r"\.check_access_rights\(operation=(\w+),\s*raise_exception=False\)": r".has_access(\1)",
# Fixed string without label
r"\.check_access_rights\(['\"](\w+)['\"]\)": r".check_access('\1')",
# Fixed string with operation= label
r"\.check_access_rights\(operation=['\"](\w+)['\"]\)": r".check_access('\1')",
# Variable without label
r"\.check_access_rights\((\w+)\)": r".check_access(\1)",
# Variable with operation= label
r"\.check_access_rights\(operation=(\w+)\)": r".check_access(\1)",
# Fixed string without label
r"\.check_access_rule\(['\"](\w+)['\"]\)": r".check_access('\1')",
r"_filter_access_rule\(['\"](\w+)['\"]\)": r"_filter_access('\1')",
r"_filter_access_rule_python\(['\"](\w+)['\"]\)": r"_filter_access('\1')",
# Fixed string with operation= label
r"\.check_access_rule\(operation=['\"](\w+)['\"]\)": r".check_access('\1')",
# Variable without label
r"\.check_access_rule\((\w+)\)": r".check_access(\1)",
# Variable with operation= label
r"\.check_access_rule\(operation=(\w+)\)": r".check_access(\1)",
# _filter_access_rule patterns
r"\._filter_access_rule\(['\"](\w+)['\"]\)": r"._filter_access('\1')",
# Fixed string with operation= label
r"\._filter_access_rule\(operation=['\"](\w+)['\"]\)": r"._filter_access('\1')",
# Variable without label
r"\._filter_access_rule\((\w+)\)": r"._filter_access(\1)",
# Variable with operation= label
r"\._filter_access_rule\(operation=(\w+)\)": r"._filter_access(\1)",
# _filter_access_rule_python patterns
# Fixed string operations
r"\._filter_access_rule_python\(['\"](\w+)['\"]\)": r"._filter_access('\1')",
# Fixed string with operation= label
r"\._filter_access_rule_python\(operation=['\"](\w+)['\"]\)": r"._filter_access('\1')",
# Variable without label
r"\._filter_access_rule_python\((\w+)\)": r"._filter_access(\1)",
# Variable with operation= label
r"\._filter_access_rule_python\(operation=(\w+)\)": r"._filter_access(\1)",
# Match method definition with raise_exception=True
r"def\s+check_access_rights\s*\(\s*self\s*,\s*operation\s*,\s*raise_exception\s*=\s*True\s*\)": r"def check_access(self, operation: str) -> None",
# Match method definition with operation default value and raise_exception=True
r"def\s+check_access_rights\s*\(\s*self\s*,\s*operation\s*=\s*['\"](\w+)['\"]\s*,\s*raise_exception\s*=\s*True\s*\)": r"def check_access(self, operation: str = '\1') -> None",
}

for file in files_to_process:
Expand Down
33 changes: 32 additions & 1 deletion tests/data_result/module_170_180/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,41 @@ def example_method_has_group(self):
pass

def example_method_access(self):
operation = 'read'
self.env['account.move'].check_access('read')
self.env['account.move'].check_access('read')
self.env['account.move'].check_access(operation)
self.env['account.move'].check_access(operation)

self.env['account.move'].check_access('write')
self.env['account.move'].check_access('write')
self.env['account.move'].check_access(operation)
self.env['account.move'].check_access(operation)

self.env['account.move']._filter_access('read')
self.env['account.move']._filter_access('read')

self.env['account.move']._filter_access(operation)
self.env['account.move']._filter_access(operation)

self.env['account.move']._filter_access('read')
self.env['account.move']._filter_access('read')
self.env['account.move']._filter_access(operation)
self.env['account.move']._filter_access(operation)

if not self.env['account.move'].has_access('read'):
pass

if not self.env['account.move'].has_access('read'):
pass

if not self.env['account.move'].has_access(operation):
pass

if not self.env['account.move'].has_access(operation):
pass

def check_access(self, operation: str) -> None:
pass

def check_access(self, operation: str = 'read') -> None:
pass
33 changes: 32 additions & 1 deletion tests/data_template/module_170/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,41 @@ def example_method_has_group(self):
pass

def example_method_access(self):
operation = 'read'
self.env['account.move'].check_access_rights('read')
self.env['account.move'].check_access_rights(operation='read')
self.env['account.move'].check_access_rights(operation)
self.env['account.move'].check_access_rights(operation=operation)

self.env['account.move'].check_access_rule('write')
self.env['account.move'].check_access_rule(operation='write')
self.env['account.move'].check_access_rule(operation)
self.env['account.move'].check_access_rule(operation=operation)

self.env['account.move']._filter_access_rule('read')
self.env['account.move']._filter_access_rule(operation='read')
self.env['account.move']._filter_access_rule(operation)
self.env['account.move']._filter_access_rule(operation=operation)

self.env['account.move']._filter_access_rule_python('read')

self.env['account.move']._filter_access_rule_python(operation='read')
self.env['account.move']._filter_access_rule_python(operation)
self.env['account.move']._filter_access_rule_python(operation=operation)

if not self.env['account.move'].check_access_rights('read', raise_exception=False):
pass

if not self.env['account.move'].check_access_rights(operation='read', raise_exception=False):
pass

if not self.env['account.move'].check_access_rights(operation, raise_exception=False):
pass

if not self.env['account.move'].check_access_rights(operation=operation, raise_exception=False):
pass

def check_access_rights(self, operation, raise_exception=True):
pass

def check_access_rights(self, operation="read", raise_exception=True):
pass

0 comments on commit 81eccb8

Please sign in to comment.