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.

573 lines
23 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.changePassword = function(req, res) {
  98. //if(req.body.)
  99. userModel.update({
  100. 'token': req.headers['x-access-token']
  101. }, req.body,
  102. function(err) {
  103. if (err) return console.log(err);
  104. exports.getUserByToken(req, res);
  105. });
  106. };
  107. exports.deleteTravel = function(req, res) {
  108. adminModel.findOne({
  109. 'token': req.headers['x-access-token']
  110. })
  111. .exec(function(err, admin) {
  112. if (!admin) {
  113. res.json({
  114. success: false,
  115. message: 'Admin not found'
  116. });
  117. } else if (admin) {
  118. if (err) return res.send(500, err.message);
  119. travelModel.findById(req.params.travelid, function(err, travel) {
  120. if (err) return res.send(500, err.message);
  121. //add notification to the user who has created the travel
  122. userModel.findOne({
  123. _id: travel.user
  124. })
  125. .exec(function(err, user) {
  126. if (err) return res.send(500, err.message);
  127. if (!user) {
  128. //console.log("Notification not posible, user owner of the travel not exist");
  129. } else if (user) {
  130. var notification = new notificationModel({
  131. concept: "admin",
  132. message: "an admin has deleted your travel: " + travel.title,
  133. date: new Date(),
  134. icon: 'admin',
  135. link: "users/" + user._id,
  136. user: user._id
  137. });
  138. notification.save(function(err, notification) {
  139. if (err) return res.send(500, err.message);
  140. user.notifications.push(notification._id);
  141. user.save(function(err, user) {
  142. if (err) return res.send(500, err.message);
  143. //notification added to user
  144. });
  145. });
  146. }
  147. });
  148. travel.remove(function(err) {
  149. if (err) return res.send(500, err.message);
  150. travelCtrl.getAllTravels(req, res);
  151. });
  152. });
  153. }
  154. });
  155. };
  156. exports.deleteUser = function(req, res) {
  157. adminModel.findOne({
  158. 'token': req.headers['x-access-token']
  159. })
  160. .exec(function(err, admin) {
  161. if (!admin) {
  162. res.json({
  163. success: false,
  164. message: 'Admin not found'
  165. });
  166. } else if (admin) {
  167. if (err) return res.send(500, err.message);
  168. userModel.findOne({
  169. _id: req.params.userid
  170. })
  171. .exec(function(err, user) {
  172. if (err) return res.send(500, err.message);
  173. if (!user) {
  174. res.json({
  175. success: false,
  176. message: 'Delete user not posible, user not exist'
  177. });
  178. } else if (user) {
  179. //delete all the travels of this user
  180. travelModel.find({
  181. user: user._id
  182. }).remove().exec(function(err, data) {});
  183. //delete all the comments of this user
  184. //now delete user
  185. user.remove(function(err) {
  186. if (err) return res.send(500, err.message);
  187. res.status(200).jsonp("deleted");
  188. });
  189. }
  190. });
  191. }
  192. });
  193. };
  194. function isNodeInNodes(node, nodes) {
  195. for (var i = 0; i < nodes.length; i++) {
  196. if (node.title == nodes[i].title) {
  197. return (i);
  198. }
  199. }
  200. return (-1);
  201. }
  202. exports.network = function(req, res) {
  203. adminModel.findOne({
  204. 'token': req.headers['x-access-token']
  205. })
  206. .exec(function(err, admin) {
  207. if (!admin) {
  208. res.json({
  209. success: false,
  210. message: 'Admin not found'
  211. });
  212. } else if (admin) {
  213. if (err) return res.send(500, err.message);
  214. userModel.find()
  215. .limit(pageSize)
  216. .skip(pageSize * Number(req.query.page))
  217. .lean()
  218. //.populate({path: 'travels', populate: {path: 'joins', populate: {path: 'username'}}})
  219. .populate('travels', 'title type joins')
  220. .populate('likes', 'username avatar')
  221. .exec(function(err, users) {
  222. if (err) return res.send(500, err.message);
  223. /*res.status(200).jsonp(users);*/
  224. var nodes = [];
  225. var edges = [];
  226. for (var i = 0; i < users.length; i++) {
  227. var node = {
  228. title: users[i].username,
  229. label: users[i].username,
  230. image: users[i].avatar,
  231. shape: "image",
  232. id: users[i]._id,
  233. group: users[i]._id
  234. };
  235. var lNode = isNodeInNodes(node, nodes);
  236. if (lNode < 0) {
  237. nodes.push(node);
  238. var uNode = nodes.length - 1;
  239. }
  240. for (var j = 0; j < users[i].likes.length; j++) {
  241. /*console.log(i + ", " + j);
  242. console.log(nodes);*/
  243. var node = {
  244. title: users[i].likes[j].username,
  245. label: users[i].likes[j].username,
  246. image: users[i].likes[j].avatar,
  247. shape: "image",
  248. id: users[i].likes[j]._id
  249. };
  250. var lNode = isNodeInNodes(node, nodes);
  251. if (lNode < 0) {
  252. //node no exist
  253. nodes.push(node);
  254. lNode = nodes.length - 1;
  255. } else {
  256. //node already exist
  257. }
  258. var edge = {
  259. from: users[i]._id,
  260. to: users[i].likes[j]._id,
  261. label: "like",
  262. arrows: "to",
  263. color: {
  264. color: "#E57373" //red300
  265. }
  266. };
  267. edges.push(edge);
  268. }
  269. for (var j = 0; j < users[i].travels.length; j++) {
  270. /*console.log(i + ", " + j);
  271. console.log(nodes);*/
  272. var node = {
  273. title: users[i].travels[j].title,
  274. label: users[i].travels[j].title,
  275. image: "img/" + users[i].travels[j].type + ".png",
  276. shape: "image",
  277. id: users[i].travels[j]._id,
  278. value: "0.5",
  279. group: users[i]._id
  280. };
  281. var lNode = isNodeInNodes(node, nodes);
  282. if (lNode < 0) {
  283. //node no exist
  284. nodes.push(node);
  285. lNode = nodes.length - 1;
  286. } else {
  287. //node already exist
  288. }
  289. var edge = {
  290. from: users[i]._id,
  291. to: users[i].travels[j]._id,
  292. color: {
  293. color: "#616161" //grey
  294. }
  295. };
  296. edges.push(edge);
  297. //users joining travels
  298. /*for(var k=0; k<users[i].travels[j].joins.length; k++){
  299. var node = {
  300. title: users[i].travels[j].joins[k].username,
  301. label: users[i].travels[j].joins[k].username,
  302. image: users[i].travels[j].joins[k].avatar,
  303. shape: "image",
  304. id: users[i].travels[j].joins[k]._id
  305. };
  306. var lNode = isNodeInNodes(node, nodes);
  307. if (lNode<0){
  308. //node no exist
  309. nodes.push(node);
  310. lNode = nodes.length -1;
  311. }
  312. var edge={
  313. from: users[i].travels[j].joins[k]._id,
  314. to: users[i].travels[j]._id,
  315. color: {
  316. color: "#4DD0E1"//cyan300
  317. }
  318. };
  319. edges.push(edge);
  320. }*/
  321. }
  322. }
  323. var resp = {
  324. nodes: nodes,
  325. edges: edges
  326. };
  327. res.status(200).jsonp(resp);
  328. });
  329. }
  330. });
  331. };
  332. exports.getUserNetwork = function(req, res) {
  333. adminModel.findOne({
  334. 'token': req.headers['x-access-token']
  335. })
  336. .exec(function(err, admin) {
  337. if (!admin) {
  338. res.json({
  339. success: false,
  340. message: 'Admin not found'
  341. });
  342. } else if (admin) {
  343. if (err) return res.send(500, err.message);
  344. userModel.findOne({
  345. _id: req.params.userid
  346. })
  347. .lean()
  348. .populate('travels', 'title type joins')
  349. .populate('likes', 'username avatar')
  350. .exec(function(err, user) {
  351. if (err) return res.send(500, err.message);
  352. /*res.status(200).jsonp(users);*/
  353. var nodes = [];
  354. var edges = [];
  355. //for (var i = 0; i < users.length; i++) {
  356. var node = {
  357. title: user.username,
  358. label: user.username,
  359. image: user.avatar,
  360. shape: "image",
  361. id: user._id,
  362. group: user._id
  363. };
  364. var lNode = isNodeInNodes(node, nodes);
  365. if (lNode < 0) {
  366. nodes.push(node);
  367. var uNode = nodes.length - 1;
  368. }
  369. for (var j = 0; j < user.likes.length; j++) {
  370. /*console.log(i + ", " + j);
  371. console.log(nodes);*/
  372. var node = {
  373. title: user.likes[j].username,
  374. label: user.likes[j].username,
  375. image: user.likes[j].avatar,
  376. shape: "image",
  377. id: user.likes[j]._id
  378. };
  379. var lNode = isNodeInNodes(node, nodes);
  380. if (lNode < 0) {
  381. //node no exist
  382. nodes.push(node);
  383. lNode = nodes.length - 1;
  384. } else {
  385. //node already exist
  386. }
  387. var edge = {
  388. from: user._id,
  389. to: user.likes[j]._id,
  390. label: "like",
  391. arrows: "to",
  392. color: {
  393. color: "#E57373" //red300
  394. }
  395. };
  396. edges.push(edge);
  397. }
  398. for (var j = 0; j < user.travels.length; j++) {
  399. /*console.log(i + ", " + j);
  400. console.log(nodes);*/
  401. var node = {
  402. title: user.travels[j].title,
  403. label: user.travels[j].title,
  404. image: "img/" + user.travels[j].type + ".png",
  405. shape: "image",
  406. id: user.travels[j]._id,
  407. value: "0.5",
  408. group: user._id
  409. };
  410. var lNode = isNodeInNodes(node, nodes);
  411. if (lNode < 0) {
  412. //node no exist
  413. nodes.push(node);
  414. lNode = nodes.length - 1;
  415. } else {
  416. //node already exist
  417. }
  418. var edge = {
  419. from: user._id,
  420. to: user.travels[j]._id,
  421. color: {
  422. color: "#616161" //grey
  423. }
  424. };
  425. edges.push(edge);
  426. }
  427. //add users that has add like to the user
  428. userModel.find({
  429. likes: req.params.userid
  430. })
  431. .exec(function(err, users) {
  432. for (var i = 0; i < users.length; i++) {
  433. /*console.log(i + ", " + j);
  434. console.log(nodes);*/
  435. var node = {
  436. title: users[i].username,
  437. label: users[i].username,
  438. image: users[i].avatar,
  439. shape: "image",
  440. id: users[i]._id
  441. };
  442. var lNode = isNodeInNodes(node, nodes);
  443. if (lNode < 0) {
  444. //node no exist
  445. nodes.push(node);
  446. lNode = nodes.length - 1;
  447. } else {
  448. //node already exist
  449. }
  450. var edge = {
  451. from: users[i]._id,
  452. to: req.params.userid,
  453. label: "like",
  454. arrows: "to",
  455. color: {
  456. color: "#E57373" //red300
  457. }
  458. };
  459. edges.push(edge);
  460. }
  461. var resp = {
  462. nodes: nodes,
  463. edges: edges
  464. };
  465. res.status(200).jsonp(resp);
  466. });
  467. //}
  468. });
  469. }
  470. });
  471. };
  472. // user validation
  473. exports.validateUser = function(req, res) {
  474. adminModel.findOne({
  475. 'token': req.headers['x-access-token']
  476. })
  477. .exec(function(err, admin) {
  478. if (!admin) {
  479. res.json({
  480. success: false,
  481. message: 'Admin not found'
  482. });
  483. } else if (admin) {
  484. if (err) return res.send(500, err.message);
  485. userModel.findOne({
  486. _id: req.params.userid
  487. })
  488. .exec(function(err, user) {
  489. if (err) return res.send(500, err.message);
  490. user.validated = true;
  491. user.validatedBy = admin._id;
  492. user.save(function(err, user) {
  493. if (err) return res.send(500, err.message);
  494. userController.getUserById(req, res);
  495. });
  496. });
  497. }
  498. });
  499. };
  500. // user unvalidate
  501. exports.unvalidateUser = function(req, res) {
  502. adminModel.findOne({
  503. 'token': req.headers['x-access-token']
  504. })
  505. .exec(function(err, admin) {
  506. if (!admin) {
  507. res.json({
  508. success: false,
  509. message: 'Admin not found'
  510. });
  511. } else if (admin) {
  512. if (err) return res.send(500, err.message);
  513. userModel.findOne({
  514. _id: req.params.userid
  515. })
  516. .exec(function(err, user) {
  517. if (err) return res.send(500, err.message);
  518. user.validated = false;
  519. user.validatedBy = admin._id;
  520. user.save(function(err, user) {
  521. if (err) return res.send(500, err.message);
  522. userController.getUserById(req, res);
  523. });
  524. });
  525. }
  526. });
  527. };