Browse Source

[FIX] base_user_role_profile: remove multicompany functionality

14.0
KevinKhao 4 years ago
parent
commit
460e4ecbcf
  1. 4
      base_user_role_profile/README.rst
  2. 12
      base_user_role_profile/models/user.py
  3. 1
      base_user_role_profile/readme/CONFIGURE.rst
  4. 4
      base_user_role_profile/readme/DESCRIPTION.rst
  5. 85
      base_user_role_profile/static/src/js/switch_profile_menu.js
  6. 23
      base_user_role_profile/static/src/xml/templates.xml
  7. 31
      base_user_role_profile/tests/test_user_role.py
  8. 2
      base_user_role_profile/views/role.xml
  9. 16
      base_user_role_profile/views/user.xml

4
base_user_role_profile/README.rst

@ -23,9 +23,9 @@ User profiles
:target: https://runbot.odoo-community.org/runbot/253/12.0
:alt: Try me on Runbot
|badge1| |badge2| |badge3| |badge4| |badge5|
|badge1| |badge2| |badge3| |badge4| |badge5|
Extending the base_user_role module, this one adds the notion of profiles. Effectively profiles act as an additional filter to how the roles are used. Through the new widget, much in the same way that a user can switch companies when they are part of the multi company group, users have the possibility to change profiles when they are part of the multi profiles group.
Extending the base_user_role module, this one adds the notion of profiles. Effectively profiles act as an additional filter to how the roles are used.
This allows users to switch their permission groups dynamically. This can be useful for example to:
- finer grain control on menu and model permissions (with record rules this becomes very flexible)

12
base_user_role_profile/models/user.py

@ -37,7 +37,7 @@ class ResUsers(models.Model):
@api.model
def create(self, vals):
new_record = super().create(vals)
if vals.get("company_id") or vals.get("role_line_ids"):
if vals.get("role_line_ids"):
new_record.sudo()._compute_profile_ids()
return new_record
@ -47,11 +47,7 @@ class ResUsers(models.Model):
self.sudo().write({"profile_id": vals["profile_id"]})
del vals["profile_id"]
res = super().write(vals)
if (
vals.get("company_id")
or vals.get("profile_id")
or vals.get("role_line_ids")
):
if vals.get("profile_id") or vals.get("role_line_ids"):
self.sudo()._compute_profile_ids()
return res
@ -73,9 +69,7 @@ class ResUsers(models.Model):
def _compute_profile_ids(self):
for rec in self:
role_lines = rec.role_line_ids
profiles = role_lines.filtered(
lambda r: r.company_id == rec.company_id
).mapped("profile_id")
profiles = role_lines.mapped("profile_id")
rec.profile_ids = profiles
# set defaults in case applicable profile changes
rec._update_profile_id()

1
base_user_role_profile/readme/CONFIGURE.rst

@ -1,2 +1 @@
Go to Configuration / Users / Profiles and create a profile. Go to Configuration / Users / Roles and define some role lines with profiles.
Be careful when defining role lines that company ids and profiles are correctly configured.

4
base_user_role_profile/readme/DESCRIPTION.rst

@ -1,4 +1,4 @@
Extending the base_user_role module, this one adds the notion of profiles. Effectively profiles act as an additional filter to how the roles are used. Through the new widget, much in the same way that a user can switch companies when they are part of the multi company group, users have the possibility to change profiles when they are part of the multi profiles group.
Extending the base_user_role module, this one adds the notion of profiles. Effectively profiles act as an additional filter to how the roles are used.
This allows users to switch their permission groups dynamically. This can be useful for example to:
- finer grain control on menu and model permissions (with record rules this becomes very flexible)
@ -8,5 +8,3 @@ This allows users to switch their permission groups dynamically. This can be use
When you define a role, you have the possibility to link it to a profile. Roles are applied to users in the following way:
- Apply user's roles without profiles in any case
- Apply user's roles that are linked to the currently selected profile
Note that this module assumes a multicompany environment

85
base_user_role_profile/static/src/js/switch_profile_menu.js

