user_management.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. Modified by Conner Hansen (chansen@crimson.ua.edu)
  5. This file is part of AToMPM.
  6. AToMPM is free software: you can redistribute it and/or modify it under the
  7. terms of the GNU Lesser General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later
  9. version.
  10. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  15. *******************************************************************************/
  16. /**
  17. * Creates the User Management object
  18. */
  19. UserManagement = function(){
  20. /**
  21. * Returns whether or not there is a current user logged in
  22. */
  23. this.isUserLoggedIn = function()
  24. {
  25. return window.localStorage.getItem('user');
  26. };
  27. /**
  28. * Logs in the current user
  29. * @param username the user to log in
  30. */
  31. this.login = function(username)
  32. {
  33. __user = username;
  34. window.localStorage.setItem('user',__user);
  35. __initClient();
  36. };
  37. /**
  38. * Logs out the current user
  39. */
  40. this.logout = function()
  41. {
  42. function innerLogout()
  43. {
  44. __user = undefined;
  45. window.localStorage.removeItem('user');
  46. window.location = window.location.href;
  47. }
  48. if( __prefs && __prefs['confirm-exit']['value'] && ! __isSaved() )
  49. WindowManagement.openDialog(_CUSTOM, {'widgets':[], 'title':__EXITWARNING}, innerLogout);
  50. else
  51. innerLogout();
  52. };
  53. /**
  54. * Creates a new user
  55. * @param username the username of the new user
  56. * @param password the password for the user
  57. */
  58. this.signup = function(username,password)
  59. {
  60. $('#div_signup_error').html('');
  61. HttpUtils.httpReq(
  62. 'POST',
  63. '/user?username='+username+'&password='+Sha1.hash(password),
  64. undefined,
  65. function(statusCode,resp)
  66. {
  67. if( ! utils.isHttpSuccessCode(statusCode) )
  68. $('#div_signup_error').html(
  69. 'acccount creation failed :: '+resp);
  70. else
  71. {
  72. WindowManagement.hideLoginScreen();
  73. login(username);
  74. }
  75. });
  76. };
  77. /**
  78. * Validates the associated username with a password
  79. * @param username the user the validate
  80. * @param password the password to validate against
  81. */
  82. this.validateCredentials = function(username,password)
  83. {
  84. $('#div_login_error').html('');
  85. HttpUtils.httpReq(
  86. 'GET',
  87. '/passwd',
  88. '?username='+username,
  89. function(statusCode,resp)
  90. {
  91. if( ! utils.isHttpSuccessCode(statusCode) )
  92. $('#div_login_error').html('login failed, user ' + username + ' not found or network error');
  93. else if( resp != Sha1.hash(password) )
  94. $('#div_login_error').html('incorrect password');
  95. else
  96. {
  97. WindowManagement.hideLoginScreen();
  98. login(username);
  99. }
  100. });
  101. };
  102. return this;
  103. }();