redirection-host.js 12 KB

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