[IMP] web: userService: add hasGroup function
This commit adds the 'hasGroup' function to the user service. This function holds a cache s.t. we never do twice an rpc for the same group. This is the equivalent of 'user_has_group' in session, in the legacy codebase.
This commit is contained in:
@@ -3,8 +3,11 @@
|
||||
import { registry } from "./registry";
|
||||
|
||||
export const userService = {
|
||||
start() {
|
||||
dependencies: ["rpc"],
|
||||
async: ["hasGroup"],
|
||||
start(env, { rpc }) {
|
||||
const sessionInfo = odoo.session_info;
|
||||
const groupProms = {};
|
||||
|
||||
const context = {
|
||||
...sessionInfo.user_context,
|
||||
@@ -20,6 +23,20 @@ export const userService = {
|
||||
updateContext(update) {
|
||||
Object.assign(context, update);
|
||||
},
|
||||
hasGroup(group) {
|
||||
if (!context.uid) {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
if (!groupProms[group]) {
|
||||
groupProms[group] = rpc("/web/dataset/call_kw/res.users/has_group", {
|
||||
model: "res.users",
|
||||
method: "has_group",
|
||||
args: [group],
|
||||
kwargs: { context },
|
||||
});
|
||||
}
|
||||
return groupProms[group];
|
||||
},
|
||||
name: sessionInfo.name,
|
||||
userName: sessionInfo.username,
|
||||
isAdmin: sessionInfo.is_admin,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/** @odoo-module **/
|
||||
|
||||
import { registry } from "@web/core/registry";
|
||||
import { userService } from "@web/core/user_service";
|
||||
import { makeTestEnv } from "../helpers/mock_env";
|
||||
|
||||
const serviceRegistry = registry.category("services");
|
||||
|
||||
QUnit.module("UserService");
|
||||
|
||||
QUnit.test("successive calls to hasGroup", async (assert) => {
|
||||
serviceRegistry.add("user", userService);
|
||||
const groups = ["x"];
|
||||
const mockRPC = (route, args) => {
|
||||
assert.step(`${args.model}/${args.method}/${args.args[0]}`);
|
||||
return groups.includes(args.args[0]);
|
||||
};
|
||||
const env = await makeTestEnv({ mockRPC });
|
||||
let hasGroupX = await env.services.user.hasGroup("x");
|
||||
let hasGroupY = await env.services.user.hasGroup("y");
|
||||
assert.strictEqual(hasGroupX, true);
|
||||
assert.strictEqual(hasGroupY, false);
|
||||
let hasGroupXAgain = await env.services.user.hasGroup("x");
|
||||
assert.strictEqual(hasGroupXAgain, true);
|
||||
|
||||
assert.verifySteps(["res.users/has_group/x", "res.users/has_group/y"]);
|
||||
});
|
||||
Reference in New Issue
Block a user