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.server.users; 020 021import java.util.Collection; 022import java.util.HashMap; 023 024import java.util.Set; 025 026/** 027 * This class stores the list of users of Dicoogle 028 * 029 * @author Samuel Campos <samuelcampos@ua.pt> 030 */ 031public class UsersStruct { 032 private HashMap<String, User> users; 033 034 private static UsersStruct instance = null ; 035 036 // count the number of administrators 037 private int numberOfAdmins; 038 039 public static synchronized UsersStruct getInstance() { 040 if (instance == null) { 041 instance = new UsersStruct(); 042 } 043 044 return instance; 045 } 046 047 048 049 private UsersStruct(){ 050 reset(); 051 } 052 053 054 /** 055 * Insert one default user 056 * Username: "dicoogle" 057 * Password: "DCpassword" (hashed) 058 * 059 * This user is administrator 060 */ 061 public void setDefaults(){ 062 //DebugManager.getInstance().debug("Setting default user settings"); 063 064 String username = "dicoogle"; 065 boolean admin = true; 066 String passPlainText = "dicoogle"; 067 068 String passHash = HashService.getSHA1Hash(passPlainText); //password Hash 069 String Hash = HashService.getSHA1Hash(username + admin + passHash); //user Hash 070 071 users = new HashMap<String, User>(); 072 users.put("dicoogle", new User(username, Hash, admin)); 073 } 074 075 /** 076 * Used only by UsersXML to reset User Settings 077 */ 078 protected void reset(){ 079 users = new HashMap<String, User>(); 080 numberOfAdmins = 0; 081 } 082 083 084 /** 085 * Insert user in the List of users 086 * 087 * @param user 088 * @return true - if succeeded. false - if the username already exists 089 */ 090 public boolean addUser(User user){ 091 if(users.containsKey(user.getUsername())) 092 return false; 093 094 users.put(user.getUsername(), user); 095 096 if(user.isAdmin()) 097 numberOfAdmins++; 098 099 return true; 100 } 101 102 /** 103 * Removes one user from de list 104 * Maintains at least one administrator in the list 105 * (refuses to remove the last one) 106 * 107 * @param username 108 * @return 109 */ 110 public boolean removeUser(String username){ 111 if(username == null) 112 return false; 113 114 User user = users.get(username); 115 if (user == null) 116 return false; 117 118 if(user.isAdmin() && numberOfAdmins == 1) 119 return false; 120 else if(user.isAdmin()) 121 numberOfAdmins--; 122 123 users.remove(username); 124 return true; 125 } 126 127 public Collection<User> getUsers(){ 128 return users.values(); 129 } 130 131 public Set<String> getUsernames(){ 132 return users.keySet(); 133 } 134 135 public User getUser(String username){ 136 return users.get(username); 137 } 138}