-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fetching person's sisu roles from importer
- Loading branch information
1 parent
e17e099
commit 31020c9
Showing
7 changed files
with
205 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import getImporterClient from '../util/importerClient' | ||
|
||
const importerClient = getImporterClient() | ||
|
||
const sisuRolesGivingFullAccess = [ | ||
'hy-ac-kosu1', | ||
'hy-ac-kosu2', | ||
'hy-ac-kosu3', | ||
'hy-ac-opne1', | ||
'hy-ac-opne2', | ||
'hy-ac-opne3', | ||
'hy-ac-sview', | ||
] | ||
|
||
type UserCacheEntry = { | ||
hasAccess: boolean | ||
accessedAt: Date | ||
} | ||
|
||
const userCache = new Map<string, UserCacheEntry>() | ||
|
||
export const hasFullSisuAccess = async (personId: string) => { | ||
if (!importerClient) return false | ||
|
||
if (userCache.has(personId)) { | ||
const { hasAccess, accessedAt } = userCache.get(personId) | ||
|
||
// Cache is valid for 24 hours | ||
if (new Date().getTime() - accessedAt.getTime() < 24 * 60 * 60 * 1000) { | ||
return hasAccess | ||
} | ||
} | ||
|
||
const { data } = await importerClient.get(`/jami/sisuroles/${personId}`) | ||
|
||
if (data && Array.isArray(data)) { | ||
const hasFullAccessToSisu = data.some((role) => | ||
sisuRolesGivingFullAccess.includes(role.accessroleId), | ||
) | ||
userCache.set(personId, { | ||
hasAccess: hasFullAccessToSisu, | ||
accessedAt: new Date(), | ||
}) | ||
return hasFullAccessToSisu | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
|
||
const PORT = process.env.PORT | ||
export const PORT = process.env.PORT | ||
|
||
const inProduction = process.env.NODE_ENV === 'production' | ||
export const inProduction = process.env.NODE_ENV === 'production' | ||
|
||
const DATABASE_URL = process.env.DATABASE_URL | ||
export const DATABASE_URL = process.env.DATABASE_URL | ||
|
||
export { DATABASE_URL, PORT, inProduction } | ||
export const importerUrl = | ||
'https://api-toska.apps.ocp-prod-0.k8s.it.helsinki.fi/importer' | ||
|
||
export const importerToken = process.env.IMPORTER_DB_API_TOKEN || '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import axios from 'axios' | ||
import { importerUrl, importerToken } from './config' | ||
import logger from './logger' | ||
|
||
const importerClient = axios.create({ | ||
headers: { | ||
token: importerToken, | ||
}, | ||
baseURL: importerUrl, | ||
}) | ||
|
||
const getImporterClient = () => { | ||
if (!importerToken) { | ||
logger.error("Importer token not set, can't return client!") | ||
return null | ||
} | ||
return importerClient | ||
} | ||
|
||
export default getImporterClient |