- added working typescript support to vue
- added bouncer support - added UserPolicies - added first user index call
This commit is contained in:
@@ -2,7 +2,7 @@ import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import { schema } from '@ioc:Adonis/Core/Validator'
|
||||
|
||||
export default class AuthController {
|
||||
public async login({ auth, request, response, session }: HttpContextContract){
|
||||
public async login({ auth, request, response }: HttpContextContract){
|
||||
|
||||
|
||||
const loginSchema = schema.create({
|
||||
@@ -18,27 +18,13 @@ export default class AuthController {
|
||||
}
|
||||
})
|
||||
|
||||
session.flash({
|
||||
login: {
|
||||
warning: 'test'
|
||||
}
|
||||
})
|
||||
|
||||
await auth.attempt(username, password)
|
||||
response.redirect().toRoute('events.index')
|
||||
}
|
||||
|
||||
public async logout({ auth, response, session }: HttpContextContract) {
|
||||
public async logout({ auth, response }: HttpContextContract) {
|
||||
await auth.logout()
|
||||
|
||||
session.flash('gfd', {
|
||||
warning: 'test'
|
||||
})
|
||||
session.flash('login', {
|
||||
warning: "noch eine warning"
|
||||
})
|
||||
|
||||
|
||||
response.redirect('/login')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import User from 'App/Models/User'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
import Database from '@ioc:Adonis/Lucid/Database'
|
||||
|
||||
export default class UsersController {
|
||||
public async index({ auth, response, inertia }: HttpContextContract) {
|
||||
public async index({ inertia, bouncer }: HttpContextContract) {
|
||||
|
||||
if(auth.user?.isAdmin) {
|
||||
return inertia.render('Users/Index')
|
||||
}
|
||||
else response.redirect().toRoute('events.index')
|
||||
await bouncer.with('UserPolicy').authorize('index')
|
||||
|
||||
const users = await Database
|
||||
.from('users')
|
||||
.select('id', 'username', 'is_admin')
|
||||
|
||||
return inertia.render('Users/Index', { users })
|
||||
}
|
||||
|
||||
public async create({ auth, inertia }: HttpContextContract) {
|
||||
|
||||
@@ -38,6 +38,10 @@ export default class ExceptionHandler extends HttpExceptionHandler {
|
||||
session.flash('login', { error: error.message });
|
||||
return response.redirect().back();
|
||||
}
|
||||
if(['E_AUTHORIZATION_FAILURE'].includes(error.code)) {
|
||||
session.flash('auth', { error: error.message })
|
||||
return response.redirect().back()
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward rest of the exceptions to the parent class
|
||||
|
||||
@@ -12,7 +12,10 @@ export default class User extends BaseModel {
|
||||
@column({ serializeAs: null })
|
||||
public password: string
|
||||
|
||||
@column()
|
||||
@column({
|
||||
consume: Boolean,
|
||||
serialize: Boolean
|
||||
})
|
||||
public isAdmin: boolean
|
||||
|
||||
@column()
|
||||
|
||||
25
app/Policies/UserPolicy.ts
Normal file
25
app/Policies/UserPolicy.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import User from 'App/Models/User'
|
||||
import { BasePolicy } from '@ioc:Adonis/Addons/Bouncer'
|
||||
|
||||
export default class UserPolicy extends BasePolicy {
|
||||
public async index(user: User) {
|
||||
return user.isAdmin
|
||||
}
|
||||
|
||||
public async show(user: User, query: User) {
|
||||
return user.isAdmin || user.id === query.id
|
||||
}
|
||||
|
||||
public async update(user: User, query: User) {
|
||||
return user.isAdmin || user.id === query.id
|
||||
}
|
||||
|
||||
public async destroy(user: User) {
|
||||
return user.isAdmin
|
||||
}
|
||||
|
||||
public async store(user: User) {
|
||||
return user.isAdmin
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user