Skip to content

Commit

Permalink
Merge branch 'main' into j-s/null-service-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Jun 18, 2024
2 parents 6bd4155 + f95ab14 commit d7be182
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
PageHeader,
SharedPageLayout,
} from '@island.is/judicial-system-web/src/components'
import { CaseListEntry } from '@island.is/judicial-system-web/src/graphql/schema'
import {
CaseIndictmentRulingDecision,
CaseListEntry,
} from '@island.is/judicial-system-web/src/graphql/schema'

import { useCasesQuery } from '../../Shared/Cases/cases.generated'
import CasesForReview from '../Tables/CasesForReview'
Expand All @@ -32,7 +35,12 @@ export const PublicProsecutorCases: React.FC = () => {
if (
c.state &&
isCompletedCase(c.state) &&
!c.indictmentReviewDecision
!c.indictmentReviewDecision &&
c.indictmentRulingDecision &&
[
CaseIndictmentRulingDecision.RULING,
CaseIndictmentRulingDecision.FINE,
].includes(c.indictmentRulingDecision)
) {
acc.casesForReview.push(c)
} else if (c.indictmentReviewDecision) {
Expand Down
42 changes: 24 additions & 18 deletions apps/judicial-system/web/src/routes/Shared/Cases/Cases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,23 @@ export const Cases: React.FC = () => {
</div>
) : (
<>
{isProsecutionUser(user) && filter.value !== 'INVESTIGATION' && (
{isProsecutionUser(user) && (
<>
<CasesAwaitingConfirmationTable
loading={loading}
isFiltering={isFiltering}
cases={casesAwaitingConfirmation}
onContextMenuDeleteClick={setVisibleModal}
/>
{isPublicProsecutor(user) && (
<CasesAwaitingReview
loading={loading}
cases={casesAwaitingReview}
/>
{filter.value !== 'INVESTIGATION' && (
<>
<CasesAwaitingConfirmationTable
loading={loading}
isFiltering={isFiltering}
cases={casesAwaitingConfirmation}
onContextMenuDeleteClick={setVisibleModal}
/>
{isPublicProsecutor(user) && (
<CasesAwaitingReview
loading={loading}
cases={casesAwaitingReview}
/>
)}
</>
)}
<SectionHeading title={formatMessage(m.activeRequests.title)} />
<TableWrapper loading={loading || isFiltering}>
Expand Down Expand Up @@ -317,13 +321,15 @@ export const Cases: React.FC = () => {
</TableWrapper>
</>
)}
{isDistrictCourtUser(user) && filter.value !== 'INVESTIGATION' && (
{isDistrictCourtUser(user) && (
<>
<CasesAwaitingAssignmentTable
cases={casesAwaitingAssignment}
loading={loading || isFiltering}
isFiltering={isFiltering}
/>
{filter.value !== 'INVESTIGATION' && (
<CasesAwaitingAssignmentTable
cases={casesAwaitingAssignment}
loading={loading || isFiltering}
isFiltering={isFiltering}
/>
)}
<CasesInProgressTable
loading={loading}
isFiltering={isFiltering}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export const useFilter = (
)

return {
filter: filter,
filter,
setFilter: setFilterAndStore,
options: optionsMemo,
activeCases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ const IndictmentOverview = () => {
<Box component="section" marginBottom={5}>
{caseIsClosed ? (
<InfoCardClosedIndictment
displayAppealExpirationInfo={user?.role === UserRole.DEFENDER}
displayAppealExpirationInfo={
user?.role === UserRole.DEFENDER ||
workingCase.indictmentReviewer?.id === user?.id
}
/>
) : (
<InfoCardActiveIndictment />
Expand Down
263 changes: 137 additions & 126 deletions apps/native/app/src/stores/auth-store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AsyncStorage from '@react-native-community/async-storage'
import { Alert } from 'react-native'
import {
authorize,
Expand All @@ -9,6 +10,7 @@ import {
import Keychain from 'react-native-keychain'
import createUse from 'zustand'
import create, { State } from 'zustand/vanilla'
import { persist } from 'zustand/middleware'
import { bundleId, getConfig } from '../config'
import { getIntl } from '../contexts/i18n-provider'
import { getApolloClientAsync } from '../graphql/client'
Expand Down Expand Up @@ -59,134 +61,143 @@ const getAppAuthConfig = () => {
}
}

export const authStore = create<AuthStore>((set, get) => ({
authorizeResult: undefined,
userInfo: undefined,
lockScreenActivatedAt: undefined,
lockScreenComponentId: undefined,
noLockScreenUntilNextAppStateActive: false,
isCogitoAuth: false,
cognitoDismissCount: 0,
cognitoAuthUrl: undefined,
cookies: '',
async fetchUserInfo(skipRefresh = false) {
const appAuthConfig = getAppAuthConfig()
// Detect expired token
const expiresAt = get().authorizeResult?.accessTokenExpirationDate ?? 0

if (!skipRefresh && new Date(expiresAt) < new Date()) {
await get().refresh()
}

const res = await fetch(
`${appAuthConfig.issuer.replace(/\/$/, '')}/connect/userinfo`,
{
headers: {
Authorization: `Bearer ${get().authorizeResult?.accessToken}`,
},
export const authStore = create<AuthStore>(
persist(
(set, get) => ({
authorizeResult: undefined,
userInfo: undefined,
lockScreenActivatedAt: undefined,
lockScreenComponentId: undefined,
noLockScreenUntilNextAppStateActive: false,
isCogitoAuth: false,
cognitoDismissCount: 0,
cognitoAuthUrl: undefined,
cookies: '',
async fetchUserInfo(skipRefresh = false) {
const appAuthConfig = getAppAuthConfig()
// Detect expired token
const expiresAt = get().authorizeResult?.accessTokenExpirationDate ?? 0

if (!skipRefresh && new Date(expiresAt) < new Date()) {
await get().refresh()
}

const res = await fetch(
`${appAuthConfig.issuer.replace(/\/$/, '')}/connect/userinfo`,
{
headers: {
Authorization: `Bearer ${get().authorizeResult?.accessToken}`,
},
},
)

if (res.status === 401) {
// Attempt to refresh the access token
if (!skipRefresh) {
await get().refresh()
// Retry the userInfo call
return get().fetchUserInfo(true)
}
throw new Error(UNAUTHORIZED_USER_INFO)
} else if (res.status === 200) {
const userInfo = await res.json()
set({ userInfo })

return userInfo
}
},
)

if (res.status === 401) {
// Attempt to refresh the access token
if (!skipRefresh) {
await get().refresh()
// Retry the userInfo call
return get().fetchUserInfo(true)
}
throw new Error(UNAUTHORIZED_USER_INFO)
} else if (res.status === 200) {
const userInfo = await res.json()
set({ userInfo })

return userInfo
}
},
async refresh() {
const appAuthConfig = getAppAuthConfig()
const refreshToken = get().authorizeResult?.refreshToken

if (!refreshToken) {
return
}

const newAuthorizeResult = await authRefresh(appAuthConfig, {
refreshToken,
})

const authorizeResult = {
...get().authorizeResult,
...newAuthorizeResult,
}

await Keychain.setGenericPassword(
KEYCHAIN_AUTH_KEY,
JSON.stringify(authorizeResult),
{ service: KEYCHAIN_AUTH_KEY },
)
set({ authorizeResult })
},
async login() {
const appAuthConfig = getAppAuthConfig()
const authorizeResult = await authorize({
...appAuthConfig,
additionalParameters: {
prompt: 'login',
prompt_delegations: 'true',
ui_locales: preferencesStore.getState().locale,
externalUserAgent: 'yes',
async refresh() {
const appAuthConfig = getAppAuthConfig()
const refreshToken = get().authorizeResult?.refreshToken

if (!refreshToken) {
return
}

const newAuthorizeResult = await authRefresh(appAuthConfig, {
refreshToken,
})

const authorizeResult = {
...get().authorizeResult,
...newAuthorizeResult,
}

await Keychain.setGenericPassword(
KEYCHAIN_AUTH_KEY,
JSON.stringify(authorizeResult),
{ service: KEYCHAIN_AUTH_KEY },
)
set({ authorizeResult })
},
})

if (authorizeResult) {
await Keychain.setGenericPassword(
KEYCHAIN_AUTH_KEY,
JSON.stringify(authorizeResult),
{ service: KEYCHAIN_AUTH_KEY },
)
set({ authorizeResult })
return true
}
return false
},
async logout() {
// Clear all MMKV storages
clearAllStorages()

// Clear push token if exists
const pushToken = notificationsStore.getState().pushToken
if (pushToken) {
notificationsStore.getState().deletePushToken(pushToken)
}
notificationsStore.getState().reset()

const appAuthConfig = getAppAuthConfig()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const tokenToRevoke = get().authorizeResult!.accessToken!
try {
await revoke(appAuthConfig, {
tokenToRevoke,
includeBasicAuth: true,
sendClientId: true,
})
} catch (e) {
// NOOP
}

const client = await getApolloClientAsync()
await client.cache.reset()
await Keychain.resetGenericPassword({ service: KEYCHAIN_AUTH_KEY })
set(
(state) => ({
...state,
authorizeResult: undefined,
userInfo: undefined,
}),
true,
)
return true
},
}))
async login() {
const appAuthConfig = getAppAuthConfig()
const authorizeResult = await authorize({
...appAuthConfig,
additionalParameters: {
prompt: 'login',
prompt_delegations: 'true',
ui_locales: preferencesStore.getState().locale,
externalUserAgent: 'yes',
},
})

if (authorizeResult) {
await Keychain.setGenericPassword(
KEYCHAIN_AUTH_KEY,
JSON.stringify(authorizeResult),
{ service: KEYCHAIN_AUTH_KEY },
)
set({ authorizeResult })
return true
}
return false
},
async logout() {
// Clear all MMKV storages
clearAllStorages()

// Clear push token if exists
const pushToken = notificationsStore.getState().pushToken
if (pushToken) {
notificationsStore.getState().deletePushToken(pushToken)
}
notificationsStore.getState().reset()

const appAuthConfig = getAppAuthConfig()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const tokenToRevoke = get().authorizeResult!.accessToken!
try {
await revoke(appAuthConfig, {
tokenToRevoke,
includeBasicAuth: true,
sendClientId: true,
})
} catch (e) {
// NOOP
}

const client = await getApolloClientAsync()
await client.cache.reset()
await Keychain.resetGenericPassword({ service: KEYCHAIN_AUTH_KEY })
set(
(state) => ({
...state,
authorizeResult: undefined,
userInfo: undefined,
}),
true,
)
return true
},
}),
{
name: 'auth_01',
getStorage: () => AsyncStorage,
whitelist: ['userInfo'],
},
),
)

export const useAuthStore = createUse(authStore)

Expand Down
Loading

0 comments on commit d7be182

Please sign in to comment.