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 */ 019 020package pt.ua.dicoogle.server.users; 021 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Objects; 025 026/** 027 * Class that saves information about one user 028 * 029 * @author Samuel Campos <samuelcampos@ua.pt> 030 * @author Luís Bastião Silva <bastiao@bmd-software.com> 031 */ 032public class User implements UserRoleManager{ 033 034 private final String username; 035 private String hash; //stores the Hash of this user (username + admin + passwordHash) 036 private final boolean admin; 037 038 private List<Role> roles = new ArrayList<>(); 039 040 public User(String username, String Hash, boolean admin){ 041 this.username = username; 042 this.admin = admin; 043 this.hash = Hash; 044 } 045 046 public String getUsername(){ 047 return username; 048 } 049 050 public boolean isAdmin(){ 051 return admin; 052 } 053 054 public boolean verifyPassword(String passwordHash){ 055 String tempHash = HashService.getSHA1Hash(username + admin + passwordHash); 056 057 return this.hash.equals(tempHash); 058 } 059 060 public void addRole(Role r) 061 { 062 this.roles.add(r); 063 } 064 065 public boolean hasRole(Role r) 066 { 067 return this.roles.contains(r); 068 } 069 070 public boolean changePassword(String oldPassHash, String newPassHash){ 071 String tempHash = HashService.getSHA1Hash(username + admin + oldPassHash); 072 073 if(!hash.equals(tempHash)) 074 return false; 075 076 077 tempHash = HashService.getSHA1Hash(username + admin + newPassHash); 078 079 hash = tempHash; 080 return true; 081 } 082 083 protected String getPasswordHash(){ 084 return hash; 085 } 086 087 public boolean resetPassword(String newPassHash){ 088 if(newPassHash == null || newPassHash.equals("")) 089 return false; 090 091 String tempHash = HashService.getSHA1Hash(username + admin + newPassHash); 092 093 hash = tempHash; 094 095 return true; 096 } 097 098 @Override 099 public boolean equals(Object other) { 100 if (other == null || other.getClass() != getClass()) { 101 return false; 102 } 103 104 if (other == this) { 105 return true; 106 } 107 108 User tmp = (User) other; 109 110 if (username.equals(tmp.username) && hash.equals(tmp.hash) 111 && admin == tmp.admin) { 112 return true; 113 } 114 115 return false; 116 } 117 118 @Override 119 public int hashCode() { 120 int hash = 7; 121 hash = 67 * hash + Objects.hashCode(this.username); 122 hash = 67 * hash + Objects.hashCode(this.hash); 123 hash = 67 * hash + (this.admin ? 1 : 0); 124 return hash; 125 } 126 127 @Override 128 public String toString() { 129 return "User{" + username + (admin ? ", admin" : "") + '}'; 130 } 131 132 public List<Role> getRoles() { 133 return roles; 134 } 135}