PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/user.js

https://bitbucket.org/cfield/adafruit-webide
JavaScript | 144 lines | 119 code | 17 blank | 8 comment | 19 complexity | c6310911f38a068e296fb8b0b91bc3b5 MD5 | raw file
  1. var redis = require("redis"),
  2. client = redis.createClient(),
  3. scripts_helper = require('../helpers/scripts_helper'),
  4. config = require('../config/config'),
  5. check = require('validator').check,
  6. sanitize = require('validator').sanitize;
  7. exports.login = function(req, res){
  8. res.render('users/login', { title: 'test', user: req.user, github: config.editor.github });
  9. };
  10. exports.logout = function(req, res){
  11. req.logout();
  12. res.redirect('/');
  13. };
  14. // Instructional page that displays the bitbucket setup steps,
  15. // and inputs for OAuth and Git config
  16. exports.setup = function(req, res) {
  17. var locals = {
  18. consumer_key: "",
  19. consumer_secret: "",
  20. name: "",
  21. email: "",
  22. hostname: "",
  23. github: config.editor.github
  24. };
  25. res.render('users/setup', locals);
  26. };
  27. // Saves the bitbucket and git config setup information in Redis,
  28. // submitted as a post from /setup
  29. exports.submit_setup = function(req, res) {
  30. var key, secret, name, email, message;
  31. req.session.message = undefined;
  32. function common_setup(name, email) {
  33. client.hmset("user", "name", name, "email", email, function() {
  34. req.session.message = "Settings Successfully Configured.";
  35. res.redirect('/login');
  36. });
  37. }
  38. try {
  39. key = sanitize(req.body.key).xss().trim();
  40. secret = sanitize(req.body.secret).xss().trim();
  41. name = sanitize(req.body.name).xss().trim();
  42. email = sanitize(req.body.email).xss().trim();
  43. check(email).isEmail();
  44. } catch (e) {
  45. req.session.message = e.message;
  46. console.log(e.message);
  47. }
  48. if (key && secret && name && email) {
  49. if (config.editor.github) {
  50. client.hmset("github_oauth", "consumer_key", key, "consumer_secret", secret, function() {
  51. common_setup(name, email);
  52. });
  53. } else {
  54. client.hmset("bitbucket_oauth", "consumer_key", key, "consumer_secret", secret, function() {
  55. common_setup(name, email);
  56. });
  57. }
  58. } else {
  59. if (!req.session.message) {
  60. req.session.message = "Please set all fields, at the bottom of this page, in order to continue.";
  61. }
  62. res.redirect('/setup');
  63. }
  64. };
  65. exports.config = function(req, res) {
  66. client.hgetall('server', function (err, server) {
  67. var locals = {
  68. hostname: "",
  69. wifi_ssid: "",
  70. wifi_password: "",
  71. port: (server ? (server.port || "") : "")
  72. };
  73. res.render('users/config', locals);
  74. });
  75. };
  76. // Saves the bitbucket and git config setup information in Redis,
  77. // submitted as a post from /setup
  78. //TODO: Refactor this...it's out of control!
  79. exports.submit_config = function(req, res) {
  80. var key, secret, name, email, message;
  81. req.session.message = undefined;
  82. try {
  83. hostname = sanitize(req.body.hostname).xss().trim();
  84. wifi_ssid = sanitize(req.body.wifi_ssid).xss().trim();
  85. wifi_password = sanitize(req.body.wifi_password).xss().trim();
  86. port = sanitize(req.body.port).xss().trim();
  87. if (hostname) {
  88. check(hostname).len(3, 25);
  89. }
  90. if (port) {
  91. check(port).isNumeric().min(1).max(65535);
  92. }
  93. } catch (e) {
  94. req.session.message = e.message;
  95. console.log(e.message);
  96. }
  97. if (req.session.message) {
  98. res.redirect('/config');
  99. } else {
  100. //change the wifi without waiting for it
  101. if (wifi_ssid && wifi_password) {
  102. scripts_helper.change_wifi(wifi_ssid, wifi_password, function(err) {
  103. req.session.message = "Settings Successfully Configured.";
  104. });
  105. }
  106. if (port) {
  107. client.hmset("server", "port", port, function() {
  108. });
  109. }
  110. if (hostname) {
  111. scripts_helper.change_hostname(hostname, function(err) {
  112. req.session.message = "Settings Successfully Configured.";
  113. res.redirect('http://' + hostname + '.local/login');
  114. });
  115. } else {
  116. if (port) {
  117. req.session.message = "Please restart the server for port changes to take effect.";
  118. }
  119. res.redirect('/login');
  120. }
  121. }
  122. };
  123. exports.set_datetime = function(req, res) {
  124. scripts_helper.set_datetime(function() {
  125. res.redirect('/login');
  126. });
  127. };