Convert isViewer and isAdmin to getters (#6724)

This commit is contained in:
Tom Moor
2024-03-28 17:00:35 -06:00
committed by GitHub
parent 278b81a8fb
commit 0dede0b56e
20 changed files with 113 additions and 139 deletions

View File

@@ -1,4 +1,5 @@
import invariant from "invariant";
import { UserRole } from "@shared/types";
import WelcomeEmail from "@server/emails/templates/WelcomeEmail";
import {
InvalidAuthenticationError,
@@ -132,7 +133,7 @@ async function accountProvisioner({
name: userParams.name,
email: userParams.email,
language: userParams.language,
isAdmin: isNewTeam || undefined,
role: isNewTeam ? UserRole.Admin : undefined,
avatarUrl: userParams.avatarUrl,
teamId: team.id,
ip,

View File

@@ -1,4 +1,5 @@
import { Op, Transaction } from "sequelize";
import { UserRole } from "@shared/types";
import { Event, User } from "@server/models";
import { ValidationError } from "../errors";
@@ -29,7 +30,7 @@ export default async function userDestroyer({
if (user.isAdmin) {
const otherAdminsCount = await User.count({
where: {
isAdmin: true,
role: UserRole.Admin,
teamId,
id: {
[Op.ne]: user.id,

View File

@@ -58,8 +58,12 @@ export default async function userInviter({
teamId: user.teamId,
name: invite.name,
email: invite.email,
isAdmin: user.isAdmin && invite.role === UserRole.Admin,
isViewer: user.isViewer || invite.role === UserRole.Viewer,
role:
user.isAdmin && invite.role === UserRole.Admin
? UserRole.Admin
: user.isViewer || invite.role === UserRole.Viewer
? UserRole.Viewer
: UserRole.Member,
invitedById: user.id,
flags: {
[UserFlag.InviteSent]: 1,

View File

@@ -1,5 +1,6 @@
import { faker } from "@faker-js/faker";
import { v4 as uuidv4 } from "uuid";
import { UserRole } from "@shared/types";
import { TeamDomain } from "@server/models";
import {
buildUser,
@@ -185,8 +186,7 @@ describe("userProvisioner", () => {
expect(authentication?.scopes.length).toEqual(1);
expect(authentication?.scopes[0]).toEqual("read");
expect(user.email).toEqual("test@example.com");
expect(user.isAdmin).toEqual(false);
expect(user.isViewer).toEqual(false);
expect(user.role).toEqual(UserRole.Member);
expect(isNewUser).toEqual(true);
});
@@ -200,7 +200,7 @@ describe("userProvisioner", () => {
name: "Test Name",
email: "test@example.com",
teamId: team.id,
isAdmin: true,
role: UserRole.Admin,
ip,
authentication: {
authenticationProviderId: authenticationProvider.id,
@@ -210,12 +210,12 @@ describe("userProvisioner", () => {
},
});
const { user } = result;
expect(user.isAdmin).toEqual(true);
expect(user.role).toEqual(UserRole.Admin);
});
it("should prefer defaultUserRole when isAdmin is undefined or false", async () => {
const team = await buildTeam({
defaultUserRole: "viewer",
defaultUserRole: UserRole.Viewer,
});
const authenticationProviders = await team.$get("authenticationProviders");
const authenticationProvider = authenticationProviders[0];
@@ -232,13 +232,11 @@ describe("userProvisioner", () => {
},
});
const { user: tname } = result;
expect(tname.isAdmin).toEqual(false);
expect(tname.isViewer).toEqual(true);
expect(tname.role).toEqual(UserRole.Viewer);
const tname2Result = await userProvisioner({
name: "Test2 Name",
email: "tes2@example.com",
teamId: team.id,
isAdmin: false,
ip,
authentication: {
authenticationProviderId: authenticationProvider.id,
@@ -248,8 +246,7 @@ describe("userProvisioner", () => {
},
});
const { user: tname2 } = tname2Result;
expect(tname2.isAdmin).toEqual(false);
expect(tname2.isViewer).toEqual(true);
expect(tname2.role).toEqual(UserRole.Viewer);
});
it("should create a user from an invited user", async () => {

View File

@@ -1,4 +1,5 @@
import { InferCreationAttributes } from "sequelize";
import { UserRole } from "@shared/types";
import InviteAcceptedEmail from "@server/emails/templates/InviteAcceptedEmail";
import {
DomainNotAllowedError,
@@ -22,8 +23,8 @@ type Props = {
email: string;
/** The language of the user, if known */
language?: string;
/** Provision the new user as an administrator */
isAdmin?: boolean;
/** The role for new user, Member if none is provided */
role?: UserRole;
/** The public url of an image representing the user */
avatarUrl?: string | null;
/**
@@ -52,7 +53,7 @@ type Props = {
export default async function userProvisioner({
name,
email,
isAdmin,
role,
language,
avatarUrl,
teamId,
@@ -230,15 +231,12 @@ export default async function userProvisioner({
throw DomainNotAllowedError();
}
const defaultUserRole = team?.defaultUserRole;
const user = await User.create(
{
name,
email,
language,
isAdmin: typeof isAdmin === "boolean" && isAdmin,
isViewer: isAdmin === true ? false : defaultUserRole === "viewer",
role: role ?? team?.defaultUserRole,
teamId,
avatarUrl,
authentications: authentication ? [authentication] : [],