Skip to content

Commit

Permalink
feat: show permissions in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 23, 2022
1 parent 27b9920 commit 7b9498c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/lang/en/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"permission": {
"see_hides": "Can see hides",
"access_without_password": "Access without password",
"add_aria2": "Add aria2 tasks",
"write": "Makedir or upload",
"rename": "Rename",
"move": "Move",
"copy": "Copy",
"remove": "Delete",
"webdav_read": "Webdav read",
"webdav_manage": "Webdav manage"
}
}
44 changes: 43 additions & 1 deletion src/pages/manage/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Badge,
Button,
FormControl,
FormHelperText,
Expand All @@ -9,11 +10,20 @@ import {
SimpleGrid,
VStack,
} from "@hope-ui/solid";
import { createSignal } from "solid-js";
import { createSignal, JSXElement } from "solid-js";
import { useFetch, useRouter, useT } from "~/hooks";
import { setUser, user } from "~/store";
import { UserMethods } from "~/types";
import { handleRresp, notify, r } from "~/utils";

const PermissionBadge = (props: { can: boolean; children: JSXElement }) => {
return (
<Badge colorScheme={props.can ? "success" : "danger"}>
{props.children}
</Badge>
);
};

const Profile = () => {
const t = useT();
const { to } = useRouter();
Expand Down Expand Up @@ -65,6 +75,38 @@ const Profile = () => {
>
{t("global.save")}
</Button>
<HStack wrap="wrap" gap="$2" mt="$2">
<PermissionBadge can={UserMethods.can_see_hides(user())}>
{t("user.permission.see_hides")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_access_without_password(user())}>
{t("user.permission.access_without_password")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_add_aria2_tasks(user())}>
{t("user.permission.add_aria2")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_write(user())}>
{t("user.permission.write")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_rename(user())}>
{t("user.permission.rename")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_move(user())}>
{t("user.permission.move")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_copy(user())}>
{t("user.permission.copy")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_remove(user())}>
{t("user.permission.remove")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_webdav_read(user())}>
{t("user.permission.webdav_read")}
</PermissionBadge>
<PermissionBadge can={UserMethods.can_webdav_manage(user())}>
{t("user.permission.webdav_manage")}
</PermissionBadge>
</HStack>
</VStack>
);
};
Expand Down
29 changes: 19 additions & 10 deletions src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ export const UserMethods = {
is_guest: (user: User) => user.role === UserRole.GUEST,
is_admin: (user: User) => user.role === UserRole.ADMIN,
is_general: (user: User) => user.role === UserRole.GENERAL,
can_see_hides: (user: User) => (user.permissions & 1) == 1,
can_see_hides: (user: User) =>
UserMethods.is_admin(user) || (user.permissions & 1) == 1,
can_access_without_password: (user: User) =>
((user.permissions >> 1) & 1) == 1,
can_add_aria2_tasks: (user: User) => ((user.permissions >> 2) & 1) == 1,
can_write: (user: User) => ((user.permissions >> 3) & 1) == 1,
can_rename: (user: User) => ((user.permissions >> 4) & 1) == 1,
can_move: (user: User) => ((user.permissions >> 5) & 1) == 1,
can_copy: (user: User) => ((user.permissions >> 6) & 1) == 1,
can_remove: (user: User) => ((user.permissions >> 7) & 1) == 1,
can_webdav_read: (user: User) => ((user.permissions >> 8) & 1) == 1,
can_webdav_write: (user: User) => ((user.permissions >> 9) & 1) == 1,
UserMethods.is_admin(user) || ((user.permissions >> 1) & 1) == 1,
can_add_aria2_tasks: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 2) & 1) == 1,
can_write: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 3) & 1) == 1,
can_rename: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 4) & 1) == 1,
can_move: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 5) & 1) == 1,
can_copy: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 6) & 1) == 1,
can_remove: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 7) & 1) == 1,
can_webdav_read: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 8) & 1) == 1,
can_webdav_manage: (user: User) =>
UserMethods.is_admin(user) || ((user.permissions >> 9) & 1) == 1,
};

0 comments on commit 7b9498c

Please sign in to comment.