-
Notifications
You must be signed in to change notification settings - Fork 0
RBAC System #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cadencheng888
wants to merge
5
commits into
main
Choose a base branch
from
feature/rbac-system
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
RBAC System #10
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2724c72
Add user model with RBAC fields
cadencheng888 704cf85
Updated user model schema with comments
cadencheng888 f5c885b
Add getSession() and ROLE_PERMISSIONS to rbac.ts
cadencheng888 5120b17
Updated based on comments
cadencheng888 152e178
Add email validation and labs.labsId index to User model
cadencheng888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { connectToDatabase } from "@/lib/mongoose"; | ||
| import { auth } from "@/auth"; | ||
| import { User, Role, IUser } from "@/models/User"; | ||
|
|
||
| // establish the role permissions | ||
|
|
||
| const ROLE_PERMISSIONS: Record<Role, string[]> = { | ||
| PI: [ | ||
| "inventory:create", | ||
| "inventory:update", | ||
| "inventory:delete", | ||
| "inventory:set_threshold", | ||
| "transfer:approve", | ||
| "payment:approve", | ||
| "lab:manage_users", | ||
| ], | ||
| LAB_MANAGER: [ | ||
| "inventory:create", | ||
| "inventory:update", | ||
| "inventory:set_threshold", | ||
| "transfer:request" | ||
| ], | ||
| RESEARCHER: [ | ||
| "inventory:view", | ||
| "transfer:request", | ||
| "listing:create" | ||
| ], | ||
| VIEWER: [ | ||
| "inventory:view" | ||
| ] | ||
| }; | ||
|
|
||
| // checks if the user has permission to perform an action | ||
|
|
||
| export async function getSession(permission: string) { | ||
| const session = await auth(); | ||
|
|
||
| if (!session?.user?.email) { | ||
| return { allowed: false, user: null, reason: "Unauthenticated" }; | ||
| } | ||
|
|
||
| await connectToDatabase(); | ||
| const user = await User.findOne({ email: session.user.email }).lean<IUser>(); | ||
|
|
||
| if (!user){ | ||
| return { allowed: false, user: null, reason: "User not found" }; | ||
| } | ||
|
|
||
arnavjk007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (user.status !== "ACTIVE") { | ||
| return { allowed: false, user, reason: "Account inactive" }; | ||
| } | ||
|
|
||
| const allowed = ROLE_PERMISSIONS[user.role]?.includes(permission) ?? false; | ||
| return { allowed, user, reason: allowed ? undefined : "Insufficient permissions" }; | ||
| } | ||
This file contains hidden or 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,9 +1,105 @@ | ||
| // Example | ||
|
|
||
| export type User = { | ||
| id: number; | ||
| name: string; | ||
| email: string; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| }; | ||
| import mongoose, { Schema, Document, Types } from "mongoose"; | ||
|
|
||
| export type Role = "PI" | "LAB_MANAGER" | "RESEARCHER" | "VIEWER"; | ||
| export type Status = "ACTIVE" | "INACTIVE" | "SUSPENDED"; | ||
|
|
||
cadencheng888 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // describes what one lab membership looks like | ||
| export interface ILabMembership { | ||
| labId: Types.ObjectId; | ||
| role: Role; | ||
| } | ||
|
|
||
| // shape of a user document in mongodb | ||
| export interface IUser extends Document { | ||
| ucsdId: string; // ucsd pid | ||
| email: string; // ucsd email | ||
| name: { | ||
| first: string; | ||
| last: string; | ||
| }; | ||
| role: Role; | ||
| labs: ILabMembership[]; | ||
| notificationPreferences: { | ||
| email: boolean; | ||
| sms: boolean; | ||
| inApp: boolean; | ||
| }; | ||
| safety: { | ||
| trainingCompleted: string[]; | ||
| clearanceLevel: string; | ||
| lastReviewedAt: Date; | ||
| }; | ||
| profile: { | ||
| title: string; | ||
| department: string; | ||
| phone: string; | ||
| } | ||
| status: Status; | ||
| createdAt: Date; | ||
| lastLoginAt: Date; | ||
| } | ||
|
|
||
| // mongoose schema to valid the data | ||
| const userSchema = new Schema<IUser>({ | ||
|
|
||
| ucsdId: { type: String, required: true, unique: true }, // ucsd pid | ||
| email: { type: String, required: true, unique: true, trim: true, lowercase: true, | ||
| match:[/^\S+@ucsd\.edu$/, "Must be a valid UCSD email (@ucsd.edu)"], | ||
| }, // ucsd email | ||
|
|
||
| name: { | ||
| first: { type: String, required: true }, | ||
| last: { type: String, required: true }, | ||
| }, | ||
| role: { // global role - enum restricts to only these 4 valid roles | ||
| type: String, | ||
| enum: ["PI", "LAB_MANAGER", "RESEARCHER", "VIEWER"], | ||
| required: true, | ||
| }, | ||
|
|
||
| // each entry represents membership in one lab | ||
|
|
||
| labs: [ | ||
| { | ||
| labId: { type: Schema.Types.ObjectId, ref: "Lab", required: true }, | ||
| role: { | ||
| type: String, | ||
| enum: ["PI", "LAB_MANAGER", "RESEARCHER", "VIEWER"], | ||
| required: true, | ||
| }, | ||
| }, | ||
| ], | ||
| notificationPreferences: { // default notification preferences | ||
| email: { type: Boolean, default: true }, | ||
| inApp: { type: Boolean, default: true }, | ||
| sms: { type: Boolean, default: false }, | ||
| }, | ||
|
|
||
| // checks for if you completed training and if you are cleared | ||
| safety: { | ||
| trainingCompleted: [{ type: String }], | ||
| clearanceLevel: { type: String }, | ||
| lastReviewedAt: { type: Date }, | ||
| }, | ||
|
|
||
| profile: { | ||
| title: { type: String }, | ||
| department: { type: String }, | ||
| phone: { type: String }, | ||
| }, | ||
| status: { | ||
| type: String, | ||
| enum: ["ACTIVE", "INACTIVE", "SUSPENDED"], | ||
| default: "ACTIVE", | ||
| }, | ||
| lastLoginAt: { type: Date }, | ||
|
|
||
| }, | ||
| { | ||
| timestamps: true, | ||
| }); | ||
| userSchema.index({"labs.labId": 1}); | ||
|
|
||
cadencheng888 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| //creates and exports the mongoose model | ||
| export const User = mongoose.models.User || mongoose.model<IUser>("User", userSchema); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PIs and LAB_MANAGERs should also have "inventory:view"