PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/usermanagement/MPSecurityManager.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 160 lines | 99 code | 16 blank | 45 comment | 13 complexity | 285ac33966043eadd1ae95d08a460ee9 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.usermanagement;
  18. import mpv5.db.objects.User;
  19. import java.security.NoSuchAlgorithmException;
  20. import java.util.ArrayList;
  21. import javax.swing.ComboBoxModel;
  22. import javax.swing.DefaultComboBoxModel;
  23. import mpv5.db.common.Context;
  24. import mpv5.globals.Messages;
  25. import mpv5.logging.Log;
  26. import mpv5.ui.dialogs.Popup;
  27. import mpv5.ui.frames.MPView;
  28. import mpv5.utils.models.MPComboBoxModelItem;
  29. import mpv5.utils.text.MD5HashGenerator;
  30. /**
  31. *
  32. *
  33. */
  34. public class MPSecurityManager {
  35. public static final int SYSTEM_RIGHT = -1;
  36. public static final int RIGHT_TO_VIEW = 4;
  37. public static final int RIGHT_TO_EXPORT = 3;
  38. public static final int RIGHT_TO_EDIT = 2;
  39. public static final int RIGHT_TO_CREATE_OR_DELETE = 1;
  40. public static final int RIGHT_TO_ADMINISTRATE = 0;
  41. public static final int VIEW = 4;
  42. public static final int EXPORT = 3;
  43. public static final int EDIT = 2;
  44. public static final int CREATE_OR_DELETE = 1;
  45. public static final int ADMINISTRATE = 0;
  46. public static ArrayList<Context> securedContexts = Context.getSecuredContexts();
  47. private static String usern;
  48. private static Object[][] availableRights = new Object[][]{
  49. {RIGHT_TO_ADMINISTRATE, "Administrator"},
  50. {RIGHT_TO_CREATE_OR_DELETE, "User"},
  51. {RIGHT_TO_EDIT, "Editor"},
  52. {RIGHT_TO_EXPORT, "Exporter"},
  53. {RIGHT_TO_VIEW, "Viewer"}
  54. };
  55. /**
  56. * Checks whether the currently logged in user has to right to do this
  57. * action in the given context
  58. * @param context
  59. * @param action
  60. * @return True if the highest right of the user is equal or higher as
  61. * the right to do requested action
  62. */
  63. public static Boolean check(Context context, int action) {
  64. for (Context item : securedContexts) {
  65. if (item.getDbIdentity().equals(context.getDbIdentity())) {
  66. if (mpv5.db.objects.User.getCurrentUser().__getInthighestright() <= action) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. }
  72. }
  73. return true;
  74. }
  75. /**
  76. * Checks whether the currently logged in user has to right to do admin tasks
  77. * @return
  78. */
  79. public static boolean checkAdminAccess() {
  80. if (mpv5.db.objects.User.getCurrentUser().__getInthighestright() <= RIGHT_TO_ADMINISTRATE) {
  81. return true;
  82. } else {
  83. Popup.notice(Messages.ADMIN_ACCESS);
  84. return false;
  85. }
  86. }
  87. /**
  88. * Checks the credentials for this user. Will return NULL if the user is <br/>
  89. * not existing, disabled or the wrong password is provided.
  90. * @param username
  91. * @param password
  92. * @return
  93. */
  94. public static User checkAuth(String username, String password) {
  95. User usern1 = new User();
  96. if (usern1.fetchDataOf(username)) {
  97. try {
  98. if (MD5HashGenerator.getInstance().hashData(password.getBytes()).equalsIgnoreCase(usern1.__getPassword())) {
  99. return usern1;
  100. } else {
  101. return null;
  102. }
  103. } catch (NoSuchAlgorithmException ex) {
  104. Log.Debug(MPSecurityManager.class, ex);
  105. return null;
  106. }
  107. } else {
  108. return null;
  109. }
  110. }
  111. /**
  112. * Checks the user credentials against the stored hash in cleartext, only for internal use!
  113. * @param user
  114. * @param passwordhash
  115. * @return
  116. */
  117. public static User checkAuthInternal(User user, String passwordhash)
  118. {
  119. if (passwordhash.equalsIgnoreCase(user.__getPassword())) {
  120. return user;
  121. } else {
  122. return null;
  123. }
  124. }
  125. public static String getActionName(int action) {
  126. switch (action) {
  127. case CREATE_OR_DELETE:
  128. return Messages.ACTION_CREATE.getValue();
  129. case EDIT:
  130. return Messages.ACTION_EDIT.getValue();
  131. case EXPORT:
  132. return Messages.ACTION_EXPORT.getValue();
  133. case VIEW:
  134. return Messages.ACTION_VIEW.getValue();
  135. }
  136. return null;
  137. }
  138. public static ComboBoxModel getRolesAsComboBoxModel() {
  139. Object[][] data = availableRights;
  140. MPComboBoxModelItem[] t = null;
  141. t = MPComboBoxModelItem.toItems(data);
  142. return new DefaultComboBoxModel(t);
  143. }
  144. }