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

fix: read disable user login feature in Login dialog #1457

Merged
merged 1 commit into from
Apr 29, 2024
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
8 changes: 6 additions & 2 deletions backend/application/commons/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,19 @@ def get(self, request):

class StatusSettingsView(APIView):
serializer_class = StatusSettingsSerializer
permission_classes = []

@action(detail=True, methods=["get"], url_name="settings")
def get(self, request):
features = []

settings = Settings.load()
if settings.feature_vex:
features.append("feature_vex")
if settings.feature_disable_user_login:
features.append("feature_disable_user_login")
if request.user.is_authenticated:
if settings.feature_vex:
features.append("feature_vex")

content = {"features": features}
return Response(content)

Expand Down
51 changes: 42 additions & 9 deletions frontend/src/access_control/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import PersonIcon from "@mui/icons-material/Person";
import { Avatar, Button, Card, CardActions, CircularProgress, Stack } from "@mui/material";
import Box from "@mui/material/Box";
import PropTypes from "prop-types";
import { Fragment } from "react";
import { useState } from "react";
import { Fragment, useState } from "react";
import { Form, TextInput, required, useLogin, useNotify, useTheme } from "react-admin";
import { useAuth } from "react-oidc-context";
import { Navigate, useLocation } from "react-router-dom";

import { feature_disable_user_login_enabled } from "../commons/functions";
import { getTheme } from "../commons/user_settings/functions";
import { OIDCSignInButton } from "./OIDCSignInButton";
import { jwt_signed_in } from "./authProvider";
Expand All @@ -19,11 +17,43 @@ const Login = () => {
const [, setTheme] = useTheme();
const auth = useAuth();

const [feature_loaded, setFeatureLoaded] = useState(false);
const [feature_disable_user_login, setFeatureDisableUserLogin] = useState(false);

const notify = useNotify();
const login = useLogin();
const location = useLocation();
const isAuthenticated = jwt_signed_in() || auth.isAuthenticated;

function get_disable_login_feature() {
const request = new Request(window.__RUNTIME_CONFIG__.API_BASE_URL + "/status/settings/", {
method: "GET",
headers: new Headers({
"Content-Type": "application/json",
}),
});
return fetch(request)
.then((response) => {
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
return response.json();
})
.then((data) => {
const features = data.features || [];
const feature_disable_user_login_position = features.indexOf("feature_disable_user_login");
return setFeatureDisableUserLogin(feature_disable_user_login_position !== -1);
})
.catch(() => {});
}

if (!feature_loaded) {
if (window.__RUNTIME_CONFIG__.OIDC_ENABLE == "true") {
get_disable_login_feature();
}
setFeatureLoaded(true);
}

const handleSubmit = (auth: FormValues) => {
setLoading(true);
login(auth, location.state ? (location.state as any).nextPathname : "/")
Expand All @@ -48,10 +78,13 @@ const Login = () => {
});
};

const show_user_login =
window.__RUNTIME_CONFIG__.OIDC_ENABLE == "false" ||
!feature_disable_user_login_enabled() ||
location.hash == "#force_user_login";
function show_user_login() {
return (
window.__RUNTIME_CONFIG__.OIDC_ENABLE == "false" ||
!feature_disable_user_login ||
location.hash == "#force_user_login"
);
}

return (
<Fragment>
Expand Down Expand Up @@ -82,7 +115,7 @@ const Login = () => {
<LockIcon />
</Avatar>
</Box>
{show_user_login && (
{show_user_login() && (
<Box sx={{ padding: "0 1em 1em 1em" }}>
<Box sx={{ marginTop: "1em" }}>
<TextInput
Expand All @@ -108,7 +141,7 @@ const Login = () => {
)}
<CardActions sx={{ padding: "0 1em 1em 1em" }}>
<Stack spacing={2} sx={{ width: "100%" }}>
{show_user_login && (
{show_user_login() && (
<Button
variant="contained"
type="submit"
Expand Down
19 changes: 8 additions & 11 deletions frontend/src/commons/functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,14 @@ export async function set_settings_in_local_storage() {
}

export const feature_vex_enabled = () => {
const settings = JSON.parse(localStorage.getItem("settings") || "{}");
const features = settings.features || [];
const feature_vex_position = features.indexOf("feature_vex");
return feature_vex_position !== -1;
};

export const feature_disable_user_login_enabled = () => {
const settings = JSON.parse(localStorage.getItem("settings") || "{}");
const features = settings.features || [];
const feature_disable_user_login_position = features.indexOf("feature_disable_user_login");
return feature_disable_user_login_position !== -1;
try {
const settings = JSON.parse(localStorage.getItem("settings") || "{}");
const features = settings.features || [];
const feature_vex_position = features.indexOf("feature_vex");
return feature_vex_position !== -1;
} catch (e) {
return false;
}
};

export const justificationIsEnabledForStatus = (status: string) => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/commons/settings/SettingsEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const SettingsEdit = () => {
<BooleanInput
source="feature_disable_user_login"
label="Disable user login"
helperText="Do not show user and password fields if OIDC login is enabled"
sx={{ marginBottom: 2 }}
/>

Expand Down
Loading