Skip to content

Commit

Permalink
fix: stop exposing isDustSuperUser (#2444)
Browse files Browse the repository at this point in the history
* fix: stop exposing isDustSuperUser

* last one
  • Loading branch information
fontanierh authored Nov 8, 2023
1 parent d0b3abc commit 09717cb
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 85 deletions.
1 change: 0 additions & 1 deletion front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ async function renderUserMessage(
fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""),
image: null,
workspaces: [],
isDustSuperUser: false,
}
: null,
mentions: mentions.map((m) => {
Expand Down
1 change: 0 additions & 1 deletion front/lib/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export async function getMembers(auth: Authenticator): Promise<UserType[]> {
lastName: u.lastName,
image: null,
workspaces: [{ ...owner, role }],
isDustSuperUser: u.isDustSuperUser,
};
});
}
Expand Down
11 changes: 8 additions & 3 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ export class Authenticator {
*/
static async fromSuperUserSession(
session: any,
wId: string
wId: string | null
): Promise<Authenticator> {
const [workspace, user] = await Promise.all([
(async () => {
if (!wId) {
return null;
}
return await Workspace.findOne({
where: {
sId: wId,
Expand Down Expand Up @@ -325,10 +328,13 @@ export class Authenticator {
// Not available from this method
image: null,
workspaces: [],
isDustSuperUser: false,
}
: null;
}

isDustSuperUser(): boolean {
return this._user ? this._user.isDustSuperUser : false;
}
}

/**
Expand Down Expand Up @@ -411,7 +417,6 @@ export async function getUserFromSession(
role,
};
}),
isDustSuperUser: user.isDustSuperUser,
};
}

Expand Down
4 changes: 2 additions & 2 deletions front/lib/plans/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export const pokeInviteWorkspaceToEnterprisePlan = async (
if (!owner) {
throw new Error("Cannot find workspace}");
}
const user = auth.user();
if (!user?.isDustSuperUser) {

if (!auth.isDustSuperUser()) {
throw new Error("Cannot invite workspace to enterprise plan: not allowed.");
}

Expand Down
7 changes: 4 additions & 3 deletions front/pages/api/poke/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as reporter from "io-ts-reporters";
import { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";

import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { ReturnedAPIErrorType } from "@app/lib/error";
import { Plan } from "@app/lib/models";
import { getProduct } from "@app/lib/plans/stripe";
Expand Down Expand Up @@ -60,9 +60,10 @@ async function handler(
>
): Promise<void> {
const session = await getSession(req, res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user || !user.isDustSuperUser) {
if (!user || !auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";

import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { ConnectorsAPI } from "@app/lib/connectors_api";
import { ReturnedAPIErrorType } from "@app/lib/error";
import { DataSource, Workspace } from "@app/lib/models";
Expand All @@ -15,7 +15,8 @@ async function handler(
res: NextApiResponse<BotEnabledResponseBody | ReturnedAPIErrorType>
): Promise<void> {
const session = await getSession(req, res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user) {
return apiError(req, res, {
Expand All @@ -27,7 +28,7 @@ async function handler(
});
}

if (!user.isDustSuperUser) {
if (!auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
17 changes: 4 additions & 13 deletions front/pages/api/poke/workspaces/[wId]/data_sources/[name]/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";

import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { ConnectorsAPI } from "@app/lib/connectors_api";
import { CoreAPI } from "@app/lib/core_api";
import { ReturnedAPIErrorType } from "@app/lib/error";
Expand All @@ -17,19 +17,10 @@ async function handler(
res: NextApiResponse<DeleteDataSourceResponseBody | ReturnedAPIErrorType>
): Promise<void> {
const session = await getSession(req, res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "user_not_found",
message: "Could not find the user.",
},
});
}

if (!user.isDustSuperUser) {
if (!user || !auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
17 changes: 4 additions & 13 deletions front/pages/api/poke/workspaces/[wId]/downgrade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";

import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { ReturnedAPIErrorType } from "@app/lib/error";
import { Workspace } from "@app/lib/models";
import { internalSubscribeWorkspaceToFreeTestPlan } from "@app/lib/plans/subscription";
Expand All @@ -16,19 +16,10 @@ async function handler(
res: NextApiResponse<DowngradeWorkspaceResponseBody | ReturnedAPIErrorType>
): Promise<void> {
const session = await getSession(req, res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "user_not_found",
message: "Could not find the user.",
},
});
}

if (!user.isDustSuperUser) {
if (!user || !auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
7 changes: 4 additions & 3 deletions front/pages/api/poke/workspaces/[wId]/revoke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";

import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { ReturnedAPIErrorType } from "@app/lib/error";
import { Membership, Workspace } from "@app/lib/models";
import { updateWorkspacePerSeatSubscriptionUsage } from "@app/lib/plans/subscription";
Expand All @@ -15,7 +15,8 @@ async function handler(
res: NextApiResponse<RevokeUserResponseBody | ReturnedAPIErrorType>
): Promise<void> {
const session = await getSession(req, res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user) {
return apiError(req, res, {
Expand All @@ -27,7 +28,7 @@ async function handler(
});
}

if (!user.isDustSuperUser) {
if (!auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
3 changes: 2 additions & 1 deletion front/pages/api/poke/workspaces/[wId]/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function handler(
res: NextApiResponse<UpgradeWorkspaceResponseBody | ReturnedAPIErrorType>
): Promise<void> {
const session = await getSession(req, res);

const { wId } = req.query;
if (!wId || typeof wId !== "string") {
return apiError(req, res, {
Expand All @@ -43,7 +44,7 @@ async function handler(
});
}

if (!user.isDustSuperUser) {
if (!auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
17 changes: 4 additions & 13 deletions front/pages/api/poke/workspaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
import { FindOptions, Op, WhereOptions } from "sequelize";

import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { ReturnedAPIErrorType } from "@app/lib/error";
import { Subscription, Workspace } from "@app/lib/models";
import { apiError, withLogging } from "@app/logger/withlogging";
Expand All @@ -16,19 +16,10 @@ async function handler(
res: NextApiResponse<GetWorkspacesResponseBody | ReturnedAPIErrorType>
): Promise<void> {
const session = await getSession(req, res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "user_not_found",
message: "Could not find the user.",
},
});
}

if (!user.isDustSuperUser) {
if (!user || !auth.isDustSuperUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down
1 change: 0 additions & 1 deletion front/pages/api/w/[wId]/members/[userId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ async function handler(
fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""),
image: null,
workspaces: [w],
isDustSuperUser: user.isDustSuperUser,
};

res.status(200).json({ member });
Expand Down
23 changes: 11 additions & 12 deletions front/pages/poke/[wId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from "react";

import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getDataSources } from "@app/lib/api/data_sources";
import { Authenticator, getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { useSubmitFunction } from "@app/lib/client/utils";
import { ConnectorsAPI } from "@app/lib/connectors_api";
import { CoreAPI } from "@app/lib/core_api";
Expand Down Expand Up @@ -37,9 +37,16 @@ export const getServerSideProps: GetServerSideProps<{
documentCounts: Record<string, number>;
dataSourcesSynchronizedAgo: Record<string, string>;
}> = async (context) => {
const session = await getSession(context.req, context.res);
const user = await getUserFromSession(session);
const wId = context.params?.wId;
if (!wId || typeof wId !== "string") {
return {
notFound: true,
};
}

const session = await getSession(context.req, context.res);
const auth = await Authenticator.fromSuperUserSession(session, wId);
const user = auth.user();

if (!user) {
return {
Expand All @@ -50,20 +57,12 @@ export const getServerSideProps: GetServerSideProps<{
};
}

if (!user.isDustSuperUser) {
if (!auth.isDustSuperUser()) {
return {
notFound: true,
};
}

if (!wId || typeof wId !== "string") {
return {
notFound: true,
};
}

const auth = await Authenticator.fromSuperUserSession(session, wId);

const owner = auth.workspace();
const subscription = auth.subscription();

Expand Down
23 changes: 11 additions & 12 deletions front/pages/poke/[wId]/memberships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import React from "react";

import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getMembers } from "@app/lib/api/workspace";
import { Authenticator, getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { UserType, WorkspaceType } from "@app/types/user";

export const getServerSideProps: GetServerSideProps<{
user: UserType;
owner: WorkspaceType;
members: UserType[];
}> = async (context) => {
const session = await getSession(context.req, context.res);
const user = await getUserFromSession(session);
const wId = context.params?.wId;
if (!wId || typeof wId !== "string") {
return {
notFound: true,
};
}

const session = await getSession(context.req, context.res);
const auth = await Authenticator.fromSuperUserSession(session, wId);
const user = auth.user();

if (!user) {
return {
Expand All @@ -26,20 +33,12 @@ export const getServerSideProps: GetServerSideProps<{
};
}

if (!user.isDustSuperUser) {
if (!auth.isDustSuperUser()) {
return {
notFound: true,
};
}

if (!wId || typeof wId !== "string") {
return {
notFound: true,
};
}

const auth = await Authenticator.fromSuperUserSession(session, wId);

const owner = auth.workspace();

if (!owner) {
Expand Down
7 changes: 4 additions & 3 deletions front/pages/poke/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import Link from "next/link";
import React, { ChangeEvent, useState } from "react";

import PokeNavbar from "@app/components/poke/PokeNavbar";
import { getSession, getUserFromSession } from "@app/lib/auth";
import { Authenticator, getSession } from "@app/lib/auth";
import { usePokeWorkspaces } from "@app/lib/swr";
import { UserType } from "@app/types/user";

export const getServerSideProps: GetServerSideProps<{
user: UserType;
}> = async (context) => {
const session = await getSession(context.req, context.res);
const user = await getUserFromSession(session);
const auth = await Authenticator.fromSuperUserSession(session, null);
const user = auth.user();

if (!user) {
return {
Expand All @@ -22,7 +23,7 @@ export const getServerSideProps: GetServerSideProps<{
};
}

if (!user.isDustSuperUser) {
if (!auth.isDustSuperUser()) {
return {
notFound: true,
};
Expand Down
1 change: 0 additions & 1 deletion front/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export type UserType = {
fullName: string;
image: string | null;
workspaces: WorkspaceType[];
isDustSuperUser: boolean;
};

export type UserMetadataType = {
Expand Down

0 comments on commit 09717cb

Please sign in to comment.