mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-10-09 12:47:13 +02:00
Added user demotion functionality. (#1444)
This commit is contained in:
@@ -41,6 +41,27 @@ async def promote_admin(
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@router.patch("/demote-admin-to-user")
|
||||||
|
async def demote_admin(
|
||||||
|
user_email: UserByEmail, user: User = Depends(current_admin_user)
|
||||||
|
) -> None:
|
||||||
|
if user.role != UserRole.ADMIN:
|
||||||
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
async with AsyncSession(get_sqlalchemy_async_engine()) as asession:
|
||||||
|
user_db = SQLAlchemyUserDatabase[User, UUID_ID](asession, User)
|
||||||
|
user_to_demote = await user_db.get_by_email(user_email.user_email)
|
||||||
|
if not user_to_demote:
|
||||||
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
if user_to_demote.id == user.id:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=400, detail="Cannot demote yourself from admin role"
|
||||||
|
)
|
||||||
|
user_to_demote.role = UserRole.BASIC
|
||||||
|
asession.add(user_to_demote)
|
||||||
|
await asession.commit()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
@router.get("/users")
|
@router.get("/users")
|
||||||
def list_all_users(
|
def list_all_users(
|
||||||
_: User | None = Depends(current_admin_user),
|
_: User | None = Depends(current_admin_user),
|
||||||
|
@@ -45,59 +45,90 @@ const UsersTable = () => {
|
|||||||
<TableHeaderCell>Role</TableHeaderCell>
|
<TableHeaderCell>Role</TableHeaderCell>
|
||||||
<TableHeaderCell>
|
<TableHeaderCell>
|
||||||
<div className="flex">
|
<div className="flex">
|
||||||
<div className="ml-auto">Promote</div>
|
<div className="ml-auto">Actions</div>
|
||||||
</div>
|
</div>
|
||||||
</TableHeaderCell>
|
</TableHeaderCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{users.map((user) => {
|
{users.map((user) => (
|
||||||
return (
|
<TableRow key={user.id}>
|
||||||
<TableRow key={user.id}>
|
<TableCell>{user.email}</TableCell>
|
||||||
<TableCell>{user.email}</TableCell>
|
<TableCell>
|
||||||
<TableCell>
|
<i>{user.role === "admin" ? "Admin" : "User"}</i>
|
||||||
<i>{user.role === "admin" ? "Admin" : "User"}</i>
|
</TableCell>
|
||||||
</TableCell>
|
<TableCell>
|
||||||
<TableCell>
|
<div className="flex justify-end space-x-2">
|
||||||
<div className="flex">
|
{user.role !== "admin" && (
|
||||||
<div className="ml-auto">
|
<Button
|
||||||
<Button
|
onClick={async () => {
|
||||||
onClick={async () => {
|
const res = await fetch(
|
||||||
const res = await fetch(
|
"/api/manage/promote-user-to-admin",
|
||||||
"/api/manage/promote-user-to-admin",
|
{
|
||||||
{
|
method: "PATCH",
|
||||||
method: "PATCH",
|
headers: {
|
||||||
headers: {
|
"Content-Type": "application/json",
|
||||||
"Content-Type": "application/json",
|
},
|
||||||
},
|
body: JSON.stringify({
|
||||||
body: JSON.stringify({
|
user_email: user.email,
|
||||||
user_email: user.email,
|
}),
|
||||||
}),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (!res.ok) {
|
|
||||||
const errorMsg = await res.text();
|
|
||||||
setPopup({
|
|
||||||
message: `Unable to promote user - ${errorMsg}`,
|
|
||||||
type: "error",
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
mutate("/api/manage/users");
|
|
||||||
setPopup({
|
|
||||||
message: "User promoted to admin!",
|
|
||||||
type: "success",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}}
|
);
|
||||||
>
|
if (!res.ok) {
|
||||||
Promote to Admin!
|
const errorMsg = await res.text();
|
||||||
</Button>
|
setPopup({
|
||||||
</div>
|
message: `Unable to promote user - ${errorMsg}`,
|
||||||
</div>
|
type: "error",
|
||||||
</TableCell>
|
});
|
||||||
</TableRow>
|
} else {
|
||||||
);
|
mutate("/api/manage/users");
|
||||||
})}
|
setPopup({
|
||||||
|
message: "User promoted to admin!",
|
||||||
|
type: "success",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Promote to Admin!
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{user.role === "admin" && (
|
||||||
|
<Button
|
||||||
|
onClick={async () => {
|
||||||
|
const res = await fetch(
|
||||||
|
"/api/manage/demote-admin-to-user",
|
||||||
|
{
|
||||||
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
user_email: user.email,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (!res.ok) {
|
||||||
|
const errorMsg = await res.text();
|
||||||
|
setPopup({
|
||||||
|
message: `Unable to demote admin - ${errorMsg}`,
|
||||||
|
type: "error",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
mutate("/api/manage/users");
|
||||||
|
setPopup({
|
||||||
|
message: "Admin demoted to user!",
|
||||||
|
type: "success",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Demote to User
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user