@ -1,85 +0,0 @@
odoo.define("web.SwitchProfileMenu", function (require) {
"use strict";
var config = require("web.config");
var core = require("web.core");
var session = require("web.session");
var SystrayMenu = require("web.SystrayMenu");
var Widget = require("web.Widget");
var _t = core._t;
var SwitchProfileMenu = Widget.extend({
template: "SwitchProfileMenu",
events: {
"click .dropdown-item[data-menu]": "_onClick",
},
init: function () {
this._super.apply(this, arguments);
this.isMobile = config.device.isMobile;
this._onClick = _.debounce(this._onClick, 1500, true);
},
willStart: function () {
return session.user_profiles ? this._super() : $.Deferred().reject();
},
start: function () {
var profilesList = "";
if (this.isMobile) {
profilesList =
'<li class="bg-info">' +
_t("Tap on the list to change profile") +
"</li>";
} else {
this.$(".oe_topbar_name").text(
session.user_profiles.current_profile[1]
);
}
_.each(session.user_profiles.allowed_profiles, function (profile) {
var a = "";
if (profile[0] == session.user_profiles.current_profile[0]) {
a = '<i class="fa fa-check mr8"></i>';
} else {
a = '<span style="margin-right: 24px;"/>';
}
profilesList +=
'<a role="menuitem" href="#" class="dropdown-item" data-menu="profile" data-profile-id="' +
profile[0] +
'">' +
a +
profile[1] +
"</a>";
});
this.$(".dropdown-menu").html(profilesList);
return this._super();
},
_onClick: function (ev) {
var self = this;
ev.preventDefault();
var profileID = $(ev.currentTarget).data("profile-id");
// We use this instead of the location.reload() because permissions change
// and we might land on a menu that we don't have permissions for. Thus it
// is cleaner to reload any root menu
this._rpc({
model: "res.users",
method: "action_profile_change",
args: [
[session.uid],
{
profile_id: profileID,
},
],
}).done(function (result) {
self.trigger_up("do_action", {
action: result,
});
});
},
});
SystrayMenu.Items.push(SwitchProfileMenu);
return SwitchProfileMenu;
});

23
base_user_role_profile/static/src/xml/templates.xml

@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates id="template" xml:space="preserve">
<t t-name="SwitchProfileMenu">
<li class="o_switch_profile_menu">
<a
role="button"
class="dropdown-toggle"
data-toggle="dropdown"
aria-expanded="false"
href="#"
aria-label="Dropdown menu"
title="Dropdown menu"
>
<span
t-attf-class="#{widget.isMobile ? 'fa fa-building-o' : 'oe_topbar_name'}"
/>
</a>
<div class="dropdown-menu dropdown-menu-right" role="menu" />
</li>
</t>
</templates>

31
base_user_role_profile/tests/test_user_role.py

@ -18,15 +18,10 @@ class TestUserProfile(TransactionCase):
self.user_model = self.env["res.users"]
self.role_model = self.env["res.users.role"]
self.company1 = self.env.ref("base.main_company")
self.company2 = self.env["res.company"].create({"name": "company2"})
self.default_user = self.env.ref("base.default_user")
user_vals = {
"name": "USER TEST (ROLES)",
"login": "user_test_roles",
"company_ids": [(6, 0, [self.company1.id, self.company2.id])],
"company_id": self.company1.id,
}
self.user_id = self.user_model.create(user_vals)
@ -115,29 +110,3 @@ class TestUserProfile(TransactionCase):
user_group_ids = set(user_group_ids)
expected_groups = set(self.role1_group_ids + self.role3_group_ids)
self.assertEqual(user_group_ids, expected_groups)
def test_sync_profile_change_company(self):
line1_vals = {
"role_id": self.role1_id.id,
"user_id": self.user_id.id,
"company_id": self.company1.id,
}
self.user_id.write({"role_line_ids": [(0, 0, line1_vals)]})
line2_vals = {
"role_id": self.role2_id.id,
"user_id": self.user_id.id,
"company_id": self.company2.id,
}
self.user_id.write({"role_line_ids": [(0, 0, line2_vals)]})
self.assertEqual(self.user_id.profile_ids, self.profile1_id)
user_group_ids = sorted({group.id for group in self.user_id.groups_id})
expected_group_ids = sorted(set(self.role1_group_ids))
self.assertEqual(user_group_ids, expected_group_ids)
self.user_id.company_id = self.company2
self.assertEqual(self.user_id.profile_ids, self.profile2_id)
user_group_ids = sorted({group.id for group in self.user_id.groups_id})
expected_group_ids = sorted(set(self.role2_group_ids))
self.assertEqual(user_group_ids, expected_group_ids)

2
base_user_role_profile/views/role.xml

@ -8,7 +8,7 @@
<field name="inherit_id" ref="base_user_role.view_res_users_role_form" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='line_ids']//field[@name='company_id']"
expr="//field[@name='line_ids']//field[@name='is_enabled']"
position="after"
>
<field name="profile_id" />

16
base_user_role_profile/views/user.xml

@ -22,25 +22,11 @@
</group>
</xpath>
<xpath
expr="//field[@name='role_line_ids']//field[@name='company_id']"
expr="//field[@name='role_line_ids']//field[@name='is_enabled']"
position="after"
>
<field name="profile_id" />
</xpath>
</field>
</record>
<record id="view_res_users_form_show_company" model="ir.ui.view">
<field name="name">res.users.form.inherit</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base_user_role.view_res_users_form_inherit" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='role_line_ids']//field[@name='company_id']"
position="attributes"
>
<attribute name="groups" eval="" />
</xpath>
</field>
</record>
</odoo>
Loading…
Cancel
Save