feat: display user's raw permissions
This commit is contained in:
@ -6,17 +6,13 @@ import (
|
||||
"net/http"
|
||||
|
||||
"gitea.local/admin/hspguard/internal/repository"
|
||||
"gitea.local/admin/hspguard/internal/util"
|
||||
"gitea.local/admin/hspguard/internal/web"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (h *AdminHandler) GetPermissions(w http.ResponseWriter, r *http.Request) {
|
||||
userId, ok := util.GetRequestUserId(r.Context())
|
||||
if !ok {
|
||||
web.Error(w, "failed to get user id from auth session", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
userId := chi.URLParam(r, "user_id")
|
||||
|
||||
permissions, err := h.repo.GetUserPermissions(r.Context(), uuid.MustParse(userId))
|
||||
if err != nil {
|
||||
|
@ -42,7 +42,7 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) {
|
||||
r.Get("/service-sessions", h.GetServiceSessions)
|
||||
r.Patch("/service-sessions/revoke/{id}", h.RevokeUserSession)
|
||||
|
||||
r.Get("/permissions", h.GetPermissions)
|
||||
r.Get("/permissions/{user_id}", h.GetPermissions)
|
||||
})
|
||||
|
||||
router.Get("/api-services/client/{client_id}", h.GetApiServiceCID)
|
||||
|
@ -3,14 +3,15 @@ import { axios, handleApiError } from "..";
|
||||
|
||||
export type FetchPermissionsResponse = AppPermission[];
|
||||
|
||||
export const getPermissionsApi =
|
||||
async (): Promise<FetchPermissionsResponse> => {
|
||||
const response = await axios.get<FetchPermissionsResponse>(
|
||||
"/api/v1/admin/permissions",
|
||||
);
|
||||
export const getPermissionsApi = async (
|
||||
userId: string,
|
||||
): Promise<FetchPermissionsResponse> => {
|
||||
const response = await axios.get<FetchPermissionsResponse>(
|
||||
`/api/v1/admin/permissions/${userId}`,
|
||||
);
|
||||
|
||||
if (response.status !== 200 && response.status !== 201)
|
||||
throw await handleApiError(response);
|
||||
if (response.status !== 200 && response.status !== 201)
|
||||
throw await handleApiError(response);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
return response.data;
|
||||
};
|
||||
|
@ -25,6 +25,7 @@ const InfoCard = ({
|
||||
const AdminViewUserPage: FC = () => {
|
||||
const { userId } = useParams();
|
||||
const user = useUsers((state) => state.current);
|
||||
const userPermissions = useUsers((s) => s.userPermissions);
|
||||
// const loading = useApiServices((state) => state.fetchingApiService);
|
||||
|
||||
const loadUser = useUsers((state) => state.fetchUser);
|
||||
@ -117,6 +118,10 @@ const AdminViewUserPage: FC = () => {
|
||||
</div>
|
||||
</InfoCard>
|
||||
|
||||
<InfoCard title="Roles & Permissions">
|
||||
<pre>{JSON.stringify(userPermissions, null, 2)}</pre>
|
||||
</InfoCard>
|
||||
|
||||
{/* 🚀 Actions */}
|
||||
<div className="flex flex-wrap gap-4 mt-6 justify-between items-center">
|
||||
<Link to="/admin/users">
|
||||
|
@ -1,10 +1,11 @@
|
||||
import { getPermissionsApi } from "@/api/admin/permissions";
|
||||
import {
|
||||
adminGetUserApi,
|
||||
adminGetUsersApi,
|
||||
postUser,
|
||||
type CreateUserRequest,
|
||||
} from "@/api/admin/users";
|
||||
import type { UserProfile } from "@/types";
|
||||
import type { AppPermission, UserProfile } from "@/types";
|
||||
import { create } from "zustand";
|
||||
|
||||
export interface IUsersState {
|
||||
@ -14,14 +15,18 @@ export interface IUsersState {
|
||||
current: UserProfile | null;
|
||||
fetchingCurrent: boolean;
|
||||
|
||||
userPermissions: AppPermission[];
|
||||
fetchingPermissions: boolean;
|
||||
|
||||
creating: boolean;
|
||||
createUser: (req: CreateUserRequest) => Promise<boolean>;
|
||||
|
||||
fetchUsers: () => Promise<void>;
|
||||
fetchUser: (id: string) => Promise<void>;
|
||||
fetchUserPermissions: () => Promise<void>;
|
||||
}
|
||||
|
||||
export const useUsers = create<IUsersState>((set) => ({
|
||||
export const useUsers = create<IUsersState>((set, get) => ({
|
||||
users: [],
|
||||
fetching: false,
|
||||
|
||||
@ -30,6 +35,9 @@ export const useUsers = create<IUsersState>((set) => ({
|
||||
current: null,
|
||||
fetchingCurrent: false,
|
||||
|
||||
userPermissions: [],
|
||||
fetchingPermissions: false,
|
||||
|
||||
createUser: async (req: CreateUserRequest) => {
|
||||
set({ creating: true });
|
||||
|
||||
@ -70,4 +78,23 @@ export const useUsers = create<IUsersState>((set) => ({
|
||||
set({ fetchingCurrent: false });
|
||||
}
|
||||
},
|
||||
|
||||
fetchUserPermissions: async () => {
|
||||
const user = get().current;
|
||||
if (!user) {
|
||||
console.warn("Trying to fetch user permissions without selected user");
|
||||
return;
|
||||
}
|
||||
|
||||
set({ fetchingPermissions: true });
|
||||
|
||||
try {
|
||||
const response = await getPermissionsApi(user.id);
|
||||
set({ userPermissions: response });
|
||||
} catch (err) {
|
||||
console.log("ERR: Failed to fetch single user for admin:", err);
|
||||
} finally {
|
||||
set({ fetchingPermissions: false });
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
Reference in New Issue
Block a user