Skip to content

Commit

Permalink
added role for profile management
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed Jun 3, 2024
1 parent 996ee84 commit da6a1e2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const normalizeRoles = (
canManageNotifications: !!roles?.canManageNotifications,
canManageReturns: !!roles?.canManageReturns,
canManagePicked: !!roles?.canManagePicked,
canManageProfile: !!roles?.canManageProfile,
})

export const createLoginServiceFromEnv = (
Expand Down Expand Up @@ -86,6 +87,7 @@ export const makeRoles = (
canManageNotifications: defaultEnabled,
canManageReturns: defaultEnabled,
canManagePicked: defaultEnabled,
canManageProfile: defaultEnabled,
})

export const combineRoles = (
Expand Down Expand Up @@ -115,6 +117,7 @@ export const combineRoles = (
a.canManageNotifications || b.canManageNotifications,
canManageReturns: a.canManageReturns || b.canManageReturns,
canManagePicked: a.canManagePicked || b.canManagePicked,
canManageProfile: a.canManageProfile || b.canManageProfile,
})
)

Expand Down
1 change: 1 addition & 0 deletions src/login/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface HaffaUserRoles {
canManageNotifications?: boolean
canManageReturns?: boolean
canManagePicked?: boolean
canManageProfile?: boolean
}

export interface RequestPincodeResult {
Expand Down
8 changes: 7 additions & 1 deletion src/profile/profile-gql-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import HttpStatusCodes from 'http-status-codes'
import type { GraphQLModule } from '@helsingborg-stad/gdi-api-node'
import { profileGqlSchema } from './profile.gql.schema'
import type { Services } from '../types'
import { elevateUser } from '../login'
import { elevateUser, normalizeRoles } from '../login'
import { waitForAll } from '../lib'
import { waitRepeat } from '../lib/wait'
import type { RemoveProfileInput } from './types'
Expand All @@ -28,6 +28,9 @@ export const createProfileGqlModule = ({
if (user.guest) {
return ctx.throw(HttpStatusCodes.UNAUTHORIZED)
}
if (!normalizeRoles(user?.roles).canManageProfile) {
return ctx.throw(HttpStatusCodes.UNAUTHORIZED)
}
return profiles.updateProfile(user, input)
},

Expand All @@ -37,6 +40,9 @@ export const createProfileGqlModule = ({
if (user.guest) {
return ctx.throw(HttpStatusCodes.UNAUTHORIZED)
}
if (!normalizeRoles(user?.roles).canManageProfile) {
return ctx.throw(HttpStatusCodes.UNAUTHORIZED)
}
const effectiveUser = elevateUser(user, { canRemoveOwnAdverts: true })

if (p?.removeAdverts) {
Expand Down

0 comments on commit da6a1e2

Please sign in to comment.