some shit
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
import Employee from 'App/Models/Employee'
|
|
||||||
import SetSettingsValidator from 'App/Validators/SetSettingsValidator'
|
import SetSettingsValidator from 'App/Validators/SetSettingsValidator'
|
||||||
|
import Logger from '@ioc:Adonis/Core/Logger'
|
||||||
|
|
||||||
type ResultSetting = {
|
type ResultSetting = {
|
||||||
key: string,
|
key: string,
|
||||||
@@ -9,15 +9,10 @@ type ResultSetting = {
|
|||||||
|
|
||||||
export default class SettingsController {
|
export default class SettingsController {
|
||||||
|
|
||||||
public async list({ params, bouncer }: HttpContextContract ): Promise<ResultSetting[]> {
|
public async list({ auth }: HttpContextContract ): Promise<ResultSetting[]> {
|
||||||
const userId = params.userId
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await Employee.findOrFail(userId)
|
const result = await auth.user.related('settings').query().select(['key', 'value'])
|
||||||
|
|
||||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
|
||||||
|
|
||||||
const result = await user.related('settings').query().select(['key', 'value'])
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -27,16 +22,12 @@ export default class SettingsController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async get({params, bouncer}: HttpContextContract): Promise<ResultSetting | null> {
|
public async get({params, auth}: HttpContextContract): Promise<ResultSetting | null> {
|
||||||
const userId = params.userId
|
|
||||||
const key = params.key
|
const key = params.key
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await Employee.findOrFail(userId)
|
const result = auth.user.related('settings').query().select(['key', 'value']).where('key', key).first()
|
||||||
|
|
||||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
|
||||||
|
|
||||||
const result = user.related('settings').query().select(['key', 'value']).where('key', key).first()
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -46,34 +37,35 @@ export default class SettingsController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async set({params, request, bouncer}: HttpContextContract): Promise<'ok'> {
|
/**
|
||||||
const userId = params.userId
|
* Expects:
|
||||||
|
* {
|
||||||
|
* settings: [
|
||||||
|
* {key: 'key1', value: 'value1'},
|
||||||
|
* ]
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
public async set({request, auth}: HttpContextContract): Promise<'ok'> {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = await request.validate(SetSettingsValidator)
|
const payload = await request.validate(SetSettingsValidator)
|
||||||
const user = await Employee.findOrFail(userId)
|
|
||||||
|
|
||||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
await auth.user.related('settings').updateOrCreateMany(payload.settings, 'key')
|
||||||
|
|
||||||
await user.related('settings').updateOrCreateMany(payload.settings, 'key')
|
|
||||||
|
|
||||||
return "ok"
|
return "ok"
|
||||||
}
|
}
|
||||||
catch(error){
|
catch(error){
|
||||||
|
Logger.error(error)
|
||||||
return error.message
|
return error.message
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete({ params, bouncer }: HttpContextContract): Promise<(0 | 1)[]> {
|
public async delete({ params, auth }: HttpContextContract): Promise<(0 | 1)[]> {
|
||||||
const userId = params.userId
|
|
||||||
const key = params.key
|
const key = params.key
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await Employee.findOrFail(userId)
|
return await auth.user.related('settings').query().where('key', key).delete()
|
||||||
await bouncer.with('SettingsPolicy').authorize('do', user)
|
|
||||||
|
|
||||||
return await user.related('settings').query().where('key', key).delete()
|
|
||||||
}
|
}
|
||||||
catch(error){
|
catch(error){
|
||||||
return error.message
|
return error.message
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
import { BasePolicy } from '@ioc:Adonis/Addons/Bouncer'
|
|
||||||
import Employee from 'App/Models/Employee'
|
|
||||||
|
|
||||||
export default class SettingsPolicy extends BasePolicy {
|
|
||||||
public async do(user: Employee, query: Employee){
|
|
||||||
return user.isAdmin() || user.id === query.id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { schema, rules } from '@ioc:Adonis/Core/Validator'
|
import { schema, rules, validator } from '@ioc:Adonis/Core/Validator'
|
||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
|
||||||
export default class SetSettingsValidator {
|
export default class SetSettingsValidator {
|
||||||
@@ -52,5 +52,11 @@ export default class SetSettingsValidator {
|
|||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public messages = {}
|
public messages = {
|
||||||
|
'settings.required': 'Settings are required',
|
||||||
|
'settings.key.required': 'Key is required',
|
||||||
|
'settings.key.alpha': 'Key must be alphabetic',
|
||||||
|
'settings.value.required': 'Value is required',
|
||||||
|
'settings.value.alpha': 'Value must be alphabetic',
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,5 +56,4 @@ export const { actions } = Bouncer
|
|||||||
*/
|
*/
|
||||||
export const { policies } = Bouncer.registerPolicies({
|
export const { policies } = Bouncer.registerPolicies({
|
||||||
EmployeesPolicy: () => import('App/Policies/EmployeesPolicy'),
|
EmployeesPolicy: () => import('App/Policies/EmployeesPolicy'),
|
||||||
SettingsPolicy: () => import('App/Policies/SettingsPolicy'),
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ Route.group(() => {
|
|||||||
|
|
||||||
Route.resource('employees', 'EmployeesController').apiOnly()
|
Route.resource('employees', 'EmployeesController').apiOnly()
|
||||||
|
|
||||||
Route.get('settings/:userId', 'SettingsController.list').as('settings.list')
|
Route.get('settings', 'SettingsController.list').as('settings.list')
|
||||||
Route.get('settings/:userId/:key', 'SettingsController.get').as('settings.get')
|
Route.get('settings/:key', 'SettingsController.get').as('settings.get')
|
||||||
Route.post('settings/:userId', 'SettingsController.set').as('settings.set')
|
Route.post('settings', 'SettingsController.set').as('settings.set')
|
||||||
Route.delete('settings/:userId/:key', 'SettingsController.delete').as('settings.delete')
|
Route.delete('settings:key', 'SettingsController.delete').as('settings.delete')
|
||||||
})
|
})
|
||||||
.prefix('api/v1')
|
.prefix('api/v1')
|
||||||
.middleware('auth')
|
.middleware('auth')
|
||||||
|
|||||||
Reference in New Issue
Block a user