Skip to content

Commit

Permalink
Merge pull request #322 from FalkorDB/fix-user-section
Browse files Browse the repository at this point in the history
Fix #317 fix user section
  • Loading branch information
AviAvni authored Aug 18, 2024
2 parents 7397d65 + e04fbd2 commit 6a2b328
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 33 deletions.
23 changes: 23 additions & 0 deletions app/api/user/[user]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NextRequest, NextResponse } from "next/server"
import { getClient } from "../../auth/[...nextauth]/options"
import { ROLE } from "../options"

// eslint-disable-next-line import/prefer-default-export
export async function PATCH(req: NextRequest, { params }: { params: { user: string } }) {

const client = await getClient()
if (client instanceof NextResponse) {
return client
}

const username = params.user
const role = ROLE.get(req.nextUrl.searchParams.get("role") || "")
try {
if (!role) throw new Error("Role is missing")

await client.connection.aclSetUser(username, role)
return NextResponse.json({ message: "User created" },{status: 200})
} catch (err: unknown) {
return NextResponse.json({ message: (err as Error).message }, { status: 400 })
}
}
13 changes: 13 additions & 0 deletions app/api/user/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface CreateUser {
username: string
password: string
role: string
}

export const ROLE = new Map<string, string[]>(
[
["Admin", ["on", "~*", "&*", "+@all"]],
["Read-Write", ["on", "~*", "resetchannels", "-@all", "+graph.query", "+graph.explain", "+graph.list", "+ping", "+graph.profile",]],
["Read-Only", ["on", "~*", "resetchannels", "-@all", "+graph.ro_query", "+graph.explain", "+graph.list", "+ping"]]
]
)
18 changes: 1 addition & 17 deletions app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import { NextRequest, NextResponse } from "next/server";
import { getClient } from "@/app/api/auth/[...nextauth]/options";
import { User } from "./model"
import { CreateUser, ROLE } from "./options";

const ROLE = new Map<string, string[]>(
[
["Admin", ["on", "~*", "&*", "+@all"]],
["Read-Write", ["on", "~*", "resetchannels", "-@all", "+graph.query", "+graph.explain", "+graph.list", "+ping", "+graph.profile",]],
["Read-Only", ["on", "~*", "resetchannels", "-@all", "+graph.ro_query", "+graph.explain", "+graph.list", "+ping"]]
]
)

interface CreateUser {
username: string
password: string
role: string
}

// eslint-disable-next-line import/prefer-default-export
export async function GET() {

const client = await getClient()
Expand Down Expand Up @@ -50,7 +36,6 @@ export async function GET() {
}
}

// eslint-disable-next-line import/prefer-default-export
export async function POST(req: NextRequest) {

const client = await getClient()
Expand Down Expand Up @@ -89,7 +74,6 @@ export async function POST(req: NextRequest) {
}
}

// eslint-disable-next-line import/prefer-default-export
export async function DELETE(req: NextRequest) {

const client = await getClient()
Expand Down
2 changes: 1 addition & 1 deletion app/settings/users/AddUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function AddUser({ setUsers }: {
})

if (response.ok) {
Toast("Success", "User added successfully")
Toast("User added successfully", "Success")
setUsers(prev => [...prev, { username, role, selected: false }])
}
setOpen(false)
Expand Down
42 changes: 27 additions & 15 deletions app/settings/users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ export default function Users() {
run()
}, [])

const handelSetRole = async (role: string, username?: string) => {
const updatedUsers = await Promise.all(users.map(async user => {
const updated = username ? user.username === username : user.selected

if (!updated) return user

const result = await securedFetch(`api/user/${username}/?role=${role}`, {
method: 'PATCH'
})

if (result.ok) {
return {
...user,
role
}
}

return user
}))

setUsers(updatedUsers)
}


return (
<div className="w-full h-full flex flex-col space-y-4">
<div className="flex flex-row-reverse gap-4">
Expand All @@ -51,13 +75,7 @@ export default function Users() {
options={ROLES}
setSelectedValue={(role) => {
setChecked(false)
setUsers(prev => prev.map(user => {
if (!user.selected) return user
const u = user
u.role = role
u.selected = false
return u
}))
handelSetRole(role)
}}
/>
</div>
Expand Down Expand Up @@ -93,7 +111,7 @@ export default function Users() {
<TableCell className="w-[5%] py-6">
<Checkbox
key="checkbox"
className={cn(!(index % 2) && "data-[state=checked]:text-[#57577B] border-[#57577B] data-[state=checked]:bg-[#272746]", "data-[state=checked]:text-[#272746] border-[#272746] data-[state=checked]:bg-[#57577B] rounded-lg w-5 h-5")}
className={cn((index % 2) && "data-[state=checked]:text-[#57577B] border-[#57577B] data-[state=checked]:bg-[#272746]", !(index % 2) && "data-[state=checked]:text-[#272746] border-[#272746] data-[state=checked]:bg-[#57577B]", "rounded-lg w-5 h-5")}
checked={selected}
onCheckedChange={(check) => {
setUsers(prev => prev.map(currentUser => {
Expand All @@ -112,13 +130,7 @@ export default function Users() {
type="Role"
options={ROLES}
selectedValue={role || ""}
setSelectedValue={(r) => setUsers(prev => prev.map((user) => {
if (username !== user.username) return user
return {
...user,
role: r
}
}))} />
setSelectedValue={(r) => handelSetRole(r, username)} />
</TableCell>
<TableCell className="w-[10%]">
{
Expand Down

0 comments on commit 6a2b328

Please sign in to comment.