Skip to content

Commit

Permalink
Only show user list to logged in users with appropriate permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
floscher committed Aug 1, 2024
1 parent f7f401b commit ecc6031
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion client/src/util/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class AuthEndpoints {
static async getLoggedInUser(): Promise<LoggedInUserInfo> {
const token = loadIdToken();
if (token) {
return callServer<null, JsonMimeType, LoggedInUserInfo>("/api/auth/loggedInUser/", "POST", "application/json");
return callServer<null, JsonMimeType, LoggedInUserInfo>("/api/utility/loggedInUser", "POST", "application/json");
}
return Promise.reject();
}
Expand Down
10 changes: 8 additions & 2 deletions server/src/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { toProviderId, UserWithOAuthProviders } from "@fumix/fu-blog-common";
import { LoggedInUserInfo, toProviderId, UserWithOAuthProviders } from "@fumix/fu-blog-common";
import { authMiddleware } from "../service/middleware/auth.js";
import express, { Request, Response, Router } from "express";
import { AppDataSource } from "../data-source.js";
import { OAuthAccountEntity } from "../entity/OAuthAccount.entity.js";

const router: Router = express.Router();

router.get("/users", async (req, res, next) => {
router.get("/users", authMiddleware, async (req, res, next) => {
const loggedInUser: LoggedInUserInfo | undefined = await req.loggedInUser?.();
if (loggedInUser?.permissions?.canEditUserRoles ?? true) {
return res.status(401).json({ message: "Unauthorized" });
}

await AppDataSource.manager
.getRepository(OAuthAccountEntity)
.find({ relations: { user: true }, order: { user: { id: "ASC" } } })
Expand Down
10 changes: 0 additions & 10 deletions server/src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ async function getAuthorizationUrl(
}
}

router.post("/loggedInUser", authMiddleware, async (req, res) => {
const account = await req.loggedInUser?.();

if (account) {
res.status(200).json(account);
} else {
res.status(403).json({ error: "Unauthorized" });
}
});

/**
* Endpoint to get a {@link OAuthUserInfoDto}.
*
Expand Down
10 changes: 10 additions & 0 deletions server/src/routes/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ router.post("/dallEGenerateImage", authMiddleware, async (req, res, next) => {
.catch((e) => res.status(502).json({ error: e }));
});

router.post("/loggedInUser", authMiddleware, async (req, res) => {
const account = await req.loggedInUser?.();

if (account) {
res.status(200).json(account);
} else {
res.status(403).json({ error: "Unauthorized" });
}
});

export default router;

0 comments on commit ecc6031

Please sign in to comment.