You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

609 lines
25 KiB

  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var adminModel = mongoose.model('adminModel');
  4. var userModel = mongoose.model('userModel');
  5. var userController = require('../controllers/userController');
  6. var notificationModel = mongoose.model('notificationModel');
  7. var travelModel = mongoose.model('travelModel');
  8. var travelCtrl = require('../controllers/travelController');
  9. var config = require('../config');
  10. var adminConfig = require('../adminConfig'); // get our config file
  11. var pageSize = config.pageSize;
  12. /* */
  13. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  14. var express = require("express");
  15. var app = express();
  16. var config = require('../config'); // get our config file
  17. app.set('superSecret', config.secret); // secret variable
  18. var crypto = require('crypto');
  19. /* */
  20. var request = require('request');
  21. //POST - Insert a new User in the DB
  22. exports.signup = function(req, res) {
  23. var admin = new adminModel({
  24. username: req.body.username,
  25. password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
  26. email: req.body.email,
  27. phone: req.body.phone,
  28. telegram: req.body.telegram
  29. });
  30. if (admin.username == undefined) {
  31. return res.status(500).jsonp("empty inputs");
  32. } else if (admin.password == undefined) {
  33. return res.status(500).jsonp("empty inputs");
  34. } else if (admin.email == undefined) {
  35. return res.status(500).jsonp("empty inputs");
  36. }
  37. adminPasswordGetted = crypto.createHash('sha256').update(req.body.adminPassword).digest('base64');
  38. console.log(adminPasswordGetted);
  39. console.log(adminConfig.passwordHash);
  40. if (adminPasswordGetted != adminConfig.passwordHash) {
  41. return res.status(500).jsonp("admin password not valid");
  42. }
  43. admin.save(function(err, admin) {
  44. if (err) return res.send(500, err.message);
  45. exports.login(req, res);
  46. });
  47. };
  48. //POST - auth user
  49. exports.login = function(req, res) {
  50. // find the user
  51. adminModel.findOne({
  52. username: req.body.username
  53. })
  54. .select('+password')
  55. .exec(function(err, user) {
  56. if (err) throw err;
  57. if (!user) {
  58. res.json({
  59. success: false,
  60. message: 'Authentication failed. User not found.'
  61. });
  62. } else if (user) {
  63. req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
  64. // check if password matches
  65. if (user.password != req.body.password) {
  66. res.json({
  67. success: false,
  68. message: 'Authentication failed. Wrong password.'
  69. });
  70. } else {
  71. // if user is found and password is right
  72. // create a token
  73. var token = jwt.sign({
  74. foo: 'bar'
  75. }, app.get('superSecret'), {
  76. //expiresInMinutes: 1440 // expires in 24 hours
  77. //expiresIn: '60m'
  78. });
  79. user.token = token;
  80. user.save(function(err, user) {
  81. if (err) return res.send(500, err.message);
  82. //res.status(200).jsonp(travel);
  83. console.log(user);
  84. // return the information including token as JSON
  85. user.password = "";
  86. res.json({
  87. success: true,
  88. message: 'Enjoy your token!',
  89. token: token,
  90. user: user
  91. });
  92. });
  93. }
  94. }
  95. });
  96. };
  97. exports.getAllAdmins = function(req, res) {
  98. adminModel.find()
  99. .limit(pageSize)
  100. .skip(pageSize * Number(req.query.page))
  101. .exec(function(err, admins) {
  102. if (err) return res.send(500, err.message);
  103. res.status(200).jsonp(admins);
  104. });
  105. };
  106. exports.changePassword = function(req, res) {
  107. //if(req.body.)
  108. userModel.update({
  109. 'token': req.headers['x-access-token']
  110. }, req.body,
  111. function(err) {
  112. if (err) return console.log(err);
  113. exports.getUserByToken(req, res);
  114. });
  115. };
  116. exports.deleteTravel = function(req, res) {
  117. adminModel.findOne({
  118. 'token': req.headers['x-access-token']
  119. })
  120. .exec(function(err, admin) {
  121. if (!admin) {
  122. res.json({
  123. success: false,
  124. message: 'Admin not found'
  125. });
  126. } else if (admin) {
  127. if (err) return res.send(500, err.message);
  128. travelModel.findById(req.params.travelid, function(err, travel) {
  129. if (err) return res.send(500, err.message);
  130. //add notification to the user who has created the travel
  131. userModel.findOne({
  132. _id: travel.user
  133. })
  134. .exec(function(err, user) {
  135. if (err) return res.send(500, err.message);
  136. if (!user) {
  137. //console.log("Notification not posible, user owner of the travel not exist");
  138. } else if (user) {
  139. var notification = new notificationModel({
  140. concept: "admin",
  141. message: "an admin has deleted your travel: " + travel.title,
  142. date: new Date(),
  143. icon: 'admin',
  144. link: "users/" + user._id,
  145. user: user._id
  146. });
  147. notification.save(function(err, notification) {
  148. if (err) return res.send(500, err.message);
  149. user.notifications.push(notification._id);
  150. user.save(function(err, user) {
  151. if (err) return res.send(500, err.message);
  152. //notification added to user
  153. });
  154. });
  155. }
  156. });
  157. travel.remove(function(err) {
  158. if (err) return res.send(500, err.message);
  159. travelCtrl.getAllTravels(req, res);
  160. });
  161. });
  162. }
  163. });
  164. };
  165. exports.deleteUser = function(req, res) {
  166. adminModel.findOne({
  167. 'token': req.headers['x-access-token']
  168. })
  169. .exec(function(err, admin) {
  170. if (!admin) {
  171. res.json({
  172. success: false,
  173. message: 'Admin not found'
  174. });
  175. } else if (admin) {
  176. if (err) return res.send(500, err.message);
  177. userModel.findOne({
  178. _id: req.params.userid
  179. })
  180. .exec(function(err, user) {
  181. if (err) return res.send(500, err.message);
  182. if (!user) {
  183. res.json({
  184. success: false,
  185. message: 'Delete user not posible, user not exist'
  186. });
  187. } else if (user) {
  188. //delete all the travels of this user
  189. travelModel.find({
  190. user: user._id
  191. }).remove().exec(function(err, data) {});
  192. //delete all the comments of this user
  193. //now delete user
  194. user.remove(function(err) {
  195. if (err) return res.send(500, err.message);
  196. res.status(200).jsonp("deleted");
  197. });
  198. }
  199. });
  200. }
  201. });
  202. };
  203. function isNodeInNodes(node, nodes) {
  204. for (var i = 0; i < nodes.length; i++) {
  205. if (node.title == nodes[i].title) {
  206. return (i);
  207. }
  208. }
  209. return (-1);
  210. }
  211. exports.network = function(req, res) {
  212. adminModel.findOne({
  213. 'token': req.headers['x-access-token']
  214. })
  215. .exec(function(err, admin) {
  216. if (!admin) {
  217. res.json({
  218. success: false,
  219. message: 'Admin not found'
  220. });
  221. } else if (admin) {
  222. if (err) return res.send(500, err.message);
  223. userModel.find()
  224. .limit(pageSize)
  225. .skip(pageSize * Number(req.query.page))
  226. .lean()
  227. //.populate({path: 'travels', populate: {path: 'joins', populate: {path: 'username'}}})
  228. .populate('travels', 'title type joins')
  229. .populate('likes', 'username avatar')
  230. .exec(function(err, users) {
  231. if (err) return res.send(500, err.message);
  232. /*res.status(200).jsonp(users);*/
  233. var nodes = [];
  234. var edges = [];
  235. for (var i = 0; i < users.length; i++) {
  236. var node = {
  237. title: users[i].username,
  238. label: users[i].username,
  239. image: users[i].avatar,
  240. shape: "image",
  241. id: users[i]._id,
  242. group: users[i]._id
  243. };
  244. var lNode = isNodeInNodes(node, nodes);
  245. if (lNode < 0) {
  246. nodes.push(node);
  247. var uNode = nodes.length - 1;
  248. }
  249. for (var j = 0; j < users[i].likes.length; j++) {
  250. /*console.log(i + ", " + j);
  251. console.log(nodes);*/
  252. var node = {
  253. title: users[i].likes[j].username,
  254. label: users[i].likes[j].username,
  255. image: users[i].likes[j].avatar,
  256. shape: "image",
  257. id: users[i].likes[j]._id
  258. };
  259. var lNode = isNodeInNodes(node, nodes);
  260. if (lNode < 0) {
  261. //node no exist
  262. nodes.push(node);
  263. lNode = nodes.length - 1;
  264. } else {
  265. //node already exist
  266. }
  267. var edge = {
  268. from: users[i]._id,
  269. to: users[i].likes[j]._id,
  270. label: "like",
  271. arrows: "to",
  272. color: {
  273. color: "#E57373" //red300
  274. }
  275. };
  276. edges.push(edge);
  277. }
  278. for (var j = 0; j < users[i].travels.length; j++) {
  279. /*console.log(i + ", " + j);
  280. console.log(nodes);*/
  281. var node = {
  282. title: users[i].travels[j].title,
  283. label: users[i].travels[j].title,
  284. image: "img/" + users[i].travels[j].type + ".png",
  285. shape: "image",
  286. id: users[i].travels[j]._id,
  287. value: "0.5",
  288. group: users[i]._id
  289. };
  290. var lNode = isNodeInNodes(node, nodes);
  291. if (lNode < 0) {
  292. //node no exist
  293. nodes.push(node);
  294. lNode = nodes.length - 1;
  295. } else {
  296. //node already exist
  297. }
  298. var edge = {
  299. from: users[i]._id,
  300. to: users[i].travels[j]._id,
  301. color: {
  302. color: "#616161" //grey
  303. }
  304. };
  305. edges.push(edge);
  306. //users joining travels
  307. /*for(var k=0; k<users[i].travels[j].joins.length; k++){
  308. var node = {
  309. title: users[i].travels[j].joins[k].username,
  310. label: users[i].travels[j].joins[k].username,
  311. image: users[i].travels[j].joins[k].avatar,
  312. shape: "image",
  313. id: users[i].travels[j].joins[k]._id
  314. };
  315. var lNode = isNodeInNodes(node, nodes);
  316. if (lNode<0){
  317. //node no exist
  318. nodes.push(node);
  319. lNode = nodes.length -1;
  320. }
  321. var edge={
  322. from: users[i].travels[j].joins[k]._id,
  323. to: users[i].travels[j]._id,
  324. color: {
  325. color: "#4DD0E1"//cyan300
  326. }
  327. };
  328. edges.push(edge);
  329. }*/
  330. }
  331. }
  332. var resp = {
  333. nodes: nodes,
  334. edges: edges
  335. };
  336. res.status(200).jsonp(resp);
  337. });
  338. }
  339. });
  340. };
  341. exports.getUserNetwork = function(req, res) {
  342. adminModel.findOne({
  343. 'token': req.headers['x-access-token']
  344. })
  345. .exec(function(err, admin) {
  346. if (!admin) {
  347. res.json({
  348. success: false,
  349. message: 'Admin not found'
  350. });
  351. } else if (admin) {
  352. if (err) return res.send(500, err.message);
  353. userModel.findOne({
  354. _id: req.params.userid
  355. })
  356. .lean()
  357. .populate('travels', 'title type joins')
  358. .populate('likes', 'username avatar')
  359. .exec(function(err, user) {
  360. if (err) return res.send(500, err.message);
  361. /*res.status(200).jsonp(users);*/
  362. var nodes = [];
  363. var edges = [];
  364. //for (var i = 0; i < users.length; i++) {
  365. var node = {
  366. title: user.username,
  367. label: user.username,
  368. image: user.avatar,
  369. shape: "image",
  370. id: user._id,
  371. group: user._id
  372. };
  373. var lNode = isNodeInNodes(node, nodes);
  374. if (lNode < 0) {
  375. nodes.push(node);
  376. var uNode = nodes.length - 1;
  377. }
  378. for (var j = 0; j < user.likes.length; j++) {
  379. /*console.log(i + ", " + j);
  380. console.log(nodes);*/
  381. var node = {
  382. title: user.likes[j].username,
  383. label: user.likes[j].username,
  384. image: user.likes[j].avatar,
  385. shape: "image",
  386. id: user.likes[j]._id
  387. };
  388. var lNode = isNodeInNodes(node, nodes);
  389. if (lNode < 0) {
  390. //node no exist
  391. nodes.push(node);
  392. lNode = nodes.length - 1;
  393. } else {
  394. //node already exist
  395. }
  396. var edge = {
  397. from: user._id,
  398. to: user.likes[j]._id,
  399. label: "like",
  400. arrows: "to",
  401. color: {
  402. color: "#E57373" //red300
  403. }
  404. };
  405. edges.push(edge);
  406. }
  407. for (var j = 0; j < user.travels.length; j++) {
  408. /*console.log(i + ", " + j);
  409. console.log(nodes);*/
  410. var node = {
  411. title: user.travels[j].title,
  412. label: user.travels[j].title,
  413. image: "img/" + user.travels[j].type + ".png",
  414. shape: "image",
  415. id: user.travels[j]._id,
  416. value: "0.5",
  417. group: user._id
  418. };
  419. var lNode = isNodeInNodes(node, nodes);
  420. if (lNode < 0) {
  421. //node no exist
  422. nodes.push(node);
  423. lNode = nodes.length - 1;
  424. } else {
  425. //node already exist
  426. }
  427. var edge = {
  428. from: user._id,
  429. to: user.travels[j]._id,
  430. color: {
  431. color: "#616161" //grey
  432. }
  433. };
  434. edges.push(edge);
  435. }
  436. //add users that has add like to the user
  437. userModel.find({
  438. likes: req.params.userid
  439. })
  440. .exec(function(err, users) {
  441. for (var i = 0; i < users.length; i++) {
  442. /*console.log(i + ", " + j);
  443. console.log(nodes);*/
  444. var node = {
  445. title: users[i].username,
  446. label: users[i].username,
  447. image: users[i].avatar,
  448. shape: "image",
  449. id: users[i]._id
  450. };
  451. var lNode = isNodeInNodes(node, nodes);
  452. if (lNode < 0) {
  453. //node no exist
  454. nodes.push(node);
  455. lNode = nodes.length - 1;
  456. } else {
  457. //node already exist
  458. }
  459. var edge = {
  460. from: users[i]._id,
  461. to: req.params.userid,
  462. label: "like",
  463. arrows: "to",
  464. color: {
  465. color: "#E57373" //red300
  466. }
  467. };
  468. edges.push(edge);
  469. }
  470. var resp = {
  471. nodes: nodes,
  472. edges: edges
  473. };
  474. res.status(200).jsonp(resp);
  475. });
  476. //}
  477. });
  478. }
  479. });
  480. };
  481. // user validation
  482. exports.validateUser = function(req, res) {
  483. adminModel.findOne({
  484. 'token': req.headers['x-access-token']
  485. })
  486. .exec(function(err, admin) {
  487. if (!admin) {
  488. res.json({
  489. success: false,
  490. message: 'Admin not found'
  491. });
  492. } else if (admin) {
  493. if (err) return res.send(500, err.message);
  494. userModel.findOne({
  495. _id: req.params.userid
  496. })
  497. .exec(function(err, user) {
  498. if (err) return res.send(500, err.message);
  499. user.validated = true;
  500. user.validatedBy = admin._id;
  501. var notification = new notificationModel({
  502. concept: "admin",
  503. message: "an admin has validated your account",
  504. date: new Date(),
  505. icon: 'admin',
  506. link: "users/" + user._id,
  507. user: user._id
  508. });
  509. notification.save(function(err, notification) {
  510. if (err) return res.send(500, err.message);
  511. user.notifications.push(notification._id);
  512. user.save(function(err, user) {
  513. if (err) return res.send(500, err.message);
  514. userController.getUserById(req, res);
  515. });
  516. });
  517. });
  518. }
  519. });
  520. };
  521. // user unvalidate
  522. exports.unvalidateUser = function(req, res) {
  523. adminModel.findOne({
  524. 'token': req.headers['x-access-token']
  525. })
  526. .exec(function(err, admin) {
  527. if (!admin) {
  528. res.json({
  529. success: false,
  530. message: 'Admin not found'
  531. });
  532. } else if (admin) {
  533. if (err) return res.send(500, err.message);
  534. userModel.findOne({
  535. _id: req.params.userid
  536. })
  537. .exec(function(err, user) {
  538. if (err) return res.send(500, err.message);
  539. user.validated = false;
  540. user.validatedBy = admin._id;
  541. var notification = new notificationModel({
  542. concept: "admin",
  543. message: "an admin has unvalidated your account",
  544. date: new Date(),
  545. icon: 'admin',
  546. link: "users/" + user._id,
  547. user: user._id
  548. });
  549. notification.save(function(err, notification) {
  550. if (err) return res.send(500, err.message);
  551. user.notifications.push(notification._id);
  552. user.save(function(err, user) {
  553. if (err) return res.send(500, err.message);
  554. userController.getUserById(req, res);
  555. });
  556. });
  557. });
  558. }
  559. });
  560. };