user.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. import gravatar from "gravatar";
  2. import _ from "lodash";
  3. import errs from "../lib/error.js";
  4. import utils from "../lib/utils.js";
  5. import authModel from "../models/auth.js";
  6. import userModel from "../models/user.js";
  7. import userPermissionModel from "../models/user_permission.js";
  8. import internalAuditLog from "./audit-log.js";
  9. import internalToken from "./token.js";
  10. const omissions = () => {
  11. return ["is_deleted"];
  12. }
  13. const DEFAULT_AVATAR = 'https://gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=200&d=mp&r=g';
  14. const internalUser = {
  15. /**
  16. * @param {Access} access
  17. * @param {Object} data
  18. * @returns {Promise}
  19. */
  20. create: (access, data) => {
  21. const auth = data.auth || null;
  22. delete data.auth;
  23. data.avatar = data.avatar || "";
  24. data.roles = data.roles || [];
  25. if (typeof data.is_disabled !== "undefined") {
  26. data.is_disabled = data.is_disabled ? 1 : 0;
  27. }
  28. return access
  29. .can("users:create", data)
  30. .then(() => {
  31. data.avatar = gravatar.url(data.email, { default: "mm" });
  32. return userModel.query().insertAndFetch(data).then(utils.omitRow(omissions()));
  33. })
  34. .then((user) => {
  35. if (auth) {
  36. return authModel
  37. .query()
  38. .insert({
  39. user_id: user.id,
  40. type: auth.type,
  41. secret: auth.secret,
  42. meta: {},
  43. })
  44. .then(() => {
  45. return user;
  46. });
  47. }
  48. return user;
  49. })
  50. .then((user) => {
  51. // Create permissions row as well
  52. const is_admin = data.roles.indexOf("admin") !== -1;
  53. return userPermissionModel
  54. .query()
  55. .insert({
  56. user_id: user.id,
  57. visibility: is_admin ? "all" : "user",
  58. proxy_hosts: "manage",
  59. redirection_hosts: "manage",
  60. dead_hosts: "manage",
  61. streams: "manage",
  62. access_lists: "manage",
  63. certificates: "manage",
  64. })
  65. .then(() => {
  66. return internalUser.get(access, { id: user.id, expand: ["permissions"] });
  67. });
  68. })
  69. .then((user) => {
  70. // Add to audit log
  71. return internalAuditLog
  72. .add(access, {
  73. action: "created",
  74. object_type: "user",
  75. object_id: user.id,
  76. meta: user,
  77. })
  78. .then(() => {
  79. return user;
  80. });
  81. });
  82. },
  83. /**
  84. * @param {Access} access
  85. * @param {Object} data
  86. * @param {Integer} data.id
  87. * @param {String} [data.email]
  88. * @param {String} [data.name]
  89. * @return {Promise}
  90. */
  91. update: (access, data) => {
  92. if (typeof data.is_disabled !== "undefined") {
  93. data.is_disabled = data.is_disabled ? 1 : 0;
  94. }
  95. return access
  96. .can("users:update", data.id)
  97. .then(() => {
  98. // Make sure that the user being updated doesn't change their email to another user that is already using it
  99. // 1. get user we want to update
  100. return internalUser.get(access, { id: data.id }).then((user) => {
  101. // 2. if email is to be changed, find other users with that email
  102. if (typeof data.email !== "undefined") {
  103. data.email = data.email.toLowerCase().trim();
  104. if (user.email !== data.email) {
  105. return internalUser.isEmailAvailable(data.email, data.id).then((available) => {
  106. if (!available) {
  107. throw new errs.ValidationError(`Email address already in use - ${data.email}`);
  108. }
  109. return user;
  110. });
  111. }
  112. }
  113. // No change to email:
  114. return user;
  115. });
  116. })
  117. .then((user) => {
  118. if (user.id !== data.id) {
  119. // Sanity check that something crazy hasn't happened
  120. throw new errs.InternalValidationError(
  121. `User could not be updated, IDs do not match: ${user.id} !== ${data.id}`,
  122. );
  123. }
  124. data.avatar = gravatar.url(data.email || user.email, { default: "mm" });
  125. return userModel.query().patchAndFetchById(user.id, data).then(utils.omitRow(omissions()));
  126. })
  127. .then(() => {
  128. return internalUser.get(access, { id: data.id });
  129. })
  130. .then((user) => {
  131. // Add to audit log
  132. return internalAuditLog
  133. .add(access, {
  134. action: "updated",
  135. object_type: "user",
  136. object_id: user.id,
  137. meta: data,
  138. })
  139. .then(() => {
  140. return user;
  141. });
  142. });
  143. },
  144. /**
  145. * @param {Access} access
  146. * @param {Object} [data]
  147. * @param {Integer} [data.id] Defaults to the token user
  148. * @param {Array} [data.expand]
  149. * @param {Array} [data.omit]
  150. * @return {Promise}
  151. */
  152. get: (access, data) => {
  153. const thisData = data || {};
  154. if (typeof thisData.id === "undefined" || !thisData.id) {
  155. thisData.id = access.token.getUserId(0);
  156. }
  157. return access
  158. .can("users:get", thisData.id)
  159. .then(() => {
  160. const query = userModel
  161. .query()
  162. .where("is_deleted", 0)
  163. .andWhere("id", thisData.id)
  164. .allowGraph("[permissions]")
  165. .first();
  166. if (typeof thisData.expand !== "undefined" && thisData.expand !== null) {
  167. query.withGraphFetched(`[${thisData.expand.join(", ")}]`);
  168. }
  169. return query.then(utils.omitRow(omissions()));
  170. })
  171. .then((row) => {
  172. if (!row || !row.id) {
  173. throw new errs.ItemNotFoundError(thisData.id);
  174. }
  175. // Custom omissions
  176. if (typeof thisData.omit !== "undefined" && thisData.omit !== null) {
  177. return _.omit(row, thisData.omit);
  178. }
  179. if (row.avatar === "") {
  180. row.avatar = DEFAULT_AVATAR;
  181. }
  182. return row;
  183. });
  184. },
  185. /**
  186. * Checks if an email address is available, but if a user_id is supplied, it will ignore checking
  187. * against that user.
  188. *
  189. * @param email
  190. * @param user_id
  191. */
  192. isEmailAvailable: (email, user_id) => {
  193. const query = userModel.query().where("email", "=", email.toLowerCase().trim()).where("is_deleted", 0).first();
  194. if (typeof user_id !== "undefined") {
  195. query.where("id", "!=", user_id);
  196. }
  197. return query.then((user) => {
  198. return !user;
  199. });
  200. },
  201. /**
  202. * @param {Access} access
  203. * @param {Object} data
  204. * @param {Integer} data.id
  205. * @param {String} [data.reason]
  206. * @returns {Promise}
  207. */
  208. delete: (access, data) => {
  209. return access
  210. .can("users:delete", data.id)
  211. .then(() => {
  212. return internalUser.get(access, { id: data.id });
  213. })
  214. .then((user) => {
  215. if (!user) {
  216. throw new errs.ItemNotFoundError(data.id);
  217. }
  218. // Make sure user can't delete themselves
  219. if (user.id === access.token.getUserId(0)) {
  220. throw new errs.PermissionError("You cannot delete yourself.");
  221. }
  222. return userModel
  223. .query()
  224. .where("id", user.id)
  225. .patch({
  226. is_deleted: 1,
  227. })
  228. .then(() => {
  229. // Add to audit log
  230. return internalAuditLog.add(access, {
  231. action: "deleted",
  232. object_type: "user",
  233. object_id: user.id,
  234. meta: _.omit(user, omissions()),
  235. });
  236. });
  237. })
  238. .then(() => {
  239. return true;
  240. });
  241. },
  242. /**
  243. * This will only count the users
  244. *
  245. * @param {Access} access
  246. * @param {String} [search_query]
  247. * @returns {*}
  248. */
  249. getCount: (access, search_query) => {
  250. return access
  251. .can("users:list")
  252. .then(() => {
  253. const query = userModel.query().count("id as count").where("is_deleted", 0).first();
  254. // Query is used for searching
  255. if (typeof search_query === "string") {
  256. query.where(function () {
  257. this.where("user.name", "like", `%${search_query}%`).orWhere(
  258. "user.email",
  259. "like",
  260. `%${search_query}%`,
  261. );
  262. });
  263. }
  264. return query;
  265. })
  266. .then((row) => {
  267. return Number.parseInt(row.count, 10);
  268. });
  269. },
  270. /**
  271. * All users
  272. *
  273. * @param {Access} access
  274. * @param {Array} [expand]
  275. * @param {String} [search_query]
  276. * @returns {Promise}
  277. */
  278. getAll: async (access, expand, search_query) => {
  279. await access.can("users:list");
  280. const query = userModel
  281. .query()
  282. .where("is_deleted", 0)
  283. .groupBy("id")
  284. .allowGraph("[permissions]")
  285. .orderBy("name", "ASC");
  286. // Query is used for searching
  287. if (typeof search_query === "string") {
  288. query.where(function () {
  289. this.where("name", "like", `%${search_query}%`).orWhere(
  290. "email",
  291. "like",
  292. `%${search_query}%`,
  293. );
  294. });
  295. }
  296. if (typeof expand !== "undefined" && expand !== null) {
  297. query.withGraphFetched(`[${expand.join(", ")}]`);
  298. }
  299. const res = await query;
  300. return utils.omitRows(omissions())(res);
  301. },
  302. /**
  303. * @param {Access} access
  304. * @param {Integer} [id_requested]
  305. * @returns {[String]}
  306. */
  307. getUserOmisionsByAccess: (access, idRequested) => {
  308. let response = []; // Admin response
  309. if (!access.token.hasScope("admin") && access.token.getUserId(0) !== idRequested) {
  310. response = ["is_deleted"]; // Restricted response
  311. }
  312. return response;
  313. },
  314. /**
  315. * @param {Access} access
  316. * @param {Object} data
  317. * @param {Integer} data.id
  318. * @param {String} data.type
  319. * @param {String} data.secret
  320. * @return {Promise}
  321. */
  322. setPassword: (access, data) => {
  323. return access
  324. .can("users:password", data.id)
  325. .then(() => {
  326. return internalUser.get(access, { id: data.id });
  327. })
  328. .then((user) => {
  329. if (user.id !== data.id) {
  330. // Sanity check that something crazy hasn't happened
  331. throw new errs.InternalValidationError(
  332. `User could not be updated, IDs do not match: ${user.id} !== ${data.id}`,
  333. );
  334. }
  335. if (user.id === access.token.getUserId(0)) {
  336. // they're setting their own password. Make sure their current password is correct
  337. if (typeof data.current === "undefined" || !data.current) {
  338. throw new errs.ValidationError("Current password was not supplied");
  339. }
  340. return internalToken
  341. .getTokenFromEmail({
  342. identity: user.email,
  343. secret: data.current,
  344. })
  345. .then(() => {
  346. return user;
  347. });
  348. }
  349. return user;
  350. })
  351. .then((user) => {
  352. // Get auth, patch if it exists
  353. return authModel
  354. .query()
  355. .where("user_id", user.id)
  356. .andWhere("type", data.type)
  357. .first()
  358. .then((existing_auth) => {
  359. if (existing_auth) {
  360. // patch
  361. return authModel.query().where("user_id", user.id).andWhere("type", data.type).patch({
  362. type: data.type, // This is required for the model to encrypt on save
  363. secret: data.secret,
  364. });
  365. }
  366. // insert
  367. return authModel.query().insert({
  368. user_id: user.id,
  369. type: data.type,
  370. secret: data.secret,
  371. meta: {},
  372. });
  373. })
  374. .then(() => {
  375. // Add to Audit Log
  376. return internalAuditLog.add(access, {
  377. action: "updated",
  378. object_type: "user",
  379. object_id: user.id,
  380. meta: {
  381. name: user.name,
  382. password_changed: true,
  383. auth_type: data.type,
  384. },
  385. });
  386. });
  387. })
  388. .then(() => {
  389. return true;
  390. });
  391. },
  392. /**
  393. * @param {Access} access
  394. * @param {Object} data
  395. * @return {Promise}
  396. */
  397. setPermissions: (access, data) => {
  398. return access
  399. .can("users:permissions", data.id)
  400. .then(() => {
  401. return internalUser.get(access, { id: data.id });
  402. })
  403. .then((user) => {
  404. if (user.id !== data.id) {
  405. // Sanity check that something crazy hasn't happened
  406. throw new errs.InternalValidationError(
  407. `User could not be updated, IDs do not match: ${user.id} !== ${data.id}`,
  408. );
  409. }
  410. return user;
  411. })
  412. .then((user) => {
  413. // Get perms row, patch if it exists
  414. return userPermissionModel
  415. .query()
  416. .where("user_id", user.id)
  417. .first()
  418. .then((existing_auth) => {
  419. if (existing_auth) {
  420. // patch
  421. return userPermissionModel
  422. .query()
  423. .where("user_id", user.id)
  424. .patchAndFetchById(existing_auth.id, _.assign({ user_id: user.id }, data));
  425. }
  426. // insert
  427. return userPermissionModel.query().insertAndFetch(_.assign({ user_id: user.id }, data));
  428. })
  429. .then((permissions) => {
  430. // Add to Audit Log
  431. return internalAuditLog.add(access, {
  432. action: "updated",
  433. object_type: "user",
  434. object_id: user.id,
  435. meta: {
  436. name: user.name,
  437. permissions: permissions,
  438. },
  439. });
  440. });
  441. })
  442. .then(() => {
  443. return true;
  444. });
  445. },
  446. /**
  447. * @param {Access} access
  448. * @param {Object} data
  449. * @param {Integer} data.id
  450. */
  451. loginAs: (access, data) => {
  452. return access
  453. .can("users:loginas", data.id)
  454. .then(() => {
  455. return internalUser.get(access, data);
  456. })
  457. .then((user) => {
  458. return internalToken.getTokenFromUser(user);
  459. });
  460. },
  461. };
  462. export default internalUser;