update
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
|
||||
export default class TenantController {
|
||||
/**
|
||||
* @summary Resolve current tenant context
|
||||
* @responseBody 200 - {"tenant":{"id":42,"name":"Acme GmbH","slug":"acme-gmbh"}}
|
||||
* @responseBody 400 - {"code":"E_TENANT_REQUIRED","message":"The X-Tenant-Id header is required."}
|
||||
* @responseBody 403 - {"code":"E_TENANT_ACCESS_DENIED","message":"You do not have access to this tenant."}
|
||||
*/
|
||||
async current({ response, tenant }: HttpContext) {
|
||||
return response.ok({ tenant })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { BaseModel, column, manyToMany } from '@adonisjs/lucid/orm'
|
||||
import type { ManyToMany } from '@adonisjs/lucid/types/relations'
|
||||
import { DateTime } from 'luxon'
|
||||
import Admin from '#base_modules/Admin/model/admin'
|
||||
|
||||
export default class Tenant extends BaseModel {
|
||||
@column({ isPrimary: true, columnName: 'id' })
|
||||
declare id: number
|
||||
|
||||
@column({ columnName: 'name' })
|
||||
declare name: string
|
||||
|
||||
@column({ columnName: 'slug' })
|
||||
declare slug: string
|
||||
|
||||
@column({ columnName: 'status' })
|
||||
declare status: 'active' | 'inactive'
|
||||
|
||||
@column.dateTime({ autoCreate: true, columnName: 'created_at' })
|
||||
declare createdAt: DateTime
|
||||
|
||||
@column.dateTime({ autoCreate: true, autoUpdate: true, columnName: 'updated_at' })
|
||||
declare updatedAt: DateTime | null
|
||||
|
||||
@manyToMany(() => Admin, {
|
||||
pivotTable: 'admin_tenants',
|
||||
pivotForeignKey: 'tenant_id',
|
||||
pivotRelatedForeignKey: 'admin_id',
|
||||
})
|
||||
declare admins: ManyToMany<typeof Admin>
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import router from '@adonisjs/core/services/router'
|
||||
import { middleware } from '#start/kernel'
|
||||
|
||||
const TenantController = () => import('#base_modules/Tenant/controller/tenant_controller')
|
||||
|
||||
router
|
||||
.get('/tenants/current', [TenantController, 'current'])
|
||||
.use([middleware.auth(), middleware.tenant()])
|
||||
@@ -0,0 +1,15 @@
|
||||
import Tenant from '#base_modules/Tenant/model/tenant'
|
||||
|
||||
export type ResolvedTenant = { id: number; name: string; slug: string }
|
||||
|
||||
export default class TenantAccessService {
|
||||
async resolveForAdmin(adminId: number, tenantId: number): Promise<ResolvedTenant | null> {
|
||||
const tenant = await Tenant.query()
|
||||
.where('id', tenantId)
|
||||
.where('status', 'active')
|
||||
.whereHas('admins', (admins) => admins.where('admins.id', adminId))
|
||||
.first()
|
||||
|
||||
return tenant ? { id: tenant.id, name: tenant.name, slug: tenant.slug } : null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user