- Added permission checks to conditionally display the finance tab in the project view based on user roles. - Introduced `hasFinanceViewPermission` utility to determine access rights for the finance tab. - Updated tab management logic to handle redirection and default tab selection when permissions change.
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { ILocalSession } from '@/types/auth/local-session.types';
|
|
import { IProjectViewModel } from '@/types/project/projectViewModel.types';
|
|
|
|
/**
|
|
* Checks if the current user has permission to edit finance data
|
|
* Only users with project admin, admin or owner roles should be able to:
|
|
* - Change fixed cost values
|
|
* - Add members to rate cards
|
|
* - Change rate per hour values
|
|
*/
|
|
export const hasFinanceEditPermission = (
|
|
currentSession: ILocalSession | null,
|
|
currentProject?: IProjectViewModel | null
|
|
): boolean => {
|
|
if (!currentSession) return false;
|
|
|
|
// Team owner or admin always have permission
|
|
if (currentSession.owner || currentSession.is_admin) {
|
|
return true;
|
|
}
|
|
|
|
// Project manager has permission
|
|
if (currentProject?.project_manager?.id === currentSession.team_member_id) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Checks if the current user has permission to view finance data
|
|
* Only project managers, admins, and owners should be able to view the finance tab
|
|
*/
|
|
export const hasFinanceViewPermission = (
|
|
currentSession: ILocalSession | null,
|
|
currentProject?: IProjectViewModel | null
|
|
): boolean => {
|
|
if (!currentSession) return false;
|
|
|
|
// Team owner or admin always have permission
|
|
if (currentSession.owner || currentSession.is_admin) {
|
|
return true;
|
|
}
|
|
|
|
// Project manager has permission
|
|
if (currentProject?.project_manager?.id === currentSession.team_member_id) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Checks if the current user can edit fixed costs
|
|
*/
|
|
export const canEditFixedCost = (
|
|
currentSession: ILocalSession | null,
|
|
currentProject?: IProjectViewModel | null
|
|
): boolean => {
|
|
return hasFinanceEditPermission(currentSession, currentProject);
|
|
};
|
|
|
|
/**
|
|
* Checks if the current user can edit rate card data
|
|
*/
|
|
export const canEditRateCard = (
|
|
currentSession: ILocalSession | null,
|
|
currentProject?: IProjectViewModel | null
|
|
): boolean => {
|
|
return hasFinanceEditPermission(currentSession, currentProject);
|
|
};
|
|
|
|
/**
|
|
* Checks if the current user can add members to rate cards
|
|
*/
|
|
export const canAddMembersToRateCard = (
|
|
currentSession: ILocalSession | null,
|
|
currentProject?: IProjectViewModel | null
|
|
): boolean => {
|
|
return hasFinanceEditPermission(currentSession, currentProject);
|
|
};
|