Skip to content

Commit

Permalink
Fix: Consent type flipping (#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
Onokaev authored Mar 16, 2023
1 parent ec45297 commit 8261a09
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
17 changes: 15 additions & 2 deletions src/app/services/actions/permissions-action-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ export function getPermissionsScopeType(profile: IUser | null | undefined) {
export function consentToScopes(scopes: string[]) {
return async (dispatch: Function, getState: Function) => {
try {
const { profile }: ApplicationState = getState();
const { profile, consentedScopes }: ApplicationState = getState();
const authResponse = await authenticationWrapper.consentToScopes(scopes);
if (authResponse && authResponse.accessToken) {
dispatch(getAuthTokenSuccess(true));
dispatch(getConsentedScopesSuccess(authResponse.scopes));
const validatedScopes = validateConsentedScopes(scopes, consentedScopes, authResponse.scopes);
dispatch(getConsentedScopesSuccess(validatedScopes));
if (
authResponse.account &&
authResponse.account.localAccountId !== profile?.id
Expand Down Expand Up @@ -207,6 +208,18 @@ export function consentToScopes(scopes: string[]) {
};
}

const validateConsentedScopes = (scopeToBeConsented: string[], consentedScopes: string[],
consentedResponse: string[]) => {
if(!consentedScopes || !consentedResponse || !scopeToBeConsented) {
return consentedResponse;
}
const expectedScopes = [...consentedScopes, ...scopeToBeConsented];
if (expectedScopes.length === consentedResponse.length) {
return consentedResponse;
}
return expectedScopes;
}

export function revokeScopes(permissionToRevoke: string) {
return async (dispatch: Function, getState: Function) => {
const { consentedScopes, profile } = getState();
Expand Down
4 changes: 3 additions & 1 deletion src/app/views/query-runner/request/permissions/PanelList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ const PanelList = ({ messages,
const { permissionPanelStyles } = profileStyles(theme);

useEffect(() => {
setPermissions(sortPermissions(fullPermissions));
if(!searchValue && groups && groups.length === 0){
setPermissions(sortPermissions(fullPermissions));
}
}, [permissionsPanelOpen, scopes.data]);

const shouldGenerateGroups = useRef(true)
Expand Down
55 changes: 23 additions & 32 deletions src/app/views/query-runner/request/permissions/Permission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,19 @@ export const Permission = (permissionProps?: IPermissionProps): JSX.Element => {

const getPermissions = (): void => {
dispatch(fetchScopes());
fetchPermissionGrants();
}

useEffect(() => {
dispatch(fetchAllPrincipalGrants());
}, [])

const fetchPermissionGrants = (): void => {
if (tokenPresent) {
dispatch(fetchAllPrincipalGrants());
}
}

useEffect(() => {
getPermissions();
},[sampleQuery]);
},[sampleQuery, consentedScopes]);

const handleConsent = async (permission: IPermission): Promise<void> => {
const consentScopes = [permission.value];
Expand Down Expand Up @@ -90,16 +94,16 @@ export const Permission = (permissionProps?: IPermissionProps): JSX.Element => {
switch (column.key) {

case 'isAdmin':
return adminLabel(item);
return setAdminLabel(item);

case 'consented':
return consentedButton(consented, item, hostId);
return createConsentButton(consented, item, hostId);

case 'consentDescription':
return consentDescriptionJSX(item, hostId);
return setConsentDescription(item, hostId);

case 'consentType':
return consentTypeProperty(consented, item);
return setConsentTypeProperty(consented, item);

default:
return (
Expand All @@ -118,7 +122,7 @@ export const Permission = (permissionProps?: IPermissionProps): JSX.Element => {
}
};

const consentDescriptionJSX = (item: any, hostId: string) => {
const setConsentDescription = (item: any, hostId: string) => {
return(
<>
<TooltipHost
Expand All @@ -136,7 +140,7 @@ export const Permission = (permissionProps?: IPermissionProps): JSX.Element => {
)
}

const adminLabel = (item: any): JSX.Element => {
const setAdminLabel = (item: any): JSX.Element => {
if (item.isAdmin) {
return <div style={{ textAlign: 'left', paddingLeft:'10px' }}>
<Label><FormattedMessage id='Yes' /></Label>
Expand All @@ -148,7 +152,7 @@ export const Permission = (permissionProps?: IPermissionProps): JSX.Element => {
}
}

const consentedButton = (consented: boolean, item: any, hostId: string): JSX.Element => {
const createConsentButton = (consented: boolean, item: any, hostId: string): JSX.Element => {
if (consented) {
if(userHasRequiredPermissions()){
return <PrimaryButton onClick={() => handleRevoke(item)} style={{width: '100px', textAlign:'left'}}>
Expand Down Expand Up @@ -180,34 +184,21 @@ export const Permission = (permissionProps?: IPermissionProps): JSX.Element => {
}
}

const consentTypeProperty = (consented: boolean, item: any): JSX.Element => {
if(scopes && scopes.data.tenantWidePermissionsGrant && scopes.data.tenantWidePermissionsGrant.length > 0
&& consented) {
const setConsentTypeProperty = (consented: boolean, item: any): JSX.Element => {
if (scopes && scopes.data.tenantWidePermissionsGrant && scopes.data.tenantWidePermissionsGrant.length > 0
&& consented) {

const tenantWideGrant : IPermissionGrant[] = scopes.data.tenantWidePermissionsGrant;
const tenantWideGrant: IPermissionGrant[] = scopes.data.tenantWidePermissionsGrant;
const allPrincipalPermissions = getAllPrincipalPermissions(tenantWideGrant);
const permissionInAllPrincipal = allPrincipalPermissions.some((permission: string) =>
item.value === permission);
return permissionConsentTypeLabel(permissionInAllPrincipal);
}
return <div/>
}

const permissionConsentTypeLabel = (permissionInAllPrincipal : boolean) : JSX.Element => {
if(permissionInAllPrincipal){
return (
<div style={{textAlign: 'left', paddingLeft: '10px'}}>
<Label>{translateMessage('AllPrincipal')}</Label>
</div>
)
}
else{
return (
<div style={{textAlign: 'left', paddingLeft: '10px'}}>
<Label>{translateMessage('Principal')}</Label>
</div>
<Label>
{permissionInAllPrincipal ? translateMessage('AllPrincipal') : translateMessage('Principal')}
</Label>
)
}
return <div />
}

const getAllPrincipalPermissions = (tenantWidePermissionsGrant: IPermissionGrant[]): string[] => {
Expand Down
3 changes: 2 additions & 1 deletion src/types/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IPermission {
consentDescription: string;
isAdmin: boolean;
consented: boolean;
consentType?: 'Principal' | 'AllPrinripal';
}

export interface IPermissionProps {
Expand Down Expand Up @@ -41,7 +42,7 @@ export interface IScopes {
data: {
specificPermissions: IPermission[];
fullPermissions: IPermission[];
tenantWidePermissionsGrant?: any
tenantWidePermissionsGrant?: IPermissionGrant[];
};
error: any | null;
}
Expand Down

0 comments on commit 8261a09

Please sign in to comment.