access_list.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Objection Docs:
  2. // http://vincit.github.io/objection.js/
  3. const db = require('../db');
  4. const helpers = require('../lib/helpers');
  5. const Model = require('objection').Model;
  6. const User = require('./user');
  7. const AccessListAuth = require('./access_list_auth');
  8. const AccessListClient = require('./access_list_client');
  9. const now = require('./now_helper');
  10. Model.knex(db);
  11. const boolFields = [
  12. 'is_deleted',
  13. 'satisfy_any',
  14. 'pass_auth',
  15. ];
  16. class AccessList extends Model {
  17. $beforeInsert () {
  18. this.created_on = now();
  19. this.modified_on = now();
  20. // Default for meta
  21. if (typeof this.meta === 'undefined') {
  22. this.meta = {};
  23. }
  24. }
  25. $beforeUpdate () {
  26. this.modified_on = now();
  27. }
  28. $parseDatabaseJson(json) {
  29. json = super.$parseDatabaseJson(json);
  30. return helpers.convertIntFieldsToBool(json, boolFields);
  31. }
  32. $formatDatabaseJson(json) {
  33. json = helpers.convertBoolFieldsToInt(json, boolFields);
  34. return super.$formatDatabaseJson(json);
  35. }
  36. static get name () {
  37. return 'AccessList';
  38. }
  39. static get tableName () {
  40. return 'access_list';
  41. }
  42. static get jsonAttributes () {
  43. return ['meta'];
  44. }
  45. static get relationMappings () {
  46. const ProxyHost = require('./proxy_host');
  47. return {
  48. owner: {
  49. relation: Model.HasOneRelation,
  50. modelClass: User,
  51. join: {
  52. from: 'access_list.owner_user_id',
  53. to: 'user.id'
  54. },
  55. modify: function (qb) {
  56. qb.where('user.is_deleted', 0);
  57. }
  58. },
  59. items: {
  60. relation: Model.HasManyRelation,
  61. modelClass: AccessListAuth,
  62. join: {
  63. from: 'access_list.id',
  64. to: 'access_list_auth.access_list_id'
  65. }
  66. },
  67. clients: {
  68. relation: Model.HasManyRelation,
  69. modelClass: AccessListClient,
  70. join: {
  71. from: 'access_list.id',
  72. to: 'access_list_client.access_list_id'
  73. }
  74. },
  75. proxy_hosts: {
  76. relation: Model.HasManyRelation,
  77. modelClass: ProxyHost,
  78. join: {
  79. from: 'access_list.id',
  80. to: 'proxy_host.access_list_id'
  81. },
  82. modify: function (qb) {
  83. qb.where('proxy_host.is_deleted', 0);
  84. }
  85. }
  86. };
  87. }
  88. }
  89. module.exports = AccessList;