-
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.
- Loading branch information
Showing
19 changed files
with
616 additions
and
389 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,31 +1,50 @@ | ||
import { getDatabase, NewRole } from '../util/db'; | ||
import {LambdaBuilder, LambdaInput} from '../util/middleware/middleware'; | ||
import { SuccessResponse } from '../util/middleware/response'; | ||
import { Authorizer } from '../util/middleware/authorizer'; | ||
import { InputValidator } from '../util/middleware/inputValidator'; | ||
import { LambdaBuilder, LambdaInput } from '../util/middleware/middleware'; | ||
import { BadRequestError, SuccessResponse } from '../util/middleware/response'; | ||
import { | ||
ACCESS_SCOPES, | ||
ScopeController, | ||
} from '../util/middleware/scopeHandler'; | ||
import { getRoles, refreshCache } from './roles'; | ||
import { Authorizer } from '../util/middleware/authorizer'; | ||
import {ScopeController} from "../util/middleware/scopeHandler"; | ||
|
||
const db = getDatabase(); | ||
|
||
// Only valid for Admin role | ||
const validScopes = [ACCESS_SCOPES.ADMIN_WRITE]; | ||
export const handler = new LambdaBuilder(createRoleRequest) | ||
.use(new InputValidator()) | ||
.use(new Authorizer()) | ||
.use(new Authorizer(db)) | ||
.use(new ScopeController(db)) | ||
.build(); | ||
|
||
async function createRoleRequest(event: LambdaInput) { | ||
ScopeController.verifyScopes(event.userScopes, ['admin:write']) | ||
if (!event.userScopes) { | ||
throw new BadRequestError('Event userScopes missing'); | ||
} | ||
if (!event.body) { | ||
throw new BadRequestError('Event body missing'); | ||
} | ||
|
||
ScopeController.verifyScopes(event.userScopes, validScopes); | ||
|
||
const { label } = JSON.parse(event.body); | ||
await createRole({ label }); | ||
await refreshCache(db); | ||
return new SuccessResponse(await getRoles(db)); | ||
} | ||
|
||
export const createRole = async (newRole: NewRole) => { | ||
const { id } = await db | ||
const result = await db | ||
.insertInto('role') | ||
.values(newRole) | ||
.returning('id') | ||
.executeTakeFirst(); | ||
return id; | ||
|
||
if (result === undefined) { | ||
throw new BadRequestError('Role not created'); | ||
} | ||
|
||
return result.id; | ||
}; |
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,26 +1,40 @@ | ||
import { getDatabase } from '../util/db'; | ||
import {LambdaBuilder, LambdaInput} from '../util/middleware/middleware'; | ||
import { SuccessResponse } from '../util/middleware/response'; | ||
import { Authorizer } from '../util/middleware/authorizer'; | ||
import { InputValidator } from '../util/middleware/inputValidator'; | ||
import { LambdaBuilder, LambdaInput } from '../util/middleware/middleware'; | ||
import { BadRequestError, SuccessResponse } from '../util/middleware/response'; | ||
import { | ||
ACCESS_SCOPES, | ||
ScopeController, | ||
} from '../util/middleware/scopeHandler'; | ||
import { getRoles, refreshCache } from './roles'; | ||
import { Authorizer } from '../util/middleware/authorizer'; | ||
import {ScopeController} from "../util/middleware/scopeHandler"; | ||
|
||
const db = getDatabase(); | ||
|
||
// Only valid for Admin role | ||
const validScopes = [ACCESS_SCOPES.ADMIN_WRITE]; | ||
export const handler = new LambdaBuilder(deleteRoleRequest) | ||
.use(new InputValidator()) | ||
.use(new Authorizer()) | ||
.use(new Authorizer(db)) | ||
.use(new ScopeController(db)) | ||
.build(); | ||
|
||
async function deleteRoleRequest(event: LambdaInput) { | ||
ScopeController.verifyScopes(event.userScopes, ['admin:write']) | ||
const { id } = event.pathParameters; | ||
if (!event.pathParameters) { | ||
throw new BadRequestError('Event path parameters missing'); | ||
} | ||
if (!event.pathParameters.id) { | ||
throw new BadRequestError('ID is undefined'); | ||
} | ||
const id = parseInt(event.pathParameters.id); | ||
|
||
ScopeController.verifyScopes(event.userScopes, validScopes); | ||
|
||
await deleteRole(id); | ||
await refreshCache(db); | ||
return new SuccessResponse(await getRoles(db)); | ||
} | ||
|
||
export const deleteRole = async (id: string) => { | ||
export const deleteRole = async (id: number) => { | ||
await db.deleteFrom('role').where('id', '=', id).execute(); | ||
}; |
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,36 +1,53 @@ | ||
import { getDatabase } from '../util/db'; | ||
import { LambdaBuilder } from '../util/middleware/middleware'; | ||
import { BadRequestError, SuccessResponse } from '../util/middleware/response'; | ||
import { InputValidator } from '../util/middleware/inputValidator'; | ||
import { APIGatewayEvent } from 'aws-lambda'; | ||
import { Authorizer } from '../util/middleware/authorizer'; | ||
import { InputValidator } from '../util/middleware/inputValidator'; | ||
import { LambdaBuilder, LambdaInput } from '../util/middleware/middleware'; | ||
import { BadRequestError, SuccessResponse } from '../util/middleware/response'; | ||
import { | ||
ACCESS_SCOPES, | ||
ScopeController, | ||
} from '../util/middleware/scopeHandler'; | ||
|
||
const db = getDatabase(); | ||
|
||
// Only valid for user with Admin role | ||
const validScopes = [ACCESS_SCOPES.ADMIN_WRITE]; | ||
export const handler = new LambdaBuilder(deleteUserRoleRequest) | ||
.use(new InputValidator()) | ||
.use(new Authorizer()) | ||
.use(new Authorizer(db)) | ||
.use(new ScopeController(db)) | ||
.build(); | ||
|
||
async function deleteUserRoleRequest(event: APIGatewayEvent) { | ||
const { id, roleId } = JSON.parse(event.body); | ||
await deleteUserRole(id, roleId); | ||
async function deleteUserRoleRequest(event: LambdaInput) { | ||
if (!event.pathParameters) { | ||
throw new BadRequestError('Event pathParameters missing'); | ||
} | ||
if (!event.pathParameters.id || !event.pathParameters.roleId) { | ||
throw new BadRequestError('User id or role id missing'); | ||
} | ||
|
||
const { id, roleId } = event.pathParameters; | ||
ScopeController.verifyScopes(event.userScopes, validScopes); | ||
await deleteUserRole(parseInt(id), parseInt(roleId)); | ||
return new SuccessResponse({ message: 'Role deleted successfully' }); | ||
} | ||
|
||
export const deleteUserRole = async (userId: string, roleId: string) => { | ||
export const deleteUserRole = async (userId: number, roleId: number) => { | ||
const verifyRole = await db | ||
.selectFrom('person_role') | ||
.select(['user_id', 'role_id']) | ||
.where('user_id', '=', userId) | ||
.select(['person_id', 'role_id']) | ||
.where('person_id', '=', userId) | ||
.where('role_id', '=', roleId) | ||
.executeTakeFirst(); | ||
if (!verifyRole) { | ||
throw new BadRequestError(`role id: ${roleId} does not exist for user id: ${userId}`); | ||
throw new BadRequestError( | ||
`role id: ${roleId} does not exist for user id: ${userId}` | ||
); | ||
} | ||
|
||
await db | ||
.deleteFrom('person_role') | ||
.where('user_id', '=', userId) | ||
.where('person_id', '=', userId) | ||
.where('role_id', '=', roleId) | ||
.execute(); | ||
}; |
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,38 +1,47 @@ | ||
import {Database, getDatabase} from '../util/db'; | ||
import { LambdaBuilder } from '../util/middleware/middleware'; | ||
import { SuccessResponse } from '../util/middleware/response'; | ||
import { InputValidator } from '../util/middleware/inputValidator'; | ||
import { APIGatewayEvent } from 'aws-lambda'; | ||
import { Kysely } from 'kysely'; | ||
import { Database, getDatabase } from '../util/db'; | ||
import { InputValidator } from '../util/middleware/inputValidator'; | ||
import { LambdaBuilder } from '../util/middleware/middleware'; | ||
import { BadRequestError, SuccessResponse } from '../util/middleware/response'; | ||
import { Authorizer } from '../util/middleware/authorizer'; | ||
import {Kysely} from "kysely"; | ||
|
||
const db = getDatabase(); | ||
export const handler = new LambdaBuilder(getUserRoleRequest) | ||
.use(new InputValidator()) | ||
.use(new Authorizer()) | ||
.use(new Authorizer(db)) | ||
.build(); | ||
|
||
async function getUserRoleRequest(event: APIGatewayEvent) { | ||
const {id} = event.pathParameters; | ||
const roles = await getUserRoles(id); | ||
const userRoles: {id: string, label: string, scopes: string[]}[] = []; | ||
roles.map(role => userRoles.push({id: role.id, label: role.label, scopes: []})); | ||
if (!event.pathParameters) { | ||
throw new BadRequestError('Event path parameters missing'); | ||
} | ||
const { id } = event.pathParameters; | ||
const roles = await getUserRoles(Number(id)); | ||
const userRoles: { id: number; label: string; scopes: string[] }[] = []; | ||
roles.map((role) => | ||
userRoles.push({ id: role.id, label: role.label, scopes: [] }) | ||
); | ||
for (const role of userRoles) { | ||
role.scopes = await attachScopes(db, role.label); | ||
} | ||
return new SuccessResponse(userRoles); | ||
} | ||
|
||
export const getUserRoles = async (userId: string) => { | ||
export const getUserRoles = async (userId: number) => { | ||
return await db | ||
.selectFrom('person_role') | ||
.innerJoin('role', 'role.id', 'person_role.role_id') | ||
.selectFrom('role') | ||
.innerJoin('person_role', 'role.id', 'person_role.role_id') | ||
.select(['role.id', 'role.label']) | ||
.where('user_id', '=', userId) | ||
.where('person_role.person_id', '=', userId) | ||
.execute(); | ||
}; | ||
|
||
export const attachScopes = async (db: Kysely<Database>, role: string) => { | ||
const scopes = await db.selectFrom('scope_role').select(['scope_label']).where('role_label', '=', role).execute(); | ||
return scopes.map(scope => scope.scope_label); | ||
} | ||
const scopes = await db | ||
.selectFrom('scope_role') | ||
.select(['scope_label']) | ||
.where('role_label', '=', role) | ||
.execute(); | ||
return scopes.map((scope) => scope.scope_label); | ||
}; |
Oops, something went wrong.