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

Feat(root): Add link to root admin + remove _ from naming convention #1748

Merged
merged 2 commits into from
Oct 26, 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
1 change: 1 addition & 0 deletions src/api/controller/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ if (config.userAuth) {
if (
!ctx.state.cookie &&
!ctx.request.url.match(/instance\/([^\/]*)\/login/) &&
!ctx.request.url.match(/instance\/([^\/]*)\/admin/) &&
!ctx.request.url.startsWith('/instances') &&
!ctx.request.url.match(/[^\\]*\.(\w+)$/) &&
!ctx.request.url.match('__webpack_hmr')
Expand Down
13 changes: 6 additions & 7 deletions src/api/controller/rootAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ app.use(
app.use(jwt({ secret: auth.headerSecret, key: 'header', passthrough: true }));

app.use(async (ctx, next) => {
// if cookie is not root redirect
if (ctx.state.cookie.role !== ROOT_ROLE) {
if (!ctx.state.cookie) {
ctx.status = 401;
ctx.cookies.set('lodex_token_root', '', { expires: new Date() });
ctx.body = 'No root token found';
ctx.body = 'No authentication token found';
return;
}

if (!ctx.state.cookie) {
if (ctx.state.cookie.role !== ROOT_ROLE) {
ctx.status = 401;
ctx.cookies.set('lodex_token_root', '', { expires: new Date() });
ctx.body = 'No authentication token found';
ctx.body = 'No root token found';
return;
}

Expand All @@ -49,7 +48,7 @@ const postTenant = async ctx => {
const { name, description, author } = ctx.request.body;
const tenantExists = await ctx.tenantCollection.count({ name });
if (tenantExists || checkForbiddenNames(name)) {
ctx.status = 401;
ctx.status = 403;
ctx.body = { error: `Invalid name: "${name}"` };
} else {
await ctx.tenantCollection.create({
Expand All @@ -71,7 +70,7 @@ const deleteTenant = async ctx => {
name,
});
if (!tenantExists || name !== tenantExists.name) {
ctx.status = 401;
ctx.status = 403;
ctx.body = { error: `Invalid name: "${name}"` };
} else {
deleteWorkerQueue(tenantExists.name);
Expand Down
21 changes: 21 additions & 0 deletions src/app/js/admin/LoginAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import Link from '@mui/material/Link';

import Login from '../user/Login';

const LoginAdmin = () => {
return (
<>
<Login />
<Link
color="primary"
sx={{ display: 'flex', justifyContent: 'flex-end' }}
href="/instances"
>
Root adminstration
</Link>
</>
);
};

export default LoginAdmin;
4 changes: 2 additions & 2 deletions src/app/js/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import scrollToTop from '../lib/scrollToTop';
import phrasesFor from '../i18n/translations';
import getLocale from '../../../common/getLocale';
import App from './App';
import Login from '../user/Login';
import PrivateRoute from './PrivateRoute';
import { Display } from './Display';
import { Data } from './Data';
import { frFR as frFRDatagrid, enUS as enUSDatagrid } from '@mui/x-data-grid';
import { frFR, enUS } from '@mui/material/locale';
import customTheme from '../../custom/customTheme';
import LoginAdmin from './LoginAdmin';

const localesMUI = new Map([
['fr', { ...frFR, ...frFRDatagrid }],
Expand Down Expand Up @@ -72,7 +72,7 @@ render(
/>
<PrivateRoute path="/data" component={Data} />
<PrivateRoute path="/display" component={Display} />
<Route path="/login" exact component={Login} />
<Route path="/login" exact component={LoginAdmin} />
</App>
</ConnectedRouter>
</ThemeProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/app/js/root-admin/CreateTenantDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const CreateTenantDialog = ({ isOpen, handleClose, createAction }) => {
>
Le nom ne peut pas être {forbiddenNamesMessage}. Les
majuscules ne sont pas autorisées. Seules les lettres,
les chiffres, - et _ sont autorisés.
les chiffres et - sont autorisés.
</FormHelperText>
</FormControl>

Expand Down
2 changes: 1 addition & 1 deletion src/app/js/root-admin/NameField.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TextMaskCustom = forwardRef(function TextMaskCustom(props, ref) {
{...other}
mask={[
{
mask: /^[a-z0-9-_]+$/,
mask: /^[a-z0-9-]+$/,
definitions,
},
]}
Expand Down
84 changes: 80 additions & 4 deletions src/app/js/root-admin/Tenants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useEffect, useState } from 'react';

import { toast, ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import PropTypes from 'prop-types';
import AddBoxIcon from '@mui/icons-material/AddBox';
import DeleteIcon from '@mui/icons-material/Delete';

Expand All @@ -17,7 +19,7 @@ import { Button, Tooltip } from '@mui/material';

const baseUrl = getHost();

const Tenants = () => {
const Tenants = ({ handleLogout }) => {
const [tenants, setTenants] = useState([]);
const [openCreateTenantDialog, setOpenCreateTenantDialog] = useState(false);
const [openDeleteTenantDialog, setOpenDeleteTenantDialog] = useState(false);
Expand All @@ -35,6 +37,13 @@ const Tenants = () => {
'X-Lodex-Tenant': 'admin',
},
})
.then(response => {
if (response.status === 401) {
handleLogout();
return;
}
return response;
})
.then(response => response.json())
.then(onChangeTenants);
}, []);
Expand All @@ -49,7 +58,37 @@ const Tenants = () => {
method: 'POST',
body: JSON.stringify({ name, description, author }),
})
.then(response => response.json())
.then(response => {
if (response.status === 401) {
handleLogout();
return;
}

if (response.status === 403) {
toast.error('Action non autorisée', {
position: 'top-right',
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
theme: 'light',
});
return;
}

if (response.status === 200) {
toast.success('Instance créée', {
position: 'top-right',
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
theme: 'light',
});
}

return response.json();
})
.then(data => {
onChangeTenants(data);
setOpenCreateTenantDialog(false);
Expand All @@ -66,7 +105,39 @@ const Tenants = () => {
method: 'DELETE',
body: JSON.stringify({ _id, name }),
})
.then(response => response.json())
.then(response => {
if (response.status === 401) {
handleLogout();
return;
}

if (response.status === 403) {
toast.error('Action non autorisée', {
position: 'top-right',
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
theme: 'light',
});
return;
}

if (response.status === 200) {
if (response.status === 200) {
toast.success('Instance supprimée', {
position: 'top-right',
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
theme: 'light',
});
}
}

return response.json();
})
.then(data => {
onChangeTenants(data);
setOpenDeleteTenantDialog(false);
Expand Down Expand Up @@ -198,8 +269,13 @@ const Tenants = () => {
handleClose={() => setOpenDeleteTenantDialog(false)}
deleteAction={deleteTenant}
/>
<ToastContainer />
</>
);
};

Tenants.propTypes = {
handleLogout: PropTypes.func.isRequired,
};

export default Tenants;
3 changes: 2 additions & 1 deletion src/app/js/root-admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function RootAdmin() {
const [role, setRole] = useState('');

useEffect(() => {
// if no cookie found, remove the user from localStorage
const user = JSON.parse(localStorage.getItem('root-admin-user'));
if (user && user.role === ROOT_ROLE) {
setIsLoggedIn(true);
Expand Down Expand Up @@ -107,7 +108,7 @@ export default function RootAdmin() {
</Route>
<Route path="/admin">
{isLoggedIn && role === ROOT_ROLE ? (
<Tenants />
<Tenants handleLogout={handleLogout} />
) : (
<Redirect to="/login" />
)}
Expand Down