Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depriving Regular Users of Acess The Platform When It is Private #997

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion public/locales/en/unauthorized.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"firstPartMessage": "You do not have permission to access the page ",
"firstPartMessageURL": "You do not have permission to access the page ",
"firstPartMessage": "You do not have permission to access the page",
"secondPartMessage": ", because you're a user with the role ",
"inactiveAccountTitle": "Your account has been disabled.",
"inactiveAccountBody": "Contact us by e-mail - contato@aletheiafact.org"
Expand Down
3 changes: 2 additions & 1 deletion public/locales/pt/unauthorized.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"firstPartMessage": "Você não tem permissão de acesso a página ",
"firstPartMessageURL": "Você não tem permissão de acesso a página ",
"firstPartMessage": "Você não tem permissão de acesso a página",
"secondPartMessage": ", pois você é um usuário com o papel ",
"inactiveAccountTitle": "Sua conta foi desativada.",
"inactiveAccountBody": "Entre em contato pelo nosso e-mail - contato@aletheiafact.org"
Expand Down
37 changes: 27 additions & 10 deletions server/auth/session.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
import { Configuration, V0alpha2Api } from "@ory/client";
import { Reflector } from "@nestjs/core";
import { ConfigService } from "@nestjs/config";
import { Roles } from "./ability/ability.factory";

@Injectable()
export class SessionGuard implements CanActivate {
Expand Down Expand Up @@ -42,33 +43,49 @@ export class SessionGuard implements CanActivate {
role: session?.identity?.traits?.role,
status: session?.identity.state,
};
return true;
const overridePublicRoutes =
session?.identity?.traits?.role === Roles.Regular &&
this.configService.get<string>("override_public_routes");

if (overridePublicRoutes) {
return this.checkAndRedirect(
request,
response,
isPublic,
"/unauthorized"
);
} else {
return true;
}
}

return this.next(request, response, isPublic);
return this.checkAndRedirect(request, response, isPublic, "/login");
} catch (e) {
// TODO: logging
return this.next(request, response, isPublic);
return this.checkAndRedirect(request, response, isPublic, "/login");
}
}

next(request, response, isPublic) {
private checkAndRedirect(request, response, isPublic, redirectPath) {
const isAllowedPublicUrl = [
"/login",
"/unauthorized",
"/_next",
"/api/.ory",
"/api/health",
].some((route) => request.url.startsWith(route));

const overridePublicRoutes =
!isAllowedPublicUrl &&
this.configService.get<string>("override_public_routes");
if (isPublic && !overridePublicRoutes) {

if (
(isPublic && !overridePublicRoutes) ||
request.url.startsWith("/api")
) {
return true;
}
if (request.url.startsWith("/api")) {
return false;
} else {
response.redirect("/login");
response.redirect(redirectPath);
return false;
}
}
}
4 changes: 3 additions & 1 deletion src/components/AccessDeniedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const AcessDeniedPage = ({ originalUrl, status }) => {
fontWeight: 600,
}}
>
{t("unauthorized:firstPartMessage")}
{originalUrl
? t("unauthorized:firstPartMessageURL")
: t("unauthorized:firstPartMessage")}
{originalUrl === "/" ? "Home" : originalUrl}
{t("unauthorized:secondPartMessage")}
{role}
Expand Down
8 changes: 6 additions & 2 deletions src/pages/access-denied-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export async function getServerSideProps({ locale, locales, req, query }) {
return {
props: {
...(await serverSideTranslations(locale)),
originalUrl: JSON.parse(JSON.stringify(query?.originalUrl)),
status: JSON.parse(JSON.stringify(query?.status)),
originalUrl: query?.originalUrl
? JSON.parse(JSON.stringify(query?.originalUrl))
: null,
status: query?.status
? JSON.parse(JSON.stringify(query?.status))
: null,
},
};
}
Expand Down
Loading