Browse Source
[ADD] Tests
[ADD] Tests
[RFR] Hide button instead of reconstructing div and trigger [RFR] Keep original behaviour if group is not found, courtesy of Antonio Esposito [FIX] Monkeypatch when module is actually loaded [RFR] OCA conventions [DEL] pot filepull/743/head
Stefan Rijnhart
8 years ago
11 changed files with 148 additions and 158 deletions
-
43base_import_security_group/README.rst
-
2base_import_security_group/__init__.py
-
35base_import_security_group/__openerp__.py
-
36base_import_security_group/hooks.py
-
21base_import_security_group/i18n/base_import_security_group.pot
-
5base_import_security_group/models/__init__.py
-
36base_import_security_group/models/models.py
-
51base_import_security_group/static/src/js/import.js
-
8base_import_security_group/static/src/xml/base_import_security_group.xml
-
1base_import_security_group/tests/__init__.py
-
62base_import_security_group/tests/test_base_import_security_group.py
@ -0,0 +1,36 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License and authorship info in: |
|||
# __openerp__.py file at the root folder of this module. |
|||
|
|||
from openerp import api |
|||
from openerp.models import BaseModel |
|||
import logging |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
base_load = BaseModel.load |
|||
|
|||
|
|||
@api.model |
|||
def load_import_optional(self, fields=None, data=None): |
|||
'''Overriding this method we only allow its execution if the current |
|||
user belongs to the group allowed for CSV data import. |
|||
An exception is raised otherwise, and the import attempt is logged. |
|||
This method is monkeypatched and will also affect other databases on the |
|||
same Odoo instance. Therefore, check if the group actually exist as |
|||
evidence that this module is installed. |
|||
''' |
|||
group_ref = 'base_import_security_group.group_import_csv' |
|||
group = self.env.ref(group_ref, raise_if_not_found=False) |
|||
if not group or self.env.user.has_group(group_ref): |
|||
return base_load(self, fields=fields, data=data) |
|||
msg = 'User (ID: %s) is not allowed to import data in model %s.' % ( |
|||
self.env.uid, self._name) |
|||
_logger.info(msg) |
|||
return {'ids': False, 'messages': [{ |
|||
'type': 'error', |
|||
'message': msg, |
|||
'moreinfo': False}]} |
|||
|
|||
|
|||
def post_load(): |
|||
BaseModel.load = load_import_optional |
@ -1,21 +0,0 @@ |
|||
# Translation of Odoo Server. |
|||
# This file contains the translation of the following modules: |
|||
# * base_import_security_group |
|||
# |
|||
msgid "" |
|||
msgstr "" |
|||
"Project-Id-Version: Odoo Server 8.0\n" |
|||
"Report-Msgid-Bugs-To: \n" |
|||
"POT-Creation-Date: 2015-08-06 18:37+0000\n" |
|||
"PO-Revision-Date: 2015-08-06 18:37+0000\n" |
|||
"Last-Translator: <>\n" |
|||
"Language-Team: \n" |
|||
"MIME-Version: 1.0\n" |
|||
"Content-Type: text/plain; charset=UTF-8\n" |
|||
"Content-Transfer-Encoding: \n" |
|||
"Plural-Forms: \n" |
|||
|
|||
#. module: base_import_security_group |
|||
#: model:res.groups,name:base_import_security_group.group_import_csv |
|||
msgid "Import CSV files" |
|||
msgstr "" |
@ -1,5 +0,0 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License and authorship info in: |
|||
# __openerp__.py file at the root folder of this module. |
|||
|
|||
from . import models |
@ -1,36 +0,0 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# License and authorship info in: |
|||
# __openerp__.py file at the root folder of this module. |
|||
|
|||
from openerp import api |
|||
from openerp.models import BaseModel |
|||
import logging |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
base_load = BaseModel.load |
|||
|
|||
|
|||
@api.model |
|||
def load_import_optional(self, fields=None, data=None): |
|||
'''Overriding this method we only allow its execution |
|||
if current user belongs to the group allowed for CSV data import. |
|||
An exception is raised otherwise, and also log the import attempt. |
|||
''' |
|||
res = {} |
|||
current_user = self.env['res.users'].browse(self.env.uid) |
|||
allowed_group = 'base_import_security_group.group_import_csv' |
|||
if current_user and current_user.has_group(allowed_group): |
|||
res = base_load(self, fields=fields, data=data) |
|||
else: |
|||
msg = ('User (ID: %s) is not allowed to import data ' |
|||
'in model %s.') % (self.env.uid, self._name) |
|||
_logger.error(msg) |
|||
messages = [] |
|||
info = {} |
|||
messages.append(dict(info, type='error', message=msg, moreinfo=None)) |
|||
res = {'ids': None, 'messages': messages} |
|||
return res |
|||
|
|||
# Monkey patch function |
|||
# Because BaseModel _name = None |
|||
BaseModel.load = load_import_optional |
@ -0,0 +1,8 @@ |
|||
<templates> |
|||
<!-- Hide import button, and only show after validation of privileges --> |
|||
<t t-extend="ListView.buttons"> |
|||
<t t-jquery="span.oe_alternative"> |
|||
this.attr('style', 'display: none;'); |
|||
</t> |
|||
</t> |
|||
</templates> |
@ -0,0 +1 @@ |
|||
from . import test_base_import_security_group |
@ -0,0 +1,62 @@ |
|||
# coding: utf-8 |
|||
# Copyright 2017 Stefan Rijnhart <stefan@opener.amsterdam> |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
|||
from openerp import tests |
|||
from openerp.tools import mute_logger |
|||
|
|||
|
|||
@tests.common.at_install(False) |
|||
@tests.common.post_install(True) |
|||
class TestImportGroup(tests.HttpCase): |
|||
def setUp(self): |
|||
super(TestImportGroup, self).setUp() |
|||
self.group_ref = 'base_import_security_group.group_import_csv' |
|||
self.group = self.env.ref(self.group_ref) |
|||
|
|||
def has_button_import(self, falsify=False): |
|||
""" |
|||
Verify that the button is either visible or invisible. |
|||
After the adjacent button is loaded, allow for a second for |
|||
the asynchronous call to finish and update the visibility """ |
|||
code = """ |
|||
window.setTimeout(function () { |
|||
if (%s$('.oe_list_button_import').is(':visible')) { |
|||
console.log('ok'); |
|||
} else { |
|||
console.log('error'); |
|||
}; |
|||
}, 1000); |
|||
""" % ('!' if falsify else '') |
|||
link = '/web#action=%s' % self.env.ref('base.action_res_users').id |
|||
with mute_logger('openerp.addons.base.res.res_users'): |
|||
# Mute debug log about failing row lock upon user login |
|||
self.phantom_js( |
|||
link, code, "$('button.oe_list_add').length", |
|||
login=self.env.user.login) |
|||
|
|||
def load(self): |
|||
self.env['res.partner'].load( |
|||
['name'], [['test_base_import_security_group']]) |
|||
return self.env['res.partner'].search( |
|||
[('name', '=', 'test_base_import_security_group')]) |
|||
|
|||
def test_01_in_group(self): |
|||
""" Show import button to users in the import group """ |
|||
self.env.user.groups_id += self.group |
|||
self.assertTrue(self.env.user.has_group(self.group_ref)) |
|||
self.has_button_import() |
|||
self.assertTrue(self.load()) |
|||
|
|||
def test_02_not_in_group(self): |
|||
""" Don't show import button to users not in the import group """ |
|||
self.env.user.groups_id -= self.group |
|||
self.assertFalse(self.env.user.has_group(self.group_ref)) |
|||
self.has_button_import(falsify=True) |
|||
self.assertFalse(self.load()) |
|||
|
|||
def test_03_no_group(self): |
|||
""" When the group does not exist, allow import (monkeypatch to assume |
|||
that this module is not installed in that case). """ |
|||
self.group.unlink() |
|||
self.assertFalse(self.env.user.has_group(self.group_ref)) |
|||
self.assertTrue(self.load()) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue