Skip to content

Commit

Permalink
[PUI] Updates for AdminButton
Browse files Browse the repository at this point in the history
- Hide if django admin interface is disabled
- Use correct admin URL
  • Loading branch information
SchrodingersGat committed Nov 5, 2024
1 parent d674513 commit fc90e1d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
21 changes: 15 additions & 6 deletions src/backend/InvenTree/InvenTree/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,23 @@ def get(self, request, *args, **kwargs):
'debug_mode': settings.DEBUG,
'docker_mode': settings.DOCKER,
'default_locale': settings.LANGUAGE_CODE,
# Following fields are only available to staff users
'system_health': check_system_health() if is_staff else None,
'database': InvenTree.version.inventreeDatabase() if is_staff else None,
'platform': InvenTree.version.inventreePlatform() if is_staff else None,
'installer': InvenTree.version.inventreeInstaller() if is_staff else None,
'target': InvenTree.version.inventreeTarget() if is_staff else None,
}

if is_staff:
# Following fields are only available to staff users
data.update({
'system_health': check_system_health() if is_staff else None,
'database': InvenTree.version.inventreeDatabase() if is_staff else None,
'platform': InvenTree.version.inventreePlatform() if is_staff else None,
'installer': InvenTree.version.inventreeInstaller()
if is_staff
else None,
'target': InvenTree.version.inventreeTarget() if is_staff else None,
'django_admin': settings.INVENTREE_ADMIN_URL
if settings.INVENTREE_ADMIN_ENABLED
else None,
})

return JsonResponse(data)

def check_auth_header(self, request):
Expand Down
13 changes: 9 additions & 4 deletions src/frontend/src/components/buttons/AdminButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IconUserStar } from '@tabler/icons-react';
import { useCallback, useMemo } from 'react';

import { ModelType } from '../../enums/ModelType';
import { useServerApiState } from '../../states/ApiState';
import { useLocalState } from '../../states/LocalState';
import { useUserState } from '../../states/UserState';
import { ModelInformationDict } from '../render/ModelType';
Expand All @@ -24,17 +25,21 @@ export type AdminButtonProps = {
*/
export default function AdminButton(props: Readonly<AdminButtonProps>) {
const user = useUserState();
const server = useServerApiState();

const enabled: boolean = useMemo(() => {
// Only users with superuser permission will see this button
if (!user || !user.isLoggedIn() || !user.isSuperuser()) {
return false;
}

// TODO: Check if the server has the admin interface enabled

const modelDef = ModelInformationDict[props.model];

// Check if the server has the admin interface enabled
if (!server.server.django_admin) {
return false;
}

// No admin URL associated with the model
if (!modelDef.admin_url) {
return false;
Expand All @@ -57,8 +62,8 @@ export default function AdminButton(props: Readonly<AdminButtonProps>) {
return;
}

// TODO: Check the actual "admin" URL (it may be custom)
const url = `${host}/admin${modelDef.admin_url}${props.pk}/`;
// Generate the URL for the admin interface
const url = `${host}/${server.server.django_admin}${modelDef.admin_url}${props.pk}/`;

if (event?.ctrlKey || event?.shiftKey) {
// Open the link in a new tab
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/defaults/defaults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const emptyServerAPI = {
platform: null,
installer: null,
target: null,
default_locale: null
default_locale: null,
django_admin: null
};

export interface SiteMarkProps {
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/states/states.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ServerAPIProps {
installer: null | string;
target: null | string;
default_locale: null | string;
django_admin: null | string;
}

export interface AuthProps {
Expand Down

0 comments on commit fc90e1d

Please sign in to comment.