001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.rGUI.server.controllers;
020
021import java.rmi.RemoteException;
022import java.util.ArrayList;
023import java.util.Iterator;
024import pt.ua.dicoogle.core.ServerSettings;
025import pt.ua.dicoogle.rGUI.interfaces.controllers.IUsersManager;
026import pt.ua.dicoogle.server.users.HashService;
027import pt.ua.dicoogle.server.users.User;
028import pt.ua.dicoogle.server.users.UsersStruct;
029import pt.ua.dicoogle.server.users.UsersXML;
030
031/**
032 * Controller of Users Manager Settings
033 *
034 * @author Samuel Campos <samuelcampos@ua.pt>
035 */
036@Deprecated
037public class UsersManager implements IUsersManager {
038    private static UsersManager instance = null;
039
040    private UsersStruct users;
041
042    private boolean encryptUsersFile;
043
044    //flag to indicate when setting are unsaved
045    private boolean unsavedSettings;
046
047    public static synchronized UsersManager getInstance() {
048        if (instance == null) {
049            instance = new UsersManager();
050        }
051
052        return instance;
053    }
054
055    private UsersManager(){
056        users = UsersStruct.getInstance();
057
058        unsavedSettings = false;
059        encryptUsersFile = ServerSettings.getInstance().isEncryptUsersFile();
060    }
061
062    public void loadSettings(){
063        unsavedSettings = false;
064
065        UsersXML xml = new UsersXML();
066        xml.getXML();
067
068        encryptUsersFile = ServerSettings.getInstance().isEncryptUsersFile();
069    }
070    
071    /**
072     *
073     * @return  true - if there are unsaved settings
074     *          false - not
075     */
076    public boolean unsavedSettings(){
077        return unsavedSettings || (encryptUsersFile != ServerSettings.getInstance().isEncryptUsersFile());
078    }
079
080    /**
081     * Save the current Users configuration
082     *
083     *  write in XML
084     */
085    public void saveSettings(){
086        ServerSettings.getInstance().setEncryptUsersFile(encryptUsersFile);
087        
088        UsersXML xml = new UsersXML();
089        xml.printXML();
090
091        unsavedSettings = false;
092    }
093
094    @Override
095    public ArrayList<String> getUsernames() throws RemoteException {
096
097        Iterator<String> en = users.getUsernames().iterator();
098
099        ArrayList<String> list = new ArrayList<String>();
100
101        while(en.hasNext())
102            list.add(en.next());
103
104        list.trimToSize();
105
106        return list;
107    }
108
109    @Override
110    public boolean isAdmin(String username) throws RemoteException {
111        User user = users.getUser(username);
112
113        if(user != null && user.isAdmin())
114            return true;
115
116        return false;
117    }
118
119    /**
120     * Remove one user
121     *
122     * @param username
123     * @return  true if the user is removed
124     *          false if username doesn't exist
125     * @throws RemoteException
126     */
127    @Override
128    public boolean deleteUser(String username) throws RemoteException {
129        boolean temp = users.removeUser(username);
130
131        if (temp){
132            unsavedSettings = true;
133            Logs.getInstance().addServerLog("User " + username + " deleted");
134        }
135
136        return temp;
137    }
138
139    /**
140     * Adds one user to the user list
141     * 
142     * @param username
143     * @param passwordHash
144     * @param admin
145     * 
146     * @return  true if user is successfully addes to the list
147     *          false if the username already exists or username or password are invalid
148     * 
149     * @throws RemoteException
150     */
151    @Override
152    public boolean addUser(String username, String passwordHash, boolean admin) throws RemoteException {
153        if(username == null || username.equals("") || passwordHash == null || username.equals("") || passwordHash.equals(""))
154            return false;
155
156        String Hash = HashService.getSHA1Hash(username + admin + passwordHash);   //user Hash
157
158        User user = new User(username, Hash, admin);
159
160        boolean temp = users.addUser(user);
161
162        if(temp){
163            unsavedSettings = true;
164            Logs.getInstance().addServerLog("User " + username + " created");
165        }
166
167
168        return temp;
169    }
170
171    @Override
172    public boolean getEncryptUsersFile() throws RemoteException {
173        return encryptUsersFile;
174    }
175
176    @Override
177    public void setEncryptUsersFile(boolean value) throws RemoteException {
178        encryptUsersFile = value;
179    }
180
181    @Override
182    public boolean resetPassword(String username, String passwordHash) throws RemoteException {
183        if(username == null || username.equals("") || passwordHash == null || username.equals("") || passwordHash.equals(""))
184            return false;
185
186        User user = users.getUser(username);
187
188        if(user != null && user.resetPassword(passwordHash)){
189            unsavedSettings = true;
190
191            return true;
192        }
193
194        return false;
195    }
196
197    
198}