redirection_host.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 Certificate = require('./certificate');
  8. const now = require('./now_helper');
  9. Model.knex(db);
  10. const boolFields = [
  11. 'is_deleted',
  12. 'enabled',
  13. 'preserve_path',
  14. 'ssl_forced',
  15. 'block_exploits',
  16. 'hsts_enabled',
  17. 'hsts_subdomains',
  18. 'http2_support',
  19. ];
  20. class RedirectionHost extends Model {
  21. $beforeInsert () {
  22. this.created_on = now();
  23. this.modified_on = now();
  24. // Default for domain_names
  25. if (typeof this.domain_names === 'undefined') {
  26. this.domain_names = [];
  27. }
  28. // Default for meta
  29. if (typeof this.meta === 'undefined') {
  30. this.meta = {};
  31. }
  32. this.domain_names.sort();
  33. }
  34. $beforeUpdate () {
  35. this.modified_on = now();
  36. // Sort domain_names
  37. if (typeof this.domain_names !== 'undefined') {
  38. this.domain_names.sort();
  39. }
  40. }
  41. $parseDatabaseJson(json) {
  42. json = super.$parseDatabaseJson(json);
  43. return helpers.convertIntFieldsToBool(json, boolFields);
  44. }
  45. $formatDatabaseJson(json) {
  46. json = helpers.convertBoolFieldsToInt(json, boolFields);
  47. return super.$formatDatabaseJson(json);
  48. }
  49. static get name () {
  50. return 'RedirectionHost';
  51. }
  52. static get tableName () {
  53. return 'redirection_host';
  54. }
  55. static get jsonAttributes () {
  56. return ['domain_names', 'meta'];
  57. }
  58. static get relationMappings () {
  59. return {
  60. owner: {
  61. relation: Model.HasOneRelation,
  62. modelClass: User,
  63. join: {
  64. from: 'redirection_host.owner_user_id',
  65. to: 'user.id'
  66. },
  67. modify: function (qb) {
  68. qb.where('user.is_deleted', 0);
  69. }
  70. },
  71. certificate: {
  72. relation: Model.HasOneRelation,
  73. modelClass: Certificate,
  74. join: {
  75. from: 'redirection_host.certificate_id',
  76. to: 'certificate.id'
  77. },
  78. modify: function (qb) {
  79. qb.where('certificate.is_deleted', 0);
  80. }
  81. }
  82. };
  83. }
  84. }
  85. module.exports = RedirectionHost;