Move custom rbac from controller to routes
This commit is contained in:
parent
d484dc6011
commit
d60a70af82
|
@ -23,12 +23,6 @@ const MAX_USERS_UPLOAD_LIMIT = 1000
|
||||||
|
|
||||||
export const save = async (ctx: any) => {
|
export const save = async (ctx: any) => {
|
||||||
try {
|
try {
|
||||||
const body = ctx.request.body
|
|
||||||
const isCreate = !body._id
|
|
||||||
const isAdmin = !!ctx.user.admin?.global
|
|
||||||
if (isCreate && !isAdmin) {
|
|
||||||
ctx.throw(403, "Only admin user can create new user.")
|
|
||||||
}
|
|
||||||
ctx.body = await sdk.users.save(ctx.request.body)
|
ctx.body = await sdk.users.save(ctx.request.body)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
ctx.throw(err.status || 400, err)
|
ctx.throw(err.status || 400, err)
|
||||||
|
|
|
@ -40,6 +40,14 @@ function buildInviteMultipleValidation() {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createUserAdminOnly = (ctx, next) => {
|
||||||
|
if (!ctx.request.body._id) {
|
||||||
|
return adminOnly(ctx, next)
|
||||||
|
} else {
|
||||||
|
return builderOrAdmin(ctx, next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function buildInviteAcceptValidation() {
|
function buildInviteAcceptValidation() {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return joiValidator.body(Joi.object({
|
return joiValidator.body(Joi.object({
|
||||||
|
@ -51,7 +59,7 @@ function buildInviteAcceptValidation() {
|
||||||
router
|
router
|
||||||
.post(
|
.post(
|
||||||
"/api/global/users",
|
"/api/global/users",
|
||||||
builderOrAdmin,
|
createUserAdminOnly,
|
||||||
users.buildUserSaveValidation(),
|
users.buildUserSaveValidation(),
|
||||||
controller.save
|
controller.save
|
||||||
)
|
)
|
||||||
|
|
|
@ -72,29 +72,12 @@ class TestConfiguration {
|
||||||
|
|
||||||
// UTILS
|
// UTILS
|
||||||
|
|
||||||
async _req(
|
async _req(config: any, params: any, controlFunc: any) {
|
||||||
config: any,
|
|
||||||
params: any,
|
|
||||||
controlFunc: any,
|
|
||||||
opts: { force?: boolean } = {}
|
|
||||||
) {
|
|
||||||
const request: any = {}
|
const request: any = {}
|
||||||
// fake cookies, we don't need them
|
// fake cookies, we don't need them
|
||||||
request.cookies = { set: () => {}, get: () => {} }
|
request.cookies = { set: () => {}, get: () => {} }
|
||||||
request.config = { jwtSecret: env.JWT_SECRET }
|
request.config = { jwtSecret: env.JWT_SECRET }
|
||||||
if (opts.force) {
|
request.user = { tenantId: this.getTenantId() }
|
||||||
request.user = {
|
|
||||||
tenantId: this.getTenantId(),
|
|
||||||
admin: { global: true },
|
|
||||||
builder: { global: true },
|
|
||||||
}
|
|
||||||
} else if (this.defaultUser) {
|
|
||||||
request.user = this.defaultUser
|
|
||||||
} else {
|
|
||||||
request.user = {
|
|
||||||
tenantId: this.getTenantId(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
request.query = {}
|
request.query = {}
|
||||||
request.request = {
|
request.request = {
|
||||||
body: config,
|
body: config,
|
||||||
|
@ -146,7 +129,7 @@ class TestConfiguration {
|
||||||
email: "test@test.com",
|
email: "test@test.com",
|
||||||
password: "test",
|
password: "test",
|
||||||
})
|
})
|
||||||
this.defaultUser = await this.createUser(user, { force: true })
|
this.defaultUser = await this.createUser(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
async createTenant1User() {
|
async createTenant1User() {
|
||||||
|
@ -154,16 +137,15 @@ class TestConfiguration {
|
||||||
email: "tenant1@test.com",
|
email: "tenant1@test.com",
|
||||||
password: "test",
|
password: "test",
|
||||||
})
|
})
|
||||||
this.tenant1User = await this.createUser(user, { force: true })
|
this.tenant1User = await this.createUser(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSession(user: User) {
|
async createSession(user: User) {
|
||||||
const session: any = {
|
await sessions.createASession(user._id!, {
|
||||||
sessionId: "sessionid",
|
sessionId: "sessionid",
|
||||||
tenantId: user.tenantId,
|
tenantId: user.tenantId,
|
||||||
csrfToken: CSRF_TOKEN,
|
csrfToken: CSRF_TOKEN,
|
||||||
}
|
})
|
||||||
await sessions.createASession(user._id!, session)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cookieHeader(cookies: any) {
|
cookieHeader(cookies: any) {
|
||||||
|
@ -203,11 +185,11 @@ class TestConfiguration {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async createUser(user?: User, opts: any = {}) {
|
async createUser(user?: User) {
|
||||||
if (!user) {
|
if (!user) {
|
||||||
user = structures.users.user()
|
user = structures.users.user()
|
||||||
}
|
}
|
||||||
const response = await this._req(user, null, controllers.users.save, opts)
|
const response = await this._req(user, null, controllers.users.save)
|
||||||
const body = response as CreateUserResponse
|
const body = response as CreateUserResponse
|
||||||
return this.getUser(body.email)
|
return this.getUser(body.email)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue