Skip to content

Commit

Permalink
fix: hasAuthRoleHook fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed May 30, 2022
1 parent 8ea59e3 commit e4749ba
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { map, Observable } from 'rxjs';
import { AuthRole, SetIncludesMode, ArrayOrValue, Maybe, setIncludesFunction } from '@dereekb/util';
import { AuthRole, SetIncludesMode, ArrayOrValue, Maybe, setIncludesFunction, containsAllValues, setIncludes } from '@dereekb/util';
import { TransitionService, TransitionHookFn, Transition, HookMatchCriteria } from '@uirouter/core';
import { DbxAuthService } from '../../../service/auth.service';
import { AuthTransitionDecision, AuthTransitionHookOptions, AuthTransitionStateData, makeAuthTransitionHook } from './hook';
Expand Down Expand Up @@ -45,9 +45,7 @@ export function enableHasAuthRoleHook(transitionService: TransitionService, conf
const targetState = transition.targetState();
const data: HasAuthRoleStateData = targetState.state().data;
const requiredRoles = new Set<AuthRole>(data.authRoles);
const setIncludes = setIncludesFunction(requiredRoles, data.authRolesMode);

return authService.authUserState$.pipe(map((x) => setIncludes(x)));
return authService.authRoles$.pipe(map((x) => setIncludes(x, requiredRoles, data.authRolesMode)));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export function enableHasAuthStateHook(transitionService: TransitionService, con
const targetState = transition.targetState();
const data: HasAuthStateData = targetState.state().data;
const config = toHasAuthStateObjectConfig(data.authStates);
const parsed: ParsedHasAuthStateConfig = toParsedHasAuthStateConfig(config);
const allowedStates: ParsedHasAuthStateConfig = toParsedHasAuthStateConfig(config);

return authService.authUserState$.pipe(map((x) => isAllowed(x, parsed)));
return authService.authUserState$.pipe(map((x) => isAllowed(x, allowedStates)));
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { tapLog } from '@dereekb/rxjs';
import { addToSetCopy, AuthClaims, AuthClaimsObject, AuthRoleClaimsService, AuthRoleSet } from '@dereekb/util';
import { map, Observable, switchMap } from 'rxjs';
import { DbxFirebaseAuthService, DbxFirebaseAuthServiceDelegate, DEFAULT_DBX_FIREBASE_AUTH_SERVICE_DELEGATE } from './firebase.auth.service';
Expand All @@ -15,17 +14,10 @@ export function authRolesObsWithClaimsService<T extends AuthClaimsObject>(config
const { addAuthUserStateToRoles: addAuthUserState, claimsService } = config;

return (dbxFirebaseAuthService: DbxFirebaseAuthService): Observable<AuthRoleSet> => {
let obs = dbxFirebaseAuthService.idTokenResult$.pipe(
tapLog('a'),
map((x) => claimsService.toRoles(x.claims as AuthClaims<T>))
);
let obs = dbxFirebaseAuthService.idTokenResult$.pipe(map((x) => claimsService.toRoles(x.claims as AuthClaims<T>)));

if (addAuthUserState) {
obs = obs.pipe(
tapLog('b'),
switchMap((authRoleSet: AuthRoleSet) => dbxFirebaseAuthService.authUserState$.pipe(map((userState) => addToSetCopy(authRoleSet, [userState])))),
tapLog('c')
);
obs = obs.pipe(switchMap((authRoleSet: AuthRoleSet) => dbxFirebaseAuthService.authUserState$.pipe(map((userState) => addToSetCopy(authRoleSet, [userState])))));
}

return obs;
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/lib/set/set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('setContainsAllValues', () => {
const array = ['a', 'b'];
const set = new Set([...array]);

array.push('c');
array.push('c'); // add c

const result = setContainsAllValues(set, array);
expect(result).toBe(false);
Expand Down
19 changes: 16 additions & 3 deletions packages/util/src/lib/set/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ export function filterValuesFromSet<T>(values: T[], set: Set<T>, exclude = false

/**
* Set inclusion comparison type.
* - all: All values must be included
* - any: Any value is included
* - all: The set must include all values from values (set is a subset of values)
* - all_reverse: All values must be included in the set (values is a subset of set)
* - any: Any value from values is in the set
*/
export type SetIncludesMode = 'all' | 'any';
export type SetIncludesMode = 'all' | 'all_reverse' | 'any';

/**
* Contextual function that checks whether or not the input values are included.
Expand All @@ -98,6 +99,18 @@ export function setIncludesFunction<T>(valuesSet: Set<T>, mode: SetIncludesMode
return (valuesToFind) => fn(valuesSet, valuesToFind);
}

/**
* Convenience function for calling setIncludesFunction() and passing the result a value, checking for includion.
*
* @param valuesSet
* @param valuesToFind
* @param mode
* @returns
*/
export function setIncludes<T>(valuesSet: Set<T>, valuesToFind: Iterable<T>, mode?: SetIncludesMode): boolean {
return setIncludesFunction(valuesSet, mode)(valuesToFind);
}

/**
* Returns true if the input array contains any value from the second array.
*/
Expand Down

0 comments on commit e4749ba

Please sign in to comment.