-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
🏫 Organizations and Roles #961
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
60b3a29
🏫 Organizational Support
awtkns 52487a0
Merge branch 'main' into roles
awtkns c3acc63
🔐 Add admin role to users
awtkns 4ff4d3d
🔐 Add admin role to users
awtkns c903e9d
🔒Improve admin schema
awtkns 1e400ba
🔒Improve admin schema
awtkns 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 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 |
---|---|---|
|
@@ -44,18 +44,55 @@ model User { | |
email String? @unique | ||
emailVerified DateTime? | ||
image String? | ||
superAdmin Boolean @default(false) @map("super_admin") | ||
|
||
createDate DateTime @default(now()) | ||
|
||
accounts Account[] | ||
sessions Session[] | ||
runs Run[] | ||
Agent Agent[] | ||
UserRole UserRole[] | ||
|
||
@@index([email]) | ||
@@index([createDate]) | ||
} | ||
|
||
model Organization { | ||
id String @id @default(cuid()) | ||
name String @unique | ||
|
||
created_by String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a username? email? Thoughts on making this point to a owner user? |
||
create_date DateTime @default(now()) | ||
update_date DateTime? @updatedAt | ||
delete_date DateTime? | ||
|
||
UserRole UserRole[] | ||
|
||
@@index([name]) | ||
@@map("organization") | ||
} | ||
|
||
enum RoleName { | ||
ADMIN | ||
MEMBER | ||
} | ||
|
||
model UserRole { | ||
id String @id @default(cuid()) | ||
user_id String | ||
organization_id String | ||
role RoleName | ||
|
||
user User @relation(fields: [user_id], references: [id], onDelete: NoAction) | ||
organization Organization? @relation(fields: [organization_id], references: [id], onDelete: NoAction) | ||
|
||
@@unique([user_id, organization_id]) | ||
@@index([user_id]) | ||
@@index([organization_id]) | ||
@@map("user_role") | ||
} | ||
|
||
model VerificationToken { | ||
identifier String | ||
token String @unique | ||
|
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,6 +1,7 @@ | ||
import type { Session } from "next-auth"; | ||
import { signIn, signOut, useSession } from "next-auth/react"; | ||
import { useEffect } from "react"; | ||
import { useRouter } from "next/router"; | ||
|
||
type Provider = "google" | "github" | "discord"; | ||
|
||
|
@@ -11,14 +12,26 @@ interface Auth { | |
session: Session | null; | ||
} | ||
|
||
export function useAuth({ protectedRoute } = { protectedRoute: false }): Auth { | ||
interface UseAuthOptions { | ||
protectedRoute?: boolean; | ||
isAllowed?: (user: Session) => boolean; | ||
} | ||
|
||
export function useAuth( | ||
{ protectedRoute, isAllowed }: UseAuthOptions = { protectedRoute: false, isAllowed: () => true } | ||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We specify defaults but the fields are also nullable? |
||
): Auth { | ||
const { data: session, status } = useSession(); | ||
const { push } = useRouter(); | ||
|
||
useEffect(() => { | ||
if (protectedRoute && status === "unauthenticated") { | ||
handleSignIn().catch(console.error); | ||
} | ||
}, [protectedRoute, status]); | ||
|
||
if (protectedRoute && status === "authenticated" && isAllowed && !isAllowed(session)) { | ||
void push("/404").catch(console.error); | ||
} | ||
}, [protectedRoute, isAllowed, status, session, push]); | ||
|
||
const handleSignIn = async () => { | ||
await signIn(); | ||
|
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 |
---|---|---|
|
@@ -14,5 +14,6 @@ declare module "next-auth" { | |
|
||
interface User { | ||
image?: string; | ||
superAdmin: boolean; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import clsx from "clsx"; | ||
import type { ButtonHTMLAttributes, Dispatch, SetStateAction } from "react"; | ||
|
||
export interface Props extends ButtonHTMLAttributes<HTMLInputElement> { | ||
label?: string; | ||
description?: string; | ||
model: [boolean, Dispatch<SetStateAction<boolean>>]; | ||
className?: string; | ||
} | ||
|
||
function Checkbox(props: Props) { | ||
return ( | ||
<div className={clsx("relative flex items-start", props.className)}> | ||
<div className="flex h-6 items-center"> | ||
<input | ||
type="checkbox" | ||
className="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-600" | ||
checked={props.model[0]} | ||
onChange={(e) => props.model[1](e.target.checked)} | ||
/> | ||
</div> | ||
<div className="ml-3 text-sm leading-6 text-gray-400"> | ||
{props.label && <label className="font-medium">{props.label}</label>} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default Checkbox; |
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.
a bit in-consistent with casing, and plurals
Should we call this userRoles?