Skip to content

Commit

Permalink
handle user auth group extra props (#248)
Browse files Browse the repository at this point in the history
* handle user auth group extra props

* export UserAuthGroup

---------

Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Oct 10, 2024
1 parent 960f865 commit 323048b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ export { Authorizer } from './authorizer'
export { AuthGroupName } from './authGroup'
export type { AuthGroup } from './authGroup'

export type { User, UserPrefs, UserProps, UserPrefSurveys, UserInvitation } from './user'
export type {
User,
UserAuthGroup,
UserAuthGroupProps,
UserPrefs,
UserProps,
UserPrefSurveys,
UserInvitation,
} from './user'
export type { UserService } from './userService'

export { UserStatus, UserTitle } from './user'
Expand Down
10 changes: 9 additions & 1 deletion src/auth/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ export interface UserInvitation {
expired: boolean
}

export interface UserAuthGroupProps {
extra: Dictionary<any>
}

export interface UserAuthGroup extends AuthGroup {
props: UserAuthGroupProps
}

export interface User extends ArenaObject<UserProps> {
authGroups?: AuthGroup[]
authGroups?: UserAuthGroup[]
email: string
hasProfilePicture: boolean
name: string
Expand Down
26 changes: 18 additions & 8 deletions src/auth/users.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import { User } from './user'
import { AuthGroup } from './authGroup'
import { User, UserAuthGroup } from './user'
import { AuthGroups } from './authGroups'
import { Dictionary } from '../common'

const getAuthGroups = (user: User): AuthGroup[] => user?.authGroups ?? []
const getAuthGroups = (user: User): UserAuthGroup[] => user?.authGroups ?? []

const isSystemAdmin = (user: User): boolean => (getAuthGroups(user) ?? []).some(AuthGroups.isSystemAdmin)

const getAuthGroupBySurveyUuid =
(surveyUuid?: string, includeSystemAdmin = true) =>
(user: User): AuthGroup | undefined => {
(user: User): UserAuthGroup | undefined => {
const authGroups = getAuthGroups(user)
if (includeSystemAdmin && isSystemAdmin(user) && authGroups) {
return authGroups[0]
}
if (!authGroups) return undefined
return includeSystemAdmin && isSystemAdmin(user)
? authGroups[0]
: authGroups.find((authGroup) => authGroup.surveyUuid === surveyUuid)
}

return authGroups?.find((authGroup) => authGroup?.surveyUuid === surveyUuid)
const getCombinedExtraProps =
(surveyUuid: string) =>
(user: User): Dictionary<any> => {
const userSurveyAuthGroup = user ? Users.getAuthGroupBySurveyUuid(surveyUuid)(user) : null
const userAuthGroupExtraProps = userSurveyAuthGroup?.props?.extra ?? {}
const userExtraProps = user?.props.extra ?? {}
// user auth group extra props overwrite user extra props
return { ...userExtraProps, ...userAuthGroupExtraProps }
}

export const Users = {
isSystemAdmin,
getAuthGroupBySurveyUuid,
getCombinedExtraProps,
}
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
export type { AppInfo } from './app'

export { Authorizer, AuthGroupName, UserStatus, Permission, UserTitle, UserFactory, Users } from './auth'
export type { User, UserPrefs, UserPrefSurveys, UserProps, AuthGroup, UserService } from './auth'
export type {
User,
UserAuthGroup,
UserAuthGroupProps,
UserPrefs,
UserPrefSurveys,
UserProps,
AuthGroup,
UserService,
} from './auth'

export { CategoryFactory, CategoryImportColumnType, CategoryItemFactory, CategoryLevelFactory } from './category'
export type {
Expand Down
9 changes: 8 additions & 1 deletion src/nodeDefExpressionEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Dates } from '../utils/dates'
import { NodeDefExpressionContext } from './context'
import { SystemError } from '../error'
import { ExtraProps } from '../extraProp/extraProps'
import { Users } from '../auth'

const sampleGeoJsonPolygon = {
type: 'Feature',
Expand Down Expand Up @@ -174,6 +175,12 @@ export const nodeDefExpressionFunctions: ExpressionFunctions<NodeDefExpressionCo
userProp: {
minArity: 1,
maxArity: 1,
executor: (context: NodeDefExpressionContext) => (propName: string) => context.user?.props.extra?.[propName],
executor: (context: NodeDefExpressionContext) => (propName: string) => {
const { user, survey } = context
if (!survey || !user) return undefined
const { uuid: surveyUuid } = survey
const combinedExtraProps = Users.getCombinedExtraProps(surveyUuid)(user)
return combinedExtraProps[propName]
},
},
}

0 comments on commit 323048b

Please sign in to comment.