Skip to content

Commit

Permalink
enh: remove asSuperUser param from fromSession
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanierh committed Aug 31, 2023
1 parent 43c53e5 commit c3f633b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
84 changes: 58 additions & 26 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,9 @@ export class Authenticator {
*
* @param session any NextAuth session
* @param wId string target workspace id
* @param asSuperUser boolean if true, will return an admin role for the user if the user is a
* superuser
* @returns Promise<Authenticator>
*/
static async fromSession(
session: any,
wId: string,
asSuperUser = false
): Promise<Authenticator> {
static async fromSession(session: any, wId: string): Promise<Authenticator> {
const [workspace, user] = await Promise.all([
(async () => {
return await Workspace.findOne({
Expand All @@ -84,31 +78,69 @@ export class Authenticator {
let role = "none" as RoleType;

if (user && workspace) {
if (asSuperUser && user.isDustSuperUser) {
role = "admin";
} else {
const membership = await Membership.findOne({
const membership = await Membership.findOne({
where: {
userId: user.id,
workspaceId: workspace.id,
},
});

if (membership) {
switch (membership.role) {
case "admin":
case "builder":
case "user":
role = membership.role;
break;
default:
role = "none";
}
}
}

return new Authenticator(workspace, user, role);
}

/**
* Get a an Authenticator for the target workspace and the authentified Super User user from the
* NextAuth session.
* Super User will have `role` set to `admin` regardless of their actual role in the workspace.
*
* @param session any NextAuth session
* @param wId string target workspace id
* @returns Promise<Authenticator>
*/
static async fromSuperUserSession(
session: any,
wId: string
): Promise<Authenticator> {
const [workspace, user] = await Promise.all([
(async () => {
return await Workspace.findOne({
where: {
userId: user.id,
workspaceId: workspace.id,
sId: wId,
},
});

if (membership) {
switch (membership.role) {
case "admin":
case "builder":
case "user":
role = membership.role;
break;
default:
role = "none";
}
})(),
(async () => {
if (!session) {
return null;
} else {
return await User.findOne({
where: {
provider: session.provider.provider,
providerId: session.provider.id.toString(),
},
});
}
}
})(),
]);

if (!user || !user.isDustSuperUser) {
return new Authenticator(workspace, user, "none");
}

return new Authenticator(workspace, user, role);
return new Authenticator(workspace, user, "admin");
}

/**
Expand Down
6 changes: 1 addition & 5 deletions front/pages/poke/[wId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ export const getServerSideProps: GetServerSideProps<{
};
}

const auth = await Authenticator.fromSession(
session,
wId,
true // asSuperUser
);
const auth = await Authenticator.fromSuperUserSession(session, wId);

const workspace = auth.workspace();

Expand Down
6 changes: 1 addition & 5 deletions front/pages/poke/[wId]/memberships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ export const getServerSideProps: GetServerSideProps<{
};
}

const auth = await Authenticator.fromSession(
session,
wId,
true // asSuperUser
);
const auth = await Authenticator.fromSuperUserSession(session, wId);

const workspace = auth.workspace();

Expand Down

0 comments on commit c3f633b

Please sign in to comment.