user_management.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. * Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. * See COPYING.lesser and README.md in the root of this project for full details
  4. */
  5. /**
  6. * Creates the User Management object
  7. */
  8. UserManagement = function(){
  9. /**
  10. * Returns whether or not there is a current user logged in
  11. */
  12. this.isUserLoggedIn = function()
  13. {
  14. return window.localStorage.getItem('user');
  15. };
  16. /**
  17. * Logs in the current user
  18. * @param username the user to log in
  19. */
  20. this.login = function(username)
  21. {
  22. __user = username;
  23. window.localStorage.setItem('user',__user);
  24. __initClient();
  25. };
  26. /**
  27. * Logs out the current user
  28. */
  29. this.logout = function()
  30. {
  31. function innerLogout()
  32. {
  33. __user = undefined;
  34. window.localStorage.removeItem('user');
  35. window.location = window.location.href;
  36. }
  37. if( __prefs && __prefs['confirm-exit']['value'] && ! __isSaved() )
  38. WindowManagement.openDialog(_CUSTOM, {'widgets':[], 'title':__EXITWARNING}, innerLogout);
  39. else
  40. innerLogout();
  41. };
  42. /**
  43. * Creates a new user
  44. * @param username the username of the new user
  45. * @param password the password for the user
  46. */
  47. this.signup = function(username,password)
  48. {
  49. $('#div_signup_error').html('');
  50. HttpUtils.httpReq(
  51. 'POST',
  52. '/user?username='+username+'&password='+Sha1.hash(password),
  53. undefined,
  54. function(statusCode,resp)
  55. {
  56. if( ! utils.isHttpSuccessCode(statusCode) )
  57. $('#div_signup_error').html(
  58. 'acccount creation failed :: '+resp);
  59. else
  60. {
  61. WindowManagement.hideLoginScreen();
  62. login(username);
  63. }
  64. });
  65. };
  66. /**
  67. * Validates the associated username with a password
  68. * @param username the user the validate
  69. * @param password the password to validate against
  70. */
  71. this.validateCredentials = function(username,password)
  72. {
  73. $('#div_login_error').html('');
  74. HttpUtils.httpReq(
  75. 'GET',
  76. '/passwd',
  77. '?username='+username,
  78. function(statusCode,resp)
  79. {
  80. if( ! utils.isHttpSuccessCode(statusCode) )
  81. $('#div_login_error').html('login failed, user ' + username + ' not found or network error');
  82. else if( resp != Sha1.hash(password) )
  83. $('#div_login_error').html('incorrect password');
  84. else
  85. {
  86. WindowManagement.hideLoginScreen();
  87. login(username);
  88. }
  89. });
  90. };
  91. return this;
  92. }();