PageRenderTime 133ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 39ms app.codeStats 1ms

/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
  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 */
 17package mpv5.usermanagement;
 18
 19import mpv5.db.objects.User;
 20import java.security.NoSuchAlgorithmException;
 21import java.util.ArrayList;
 22import javax.swing.ComboBoxModel;
 23import javax.swing.DefaultComboBoxModel;
 24import mpv5.db.common.Context;
 25import mpv5.globals.Messages;
 26import mpv5.logging.Log;
 27import mpv5.ui.dialogs.Popup;
 28import mpv5.ui.frames.MPView;
 29import mpv5.utils.models.MPComboBoxModelItem;
 30import mpv5.utils.text.MD5HashGenerator;
 31
 32/**
 33 *
 34 *  
 35 */
 36public class MPSecurityManager {
 37
 38    public static final int SYSTEM_RIGHT = -1;
 39    public static final int RIGHT_TO_VIEW = 4;
 40    public static final int RIGHT_TO_EXPORT = 3;
 41    public static final int RIGHT_TO_EDIT = 2;
 42    public static final int RIGHT_TO_CREATE_OR_DELETE = 1;
 43    public static final int RIGHT_TO_ADMINISTRATE = 0;
 44    public static final int VIEW = 4;
 45    public static final int EXPORT = 3;
 46    public static final int EDIT = 2;
 47    public static final int CREATE_OR_DELETE = 1;
 48    public static final int ADMINISTRATE = 0;
 49    public static ArrayList<Context> securedContexts = Context.getSecuredContexts();
 50    private static String usern;
 51    private static Object[][] availableRights = new Object[][]{
 52        {RIGHT_TO_ADMINISTRATE, "Administrator"},
 53        {RIGHT_TO_CREATE_OR_DELETE, "User"},
 54        {RIGHT_TO_EDIT, "Editor"},
 55        {RIGHT_TO_EXPORT, "Exporter"},
 56        {RIGHT_TO_VIEW, "Viewer"}
 57    };
 58
 59    /**
 60     * Checks whether the currently logged in user has to right to do this
 61     * action in the given context
 62     * @param context
 63     * @param action
 64     * @return True if the highest right of the user is equal or higher as
 65     * the right to do requested action
 66     */
 67    public static Boolean check(Context context, int action) {
 68        for (Context item : securedContexts) {
 69            if (item.getDbIdentity().equals(context.getDbIdentity())) {
 70                if (mpv5.db.objects.User.getCurrentUser().__getInthighestright() <= action) {
 71                    return true;
 72                } else {
 73                    return false;
 74                }
 75            }
 76        }
 77        return true;
 78    }
 79
 80    /**
 81     * Checks whether the currently logged in user has to right to do admin tasks
 82     * @return
 83     */
 84    public static boolean checkAdminAccess() {
 85        if (mpv5.db.objects.User.getCurrentUser().__getInthighestright() <= RIGHT_TO_ADMINISTRATE) {
 86            return true;
 87        } else {
 88            Popup.notice(Messages.ADMIN_ACCESS);
 89            return false;
 90        }
 91    }
 92
 93    /**
 94     * Checks the credentials for this user. Will return NULL if the user is <br/>
 95     * not existing, disabled or the wrong password is provided.
 96     * @param username
 97     * @param password
 98     * @return
 99     */
100    public static User checkAuth(String username, String password) {
101        User usern1 = new User();
102  
103            if (usern1.fetchDataOf(username)) {
104                try {
105                    if (MD5HashGenerator.getInstance().hashData(password.getBytes()).equalsIgnoreCase(usern1.__getPassword())) {
106                        return usern1;
107                    } else {
108                        return null;
109                    }
110                } catch (NoSuchAlgorithmException ex) {
111                    Log.Debug(MPSecurityManager.class, ex);
112                    return null;
113                }
114            } else {
115                return null;
116            }
117    }
118
119    /**
120     * Checks the user credentials against the stored hash in cleartext, only for internal use!
121     * @param user
122     * @param passwordhash
123     * @return
124     */
125    public static User checkAuthInternal(User user, String passwordhash)
126    {
127        if (passwordhash.equalsIgnoreCase(user.__getPassword())) {
128            return user;
129        } else {
130            return null;
131        }
132    }
133
134    public static String getActionName(int action) {
135
136        switch (action) {
137
138            case CREATE_OR_DELETE:
139                return Messages.ACTION_CREATE.getValue();
140
141            case EDIT:
142                return Messages.ACTION_EDIT.getValue();
143
144            case EXPORT:
145                return Messages.ACTION_EXPORT.getValue();
146
147            case VIEW:
148                return Messages.ACTION_VIEW.getValue();
149        }
150
151        return null;
152    }
153
154    public static ComboBoxModel getRolesAsComboBoxModel() {
155        Object[][] data = availableRights;
156        MPComboBoxModelItem[] t = null;
157        t = MPComboBoxModelItem.toItems(data);
158        return new DefaultComboBoxModel(t);
159    }
160}