Files

99 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2020-02-19 15:55:06 +11:00
// Objection Docs:
// http://vincit.github.io/objection.js/
2025-09-02 21:43:00 +10:00
import { Model } from "objection";
import db from "../db.js";
import { convertBoolFieldsToInt, convertIntFieldsToBool } from "../lib/helpers.js";
import AccessListAuth from "./access_list_auth.js";
import AccessListClient from "./access_list_client.js";
import now from "./now_helper.js";
import ProxyHostModel from "./proxy_host.js";
import User from "./user.js";
2020-02-19 15:55:06 +11:00
Model.knex(db());
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
const boolFields = ["is_deleted", "satisfy_any", "pass_auth"];
2024-10-10 15:53:11 +10:00
2020-02-19 15:55:06 +11:00
class AccessList extends Model {
2025-09-02 21:43:00 +10:00
$beforeInsert() {
this.created_on = now();
this.modified_on = now();
2020-02-19 15:55:06 +11:00
// Default for meta
2025-09-02 21:43:00 +10:00
if (typeof this.meta === "undefined") {
2020-02-19 15:55:06 +11:00
this.meta = {};
}
}
2025-09-02 21:43:00 +10:00
$beforeUpdate() {
this.modified_on = now();
2020-02-19 15:55:06 +11:00
}
2024-10-10 15:53:11 +10:00
$parseDatabaseJson(json) {
2025-09-02 21:43:00 +10:00
const thisJson = super.$parseDatabaseJson(json);
return convertIntFieldsToBool(thisJson, boolFields);
2024-10-10 15:53:11 +10:00
}
$formatDatabaseJson(json) {
2025-09-02 21:43:00 +10:00
const thisJson = convertBoolFieldsToInt(json, boolFields);
return super.$formatDatabaseJson(thisJson);
2024-10-10 15:53:11 +10:00
}
2025-09-02 21:43:00 +10:00
static get name() {
return "AccessList";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get tableName() {
return "access_list";
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get jsonAttributes() {
return ["meta"];
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
static get relationMappings() {
2020-02-19 15:55:06 +11:00
return {
owner: {
2025-09-02 21:43:00 +10:00
relation: Model.HasOneRelation,
2020-02-19 15:55:06 +11:00
modelClass: User,
2025-09-02 21:43:00 +10:00
join: {
from: "access_list.owner_user_id",
to: "user.id",
},
modify: (qb) => {
qb.where("user.is_deleted", 0);
2020-02-19 15:55:06 +11:00
},
},
items: {
2025-09-02 21:43:00 +10:00
relation: Model.HasManyRelation,
2020-02-19 15:55:06 +11:00
modelClass: AccessListAuth,
2025-09-02 21:43:00 +10:00
join: {
from: "access_list.id",
to: "access_list_auth.access_list_id",
},
2020-02-19 15:55:06 +11:00
},
2020-04-10 16:38:54 -07:00
clients: {
2025-09-02 21:43:00 +10:00
relation: Model.HasManyRelation,
2020-04-10 16:38:54 -07:00
modelClass: AccessListClient,
2025-09-02 21:43:00 +10:00
join: {
from: "access_list.id",
to: "access_list_client.access_list_id",
},
2020-04-10 16:38:54 -07:00
},
2020-02-19 15:55:06 +11:00
proxy_hosts: {
2025-09-02 21:43:00 +10:00
relation: Model.HasManyRelation,
modelClass: ProxyHostModel,
join: {
from: "access_list.id",
to: "proxy_host.access_list_id",
},
modify: (qb) => {
qb.where("proxy_host.is_deleted", 0);
2020-02-19 15:55:06 +11:00
},
2025-09-02 21:43:00 +10:00
},
2020-02-19 15:55:06 +11:00
};
}
}
2025-09-02 21:43:00 +10:00
export default AccessList;