Skip to content

Commit

Permalink
Feat(tenant): Add instance root also for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
arimet committed Sep 21, 2023
1 parent 9fe6da0 commit c01a67b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 21 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"username": "user",
"password": "secret"
},
"rootAuth": {
"username": "root",
"password": "secret"
},
"enrichmentBatchSize": 10,
"routinesCache": false,
"routines": [
Expand Down Expand Up @@ -145,6 +149,15 @@
"position": "advanced",
"role": "admin"
},
{
"label": {
"en": "Instances settings",
"fr": "Gestion instances"
},
"icon": "faCogs",
"position": "advanced",
"role": "root"
},
{
"label": {
"en": "Sign in",
Expand Down
9 changes: 9 additions & 0 deletions src/api/controller/api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const postLogin = date => ctx => {

const { username, password } = ctx.request.body;
const userAuth = get(ctx, 'ezMasterConfig.userAuth', {});
const rootAuth = get(ctx, 'ezMasterConfig.rootAuth', {});

let role;
if (
Expand All @@ -38,6 +39,14 @@ export const postLogin = date => ctx => {
role = 'user';
}

if (
rootAuth &&
username === rootAuth.username &&
password === rootAuth.password
) {
role = 'root';
}

if (!role) {
ctx.status = 401;
return;
Expand Down
36 changes: 23 additions & 13 deletions src/api/controller/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ const indexHtml = fs
.toString()
.replace(REGEX_JS_HOST, jsHost);

const adminIndexHtml = fs
.readFileSync(path.resolve(__dirname, '../../app/admin.html'))
.toString()
.replace(
'</body>',
` <script>window.__DBNAME__ = ${JSON.stringify(
mongo.dbName,
)}</script><script src="{|__JS_HOST__|}/admin/index.js"></script>
</body>`,
)
.replace(REGEX_JS_HOST, jsHost);

const getDefaultInitialState = (token, cookie, locale) => ({
fields: {
loading: false,
Expand Down Expand Up @@ -265,6 +253,22 @@ const handleRender = async (ctx, next) => {
ctx.body = renderFullPage(html, css, preloadedState, helmet, ctx.tenant);
};

const renderAdminIndexHtml = ctx => {
ctx.body = fs
.readFileSync(path.resolve(__dirname, '../../app/admin.html'))
.toString()
.replace(
'</body>',
` <script>window.__DBNAME__ = ${JSON.stringify(
mongo.dbName,
)}</script><script>window.__TENANT__ = ${JSON.stringify(
ctx.tenant,
)}</script><script src="{|__JS_HOST__|}/admin/index.js"></script>
</body>`,
)
.replace(REGEX_JS_HOST, jsHost);
};

const app = new Koa();

if (config.userAuth) {
Expand Down Expand Up @@ -298,9 +302,15 @@ if (config.userAuth) {

app.use(handleRender);

app.use(
route.get('/instance/:slug/admin', async ctx => {
renderAdminIndexHtml(ctx);
}),
);

app.use(
route.get('/admin', async ctx => {
ctx.body = adminIndexHtml;
ctx.body = `<div>Super Root Admin</div>`;
}),
);

Expand Down
1 change: 1 addition & 0 deletions src/app/custom/robots.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
User-agent: *
Disallow: /admin
Disallow: /instance/*/admin
Disallow: /login
Sitemap: /api/export/sitemap

7 changes: 1 addition & 6 deletions src/app/js/admin/Appbar/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,7 @@ const MenuComponent = ({
};

function setTenant(tenant) {
if (tenant === 'default') {
sessionStorage.removeItem('lodex-tenant');
} else {
sessionStorage.setItem('lodex-tenant', tenant);
}
window.location.reload();
window.location.href = `/instance/${tenant}/admin`;
}

const modelMenuItems = [
Expand Down
2 changes: 2 additions & 0 deletions src/app/js/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ if (process.env.NODE_ENV === 'e2e') {
sessionStorage.setItem('lodex-dbName', 'lodex_test');
} else {
const dbName = window.__DBNAME__;
const tenant = window.__TENANT__;
sessionStorage.setItem('lodex-dbName', dbName);
sessionStorage.setItem('lodex-tenant', tenant);
}

render(
Expand Down
26 changes: 25 additions & 1 deletion src/app/js/public/menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ const getIcon = icon => {
);
};

function extractTenantFromUrl(url) {
const match = url.match(/\/instance\/([^/]+)/);
return match ? match[1] : null;
}

const MenuItem = ({
config,
polyglot,
Expand Down Expand Up @@ -193,12 +198,31 @@ const MenuItem = ({
return (
role === 'admin' && (
<a
href="/admin"
href={`/instance/${extractTenantFromUrl(
window.location.href,
) || 'default'}/admin`}
className={classnames(
'nav-item',
styles.menuItem,
styles.link,
)}
>
{icon}
{label}
</a>
)
);
case 'root':
return (
role === 'root' && (
<a
href={`/admin`}
className={classnames(
'nav-item',
styles.menuItem,
styles.link,
)}
style={{ maxWidth: 'none' }} // TODO: Quick Fix, check why maxWidth is set to 90px
>
{icon}
{label}
Expand Down
2 changes: 1 addition & 1 deletion src/app/js/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const loginSuccess = createAction(LOGIN_SUCCESS);
export const logout = createAction(LOGOUT);
export const signOut = createAction(SIGNOUT);

export const isAdmin = state => state.role === 'admin';
export const isAdmin = state => state.role === 'admin' || state.role === 'root';
export const getRole = state => state.role || 'not logged';
export const getToken = state => state.token;
export const getCookie = state => state.cookie;
Expand Down

0 comments on commit c01a67b

Please sign in to comment.