001/* 002 * JGrapes Event Driven Framework 003 * Copyright (C) 2022 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Affero General Public License as published by 007 * the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License along 016 * with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jgrapes.webconsole.base; 020 021import jakarta.mail.Address; 022import java.security.Principal; 023import java.util.Objects; 024 025/** 026 * A {@link Principal} representing an identity used by web console 027 * components. Console users are usually not managed. Rather, the 028 * information is derived from some authentication system and provided 029 * to console components in a way that is independent from a particular 030 * authentication strategy. 031 */ 032public class ConsoleUser implements Principal { 033 034 private final String name; 035 private final String displayName; 036 private Address email; 037 038 /** 039 * Instantiates a new console user. Note that `name` must be unique. 040 * 041 * @param name the name 042 * @param displayName the display name 043 */ 044 public ConsoleUser(String name, String displayName) { 045 super(); 046 this.name = name; 047 this.displayName = displayName; 048 } 049 050 /** 051 * Instantiates a new console user with the name also being used 052 * as display name. 053 * 054 * @param name the name 055 */ 056 public ConsoleUser(String name) { 057 this(name, name); 058 } 059 060 /** 061 * Gets the unique name. Used as key when storing or retrieving 062 * user specific information. 063 * 064 * @return the name 065 */ 066 @Override 067 public String getName() { 068 return name; 069 } 070 071 /** 072 * The name to use when showing the user to humans. 073 * 074 * @return the display name 075 */ 076 public String getDisplayName() { 077 return displayName; 078 } 079 080 /** 081 * Gets the email address. 082 * 083 * @return the email 084 */ 085 public Address email() { 086 return email; 087 } 088 089 /** 090 * Sets the email address. 091 * 092 * @param email the new email 093 */ 094 public ConsoleUser setEmail(Address email) { 095 this.email = email; 096 return this; 097 } 098 099 @Override 100 public int hashCode() { 101 return Objects.hash(name); 102 } 103 104 @Override 105 public boolean equals(Object obj) { 106 if (this == obj) { 107 return true; 108 } 109 if (obj == null) { 110 return false; 111 } 112 if (getClass() != obj.getClass()) { 113 return false; 114 } 115 ConsoleUser other = (ConsoleUser) obj; 116 return Objects.equals(name, other.name); 117 } 118 119 /** 120 * Returns the name followed by the display name in parenthesis. 121 * 122 * @return the string 123 */ 124 @Override 125 public String toString() { 126 return name + " (" + displayName + ")"; 127 } 128 129}