redirection-host.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. 'use strict';
  2. const _ = require('lodash');
  3. const error = require('../lib/error');
  4. const redirectionHostModel = require('../models/redirection_host');
  5. const internalHost = require('./host');
  6. const internalNginx = require('./nginx');
  7. const internalAuditLog = require('./audit-log');
  8. function omissions () {
  9. return ['is_deleted'];
  10. }
  11. const internalRedirectionHost = {
  12. /**
  13. * @param {Access} access
  14. * @param {Object} data
  15. * @returns {Promise}
  16. */
  17. create: (access, data) => {
  18. return access.can('redirection_hosts:create', data)
  19. .then(access_data => {
  20. // Get a list of the domain names and check each of them against existing records
  21. let domain_name_check_promises = [];
  22. data.domain_names.map(function (domain_name) {
  23. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name));
  24. });
  25. return Promise.all(domain_name_check_promises)
  26. .then(check_results => {
  27. check_results.map(function (result) {
  28. if (result.is_taken) {
  29. throw new error.ValidationError(result.hostname + ' is already in use');
  30. }
  31. });
  32. });
  33. })
  34. .then(() => {
  35. // At this point the domains should have been checked
  36. data.owner_user_id = access.token.get('attrs').id;
  37. return redirectionHostModel
  38. .query()
  39. .omit(omissions())
  40. .insertAndFetch(data);
  41. })
  42. .then(row => {
  43. // Configure nginx
  44. return internalNginx.configure(redirectionHostModel, 'redirection_host', row)
  45. .then(() => {
  46. return internalRedirectionHost.get(access, {id: row.id, expand: ['owner']});
  47. });
  48. })
  49. .then(row => {
  50. // Add to audit log
  51. return internalAuditLog.add(access, {
  52. action: 'created',
  53. object_type: 'redirection-host',
  54. object_id: row.id,
  55. meta: data
  56. })
  57. .then(() => {
  58. return row;
  59. });
  60. });
  61. },
  62. /**
  63. * @param {Access} access
  64. * @param {Object} data
  65. * @param {Integer} data.id
  66. * @param {String} [data.email]
  67. * @param {String} [data.name]
  68. * @return {Promise}
  69. */
  70. update: (access, data) => {
  71. return access.can('redirection_hosts:update', data.id)
  72. .then(access_data => {
  73. // Get a list of the domain names and check each of them against existing records
  74. let domain_name_check_promises = [];
  75. if (typeof data.domain_names !== 'undefined') {
  76. data.domain_names.map(function (domain_name) {
  77. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name, 'redirection', data.id));
  78. });
  79. return Promise.all(domain_name_check_promises)
  80. .then(check_results => {
  81. check_results.map(function (result) {
  82. if (result.is_taken) {
  83. throw new error.ValidationError(result.hostname + ' is already in use');
  84. }
  85. });
  86. });
  87. }
  88. })
  89. .then(() => {
  90. return internalRedirectionHost.get(access, {id: data.id});
  91. })
  92. .then(row => {
  93. if (row.id !== data.id) {
  94. // Sanity check that something crazy hasn't happened
  95. throw new error.InternalValidationError('Redirection Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  96. }
  97. return redirectionHostModel
  98. .query()
  99. .omit(omissions())
  100. .patchAndFetchById(row.id, data)
  101. .then(saved_row => {
  102. saved_row.meta = internalHost.cleanMeta(saved_row.meta);
  103. // Add to audit log
  104. return internalAuditLog.add(access, {
  105. action: 'updated',
  106. object_type: 'redirection-host',
  107. object_id: row.id,
  108. meta: data
  109. })
  110. .then(() => {
  111. return _.omit(saved_row, omissions());
  112. });
  113. });
  114. });
  115. },
  116. /**
  117. * @param {Access} access
  118. * @param {Object} data
  119. * @param {Integer} data.id
  120. * @param {Array} [data.expand]
  121. * @param {Array} [data.omit]
  122. * @return {Promise}
  123. */
  124. get: (access, data) => {
  125. if (typeof data === 'undefined') {
  126. data = {};
  127. }
  128. return access.can('redirection_hosts:get', data.id)
  129. .then(access_data => {
  130. let query = redirectionHostModel
  131. .query()
  132. .where('is_deleted', 0)
  133. .andWhere('id', data.id)
  134. .allowEager('[owner,certificate]')
  135. .first();
  136. if (access_data.permission_visibility !== 'all') {
  137. query.andWhere('owner_user_id', access.token.get('attrs').id);
  138. }
  139. // Custom omissions
  140. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  141. query.omit(data.omit);
  142. }
  143. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  144. query.eager('[' + data.expand.join(', ') + ']');
  145. }
  146. return query;
  147. })
  148. .then(row => {
  149. if (row) {
  150. row.meta = internalHost.cleanMeta(row.meta);
  151. return _.omit(row, omissions());
  152. } else {
  153. throw new error.ItemNotFoundError(data.id);
  154. }
  155. });
  156. },
  157. /**
  158. * @param {Access} access
  159. * @param {Object} data
  160. * @param {Integer} data.id
  161. * @param {String} [data.reason]
  162. * @returns {Promise}
  163. */
  164. delete: (access, data) => {
  165. return access.can('redirection_hosts:delete', data.id)
  166. .then(() => {
  167. return internalRedirectionHost.get(access, {id: data.id});
  168. })
  169. .then(row => {
  170. if (!row) {
  171. throw new error.ItemNotFoundError(data.id);
  172. }
  173. return redirectionHostModel
  174. .query()
  175. .where('id', row.id)
  176. .patch({
  177. is_deleted: 1
  178. })
  179. .then(() => {
  180. // Delete Nginx Config
  181. return internalNginx.deleteConfig('redirection_host', row)
  182. .then(() => {
  183. return internalNginx.reload();
  184. });
  185. })
  186. .then(() => {
  187. // Add to audit log
  188. row.meta = internalHost.cleanMeta(row.meta);
  189. return internalAuditLog.add(access, {
  190. action: 'deleted',
  191. object_type: 'redirection-host',
  192. object_id: row.id,
  193. meta: _.omit(row, omissions())
  194. });
  195. });
  196. })
  197. .then(() => {
  198. return true;
  199. });
  200. },
  201. /**
  202. * @param {Access} access
  203. * @param {Object} data
  204. * @param {Integer} data.id
  205. * @param {Object} data.files
  206. * @returns {Promise}
  207. */
  208. setCerts: (access, data) => {
  209. return internalRedirectionHost.get(access, {id: data.id})
  210. .then(row => {
  211. _.map(data.files, (file, name) => {
  212. if (internalHost.allowed_ssl_files.indexOf(name) !== -1) {
  213. row.meta[name] = file.data.toString();
  214. }
  215. });
  216. return internalRedirectionHost.update(access, {
  217. id: data.id,
  218. meta: row.meta
  219. });
  220. })
  221. .then(row => {
  222. return internalAuditLog.add(access, {
  223. action: 'updated',
  224. object_type: 'redirection-host',
  225. object_id: row.id,
  226. meta: data
  227. })
  228. .then(() => {
  229. return _.pick(row.meta, internalHost.allowed_ssl_files);
  230. });
  231. });
  232. },
  233. /**
  234. * All Hosts
  235. *
  236. * @param {Access} access
  237. * @param {Array} [expand]
  238. * @param {String} [search_query]
  239. * @returns {Promise}
  240. */
  241. getAll: (access, expand, search_query) => {
  242. return access.can('redirection_hosts:list')
  243. .then(access_data => {
  244. let query = redirectionHostModel
  245. .query()
  246. .where('is_deleted', 0)
  247. .groupBy('id')
  248. .omit(['is_deleted'])
  249. .allowEager('[owner,certificate]')
  250. .orderBy('domain_names', 'ASC');
  251. if (access_data.permission_visibility !== 'all') {
  252. query.andWhere('owner_user_id', access.token.get('attrs').id);
  253. }
  254. // Query is used for searching
  255. if (typeof search_query === 'string') {
  256. query.where(function () {
  257. this.where('domain_names', 'like', '%' + search_query + '%');
  258. });
  259. }
  260. if (typeof expand !== 'undefined' && expand !== null) {
  261. query.eager('[' + expand.join(', ') + ']');
  262. }
  263. return query;
  264. })
  265. .then(rows => {
  266. rows.map(row => {
  267. row.meta = internalHost.cleanMeta(row.meta);
  268. });
  269. return rows;
  270. });
  271. },
  272. /**
  273. * Report use
  274. *
  275. * @param {Integer} user_id
  276. * @param {String} visibility
  277. * @returns {Promise}
  278. */
  279. getCount: (user_id, visibility) => {
  280. let query = redirectionHostModel
  281. .query()
  282. .count('id as count')
  283. .where('is_deleted', 0);
  284. if (visibility !== 'all') {
  285. query.andWhere('owner_user_id', user_id);
  286. }
  287. return query.first()
  288. .then(row => {
  289. return parseInt(row.count, 10);
  290. });
  291. }
  292. };
  293. module.exports = internalRedirectionHost;