redirection-host.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. const _ = require('lodash');
  2. const error = require('../lib/error');
  3. const utils = require('../lib/utils');
  4. const redirectionHostModel = require('../models/redirection_host');
  5. const internalHost = require('./host');
  6. const internalNginx = require('./nginx');
  7. const internalAuditLog = require('./audit-log');
  8. const internalCertificate = require('./certificate');
  9. function omissions () {
  10. return ['is_deleted'];
  11. }
  12. const internalRedirectionHost = {
  13. /**
  14. * @param {Access} access
  15. * @param {Object} data
  16. * @returns {Promise}
  17. */
  18. create: (access, data) => {
  19. let create_certificate = data.certificate_id === 'new';
  20. if (create_certificate) {
  21. delete data.certificate_id;
  22. }
  23. return access.can('redirection_hosts:create', data)
  24. .then((/*access_data*/) => {
  25. // Get a list of the domain names and check each of them against existing records
  26. let domain_name_check_promises = [];
  27. data.domain_names.map(function (domain_name) {
  28. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name));
  29. });
  30. return Promise.all(domain_name_check_promises)
  31. .then((check_results) => {
  32. check_results.map(function (result) {
  33. if (result.is_taken) {
  34. throw new error.ValidationError(result.hostname + ' is already in use');
  35. }
  36. });
  37. });
  38. })
  39. .then(() => {
  40. // At this point the domains should have been checked
  41. data.owner_user_id = access.token.getUserId(1);
  42. data = internalHost.cleanSslHstsData(data);
  43. return redirectionHostModel
  44. .query()
  45. .insertAndFetch(data)
  46. .then(utils.omitRow(omissions()));
  47. })
  48. .then((row) => {
  49. if (create_certificate) {
  50. return internalCertificate.createQuickCertificate(access, data)
  51. .then((cert) => {
  52. // update host with cert id
  53. return internalRedirectionHost.update(access, {
  54. id: row.id,
  55. certificate_id: cert.id
  56. });
  57. })
  58. .then(() => {
  59. return row;
  60. });
  61. }
  62. return row;
  63. })
  64. .then((row) => {
  65. // re-fetch with cert
  66. return internalRedirectionHost.get(access, {
  67. id: row.id,
  68. expand: ['certificate', 'owner']
  69. });
  70. })
  71. .then((row) => {
  72. // Configure nginx
  73. return internalNginx.configure(redirectionHostModel, 'redirection_host', row)
  74. .then(() => {
  75. return row;
  76. });
  77. })
  78. .then((row) => {
  79. data.meta = _.assign({}, data.meta || {}, row.meta);
  80. // Add to audit log
  81. return internalAuditLog.add(access, {
  82. action: 'created',
  83. object_type: 'redirection-host',
  84. object_id: row.id,
  85. meta: data
  86. })
  87. .then(() => {
  88. return row;
  89. });
  90. });
  91. },
  92. /**
  93. * @param {Access} access
  94. * @param {Object} data
  95. * @param {Number} data.id
  96. * @return {Promise}
  97. */
  98. update: (access, data) => {
  99. let create_certificate = data.certificate_id === 'new';
  100. if (create_certificate) {
  101. delete data.certificate_id;
  102. }
  103. return access.can('redirection_hosts:update', data.id)
  104. .then((/*access_data*/) => {
  105. // Get a list of the domain names and check each of them against existing records
  106. let domain_name_check_promises = [];
  107. if (typeof data.domain_names !== 'undefined') {
  108. data.domain_names.map(function (domain_name) {
  109. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name, 'redirection', data.id));
  110. });
  111. return Promise.all(domain_name_check_promises)
  112. .then((check_results) => {
  113. check_results.map(function (result) {
  114. if (result.is_taken) {
  115. throw new error.ValidationError(result.hostname + ' is already in use');
  116. }
  117. });
  118. });
  119. }
  120. })
  121. .then(() => {
  122. return internalRedirectionHost.get(access, {id: data.id});
  123. })
  124. .then((row) => {
  125. if (row.id !== data.id) {
  126. // Sanity check that something crazy hasn't happened
  127. throw new error.InternalValidationError('Redirection Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  128. }
  129. if (create_certificate) {
  130. return internalCertificate.createQuickCertificate(access, {
  131. domain_names: data.domain_names || row.domain_names,
  132. meta: _.assign({}, row.meta, data.meta)
  133. })
  134. .then((cert) => {
  135. // update host with cert id
  136. data.certificate_id = cert.id;
  137. })
  138. .then(() => {
  139. return row;
  140. });
  141. } else {
  142. return row;
  143. }
  144. })
  145. .then((row) => {
  146. // Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
  147. data = _.assign({}, {
  148. domain_names: row.domain_names
  149. }, data);
  150. data = internalHost.cleanSslHstsData(data, row);
  151. return redirectionHostModel
  152. .query()
  153. .where({id: data.id})
  154. .patch(data)
  155. .then((saved_row) => {
  156. // Add to audit log
  157. return internalAuditLog.add(access, {
  158. action: 'updated',
  159. object_type: 'redirection-host',
  160. object_id: row.id,
  161. meta: data
  162. })
  163. .then(() => {
  164. return _.omit(saved_row, omissions());
  165. });
  166. });
  167. })
  168. .then(() => {
  169. return internalRedirectionHost.get(access, {
  170. id: data.id,
  171. expand: ['owner', 'certificate']
  172. })
  173. .then((row) => {
  174. // Configure nginx
  175. return internalNginx.configure(redirectionHostModel, 'redirection_host', row)
  176. .then((new_meta) => {
  177. row.meta = new_meta;
  178. row = internalHost.cleanRowCertificateMeta(row);
  179. return _.omit(row, omissions());
  180. });
  181. });
  182. });
  183. },
  184. /**
  185. * @param {Access} access
  186. * @param {Object} data
  187. * @param {Number} data.id
  188. * @param {Array} [data.expand]
  189. * @param {Array} [data.omit]
  190. * @return {Promise}
  191. */
  192. get: (access, data) => {
  193. if (typeof data === 'undefined') {
  194. data = {};
  195. }
  196. return access.can('redirection_hosts:get', data.id)
  197. .then((access_data) => {
  198. let query = redirectionHostModel
  199. .query()
  200. .where('is_deleted', 0)
  201. .andWhere('id', data.id)
  202. .allowGraph('[owner,certificate]')
  203. .first();
  204. if (access_data.permission_visibility !== 'all') {
  205. query.andWhere('owner_user_id', access.token.getUserId(1));
  206. }
  207. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  208. query.withGraphFetched('[' + data.expand.join(', ') + ']');
  209. }
  210. return query.then(utils.omitRow(omissions()));
  211. })
  212. .then((row) => {
  213. if (!row) {
  214. throw new error.ItemNotFoundError(data.id);
  215. }
  216. row = internalHost.cleanRowCertificateMeta(row);
  217. // Custom omissions
  218. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  219. row = _.omit(row, data.omit);
  220. }
  221. return row;
  222. });
  223. },
  224. /**
  225. * @param {Access} access
  226. * @param {Object} data
  227. * @param {Number} data.id
  228. * @param {String} [data.reason]
  229. * @returns {Promise}
  230. */
  231. delete: (access, data) => {
  232. return access.can('redirection_hosts:delete', data.id)
  233. .then(() => {
  234. return internalRedirectionHost.get(access, {id: data.id});
  235. })
  236. .then((row) => {
  237. if (!row) {
  238. throw new error.ItemNotFoundError(data.id);
  239. }
  240. return redirectionHostModel
  241. .query()
  242. .where('id', row.id)
  243. .patch({
  244. is_deleted: 1
  245. })
  246. .then(() => {
  247. // Delete Nginx Config
  248. return internalNginx.deleteConfig('redirection_host', row)
  249. .then(() => {
  250. return internalNginx.reload();
  251. });
  252. })
  253. .then(() => {
  254. // Add to audit log
  255. return internalAuditLog.add(access, {
  256. action: 'deleted',
  257. object_type: 'redirection-host',
  258. object_id: row.id,
  259. meta: _.omit(row, omissions())
  260. });
  261. });
  262. })
  263. .then(() => {
  264. return true;
  265. });
  266. },
  267. /**
  268. * @param {Access} access
  269. * @param {Object} data
  270. * @param {Number} data.id
  271. * @param {String} [data.reason]
  272. * @returns {Promise}
  273. */
  274. enable: (access, data) => {
  275. return access.can('redirection_hosts:update', data.id)
  276. .then(() => {
  277. return internalRedirectionHost.get(access, {
  278. id: data.id,
  279. expand: ['certificate', 'owner']
  280. });
  281. })
  282. .then((row) => {
  283. if (!row) {
  284. throw new error.ItemNotFoundError(data.id);
  285. } else if (row.enabled) {
  286. throw new error.ValidationError('Host is already enabled');
  287. }
  288. row.enabled = 1;
  289. return redirectionHostModel
  290. .query()
  291. .where('id', row.id)
  292. .patch({
  293. enabled: 1
  294. })
  295. .then(() => {
  296. // Configure nginx
  297. return internalNginx.configure(redirectionHostModel, 'redirection_host', row);
  298. })
  299. .then(() => {
  300. // Add to audit log
  301. return internalAuditLog.add(access, {
  302. action: 'enabled',
  303. object_type: 'redirection-host',
  304. object_id: row.id,
  305. meta: _.omit(row, omissions())
  306. });
  307. });
  308. })
  309. .then(() => {
  310. return true;
  311. });
  312. },
  313. /**
  314. * @param {Access} access
  315. * @param {Object} data
  316. * @param {Number} data.id
  317. * @param {String} [data.reason]
  318. * @returns {Promise}
  319. */
  320. disable: (access, data) => {
  321. return access.can('redirection_hosts:update', data.id)
  322. .then(() => {
  323. return internalRedirectionHost.get(access, {id: data.id});
  324. })
  325. .then((row) => {
  326. if (!row) {
  327. throw new error.ItemNotFoundError(data.id);
  328. } else if (!row.enabled) {
  329. throw new error.ValidationError('Host is already disabled');
  330. }
  331. row.enabled = 0;
  332. return redirectionHostModel
  333. .query()
  334. .where('id', row.id)
  335. .patch({
  336. enabled: 0
  337. })
  338. .then(() => {
  339. // Delete Nginx Config
  340. return internalNginx.deleteConfig('redirection_host', row)
  341. .then(() => {
  342. return internalNginx.reload();
  343. });
  344. })
  345. .then(() => {
  346. // Add to audit log
  347. return internalAuditLog.add(access, {
  348. action: 'disabled',
  349. object_type: 'redirection-host',
  350. object_id: row.id,
  351. meta: _.omit(row, omissions())
  352. });
  353. });
  354. })
  355. .then(() => {
  356. return true;
  357. });
  358. },
  359. /**
  360. * All Hosts
  361. *
  362. * @param {Access} access
  363. * @param {Array} [expand]
  364. * @param {String} [search_query]
  365. * @returns {Promise}
  366. */
  367. getAll: (access, expand, search_query) => {
  368. return access.can('redirection_hosts:list')
  369. .then((access_data) => {
  370. let query = redirectionHostModel
  371. .query()
  372. .where('is_deleted', 0)
  373. .groupBy('id')
  374. .allowGraph('[owner,certificate]')
  375. .orderBy('domain_names', 'ASC');
  376. if (access_data.permission_visibility !== 'all') {
  377. query.andWhere('owner_user_id', access.token.getUserId(1));
  378. }
  379. // Query is used for searching
  380. if (typeof search_query === 'string') {
  381. query.where(function () {
  382. this.where('domain_names', 'like', '%' + search_query + '%');
  383. });
  384. }
  385. if (typeof expand !== 'undefined' && expand !== null) {
  386. query.withGraphFetched('[' + expand.join(', ') + ']');
  387. }
  388. return query.then(utils.omitRows(omissions()));
  389. })
  390. .then((rows) => {
  391. if (typeof expand !== 'undefined' && expand !== null && expand.indexOf('certificate') !== -1) {
  392. return internalHost.cleanAllRowsCertificateMeta(rows);
  393. }
  394. return rows;
  395. });
  396. },
  397. /**
  398. * Report use
  399. *
  400. * @param {Number} user_id
  401. * @param {String} visibility
  402. * @returns {Promise}
  403. */
  404. getCount: (user_id, visibility) => {
  405. let query = redirectionHostModel
  406. .query()
  407. .count('id as count')
  408. .where('is_deleted', 0);
  409. if (visibility !== 'all') {
  410. query.andWhere('owner_user_id', user_id);
  411. }
  412. return query.first()
  413. .then((row) => {
  414. return parseInt(row.count, 10);
  415. });
  416. }
  417. };
  418. module.exports = internalRedirectionHost;