-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor access control logic for all pages (#297)
* Update apps/platform/store/UserRole.ts Co-authored-by: Puru Dahal <[email protected]> * feat: Refactor access control logic for all pages * chore: format code * chore: update variable naming convention * chore: Remove unused variables * chore: update variable naming convention --------- Co-authored-by: Puru Dahal <[email protected]>
- Loading branch information
Showing
23 changed files
with
280 additions
and
453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,40 @@ | ||
import { type GetServerSidePropsContext } from "next"; | ||
import ProjectLayout from "@/layouts/Project"; | ||
import { getServerSideSession } from "@/utils/session"; | ||
import { Project } from "@prisma/client"; | ||
import prisma from "@/lib/prisma"; | ||
import { withAccessControl } from "@/utils/withAccessControl"; | ||
import { Project, UserRole } from "@prisma/client"; | ||
|
||
/** | ||
* A functional component that represents a project. | ||
* @param {Props} props - The props for the component. | ||
* @param {Projects} props.projects - The projects the user has access to. | ||
* @param {currentProject} props.currentProject - The current project. | ||
* @param {roleInProject} props.roleInProject - The user role in current project. | ||
*/ | ||
|
||
interface Props { | ||
projects: Project[]; | ||
currentProject: Project; | ||
roleInProject: UserRole; | ||
} | ||
|
||
export const AuditLogsPage = ({ projects, currentProject }: Props) => { | ||
export const AuditLogsPage = ({ | ||
projects, | ||
currentProject, | ||
roleInProject, | ||
}: Props) => { | ||
return ( | ||
<ProjectLayout | ||
tab="audits" | ||
projects={projects} | ||
currentRole={roleInProject} | ||
currentProject={currentProject} | ||
> | ||
<h1>AuditLogsPage for {currentProject.name}</h1> | ||
</ProjectLayout> | ||
); | ||
}; | ||
|
||
export async function getServerSideProps(context: GetServerSidePropsContext) { | ||
const session = await getServerSideSession(context); | ||
const user = session?.user; | ||
|
||
// @ts-ignore | ||
const { slug } = context.params; | ||
|
||
if (!user) { | ||
return { | ||
redirect: { | ||
destination: "/auth", | ||
permanent: false, | ||
}, | ||
}; | ||
} | ||
|
||
const access = await prisma.access.findMany({ | ||
where: { | ||
// @ts-ignore | ||
userId: user.id, | ||
}, | ||
select: { | ||
id: true, | ||
project: { | ||
select: { | ||
id: true, | ||
name: true, | ||
slug: true, | ||
updatedAt: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!access) { | ||
return { | ||
redirect: { | ||
destination: "/projects", | ||
permanent: false, | ||
}, | ||
}; | ||
} | ||
|
||
const projects = access.map((access) => access.project); | ||
const currentProject = projects.find((project) => project.slug === slug); | ||
|
||
if (!currentProject) { | ||
return { | ||
redirect: { | ||
destination: "/projects", | ||
permanent: false, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
props: { | ||
currentProject: JSON.parse(JSON.stringify(currentProject)), | ||
projects: JSON.parse(JSON.stringify(projects)), | ||
}, | ||
}; | ||
} | ||
export const getServerSideProps = withAccessControl({ | ||
hasAccess: { maintainer: true, developer: true, guest: true, owner: true }, | ||
}); | ||
|
||
export default AuditLogsPage; |
Oops, something went wrong.