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