mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat: api keys
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
import { SystemModel } from '@/modules/System/models/SystemModel';
|
||||
import { Model } from 'objection';
|
||||
|
||||
export class ApiKeyModel extends SystemModel {
|
||||
readonly key: string;
|
||||
readonly name?: string;
|
||||
readonly createdAt: Date;
|
||||
readonly expiresAt?: Date;
|
||||
readonly revoked?: boolean;
|
||||
readonly revokedAt?: Date;
|
||||
readonly userId: number;
|
||||
readonly tenantId: number;
|
||||
|
||||
get revoked() {
|
||||
return !!this.revokedAt;
|
||||
}
|
||||
|
||||
static get virtualAttributes() {
|
||||
return ['revoked'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
@@ -22,4 +31,42 @@ export class ApiKeyModel extends SystemModel {
|
||||
get timestamps() {
|
||||
return ['createdAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Relation mappings for Objection.js
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const { SystemUser } = require('../../System/models/SystemUser');
|
||||
const { TenantModel } = require('../../System/models/TenantModel');
|
||||
|
||||
return {
|
||||
user: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: SystemUser,
|
||||
join: {
|
||||
from: 'api_keys.userId',
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
tenant: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: TenantModel,
|
||||
join: {
|
||||
from: 'api_keys.tenantId',
|
||||
to: 'tenants.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
notRevoked(query) {
|
||||
query.whereNull('revokedAt');
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user