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 java.security.Principal;
022import java.util.Objects;
023
024/**
025 * A {@link Principal} representing a role used by web console
026 * components. Console roles are usually not managed. Rather, the
027 * information is derived from some authentication system and provided
028 * to console components in a way that is independent from a particular
029 * authentication strategy. 
030 */
031public class ConsoleRole implements Principal {
032
033    private final String name;
034    private final String displayName;
035
036    /**
037     * Instantiates a new console role.
038     *
039     * @param name the name
040     * @param displayName the display name
041     */
042    public ConsoleRole(String name, String displayName) {
043        super();
044        this.name = name;
045        this.displayName = displayName;
046    }
047
048    /**
049     * Instantiates a new console role with the name also being used 
050     * as display name.
051     *
052     * @param name the name
053     */
054    public ConsoleRole(String name) {
055        this(name, name);
056    }
057
058    /**
059     * Gets the unique name. Used as key when storing or retrieving
060     * user specific information.
061     *
062     * @return the name
063     */
064    @Override
065    public String getName() {
066        return name;
067    }
068
069    /**
070     * The name to use when showing the user to humans.
071     *
072     * @return the display name
073     */
074    public String getDisplayName() {
075        return displayName;
076    }
077
078    @Override
079    public int hashCode() {
080        return Objects.hash(name);
081    }
082
083    @Override
084    public boolean equals(Object obj) {
085        if (this == obj) {
086            return true;
087        }
088        if (obj == null) {
089            return false;
090        }
091        if (getClass() != obj.getClass()) {
092            return false;
093        }
094        ConsoleRole other = (ConsoleRole) obj;
095        return Objects.equals(name, other.name);
096    }
097
098    /**
099     * Returns the name followed by the display name in parenthesis.
100     * 
101     * @return the string
102     */
103    @Override
104    public String toString() {
105        return name + " (" + displayName + ")";
106    }
107
108}