diff --git a/front/lib/api/assistant/conversation.ts b/front/lib/api/assistant/conversation.ts index db443aca7cbd..836a15467e37 100644 --- a/front/lib/api/assistant/conversation.ts +++ b/front/lib/api/assistant/conversation.ts @@ -227,7 +227,6 @@ async function renderUserMessage( fullName: user.firstName + (user.lastName ? ` ${user.lastName}` : ""), image: null, workspaces: [], - isDustSuperUser: false, } : null, mentions: mentions.map((m) => { diff --git a/front/lib/api/workspace.ts b/front/lib/api/workspace.ts index 4d91a9c7697a..3fa522a7c40b 100644 --- a/front/lib/api/workspace.ts +++ b/front/lib/api/workspace.ts @@ -81,7 +81,6 @@ export async function getMembers(auth: Authenticator): Promise { lastName: u.lastName, image: null, workspaces: [{ ...owner, role }], - isDustSuperUser: u.isDustSuperUser, }; }); } diff --git a/front/lib/auth.ts b/front/lib/auth.ts index 6eb9537cba9a..afbb41afec3d 100644 --- a/front/lib/auth.ts +++ b/front/lib/auth.ts @@ -136,10 +136,13 @@ export class Authenticator { */ static async fromSuperUserSession( session: any, - wId: string + wId: string | null ): Promise { const [workspace, user] = await Promise.all([ (async () => { + if (!wId) { + return null; + } return await Workspace.findOne({ where: { sId: wId, @@ -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; + } } /** @@ -411,7 +417,6 @@ export async function getUserFromSession( role, }; }), - isDustSuperUser: user.isDustSuperUser, }; } diff --git a/front/lib/plans/subscription.ts b/front/lib/plans/subscription.ts index e8053283460f..71b45f3fd924 100644 --- a/front/lib/plans/subscription.ts +++ b/front/lib/plans/subscription.ts @@ -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."); } diff --git a/front/pages/api/poke/plans.ts b/front/pages/api/poke/plans.ts index 3c8792fb70a8..4553efe05f80 100644 --- a/front/pages/api/poke/plans.ts +++ b/front/pages/api/poke/plans.ts @@ -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"; @@ -60,9 +60,10 @@ async function handler( > ): Promise { 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: { diff --git a/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts b/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts index 78b58dd3177e..64553580dec5 100644 --- a/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts +++ b/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/bot_enabled.ts @@ -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"; @@ -15,7 +15,8 @@ async function handler( res: NextApiResponse ): Promise { 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, { @@ -27,7 +28,7 @@ async function handler( }); } - if (!user.isDustSuperUser) { + if (!auth.isDustSuperUser()) { return apiError(req, res, { status_code: 404, api_error: { diff --git a/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/index.ts b/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/index.ts index c26aa157d15d..3730539606ed 100644 --- a/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/index.ts +++ b/front/pages/api/poke/workspaces/[wId]/data_sources/[name]/index.ts @@ -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"; @@ -17,19 +17,10 @@ async function handler( res: NextApiResponse ): Promise { 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: { diff --git a/front/pages/api/poke/workspaces/[wId]/downgrade.ts b/front/pages/api/poke/workspaces/[wId]/downgrade.ts index 4f29106ec044..594a1a2ea287 100644 --- a/front/pages/api/poke/workspaces/[wId]/downgrade.ts +++ b/front/pages/api/poke/workspaces/[wId]/downgrade.ts @@ -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"; @@ -16,19 +16,10 @@ async function handler( res: NextApiResponse ): Promise { 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: { diff --git a/front/pages/api/poke/workspaces/[wId]/revoke.ts b/front/pages/api/poke/workspaces/[wId]/revoke.ts index e0399c81047f..d3fcafcccfc4 100644 --- a/front/pages/api/poke/workspaces/[wId]/revoke.ts +++ b/front/pages/api/poke/workspaces/[wId]/revoke.ts @@ -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"; @@ -15,7 +15,8 @@ async function handler( res: NextApiResponse ): Promise { 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, { @@ -27,7 +28,7 @@ async function handler( }); } - if (!user.isDustSuperUser) { + if (!auth.isDustSuperUser()) { return apiError(req, res, { status_code: 404, api_error: { diff --git a/front/pages/api/poke/workspaces/[wId]/upgrade.ts b/front/pages/api/poke/workspaces/[wId]/upgrade.ts index 0a2d796c9eda..e5d022ec0aba 100644 --- a/front/pages/api/poke/workspaces/[wId]/upgrade.ts +++ b/front/pages/api/poke/workspaces/[wId]/upgrade.ts @@ -19,6 +19,7 @@ async function handler( res: NextApiResponse ): Promise { const session = await getSession(req, res); + const { wId } = req.query; if (!wId || typeof wId !== "string") { return apiError(req, res, { @@ -43,7 +44,7 @@ async function handler( }); } - if (!user.isDustSuperUser) { + if (!auth.isDustSuperUser()) { return apiError(req, res, { status_code: 404, api_error: { diff --git a/front/pages/api/poke/workspaces/index.ts b/front/pages/api/poke/workspaces/index.ts index 332fc95d2fbe..1a7b80b9b8f3 100644 --- a/front/pages/api/poke/workspaces/index.ts +++ b/front/pages/api/poke/workspaces/index.ts @@ -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"; @@ -16,19 +16,10 @@ async function handler( res: NextApiResponse ): Promise { 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: { diff --git a/front/pages/api/w/[wId]/members/[userId]/index.ts b/front/pages/api/w/[wId]/members/[userId]/index.ts index 9a2750d0734e..1abe37bda78d 100644 --- a/front/pages/api/w/[wId]/members/[userId]/index.ts +++ b/front/pages/api/w/[wId]/members/[userId]/index.ts @@ -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 }); diff --git a/front/pages/poke/[wId]/index.tsx b/front/pages/poke/[wId]/index.tsx index 3d36e47ecde8..a6e7edc0570b 100644 --- a/front/pages/poke/[wId]/index.tsx +++ b/front/pages/poke/[wId]/index.tsx @@ -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"; @@ -37,9 +37,16 @@ export const getServerSideProps: GetServerSideProps<{ documentCounts: Record; dataSourcesSynchronizedAgo: Record; }> = 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 { @@ -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(); diff --git a/front/pages/poke/[wId]/memberships.tsx b/front/pages/poke/[wId]/memberships.tsx index 5d0c07517b47..abaa28257467 100644 --- a/front/pages/poke/[wId]/memberships.tsx +++ b/front/pages/poke/[wId]/memberships.tsx @@ -5,7 +5,7 @@ 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<{ @@ -13,9 +13,16 @@ export const getServerSideProps: GetServerSideProps<{ 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 { @@ -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) { diff --git a/front/pages/poke/index.tsx b/front/pages/poke/index.tsx index 90e1a3c9a054..405fce261a72 100644 --- a/front/pages/poke/index.tsx +++ b/front/pages/poke/index.tsx @@ -3,7 +3,7 @@ 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"; @@ -11,7 +11,8 @@ 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 { @@ -22,7 +23,7 @@ export const getServerSideProps: GetServerSideProps<{ }; } - if (!user.isDustSuperUser) { + if (!auth.isDustSuperUser()) { return { notFound: true, }; diff --git a/front/types/user.ts b/front/types/user.ts index 3d92c40ef8ba..20b5f15b7c2e 100644 --- a/front/types/user.ts +++ b/front/types/user.ts @@ -22,7 +22,6 @@ export type UserType = { fullName: string; image: string | null; workspaces: WorkspaceType[]; - isDustSuperUser: boolean; }; export type UserMetadataType = {