feat: Move canConnect logic into the database column for AppConfig
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
export async function up(knex) {
|
||||
await knex.schema.table('app_configs', (table) => {
|
||||
table.boolean('can_connect').defaultTo(false);
|
||||
});
|
||||
}
|
||||
|
||||
export async function down(knex) {
|
||||
await knex.schema.table('app_configs', (table) => {
|
||||
table.dropColumn('can_connect');
|
||||
});
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
import AES from 'crypto-js/aes.js';
|
||||
import enc from 'crypto-js/enc-utf8.js';
|
||||
import appConfig from '../config/app.js';
|
||||
import AppConfig from './app-config.js';
|
||||
import Base from './base.js';
|
||||
|
||||
class AppAuthClient extends Base {
|
||||
@@ -59,6 +60,21 @@ class AppAuthClient extends Base {
|
||||
this.encryptData();
|
||||
}
|
||||
|
||||
async assignCanConnectForAppConfig() {
|
||||
const appConfig = await AppConfig.query().findOne({ key: this.appKey });
|
||||
await appConfig?.assignCanConnect();
|
||||
}
|
||||
|
||||
async $afterInsert(queryContext) {
|
||||
await super.$afterInsert(queryContext);
|
||||
await this.assignCanConnectForAppConfig();
|
||||
}
|
||||
|
||||
async $afterUpdate(opt, queryContext) {
|
||||
await super.$afterUpdate(opt, queryContext);
|
||||
await this.assignCanConnectForAppConfig();
|
||||
}
|
||||
|
||||
async $afterFind() {
|
||||
this.decryptData();
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import App from './app.js';
|
||||
import AppAuthClient from './app-auth-client.js';
|
||||
import Base from './base.js';
|
||||
|
||||
class AppConfig extends Base {
|
||||
@@ -14,34 +15,48 @@ class AppConfig extends Base {
|
||||
allowCustomConnection: { type: 'boolean', default: false },
|
||||
shared: { type: 'boolean', default: false },
|
||||
disabled: { type: 'boolean', default: false },
|
||||
canConnect: { type: 'boolean', default: false },
|
||||
},
|
||||
};
|
||||
|
||||
static get virtualAttributes() {
|
||||
return ['canConnect', 'canCustomConnect'];
|
||||
}
|
||||
|
||||
get canCustomConnect() {
|
||||
return !this.disabled && this.allowCustomConnection;
|
||||
}
|
||||
|
||||
get canConnect() {
|
||||
const hasSomeActiveAppAuthClients = !!this.appAuthClients?.some(
|
||||
(appAuthClient) => appAuthClient.active
|
||||
);
|
||||
const shared = this.shared;
|
||||
const active = this.disabled === false;
|
||||
|
||||
const conditions = [hasSomeActiveAppAuthClients, shared, active];
|
||||
|
||||
return conditions.every(Boolean);
|
||||
}
|
||||
|
||||
async getApp() {
|
||||
if (!this.key) return null;
|
||||
|
||||
return await App.findOneByKey(this.key);
|
||||
}
|
||||
|
||||
async hasActiveAppAuthClients() {
|
||||
const appAuthClients = await AppAuthClient.query().where({
|
||||
appKey: this.key,
|
||||
});
|
||||
|
||||
const hasSomeActiveAppAuthClients = !!appAuthClients?.some(
|
||||
(appAuthClient) => appAuthClient.active
|
||||
);
|
||||
|
||||
return hasSomeActiveAppAuthClients;
|
||||
}
|
||||
|
||||
async assignCanConnect() {
|
||||
const shared = this.shared;
|
||||
const active = this.disabled === false;
|
||||
const hasSomeActiveAppAuthClients = await this.hasActiveAppAuthClients();
|
||||
|
||||
const conditions = [hasSomeActiveAppAuthClients, shared, active];
|
||||
const canConnect = conditions.every(Boolean);
|
||||
|
||||
this.canConnect = canConnect;
|
||||
}
|
||||
|
||||
async $beforeInsert(queryContext) {
|
||||
await super.$beforeInsert(queryContext);
|
||||
await this.assignCanConnect();
|
||||
}
|
||||
|
||||
async $beforeUpdate(opt, queryContext) {
|
||||
await super.$beforeUpdate(opt, queryContext);
|
||||
await this.assignCanConnect();
|
||||
}
|
||||
}
|
||||
|
||||
export default AppConfig;
|
||||
|
Reference in New Issue
Block a user