users.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  1. import config from "config";
  2. import async from "async";
  3. import request from "request";
  4. import bcrypt from "bcrypt";
  5. import sha256 from "sha256";
  6. import { isAdminRequired, isLoginRequired } from "./hooks";
  7. import moduleManager from "../../index";
  8. const DBModule = moduleManager.modules.db;
  9. const UtilsModule = moduleManager.modules.utils;
  10. const IOModule = moduleManager.modules.io;
  11. const CacheModule = moduleManager.modules.cache;
  12. const MailModule = moduleManager.modules.mail;
  13. const PunishmentsModule = moduleManager.modules.punishments;
  14. const ActivitiesModule = moduleManager.modules.activities;
  15. const PlaylistsModule = moduleManager.modules.playlists;
  16. CacheModule.runJob("SUB", {
  17. channel: "user.updatePreferences",
  18. cb: res => {
  19. IOModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }).then(response => {
  20. response.sockets.forEach(socket => {
  21. socket.emit("keep.event:user.preferences.changed", res.preferences);
  22. });
  23. });
  24. }
  25. });
  26. CacheModule.runJob("SUB", {
  27. channel: "user.updateUsername",
  28. cb: user => {
  29. IOModule.runJob("SOCKETS_FROM_USER", { userId: user._id }).then(response => {
  30. response.sockets.forEach(socket => {
  31. socket.emit("event:user.username.changed", user.username);
  32. });
  33. });
  34. }
  35. });
  36. CacheModule.runJob("SUB", {
  37. channel: "user.removeSessions",
  38. cb: userId => {
  39. IOModule.runJob("SOCKETS_FROM_USER_WITHOUT_CACHE", { userId }).then(response => {
  40. response.sockets.forEach(socket => {
  41. socket.emit("keep.event:user.session.removed");
  42. });
  43. });
  44. }
  45. });
  46. CacheModule.runJob("SUB", {
  47. channel: "user.linkPassword",
  48. cb: userId => {
  49. IOModule.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
  50. response.sockets.forEach(socket => {
  51. socket.emit("event:user.linkPassword");
  52. });
  53. });
  54. }
  55. });
  56. CacheModule.runJob("SUB", {
  57. channel: "user.unlinkPassword",
  58. cb: userId => {
  59. IOModule.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
  60. response.sockets.forEach(socket => {
  61. socket.emit("event:user.unlinkPassword");
  62. });
  63. });
  64. }
  65. });
  66. CacheModule.runJob("SUB", {
  67. channel: "user.linkGithub",
  68. cb: userId => {
  69. IOModule.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
  70. response.sockets.forEach(socket => {
  71. socket.emit("event:user.linkGithub");
  72. });
  73. });
  74. }
  75. });
  76. CacheModule.runJob("SUB", {
  77. channel: "user.unlinkGithub",
  78. cb: userId => {
  79. IOModule.runJob("SOCKETS_FROM_USER", { userId }).then(response => {
  80. response.sockets.forEach(socket => {
  81. socket.emit("event:user.unlinkGithub");
  82. });
  83. });
  84. }
  85. });
  86. CacheModule.runJob("SUB", {
  87. channel: "user.ban",
  88. cb: data => {
  89. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  90. response.sockets.forEach(socket => {
  91. socket.emit("keep.event:banned", data.punishment);
  92. socket.disconnect(true);
  93. });
  94. });
  95. }
  96. });
  97. CacheModule.runJob("SUB", {
  98. channel: "user.favoritedStation",
  99. cb: data => {
  100. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  101. response.sockets.forEach(socket => {
  102. socket.emit("event:user.favoritedStation", data.stationId);
  103. });
  104. });
  105. }
  106. });
  107. CacheModule.runJob("SUB", {
  108. channel: "user.unfavoritedStation",
  109. cb: data => {
  110. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  111. response.sockets.forEach(socket => {
  112. socket.emit("event:user.unfavoritedStation", data.stationId);
  113. });
  114. });
  115. }
  116. });
  117. export default {
  118. /**
  119. * Lists all Users
  120. *
  121. * @param {object} session - the session object automatically added by socket.io
  122. * @param {Function} cb - gets called with the result
  123. */
  124. index: isAdminRequired(async function index(session, cb) {
  125. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  126. async.waterfall(
  127. [
  128. next => {
  129. userModel.find({}).exec(next);
  130. }
  131. ],
  132. async (err, users) => {
  133. if (err) {
  134. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  135. this.log("ERROR", "USER_INDEX", `Indexing users failed. "${err}"`);
  136. return cb({ status: "failure", message: err });
  137. }
  138. this.log("SUCCESS", "USER_INDEX", `Indexing users successful.`);
  139. const filteredUsers = [];
  140. users.forEach(user => {
  141. filteredUsers.push({
  142. _id: user._id,
  143. username: user.username,
  144. role: user.role,
  145. liked: user.liked,
  146. disliked: user.disliked,
  147. songsRequested: user.statistics.songsRequested,
  148. email: {
  149. address: user.email.address,
  150. verified: user.email.verified
  151. },
  152. hasPassword: !!user.services.password,
  153. services: { github: user.services.github }
  154. });
  155. });
  156. return cb({ status: "success", data: filteredUsers });
  157. }
  158. );
  159. }),
  160. /**
  161. * Logs user in
  162. *
  163. * @param {object} session - the session object automatically added by socket.io
  164. * @param {string} identifier - the email of the user
  165. * @param {string} password - the plaintext of the user
  166. * @param {Function} cb - gets called with the result
  167. */
  168. async login(session, identifier, password, cb) {
  169. identifier = identifier.toLowerCase();
  170. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  171. const sessionSchema = await CacheModule.runJob(
  172. "GET_SCHEMA",
  173. {
  174. schemaName: "session"
  175. },
  176. this
  177. );
  178. async.waterfall(
  179. [
  180. // check if a user with the requested identifier exists
  181. next => {
  182. userModel.findOne(
  183. {
  184. $or: [{ "email.address": identifier }]
  185. },
  186. next
  187. );
  188. },
  189. // if the user doesn't exist, respond with a failure
  190. // otherwise compare the requested password and the actual users password
  191. (user, next) => {
  192. if (!user) return next("User not found");
  193. if (!user.services.password || !user.services.password.password)
  194. return next("The account you are trying to access uses GitHub to log in.");
  195. return bcrypt.compare(sha256(password), user.services.password.password, (err, match) => {
  196. if (err) return next(err);
  197. if (!match) return next("Incorrect password");
  198. return next(null, user);
  199. });
  200. },
  201. (user, next) => {
  202. UtilsModule.runJob("GUID", {}, this).then(sessionId => {
  203. next(null, user, sessionId);
  204. });
  205. },
  206. (user, sessionId, next) => {
  207. CacheModule.runJob(
  208. "HSET",
  209. {
  210. table: "sessions",
  211. key: sessionId,
  212. value: sessionSchema(sessionId, user._id)
  213. },
  214. this
  215. )
  216. .then(() => {
  217. next(null, sessionId);
  218. })
  219. .catch(next);
  220. }
  221. ],
  222. async (err, sessionId) => {
  223. if (err && err !== true) {
  224. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  225. this.log(
  226. "ERROR",
  227. "USER_PASSWORD_LOGIN",
  228. `Login failed with password for user "${identifier}". "${err}"`
  229. );
  230. return cb({ status: "failure", message: err });
  231. }
  232. this.log("SUCCESS", "USER_PASSWORD_LOGIN", `Login successful with password for user "${identifier}"`);
  233. return cb({
  234. status: "success",
  235. message: "Login successful",
  236. user: {},
  237. SID: sessionId
  238. });
  239. }
  240. );
  241. },
  242. /**
  243. * Registers a new user
  244. *
  245. * @param {object} session - the session object automatically added by socket.io
  246. * @param {string} username - the username for the new user
  247. * @param {string} email - the email for the new user
  248. * @param {string} password - the plaintext password for the new user
  249. * @param {object} recaptcha - the recaptcha data
  250. * @param {Function} cb - gets called with the result
  251. */
  252. async register(session, username, email, password, recaptcha, cb) {
  253. email = email.toLowerCase();
  254. const verificationToken = await UtilsModule.runJob(
  255. "GENERATE_RANDOM_STRING",
  256. {
  257. length: 64
  258. },
  259. this
  260. );
  261. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  262. const verifyEmailSchema = await MailModule.runJob(
  263. "GET_SCHEMA",
  264. {
  265. schemaName: "verifyEmail"
  266. },
  267. this
  268. );
  269. async.waterfall(
  270. [
  271. next => {
  272. if (config.get("registrationDisabled") === true)
  273. return next("Registration is not allowed at this time.");
  274. return next();
  275. },
  276. next => {
  277. if (!DBModule.passwordValid(password))
  278. return next("Invalid password. Check if it meets all the requirements.");
  279. return next();
  280. },
  281. // verify the request with google recaptcha
  282. next => {
  283. if (config.get("apis.recaptcha.enabled") === true)
  284. request(
  285. {
  286. url: "https://www.google.com/recaptcha/api/siteverify",
  287. method: "POST",
  288. form: {
  289. secret: config.get("apis").recaptcha.secret,
  290. response: recaptcha
  291. }
  292. },
  293. next
  294. );
  295. else next(null, null, null);
  296. },
  297. // check if the response from Google recaptcha is successful
  298. // if it is, we check if a user with the requested username already exists
  299. (response, body, next) => {
  300. if (config.get("apis.recaptcha.enabled") === true) {
  301. const json = JSON.parse(body);
  302. if (json.success !== true) return next("Response from recaptcha was not successful.");
  303. }
  304. return userModel.findOne({ username: new RegExp(`^${username}$`, "i") }, next);
  305. },
  306. // if the user already exists, respond with that
  307. // otherwise check if a user with the requested email already exists
  308. (user, next) => {
  309. if (user) return next("A user with that username already exists.");
  310. return userModel.findOne({ "email.address": email }, next);
  311. },
  312. // if the user already exists, respond with that
  313. // otherwise, generate a salt to use with hashing the new users password
  314. (user, next) => {
  315. if (user) return next("A user with that email already exists.");
  316. return bcrypt.genSalt(10, next);
  317. },
  318. // hash the password
  319. (salt, next) => {
  320. bcrypt.hash(sha256(password), salt, next);
  321. },
  322. (hash, next) => {
  323. UtilsModule.runJob("GENERATE_RANDOM_STRING", { length: 12 }, this).then(_id => {
  324. next(null, hash, _id);
  325. });
  326. },
  327. // create the user object
  328. (hash, _id, next) => {
  329. next(null, {
  330. _id,
  331. username,
  332. email: {
  333. address: email,
  334. verificationToken
  335. },
  336. services: {
  337. password: {
  338. password: hash
  339. }
  340. }
  341. });
  342. },
  343. // generate the url for gravatar avatar
  344. (user, next) => {
  345. UtilsModule.runJob(
  346. "CREATE_GRAVATAR",
  347. {
  348. email: user.email.address
  349. },
  350. this
  351. ).then(url => {
  352. user.avatar = {
  353. type: "gravatar",
  354. url
  355. };
  356. next(null, user);
  357. });
  358. },
  359. // save the new user to the database
  360. (user, next) => {
  361. userModel.create(user, next);
  362. },
  363. // respond with the new user
  364. (user, next) => {
  365. verifyEmailSchema(email, username, verificationToken, err => {
  366. next(err, user._id);
  367. });
  368. },
  369. // create a liked songs playlist for the new user
  370. (userId, next) => {
  371. PlaylistsModule.runJob("CREATE_READ_ONLY_PLAYLIST", {
  372. userId,
  373. displayName: "Liked Songs"
  374. })
  375. .then(likedSongsPlaylist => {
  376. next(null, likedSongsPlaylist, userId);
  377. })
  378. .catch(err => next(err));
  379. },
  380. // create a disliked songs playlist for the new user
  381. (likedSongsPlaylist, userId, next) => {
  382. PlaylistsModule.runJob("CREATE_READ_ONLY_PLAYLIST", {
  383. userId,
  384. displayName: "Disliked Songs"
  385. })
  386. .then(dislikedSongsPlaylist => {
  387. next(null, { likedSongsPlaylist, dislikedSongsPlaylist }, userId);
  388. })
  389. .catch(err => next(err));
  390. },
  391. // associate liked + disliked songs playlist to the user object
  392. ({ likedSongsPlaylist, dislikedSongsPlaylist }, userId, next) => {
  393. userModel.updateOne(
  394. { _id: userId },
  395. { $set: { likedSongsPlaylist, dislikedSongsPlaylist } },
  396. { runValidators: true },
  397. err => {
  398. if (err) return next(err);
  399. return next(null, userId);
  400. }
  401. );
  402. }
  403. ],
  404. async (err, user) => {
  405. if (err && err !== true) {
  406. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  407. this.log(
  408. "ERROR",
  409. "USER_PASSWORD_REGISTER",
  410. `Register failed with password for user "${username}"."${err}"`
  411. );
  412. return cb({ status: "failure", message: err });
  413. }
  414. ActivitiesModule.runJob("ADD_ACTIVITY", {
  415. userId: user._id,
  416. activityType: "created_account"
  417. });
  418. this.log(
  419. "SUCCESS",
  420. "USER_PASSWORD_REGISTER",
  421. `Register successful with password for user "${username}".`
  422. );
  423. const result = await this.module.runJob(
  424. "RUN_ACTION2",
  425. {
  426. session,
  427. namespace: "users",
  428. action: "login",
  429. args: [email, password]
  430. },
  431. this
  432. );
  433. const obj = {
  434. status: "success",
  435. message: "Successfully registered."
  436. };
  437. if (result.status === "success") {
  438. obj.SID = result.SID;
  439. }
  440. return cb(obj);
  441. }
  442. );
  443. },
  444. /**
  445. * Logs out a user
  446. *
  447. * @param {object} session - the session object automatically added by socket.io
  448. * @param {Function} cb - gets called with the result
  449. */
  450. logout(session, cb) {
  451. async.waterfall(
  452. [
  453. next => {
  454. CacheModule.runJob(
  455. "HGET",
  456. {
  457. table: "sessions",
  458. key: session.sessionId
  459. },
  460. this
  461. )
  462. .then(session => {
  463. next(null, session);
  464. })
  465. .catch(next);
  466. },
  467. (session, next) => {
  468. if (!session) return next("Session not found");
  469. return next(null, session);
  470. },
  471. (session, next) => {
  472. CacheModule.runJob(
  473. "HDEL",
  474. {
  475. table: "sessions",
  476. key: session.sessionId
  477. },
  478. this
  479. )
  480. .then(() => {
  481. next();
  482. })
  483. .catch(next);
  484. }
  485. ],
  486. async err => {
  487. if (err && err !== true) {
  488. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  489. this.log("ERROR", "USER_LOGOUT", `Logout failed. "${err}" `);
  490. cb({ status: "failure", message: err });
  491. } else {
  492. this.log("SUCCESS", "USER_LOGOUT", `Logout successful.`);
  493. cb({
  494. status: "success",
  495. message: "Successfully logged out."
  496. });
  497. }
  498. }
  499. );
  500. },
  501. /**
  502. * Removes all sessions for a user
  503. *
  504. * @param {object} session - the session object automatically added by socket.io
  505. * @param {string} userId - the id of the user we are trying to delete the sessions of
  506. * @param {Function} cb - gets called with the result
  507. */
  508. removeSessions: isLoginRequired(async function removeSessions(session, userId, cb) {
  509. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  510. async.waterfall(
  511. [
  512. next => {
  513. userModel.findOne({ _id: session.userId }, (err, user) => {
  514. if (err) return next(err);
  515. if (user.role !== "admin" && session.userId !== userId)
  516. return next("Only admins and the owner of the account can remove their sessions.");
  517. return next();
  518. });
  519. },
  520. next => {
  521. CacheModule.runJob("HGETALL", { table: "sessions" }, this)
  522. .then(sessions => {
  523. next(null, sessions);
  524. })
  525. .catch(next);
  526. },
  527. (sessions, next) => {
  528. if (!sessions) return next("There are no sessions for this user to remove.");
  529. const keys = Object.keys(sessions);
  530. return next(null, keys, sessions);
  531. },
  532. (keys, sessions, next) => {
  533. CacheModule.runJob("PUB", {
  534. channel: "user.removeSessions",
  535. value: userId
  536. });
  537. async.each(
  538. keys,
  539. (sessionId, callback) => {
  540. const session = sessions[sessionId];
  541. if (session.userId === userId) {
  542. // TODO Also maybe add this to this runJob
  543. CacheModule.runJob("HDEL", {
  544. channel: "sessions",
  545. key: sessionId
  546. })
  547. .then(() => {
  548. callback(null);
  549. })
  550. .catch(next);
  551. }
  552. },
  553. err => {
  554. next(err);
  555. }
  556. );
  557. }
  558. ],
  559. async err => {
  560. if (err) {
  561. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  562. this.log(
  563. "ERROR",
  564. "REMOVE_SESSIONS_FOR_USER",
  565. `Couldn't remove all sessions for user "${userId}". "${err}"`
  566. );
  567. return cb({ status: "failure", message: err });
  568. }
  569. this.log("SUCCESS", "REMOVE_SESSIONS_FOR_USER", `Removed all sessions for user "${userId}".`);
  570. return cb({
  571. status: "success",
  572. message: "Successfully removed all sessions."
  573. });
  574. }
  575. );
  576. }),
  577. /**
  578. * Updates the order of a user's playlists
  579. *
  580. * @param {object} session - the session object automatically added by socket.io
  581. * @param {Array} orderOfPlaylists - array of playlist ids (with a specific order)
  582. * @param {Function} cb - gets called with the result
  583. */
  584. updateOrderOfPlaylists: isLoginRequired(async function updateOrderOfPlaylists(session, orderOfPlaylists, cb) {
  585. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  586. async.waterfall(
  587. [
  588. next => {
  589. userModel.updateOne(
  590. { _id: session.userId },
  591. { $set: { "preferences.orderOfPlaylists": orderOfPlaylists } },
  592. { runValidators: true },
  593. next
  594. );
  595. }
  596. ],
  597. async err => {
  598. if (err) {
  599. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  600. this.log(
  601. "ERROR",
  602. "UPDATE_ORDER_OF_USER_PLAYLISTS",
  603. `Couldn't update order of playlists for user "${session.userId}" to "${orderOfPlaylists}". "${err}"`
  604. );
  605. return cb({ status: "failure", message: err });
  606. }
  607. this.log(
  608. "SUCCESS",
  609. "UPDATE_ORDER_OF_USER_PLAYLISTS",
  610. `Updated order of playlists for user "${session.userId}" to "${orderOfPlaylists}".`
  611. );
  612. return cb({
  613. status: "success",
  614. message: "Order of playlists successfully updated"
  615. });
  616. }
  617. );
  618. }),
  619. /**
  620. * Updates a user's preferences
  621. *
  622. * @param {object} session - the session object automatically added by socket.io
  623. * @param {object} preferences - object containing preferences
  624. * @param {boolean} preferences.nightmode - whether or not the user is using the night mode theme
  625. * @param {boolean} preferences.autoSkipDisliked - whether to automatically skip disliked songs
  626. * @param {Function} cb - gets called with the result
  627. */
  628. updatePreferences: isLoginRequired(async function updatePreferences(session, preferences, cb) {
  629. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  630. async.waterfall(
  631. [
  632. next => {
  633. userModel.updateOne(
  634. { _id: session.userId },
  635. {
  636. $set: {
  637. preferences: {
  638. nightmode: preferences.nightmode,
  639. autoSkipDisliked: preferences.autoSkipDisliked
  640. }
  641. }
  642. },
  643. { runValidators: true },
  644. next
  645. );
  646. }
  647. ],
  648. async err => {
  649. if (err) {
  650. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  651. this.log(
  652. "ERROR",
  653. "UPDATE_USER_PREFERENCES",
  654. `Couldn't update preferences for user "${session.userId}" to "${JSON.stringify(
  655. preferences
  656. )}". "${err}"`
  657. );
  658. return cb({ status: "failure", message: err });
  659. }
  660. CacheModule.runJob("PUB", {
  661. channel: "user.updatePreferences",
  662. value: {
  663. preferences,
  664. userId: session.userId
  665. }
  666. });
  667. this.log(
  668. "SUCCESS",
  669. "UPDATE_USER_PREFERENCES",
  670. `Updated preferences for user "${session.userId}" to "${JSON.stringify(preferences)}".`
  671. );
  672. return cb({
  673. status: "success",
  674. message: "Preferences successfully updated"
  675. });
  676. }
  677. );
  678. }),
  679. /**
  680. * Retrieves a user's preferences
  681. *
  682. * @param {object} session - the session object automatically added by socket.io
  683. * @param {Function} cb - gets called with the result
  684. */
  685. getPreferences: isLoginRequired(async function updatePreferences(session, cb) {
  686. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  687. async.waterfall(
  688. [
  689. next => {
  690. userModel.findById(session.userId).select({ preferences: -1 }).exec(next);
  691. }
  692. ],
  693. async (err, { preferences }) => {
  694. if (err) {
  695. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  696. this.log(
  697. "ERROR",
  698. "GET_USER_PREFERENCES",
  699. `Couldn't retrieve preferences for user "${session.userId}". "${err}"`
  700. );
  701. return cb({ status: "failure", message: err });
  702. }
  703. this.log(
  704. "SUCCESS",
  705. "GET_USER_PREFERENCES",
  706. `Successfully obtained preferences for user "${session.userId}".`
  707. );
  708. return cb({
  709. status: "success",
  710. message: "Preferences successfully retrieved",
  711. data: preferences
  712. });
  713. }
  714. );
  715. }),
  716. /**
  717. * Gets user object from username (only a few properties)
  718. *
  719. * @param {object} session - the session object automatically added by socket.io
  720. * @param {string} username - the username of the user we are trying to find
  721. * @param {Function} cb - gets called with the result
  722. */
  723. findByUsername: async function findByUsername(session, username, cb) {
  724. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  725. async.waterfall(
  726. [
  727. next => {
  728. userModel.findOne({ username: new RegExp(`^${username}$`, "i") }, next);
  729. },
  730. (account, next) => {
  731. if (!account) return next("User not found.");
  732. return next(null, account);
  733. }
  734. ],
  735. async (err, account) => {
  736. if (err && err !== true) {
  737. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  738. this.log("ERROR", "FIND_BY_USERNAME", `User not found for username "${username}". "${err}"`);
  739. return cb({ status: "failure", message: err });
  740. }
  741. this.log("SUCCESS", "FIND_BY_USERNAME", `User found for username "${username}".`);
  742. return cb({
  743. status: "success",
  744. data: {
  745. _id: account._id,
  746. name: account.name,
  747. username: account.username,
  748. location: account.location,
  749. bio: account.bio,
  750. role: account.role,
  751. avatar: account.avatar,
  752. createdAt: account.createdAt
  753. }
  754. });
  755. }
  756. );
  757. },
  758. /**
  759. * Gets a username from an userId
  760. *
  761. * @param {object} session - the session object automatically added by socket.io
  762. * @param {string} userId - the userId of the person we are trying to get the username from
  763. * @param {Function} cb - gets called with the result
  764. */
  765. async getUsernameFromId(session, userId, cb) {
  766. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  767. userModel
  768. .findById(userId)
  769. .then(user => {
  770. if (user) {
  771. this.log("SUCCESS", "GET_USERNAME_FROM_ID", `Found username for userId "${userId}".`);
  772. return cb({
  773. status: "success",
  774. data: user.username
  775. });
  776. }
  777. this.log(
  778. "ERROR",
  779. "GET_USERNAME_FROM_ID",
  780. `Getting the username from userId "${userId}" failed. User not found.`
  781. );
  782. return cb({
  783. status: "failure",
  784. message: "Couldn't find the user."
  785. });
  786. })
  787. .catch(async err => {
  788. if (err && err !== true) {
  789. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  790. this.log(
  791. "ERROR",
  792. "GET_USERNAME_FROM_ID",
  793. `Getting the username from userId "${userId}" failed. "${err}"`
  794. );
  795. cb({ status: "failure", message: err });
  796. }
  797. });
  798. },
  799. /**
  800. * Gets a user from a userId
  801. *
  802. * @param {object} session - the session object automatically added by socket.io
  803. * @param {string} userId - the userId of the person we are trying to get the username from
  804. * @param {Function} cb - gets called with the result
  805. */
  806. getUserFromId: isAdminRequired(async function getUserFromId(session, userId, cb) {
  807. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  808. userModel
  809. .findById(userId)
  810. .then(user => {
  811. if (user) {
  812. this.log("SUCCESS", "GET_USER_FROM_ID", `Found user for userId "${userId}".`);
  813. return cb({
  814. status: "success",
  815. data: {
  816. _id: user._id,
  817. username: user.username,
  818. role: user.role,
  819. liked: user.liked,
  820. disliked: user.disliked,
  821. songsRequested: user.statistics.songsRequested,
  822. email: {
  823. address: user.email.address,
  824. verified: user.email.verified
  825. },
  826. hasPassword: !!user.services.password,
  827. services: { github: user.services.github }
  828. }
  829. });
  830. }
  831. this.log(
  832. "ERROR",
  833. "GET_USER_FROM_ID",
  834. `Getting the user from userId "${userId}" failed. User not found.`
  835. );
  836. return cb({
  837. status: "failure",
  838. message: "Couldn't find the user."
  839. });
  840. })
  841. .catch(async err => {
  842. if (err && err !== true) {
  843. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  844. this.log("ERROR", "GET_USER_FROM_ID", `Getting the user from userId "${userId}" failed. "${err}"`);
  845. cb({ status: "failure", message: err });
  846. }
  847. });
  848. }),
  849. // TODO Fix security issues
  850. /**
  851. * Gets user info from session
  852. *
  853. * @param {object} session - the session object automatically added by socket.io
  854. * @param {Function} cb - gets called with the result
  855. */
  856. async findBySession(session, cb) {
  857. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  858. async.waterfall(
  859. [
  860. next => {
  861. CacheModule.runJob(
  862. "HGET",
  863. {
  864. table: "sessions",
  865. key: session.sessionId
  866. },
  867. this
  868. )
  869. .then(session => {
  870. next(null, session);
  871. })
  872. .catch(next);
  873. },
  874. (session, next) => {
  875. if (!session) return next("Session not found.");
  876. return next(null, session);
  877. },
  878. (session, next) => {
  879. userModel.findOne({ _id: session.userId }, next);
  880. },
  881. (user, next) => {
  882. if (!user) return next("User not found.");
  883. return next(null, user);
  884. }
  885. ],
  886. async (err, user) => {
  887. if (err && err !== true) {
  888. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  889. this.log("ERROR", "FIND_BY_SESSION", `User not found. "${err}"`);
  890. return cb({ status: "failure", message: err });
  891. }
  892. const data = {
  893. email: {
  894. address: user.email.address
  895. },
  896. avatar: user.avatar,
  897. username: user.username,
  898. name: user.name,
  899. location: user.location,
  900. bio: user.bio
  901. };
  902. if (user.services.password && user.services.password.password) data.password = true;
  903. if (user.services.github && user.services.github.id) data.github = true;
  904. this.log("SUCCESS", "FIND_BY_SESSION", `User found. "${user.username}".`);
  905. return cb({
  906. status: "success",
  907. data
  908. });
  909. }
  910. );
  911. },
  912. /**
  913. * Updates a user's username
  914. *
  915. * @param {object} session - the session object automatically added by socket.io
  916. * @param {string} updatingUserId - the updating user's id
  917. * @param {string} newUsername - the new username
  918. * @param {Function} cb - gets called with the result
  919. */
  920. updateUsername: isLoginRequired(async function updateUsername(session, updatingUserId, newUsername, cb) {
  921. const userModel = await DBModule.runJob(
  922. "GET_MODEL",
  923. {
  924. modelName: "user"
  925. },
  926. this
  927. );
  928. async.waterfall(
  929. [
  930. next => {
  931. if (updatingUserId === session.userId) return next(null, true);
  932. return userModel.findOne({ _id: session.userId }, next);
  933. },
  934. (user, next) => {
  935. if (user !== true && (!user || user.role !== "admin")) return next("Invalid permissions.");
  936. return userModel.findOne({ _id: updatingUserId }, next);
  937. },
  938. (user, next) => {
  939. if (!user) return next("User not found.");
  940. if (user.username === newUsername)
  941. return next("New username can't be the same as the old username.");
  942. return next(null);
  943. },
  944. next => {
  945. userModel.findOne({ username: new RegExp(`^${newUsername}$`, "i") }, next);
  946. },
  947. (user, next) => {
  948. if (!user) return next();
  949. if (user._id === updatingUserId) return next();
  950. return next("That username is already in use.");
  951. },
  952. next => {
  953. userModel.updateOne(
  954. { _id: updatingUserId },
  955. { $set: { username: newUsername } },
  956. { runValidators: true },
  957. next
  958. );
  959. }
  960. ],
  961. async err => {
  962. if (err && err !== true) {
  963. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  964. this.log(
  965. "ERROR",
  966. "UPDATE_USERNAME",
  967. `Couldn't update username for user "${updatingUserId}" to username "${newUsername}". "${err}"`
  968. );
  969. return cb({ status: "failure", message: err });
  970. }
  971. CacheModule.runJob("PUB", {
  972. channel: "user.updateUsername",
  973. value: {
  974. username: newUsername,
  975. _id: updatingUserId
  976. }
  977. });
  978. this.log(
  979. "SUCCESS",
  980. "UPDATE_USERNAME",
  981. `Updated username for user "${updatingUserId}" to username "${newUsername}".`
  982. );
  983. return cb({
  984. status: "success",
  985. message: "Username updated successfully"
  986. });
  987. }
  988. );
  989. }),
  990. /**
  991. * Updates a user's email
  992. *
  993. * @param {object} session - the session object automatically added by socket.io
  994. * @param {string} updatingUserId - the updating user's id
  995. * @param {string} newEmail - the new email
  996. * @param {Function} cb - gets called with the result
  997. */
  998. updateEmail: isLoginRequired(async function updateEmail(session, updatingUserId, newEmail, cb) {
  999. newEmail = newEmail.toLowerCase();
  1000. const verificationToken = await UtilsModule.runJob("GENERATE_RANDOM_STRING", { length: 64 }, this);
  1001. const userModel = await DBModule.runJob(
  1002. "GET_MODEL",
  1003. {
  1004. modelName: "user"
  1005. },
  1006. this
  1007. );
  1008. const verifyEmailSchema = await MailModule.runJob(
  1009. "GET_SCHEMA",
  1010. {
  1011. schemaName: "verifyEmail"
  1012. },
  1013. this
  1014. );
  1015. async.waterfall(
  1016. [
  1017. next => {
  1018. if (updatingUserId === session.userId) return next(null, true);
  1019. return userModel.findOne({ _id: session.userId }, next);
  1020. },
  1021. (user, next) => {
  1022. if (user !== true && (!user || user.role !== "admin")) return next("Invalid permissions.");
  1023. return userModel.findOne({ _id: updatingUserId }, next);
  1024. },
  1025. (user, next) => {
  1026. if (!user) return next("User not found.");
  1027. if (user.email.address === newEmail)
  1028. return next("New email can't be the same as your the old email.");
  1029. return next();
  1030. },
  1031. next => {
  1032. userModel.findOne({ "email.address": newEmail }, next);
  1033. },
  1034. (user, next) => {
  1035. if (!user) return next();
  1036. if (user._id === updatingUserId) return next();
  1037. return next("That email is already in use.");
  1038. },
  1039. // regenerate the url for gravatar avatar
  1040. next => {
  1041. UtilsModule.runJob("CREATE_GRAVATAR", { email: newEmail }, this).then(url => {
  1042. next(null, url);
  1043. });
  1044. },
  1045. (newAvatarUrl, next) => {
  1046. userModel.updateOne(
  1047. { _id: updatingUserId },
  1048. {
  1049. $set: {
  1050. "avatar.url": newAvatarUrl,
  1051. "email.address": newEmail,
  1052. "email.verified": false,
  1053. "email.verificationToken": verificationToken
  1054. }
  1055. },
  1056. { runValidators: true },
  1057. next
  1058. );
  1059. },
  1060. (res, next) => {
  1061. userModel.findOne({ _id: updatingUserId }, next);
  1062. },
  1063. (user, next) => {
  1064. verifyEmailSchema(newEmail, user.username, verificationToken, err => {
  1065. next(err);
  1066. });
  1067. }
  1068. ],
  1069. async err => {
  1070. if (err && err !== true) {
  1071. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1072. this.log(
  1073. "ERROR",
  1074. "UPDATE_EMAIL",
  1075. `Couldn't update email for user "${updatingUserId}" to email "${newEmail}". '${err}'`
  1076. );
  1077. return cb({ status: "failure", message: err });
  1078. }
  1079. this.log(
  1080. "SUCCESS",
  1081. "UPDATE_EMAIL",
  1082. `Updated email for user "${updatingUserId}" to email "${newEmail}".`
  1083. );
  1084. return cb({
  1085. status: "success",
  1086. message: "Email updated successfully."
  1087. });
  1088. }
  1089. );
  1090. }),
  1091. /**
  1092. * Updates a user's name
  1093. *
  1094. * @param {object} session - the session object automatically added by socket.io
  1095. * @param {string} updatingUserId - the updating user's id
  1096. * @param {string} newBio - the new name
  1097. * @param {Function} cb - gets called with the result
  1098. */
  1099. updateName: isLoginRequired(async function updateName(session, updatingUserId, newName, cb) {
  1100. const userModel = await DBModule.runJob(
  1101. "GET_MODEL",
  1102. {
  1103. modelName: "user"
  1104. },
  1105. this
  1106. );
  1107. async.waterfall(
  1108. [
  1109. next => {
  1110. if (updatingUserId === session.userId) return next(null, true);
  1111. return userModel.findOne({ _id: session.userId }, next);
  1112. },
  1113. (user, next) => {
  1114. if (user !== true && (!user || user.role !== "admin")) return next("Invalid permissions.");
  1115. return userModel.findOne({ _id: updatingUserId }, next);
  1116. },
  1117. (user, next) => {
  1118. if (!user) return next("User not found.");
  1119. return userModel.updateOne(
  1120. { _id: updatingUserId },
  1121. { $set: { name: newName } },
  1122. { runValidators: true },
  1123. next
  1124. );
  1125. }
  1126. ],
  1127. async err => {
  1128. if (err && err !== true) {
  1129. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1130. this.log(
  1131. "ERROR",
  1132. "UPDATE_NAME",
  1133. `Couldn't update name for user "${updatingUserId}" to name "${newName}". "${err}"`
  1134. );
  1135. cb({ status: "failure", message: err });
  1136. } else {
  1137. this.log(
  1138. "SUCCESS",
  1139. "UPDATE_NAME",
  1140. `Updated name for user "${updatingUserId}" to name "${newName}".`
  1141. );
  1142. cb({
  1143. status: "success",
  1144. message: "Name updated successfully"
  1145. });
  1146. }
  1147. }
  1148. );
  1149. }),
  1150. /**
  1151. * Updates a user's location
  1152. *
  1153. * @param {object} session - the session object automatically added by socket.io
  1154. * @param {string} updatingUserId - the updating user's id
  1155. * @param {string} newLocation - the new location
  1156. * @param {Function} cb - gets called with the result
  1157. */
  1158. updateLocation: isLoginRequired(async function updateLocation(session, updatingUserId, newLocation, cb) {
  1159. const userModel = await DBModule.runJob(
  1160. "GET_MODEL",
  1161. {
  1162. modelName: "user"
  1163. },
  1164. this
  1165. );
  1166. async.waterfall(
  1167. [
  1168. next => {
  1169. if (updatingUserId === session.userId) return next(null, true);
  1170. return userModel.findOne({ _id: session.userId }, next);
  1171. },
  1172. (user, next) => {
  1173. if (user !== true && (!user || user.role !== "admin")) return next("Invalid permissions.");
  1174. return userModel.findOne({ _id: updatingUserId }, next);
  1175. },
  1176. (user, next) => {
  1177. if (!user) return next("User not found.");
  1178. return userModel.updateOne(
  1179. { _id: updatingUserId },
  1180. { $set: { location: newLocation } },
  1181. { runValidators: true },
  1182. next
  1183. );
  1184. }
  1185. ],
  1186. async err => {
  1187. if (err && err !== true) {
  1188. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1189. this.log(
  1190. "ERROR",
  1191. "UPDATE_LOCATION",
  1192. `Couldn't update location for user "${updatingUserId}" to location "${newLocation}". "${err}"`
  1193. );
  1194. return cb({ status: "failure", message: err });
  1195. }
  1196. this.log(
  1197. "SUCCESS",
  1198. "UPDATE_LOCATION",
  1199. `Updated location for user "${updatingUserId}" to location "${newLocation}".`
  1200. );
  1201. return cb({
  1202. status: "success",
  1203. message: "Location updated successfully"
  1204. });
  1205. }
  1206. );
  1207. }),
  1208. /**
  1209. * Updates a user's bio
  1210. *
  1211. * @param {object} session - the session object automatically added by socket.io
  1212. * @param {string} updatingUserId - the updating user's id
  1213. * @param {string} newBio - the new bio
  1214. * @param {Function} cb - gets called with the result
  1215. */
  1216. updateBio: isLoginRequired(async function updateBio(session, updatingUserId, newBio, cb) {
  1217. const userModel = await DBModule.runJob(
  1218. "GET_MODEL",
  1219. {
  1220. modelName: "user"
  1221. },
  1222. this
  1223. );
  1224. async.waterfall(
  1225. [
  1226. next => {
  1227. if (updatingUserId === session.userId) return next(null, true);
  1228. return userModel.findOne({ _id: session.userId }, next);
  1229. },
  1230. (user, next) => {
  1231. if (user !== true && (!user || user.role !== "admin")) return next("Invalid permissions.");
  1232. return userModel.findOne({ _id: updatingUserId }, next);
  1233. },
  1234. (user, next) => {
  1235. if (!user) return next("User not found.");
  1236. return userModel.updateOne(
  1237. { _id: updatingUserId },
  1238. { $set: { bio: newBio } },
  1239. { runValidators: true },
  1240. next
  1241. );
  1242. }
  1243. ],
  1244. async err => {
  1245. if (err && err !== true) {
  1246. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1247. this.log(
  1248. "ERROR",
  1249. "UPDATE_BIO",
  1250. `Couldn't update bio for user "${updatingUserId}" to bio "${newBio}". "${err}"`
  1251. );
  1252. cb({ status: "failure", message: err });
  1253. } else {
  1254. this.log("SUCCESS", "UPDATE_BIO", `Updated bio for user "${updatingUserId}" to bio "${newBio}".`);
  1255. cb({
  1256. status: "success",
  1257. message: "Bio updated successfully"
  1258. });
  1259. }
  1260. }
  1261. );
  1262. }),
  1263. /**
  1264. * Updates the type of a user's avatar
  1265. *
  1266. * @param {object} session - the session object automatically added by socket.io
  1267. * @param {string} updatingUserId - the updating user's id
  1268. * @param {string} newType - the new type
  1269. * @param {Function} cb - gets called with the result
  1270. */
  1271. updateAvatarType: isLoginRequired(async function updateAvatarType(session, updatingUserId, newType, cb) {
  1272. const userModel = await DBModule.runJob(
  1273. "GET_MODEL",
  1274. {
  1275. modelName: "user"
  1276. },
  1277. this
  1278. );
  1279. async.waterfall(
  1280. [
  1281. next => {
  1282. if (updatingUserId === session.userId) return next(null, true);
  1283. return userModel.findOne({ _id: session.userId }, next);
  1284. },
  1285. (user, next) => {
  1286. if (user !== true && (!user || user.role !== "admin")) return next("Invalid permissions.");
  1287. return userModel.findOne({ _id: updatingUserId }, next);
  1288. },
  1289. (user, next) => {
  1290. if (!user) return next("User not found.");
  1291. return userModel.findOneAndUpdate(
  1292. { _id: updatingUserId },
  1293. { $set: { "avatar.type": newType } },
  1294. { new: true, runValidators: true },
  1295. next
  1296. );
  1297. }
  1298. ],
  1299. async err => {
  1300. if (err && err !== true) {
  1301. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1302. this.log(
  1303. "ERROR",
  1304. "UPDATE_AVATAR_TYPE",
  1305. `Couldn't update avatar type for user "${updatingUserId}" to type "${newType}". "${err}"`
  1306. );
  1307. return cb({ status: "failure", message: err });
  1308. }
  1309. this.log(
  1310. "SUCCESS",
  1311. "UPDATE_AVATAR_TYPE",
  1312. `Updated avatar type for user "${updatingUserId}" to type "${newType}".`
  1313. );
  1314. return cb({
  1315. status: "success",
  1316. message: "Avatar type updated successfully"
  1317. });
  1318. }
  1319. );
  1320. }),
  1321. /**
  1322. * Updates a user's role
  1323. *
  1324. * @param {object} session - the session object automatically added by socket.io
  1325. * @param {string} updatingUserId - the updating user's id
  1326. * @param {string} newRole - the new role
  1327. * @param {Function} cb - gets called with the result
  1328. */
  1329. updateRole: isAdminRequired(async function updateRole(session, updatingUserId, newRole, cb) {
  1330. newRole = newRole.toLowerCase();
  1331. const userModel = await DBModule.runJob(
  1332. "GET_MODEL",
  1333. {
  1334. modelName: "user"
  1335. },
  1336. this
  1337. );
  1338. async.waterfall(
  1339. [
  1340. next => {
  1341. userModel.findOne({ _id: updatingUserId }, next);
  1342. },
  1343. (user, next) => {
  1344. if (!user) return next("User not found.");
  1345. if (user.role === newRole) return next("New role can't be the same as the old role.");
  1346. return next();
  1347. },
  1348. next => {
  1349. userModel.updateOne(
  1350. { _id: updatingUserId },
  1351. { $set: { role: newRole } },
  1352. { runValidators: true },
  1353. next
  1354. );
  1355. }
  1356. ],
  1357. async err => {
  1358. if (err && err !== true) {
  1359. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1360. this.log(
  1361. "ERROR",
  1362. "UPDATE_ROLE",
  1363. `User "${session.userId}" couldn't update role for user "${updatingUserId}" to role "${newRole}". "${err}"`
  1364. );
  1365. return cb({ status: "failure", message: err });
  1366. }
  1367. this.log(
  1368. "SUCCESS",
  1369. "UPDATE_ROLE",
  1370. `User "${session.userId}" updated the role of user "${updatingUserId}" to role "${newRole}".`
  1371. );
  1372. return cb({
  1373. status: "success",
  1374. message: "Role successfully updated."
  1375. });
  1376. }
  1377. );
  1378. }),
  1379. /**
  1380. * Updates a user's password
  1381. *
  1382. * @param {object} session - the session object automatically added by socket.io
  1383. * @param {string} previousPassword - the previous password
  1384. * @param {string} newPassword - the new password
  1385. * @param {Function} cb - gets called with the result
  1386. */
  1387. updatePassword: isLoginRequired(async function updatePassword(session, previousPassword, newPassword, cb) {
  1388. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1389. async.waterfall(
  1390. [
  1391. next => {
  1392. userModel.findOne({ _id: session.userId }, next);
  1393. },
  1394. (user, next) => {
  1395. if (!user.services.password) return next("This account does not have a password set.");
  1396. return next(null, user.services.password.password);
  1397. },
  1398. (storedPassword, next) => {
  1399. bcrypt.compare(sha256(previousPassword), storedPassword).then(res => {
  1400. if (res) return next();
  1401. return next("Please enter the correct previous password.");
  1402. });
  1403. },
  1404. next => {
  1405. if (!DBModule.passwordValid(newPassword))
  1406. return next("Invalid new password. Check if it meets all the requirements.");
  1407. return next();
  1408. },
  1409. next => {
  1410. bcrypt.genSalt(10, next);
  1411. },
  1412. // hash the password
  1413. (salt, next) => {
  1414. bcrypt.hash(sha256(newPassword), salt, next);
  1415. },
  1416. (hashedPassword, next) => {
  1417. userModel.updateOne(
  1418. { _id: session.userId },
  1419. {
  1420. $set: {
  1421. "services.password.password": hashedPassword
  1422. }
  1423. },
  1424. next
  1425. );
  1426. }
  1427. ],
  1428. async err => {
  1429. if (err) {
  1430. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1431. this.log(
  1432. "ERROR",
  1433. "UPDATE_PASSWORD",
  1434. `Failed updating user password of user '${session.userId}'. '${err}'.`
  1435. );
  1436. return cb({ status: "failure", message: err });
  1437. }
  1438. this.log("SUCCESS", "UPDATE_PASSWORD", `User '${session.userId}' updated their password.`);
  1439. return cb({
  1440. status: "success",
  1441. message: "Password successfully updated."
  1442. });
  1443. }
  1444. );
  1445. }),
  1446. /**
  1447. * Requests a password for a session
  1448. *
  1449. * @param {object} session - the session object automatically added by socket.io
  1450. * @param {string} email - the email of the user that requests a password reset
  1451. * @param {Function} cb - gets called with the result
  1452. */
  1453. requestPassword: isLoginRequired(async function requestPassword(session, cb) {
  1454. const code = await UtilsModule.runJob("GENERATE_RANDOM_STRING", { length: 8 }, this);
  1455. const passwordRequestSchema = await MailModule.runJob(
  1456. "GET_SCHEMA",
  1457. {
  1458. schemaName: "passwordRequest"
  1459. },
  1460. this
  1461. );
  1462. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1463. async.waterfall(
  1464. [
  1465. next => {
  1466. userModel.findOne({ _id: session.userId }, next);
  1467. },
  1468. (user, next) => {
  1469. if (!user) return next("User not found.");
  1470. if (user.services.password && user.services.password.password)
  1471. return next("You already have a password set.");
  1472. return next(null, user);
  1473. },
  1474. (user, next) => {
  1475. const expires = new Date();
  1476. expires.setDate(expires.getDate() + 1);
  1477. userModel.findOneAndUpdate(
  1478. { "email.address": user.email.address },
  1479. {
  1480. $set: {
  1481. "services.password": {
  1482. set: { code, expires }
  1483. }
  1484. }
  1485. },
  1486. { runValidators: true },
  1487. next
  1488. );
  1489. },
  1490. (user, next) => {
  1491. passwordRequestSchema(user.email.address, user.username, code, next);
  1492. }
  1493. ],
  1494. async err => {
  1495. if (err && err !== true) {
  1496. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1497. this.log(
  1498. "ERROR",
  1499. "REQUEST_PASSWORD",
  1500. `UserId '${session.userId}' failed to request password. '${err}'`
  1501. );
  1502. return cb({ status: "failure", message: err });
  1503. }
  1504. this.log(
  1505. "SUCCESS",
  1506. "REQUEST_PASSWORD",
  1507. `UserId '${session.userId}' successfully requested a password.`
  1508. );
  1509. return cb({
  1510. status: "success",
  1511. message: "Successfully requested password."
  1512. });
  1513. }
  1514. );
  1515. }),
  1516. /**
  1517. * Verifies a password code
  1518. *
  1519. * @param {object} session - the session object automatically added by socket.io
  1520. * @param {string} code - the password code
  1521. * @param {Function} cb - gets called with the result
  1522. */
  1523. verifyPasswordCode: isLoginRequired(async function verifyPasswordCode(session, code, cb) {
  1524. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1525. async.waterfall(
  1526. [
  1527. next => {
  1528. if (!code || typeof code !== "string") return next("Invalid code.");
  1529. return userModel.findOne(
  1530. {
  1531. "services.password.set.code": code,
  1532. _id: session.userId
  1533. },
  1534. next
  1535. );
  1536. },
  1537. (user, next) => {
  1538. if (!user) return next("Invalid code.");
  1539. if (user.services.password.set.expires < new Date()) return next("That code has expired.");
  1540. return next(null);
  1541. }
  1542. ],
  1543. async err => {
  1544. if (err && err !== true) {
  1545. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1546. this.log("ERROR", "VERIFY_PASSWORD_CODE", `Code '${code}' failed to verify. '${err}'`);
  1547. cb({ status: "failure", message: err });
  1548. } else {
  1549. this.log("SUCCESS", "VERIFY_PASSWORD_CODE", `Code '${code}' successfully verified.`);
  1550. cb({
  1551. status: "success",
  1552. message: "Successfully verified password code."
  1553. });
  1554. }
  1555. }
  1556. );
  1557. }),
  1558. /**
  1559. * Adds a password to a user with a code
  1560. *
  1561. * @param {object} session - the session object automatically added by socket.io
  1562. * @param {string} code - the password code
  1563. * @param {string} newPassword - the new password code
  1564. * @param {Function} cb - gets called with the result
  1565. */
  1566. changePasswordWithCode: isLoginRequired(async function changePasswordWithCode(session, code, newPassword, cb) {
  1567. const userModel = await DBModule.runJob(
  1568. "GET_MODEL",
  1569. {
  1570. modelName: "user"
  1571. },
  1572. this
  1573. );
  1574. async.waterfall(
  1575. [
  1576. next => {
  1577. if (!code || typeof code !== "string") return next("Invalid code.");
  1578. return userModel.findOne({ "services.password.set.code": code }, next);
  1579. },
  1580. (user, next) => {
  1581. if (!user) return next("Invalid code.");
  1582. if (!user.services.password.set.expires > new Date()) return next("That code has expired.");
  1583. return next();
  1584. },
  1585. next => {
  1586. if (!DBModule.passwordValid(newPassword))
  1587. return next("Invalid password. Check if it meets all the requirements.");
  1588. return next();
  1589. },
  1590. next => {
  1591. bcrypt.genSalt(10, next);
  1592. },
  1593. // hash the password
  1594. (salt, next) => {
  1595. bcrypt.hash(sha256(newPassword), salt, next);
  1596. },
  1597. (hashedPassword, next) => {
  1598. userModel.updateOne(
  1599. { "services.password.set.code": code },
  1600. {
  1601. $set: {
  1602. "services.password.password": hashedPassword
  1603. },
  1604. $unset: { "services.password.set": "" }
  1605. },
  1606. { runValidators: true },
  1607. next
  1608. );
  1609. }
  1610. ],
  1611. async err => {
  1612. if (err && err !== true) {
  1613. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1614. this.log("ERROR", "ADD_PASSWORD_WITH_CODE", `Code '${code}' failed to add password. '${err}'`);
  1615. cb({ status: "failure", message: err });
  1616. } else {
  1617. this.log("SUCCESS", "ADD_PASSWORD_WITH_CODE", `Code '${code}' successfully added password.`);
  1618. CacheModule.runJob("PUB", {
  1619. channel: "user.linkPassword",
  1620. value: session.userId
  1621. });
  1622. cb({
  1623. status: "success",
  1624. message: "Successfully added password."
  1625. });
  1626. }
  1627. }
  1628. );
  1629. }),
  1630. /**
  1631. * Unlinks password from user
  1632. *
  1633. * @param {object} session - the session object automatically added by socket.io
  1634. * @param {Function} cb - gets called with the result
  1635. */
  1636. unlinkPassword: isLoginRequired(async function unlinkPassword(session, cb) {
  1637. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1638. async.waterfall(
  1639. [
  1640. next => {
  1641. userModel.findOne({ _id: session.userId }, next);
  1642. },
  1643. (user, next) => {
  1644. if (!user) return next("Not logged in.");
  1645. if (!user.services.github || !user.services.github.id)
  1646. return next("You can't remove password login without having GitHub login.");
  1647. return userModel.updateOne({ _id: session.userId }, { $unset: { "services.password": "" } }, next);
  1648. }
  1649. ],
  1650. async err => {
  1651. if (err && err !== true) {
  1652. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1653. this.log(
  1654. "ERROR",
  1655. "UNLINK_PASSWORD",
  1656. `Unlinking password failed for userId '${session.userId}'. '${err}'`
  1657. );
  1658. cb({ status: "failure", message: err });
  1659. } else {
  1660. this.log(
  1661. "SUCCESS",
  1662. "UNLINK_PASSWORD",
  1663. `Unlinking password successful for userId '${session.userId}'.`
  1664. );
  1665. CacheModule.runJob("PUB", {
  1666. channel: "user.unlinkPassword",
  1667. value: session.userId
  1668. });
  1669. cb({
  1670. status: "success",
  1671. message: "Successfully unlinked password."
  1672. });
  1673. }
  1674. }
  1675. );
  1676. }),
  1677. /**
  1678. * Unlinks GitHub from user
  1679. *
  1680. * @param {object} session - the session object automatically added by socket.io
  1681. * @param {Function} cb - gets called with the result
  1682. */
  1683. unlinkGitHub: isLoginRequired(async function unlinkGitHub(session, cb) {
  1684. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1685. async.waterfall(
  1686. [
  1687. next => {
  1688. userModel.findOne({ _id: session.userId }, next);
  1689. },
  1690. (user, next) => {
  1691. if (!user) return next("Not logged in.");
  1692. if (!user.services.password || !user.services.password.password)
  1693. return next("You can't remove GitHub login without having password login.");
  1694. return userModel.updateOne({ _id: session.userId }, { $unset: { "services.github": "" } }, next);
  1695. }
  1696. ],
  1697. async err => {
  1698. if (err && err !== true) {
  1699. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1700. this.log(
  1701. "ERROR",
  1702. "UNLINK_GITHUB",
  1703. `Unlinking GitHub failed for userId '${session.userId}'. '${err}'`
  1704. );
  1705. cb({ status: "failure", message: err });
  1706. } else {
  1707. this.log("SUCCESS", "UNLINK_GITHUB", `Unlinking GitHub successful for userId '${session.userId}'.`);
  1708. CacheModule.runJob("PUB", {
  1709. channel: "user.unlinkGithub",
  1710. value: session.userId
  1711. });
  1712. cb({
  1713. status: "success",
  1714. message: "Successfully unlinked GitHub."
  1715. });
  1716. }
  1717. }
  1718. );
  1719. }),
  1720. /**
  1721. * Requests a password reset for an email
  1722. *
  1723. * @param {object} session - the session object automatically added by socket.io
  1724. * @param {string} email - the email of the user that requests a password reset
  1725. * @param {Function} cb - gets called with the result
  1726. */
  1727. async requestPasswordReset(session, email, cb) {
  1728. const code = await UtilsModule.runJob("GENERATE_RANDOM_STRING", { length: 8 }, this);
  1729. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1730. const resetPasswordRequestSchema = await MailModule.runJob(
  1731. "GET_SCHEMA",
  1732. {
  1733. schemaName: "resetPasswordRequest"
  1734. },
  1735. this
  1736. );
  1737. async.waterfall(
  1738. [
  1739. next => {
  1740. if (!email || typeof email !== "string") return next("Invalid email.");
  1741. email = email.toLowerCase();
  1742. return userModel.findOne({ "email.address": email }, next);
  1743. },
  1744. (user, next) => {
  1745. if (!user) return next("User not found.");
  1746. if (!user.services.password || !user.services.password.password)
  1747. return next("User does not have a password set, and probably uses GitHub to log in.");
  1748. return next(null, user);
  1749. },
  1750. (user, next) => {
  1751. const expires = new Date();
  1752. expires.setDate(expires.getDate() + 1);
  1753. userModel.findOneAndUpdate(
  1754. { "email.address": email },
  1755. {
  1756. $set: {
  1757. "services.password.reset": {
  1758. code,
  1759. expires
  1760. }
  1761. }
  1762. },
  1763. { runValidators: true },
  1764. next
  1765. );
  1766. },
  1767. (user, next) => {
  1768. resetPasswordRequestSchema(user.email.address, user.username, code, next);
  1769. }
  1770. ],
  1771. async err => {
  1772. if (err && err !== true) {
  1773. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1774. this.log(
  1775. "ERROR",
  1776. "REQUEST_PASSWORD_RESET",
  1777. `Email '${email}' failed to request password reset. '${err}'`
  1778. );
  1779. cb({ status: "failure", message: err });
  1780. } else {
  1781. this.log(
  1782. "SUCCESS",
  1783. "REQUEST_PASSWORD_RESET",
  1784. `Email '${email}' successfully requested a password reset.`
  1785. );
  1786. cb({
  1787. status: "success",
  1788. message: "Successfully requested password reset."
  1789. });
  1790. }
  1791. }
  1792. );
  1793. },
  1794. /**
  1795. * Verifies a reset code
  1796. *
  1797. * @param {object} session - the session object automatically added by socket.io
  1798. * @param {string} code - the password reset code
  1799. * @param {Function} cb - gets called with the result
  1800. */
  1801. async verifyPasswordResetCode(session, code, cb) {
  1802. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1803. async.waterfall(
  1804. [
  1805. next => {
  1806. if (!code || typeof code !== "string") return next("Invalid code.");
  1807. return userModel.findOne({ "services.password.reset.code": code }, next);
  1808. },
  1809. (user, next) => {
  1810. if (!user) return next("Invalid code.");
  1811. if (!user.services.password.reset.expires > new Date()) return next("That code has expired.");
  1812. return next(null);
  1813. }
  1814. ],
  1815. async err => {
  1816. if (err && err !== true) {
  1817. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1818. this.log("ERROR", "VERIFY_PASSWORD_RESET_CODE", `Code '${code}' failed to verify. '${err}'`);
  1819. cb({ status: "failure", message: err });
  1820. } else {
  1821. this.log("SUCCESS", "VERIFY_PASSWORD_RESET_CODE", `Code '${code}' successfully verified.`);
  1822. cb({
  1823. status: "success",
  1824. message: "Successfully verified password reset code."
  1825. });
  1826. }
  1827. }
  1828. );
  1829. },
  1830. /**
  1831. * Changes a user's password with a reset code
  1832. *
  1833. * @param {object} session - the session object automatically added by socket.io
  1834. * @param {string} code - the password reset code
  1835. * @param {string} newPassword - the new password reset code
  1836. * @param {Function} cb - gets called with the result
  1837. */
  1838. async changePasswordWithResetCode(session, code, newPassword, cb) {
  1839. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1840. async.waterfall(
  1841. [
  1842. next => {
  1843. if (!code || typeof code !== "string") return next("Invalid code.");
  1844. return userModel.findOne({ "services.password.reset.code": code }, next);
  1845. },
  1846. (user, next) => {
  1847. if (!user) return next("Invalid code.");
  1848. if (!user.services.password.reset.expires > new Date()) return next("That code has expired.");
  1849. return next();
  1850. },
  1851. next => {
  1852. if (!DBModule.passwordValid(newPassword))
  1853. return next("Invalid password. Check if it meets all the requirements.");
  1854. return next();
  1855. },
  1856. next => {
  1857. bcrypt.genSalt(10, next);
  1858. },
  1859. // hash the password
  1860. (salt, next) => {
  1861. bcrypt.hash(sha256(newPassword), salt, next);
  1862. },
  1863. (hashedPassword, next) => {
  1864. userModel.updateOne(
  1865. { "services.password.reset.code": code },
  1866. {
  1867. $set: {
  1868. "services.password.password": hashedPassword
  1869. },
  1870. $unset: { "services.password.reset": "" }
  1871. },
  1872. { runValidators: true },
  1873. next
  1874. );
  1875. }
  1876. ],
  1877. async err => {
  1878. if (err && err !== true) {
  1879. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1880. this.log(
  1881. "ERROR",
  1882. "CHANGE_PASSWORD_WITH_RESET_CODE",
  1883. `Code '${code}' failed to change password. '${err}'`
  1884. );
  1885. cb({ status: "failure", message: err });
  1886. } else {
  1887. this.log(
  1888. "SUCCESS",
  1889. "CHANGE_PASSWORD_WITH_RESET_CODE",
  1890. `Code '${code}' successfully changed password.`
  1891. );
  1892. cb({
  1893. status: "success",
  1894. message: "Successfully changed password."
  1895. });
  1896. }
  1897. }
  1898. );
  1899. },
  1900. /**
  1901. * Bans a user by userId
  1902. *
  1903. * @param {object} session - the session object automatically added by socket.io
  1904. * @param {string} value - the user id that is going to be banned
  1905. * @param {string} reason - the reason for the ban
  1906. * @param {string} expiresAt - the time the ban expires
  1907. * @param {Function} cb - gets called with the result
  1908. */
  1909. banUserById: isAdminRequired(function banUserById(session, userId, reason, expiresAt, cb) {
  1910. async.waterfall(
  1911. [
  1912. next => {
  1913. if (!userId) return next("You must provide a userId to ban.");
  1914. if (!reason) return next("You must provide a reason for the ban.");
  1915. return next();
  1916. },
  1917. next => {
  1918. if (!expiresAt || typeof expiresAt !== "string") return next("Invalid expire date.");
  1919. const date = new Date();
  1920. switch (expiresAt) {
  1921. case "1h":
  1922. expiresAt = date.setHours(date.getHours() + 1);
  1923. break;
  1924. case "12h":
  1925. expiresAt = date.setHours(date.getHours() + 12);
  1926. break;
  1927. case "1d":
  1928. expiresAt = date.setDate(date.getDate() + 1);
  1929. break;
  1930. case "1w":
  1931. expiresAt = date.setDate(date.getDate() + 7);
  1932. break;
  1933. case "1m":
  1934. expiresAt = date.setMonth(date.getMonth() + 1);
  1935. break;
  1936. case "3m":
  1937. expiresAt = date.setMonth(date.getMonth() + 3);
  1938. break;
  1939. case "6m":
  1940. expiresAt = date.setMonth(date.getMonth() + 6);
  1941. break;
  1942. case "1y":
  1943. expiresAt = date.setFullYear(date.getFullYear() + 1);
  1944. break;
  1945. case "never":
  1946. expiresAt = new Date(3093527980800000);
  1947. break;
  1948. default:
  1949. return next("Invalid expire date.");
  1950. }
  1951. return next();
  1952. },
  1953. next => {
  1954. PunishmentsModule.runJob(
  1955. "ADD_PUNISHMENT",
  1956. {
  1957. type: "banUserId",
  1958. value: userId,
  1959. reason,
  1960. expiresAt,
  1961. punishedBy: "" // needs changed
  1962. },
  1963. this
  1964. )
  1965. .then(punishment => next(null, punishment))
  1966. .catch(next);
  1967. },
  1968. (punishment, next) => {
  1969. CacheModule.runJob("PUB", {
  1970. channel: "user.ban",
  1971. value: { userId, punishment }
  1972. });
  1973. next();
  1974. }
  1975. ],
  1976. async err => {
  1977. if (err && err !== true) {
  1978. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1979. this.log(
  1980. "ERROR",
  1981. "BAN_USER_BY_ID",
  1982. `User ${session.userId} failed to ban user ${userId} with the reason ${reason}. '${err}'`
  1983. );
  1984. cb({ status: "failure", message: err });
  1985. } else {
  1986. this.log(
  1987. "SUCCESS",
  1988. "BAN_USER_BY_ID",
  1989. `User ${session.userId} has successfully banned user ${userId} with the reason ${reason}.`
  1990. );
  1991. cb({
  1992. status: "success",
  1993. message: "Successfully banned user."
  1994. });
  1995. }
  1996. }
  1997. );
  1998. })
  1999. };