feat: inplement auth for runs and runCases
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
isAdmin as tokenIsAdmin,
|
||||
isProjectManager as tokenIsProjectManager,
|
||||
isProjectDeveloper as tokenIsProjectDeveloper,
|
||||
isProjectReporter as tokenIsProjectReporter,
|
||||
checkSignInPage as tokenCheckSignInPage,
|
||||
fetchMyRoles,
|
||||
} from './token';
|
||||
@@ -70,6 +71,10 @@ const TokenProvider = ({ toastMessages, locale, children }: TokenProps) => {
|
||||
return tokenIsProjectDeveloper(projectRoles, projectId);
|
||||
};
|
||||
|
||||
const isProjectReporter = (projectId: number) => {
|
||||
return tokenIsProjectReporter(projectRoles, projectId);
|
||||
};
|
||||
|
||||
const tokenContext = {
|
||||
token,
|
||||
projectRoles,
|
||||
@@ -77,6 +82,7 @@ const TokenProvider = ({ toastMessages, locale, children }: TokenProps) => {
|
||||
isAdmin,
|
||||
isProjectManager,
|
||||
isProjectDeveloper,
|
||||
isProjectReporter,
|
||||
setToken,
|
||||
storeTokenToLocalStorage,
|
||||
removeTokenFromLocalStorage,
|
||||
|
||||
135
frontend/utils/caseControl.ts
Normal file
135
frontend/utils/caseControl.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import Config from '@/config/config';
|
||||
const apiServer = Config.apiServer;
|
||||
import { CaseType } from '@/types/case';
|
||||
|
||||
async function fetchCase(jwt: string, caseId: number) {
|
||||
const url = `${apiServer}/cases/${caseId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching data:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchCases(jwt: string, folderId: number) {
|
||||
const url = `${apiServer}/cases?folderId=${folderId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching data:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function createCase(jwt: string, folderId: string, title: string, description: string) {
|
||||
const newCase = {
|
||||
title: title,
|
||||
state: 0,
|
||||
priority: 2,
|
||||
type: 0,
|
||||
automationStatus: 0,
|
||||
description: description,
|
||||
template: 0,
|
||||
preConditions: '',
|
||||
expectedResults: '',
|
||||
};
|
||||
|
||||
const fetchOptions = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
body: JSON.stringify(newCase),
|
||||
};
|
||||
|
||||
const url = `${apiServer}/cases?folderId=${folderId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
console.error('Error creating case:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function updateCase(jwt: string, updateCaseData: CaseType) {
|
||||
const fetchOptions = {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
body: JSON.stringify(updateCaseData),
|
||||
};
|
||||
|
||||
const url = `${apiServer}/cases/${updateCaseData.id}`;
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
} catch (error: any) {
|
||||
console.error('Error updating project:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteCases(jwt: string, deleteCaseIds: number[], projectId: number) {
|
||||
const fetchOptions = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
},
|
||||
body: JSON.stringify({ caseIds: deleteCaseIds }),
|
||||
};
|
||||
|
||||
const url = `${apiServer}/cases/bulkdelete?projectId=${projectId}`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url, fetchOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error deleting cases:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export { fetchCase, fetchCases, updateCase, createCase, deleteCases };
|
||||
@@ -112,6 +112,33 @@ function isProjectDeveloper(projectRoles: ProjectRoleType[], projectId: number)
|
||||
return false;
|
||||
}
|
||||
|
||||
function isProjectReporter(projectRoles: ProjectRoleType[], projectId: number) {
|
||||
if (!projectRoles) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const found = projectRoles.find((role) => {
|
||||
return role.projectId === projectId;
|
||||
});
|
||||
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (found.isOwner === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const managerRoleIndex = memberRoles.findIndex((entry) => entry.uid === 'manager');
|
||||
const developerRoleIndex = memberRoles.findIndex((entry) => entry.uid === 'developer');
|
||||
const reporterRoleIndex = memberRoles.findIndex((entry) => entry.uid === 'reporter');
|
||||
if (found.role === managerRoleIndex || found.role === developerRoleIndex || found.role === reporterRoleIndex) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// private paths are '/account', '/admin', '/projects/*'
|
||||
const isPrivatePath = (pathname: string) => {
|
||||
return /^\/account(\/)?$/.test(pathname) || /^\/admin(\/.*)?$/.test(pathname) || /^\/projects(\/.*)?$/.test(pathname);
|
||||
@@ -142,4 +169,13 @@ function checkSignInPage(token: TokenType, pathname: string) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
export { isSignedIn, isAdmin, isProjectManager, isProjectDeveloper, isPrivatePath, checkSignInPage, fetchMyRoles };
|
||||
export {
|
||||
isSignedIn,
|
||||
isAdmin,
|
||||
isProjectManager,
|
||||
isProjectDeveloper,
|
||||
isProjectReporter,
|
||||
isPrivatePath,
|
||||
checkSignInPage,
|
||||
fetchMyRoles,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user