Closes https://github.com/twentyhq/core-team-issues/issues/526 (for reminder: 1. Make defaultRoleId non-nullable for an active workspace 2. Remove permissions V1 feature flag 3. Set member role as default role for new workspaces About 1.: An active workspace's defaultRoleId should never be null. We can't rely on a simple postgres NOT NULL constraint as defaultRoleId will always be initially null when the workspace is first created since the roles do not exist at that time. Let's add a more complex rule to ensure that About 3.: In the first phase of our deploy of permissions, we chose to assign admin role to all existing users, not to break any existing behavior with the introduction of the feature (= existing users have less rights than before). As we deploy permissions to all existing and future workspaces, let's set the member role as default role for future workspaces. )
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { useHasSettingsPermission } from '@/settings/roles/hooks/useHasSettingsPermission';
|
|
import { SettingsPath } from '@/types/SettingsPath';
|
|
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
|
|
import { ReactNode } from 'react';
|
|
import { Navigate, Outlet } from 'react-router-dom';
|
|
import { FeatureFlagKey, SettingPermissionType } from '~/generated/graphql';
|
|
import { getSettingsPath } from '~/utils/navigation/getSettingsPath';
|
|
|
|
type SettingsProtectedRouteWrapperProps = {
|
|
children?: ReactNode;
|
|
settingsPermission?: SettingPermissionType;
|
|
requiredFeatureFlag?: FeatureFlagKey;
|
|
};
|
|
|
|
export const SettingsProtectedRouteWrapper = ({
|
|
children,
|
|
settingsPermission,
|
|
requiredFeatureFlag,
|
|
}: SettingsProtectedRouteWrapperProps) => {
|
|
const hasPermission = useHasSettingsPermission(settingsPermission);
|
|
const requiredFeatureFlagEnabled = useIsFeatureEnabled(
|
|
requiredFeatureFlag || null,
|
|
);
|
|
|
|
if ((requiredFeatureFlag && !requiredFeatureFlagEnabled) || !hasPermission) {
|
|
return <Navigate to={getSettingsPath(SettingsPath.ProfilePage)} replace />;
|
|
}
|
|
|
|
return children ?? <Outlet />;
|
|
};
|