All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.catalina.users.MemoryUser Maven / Gradle / Ivy

There is a newer version: 11.0.0-M20
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.apache.catalina.users;


import java.util.ArrayList;
import java.util.Iterator;

import org.apache.catalina.Group;
import org.apache.catalina.Role;
import org.apache.catalina.UserDatabase;
import org.apache.tomcat.util.buf.StringUtils;
import org.apache.tomcat.util.security.Escape;

/**
 * 

Concrete implementation of {@link org.apache.catalina.User} for the * {@link MemoryUserDatabase} implementation of {@link UserDatabase}.

* * @author Craig R. McClanahan * @since 4.1 */ public class MemoryUser extends AbstractUser { // ----------------------------------------------------------- Constructors /** * Package-private constructor used by the factory method in * {@link MemoryUserDatabase}. * * @param database The {@link MemoryUserDatabase} that owns this user * @param username Logon username of the new user * @param password Logon password of the new user * @param fullName Full name of the new user */ MemoryUser(MemoryUserDatabase database, String username, String password, String fullName) { super(); this.database = database; setUsername(username); setPassword(password); setFullName(fullName); } // ----------------------------------------------------- Instance Variables /** * The {@link MemoryUserDatabase} that owns this user. */ protected final MemoryUserDatabase database; /** * The set of {@link Group}s that this user is a member of. */ protected final ArrayList groups = new ArrayList<>(); /** * The set of {@link Role}s associated with this user. */ protected final ArrayList roles = new ArrayList<>(); // ------------------------------------------------------------- Properties /** * Return the set of {@link Group}s to which this user belongs. */ @Override public Iterator getGroups() { synchronized (groups) { return groups.iterator(); } } /** * Return the set of {@link Role}s assigned specifically to this user. */ @Override public Iterator getRoles() { synchronized (roles) { return roles.iterator(); } } /** * Return the {@link UserDatabase} within which this User is defined. */ @Override public UserDatabase getUserDatabase() { return this.database; } // --------------------------------------------------------- Public Methods /** * Add a new {@link Group} to those this user belongs to. * * @param group The new group */ @Override public void addGroup(Group group) { synchronized (groups) { if (!groups.contains(group)) { groups.add(group); } } } /** * Add a new {@link Role} to those assigned specifically to this user. * * @param role The new role */ @Override public void addRole(Role role) { synchronized (roles) { if (!roles.contains(role)) { roles.add(role); } } } /** * Is this user in the specified group? * * @param group The group to check */ @Override public boolean isInGroup(Group group) { synchronized (groups) { return groups.contains(group); } } /** * Is this user specifically assigned the specified {@link Role}? This * method does NOT check for roles inherited based on * {@link Group} membership. * * @param role The role to check */ @Override public boolean isInRole(Role role) { synchronized (roles) { return roles.contains(role); } } /** * Remove a {@link Group} from those this user belongs to. * * @param group The old group */ @Override public void removeGroup(Group group) { synchronized (groups) { groups.remove(group); } } /** * Remove all {@link Group}s from those this user belongs to. */ @Override public void removeGroups() { synchronized (groups) { groups.clear(); } } /** * Remove a {@link Role} from those assigned to this user. * * @param role The old role */ @Override public void removeRole(Role role) { synchronized (roles) { roles.remove(role); } } /** * Remove all {@link Role}s from those assigned to this user. */ @Override public void removeRoles() { synchronized (roles) { roles.clear(); } } /** *

Return a String representation of this user in XML format.

* *

IMPLEMENTATION NOTE - For backwards compatibility, * the reader that processes this entry will accept either * username or name for the username * property.

* @return the XML representation */ public String toXml() { StringBuilder sb = new StringBuilder(" 0) { sb.append(" groups=\""); StringUtils.join(groups, ',', (x) -> Escape.xml(x.getGroupname()), sb); sb.append("\""); } } synchronized (roles) { if (roles.size() > 0) { sb.append(" roles=\""); StringUtils.join(roles, ',', (x) -> Escape.xml(x.getRolename()), sb); sb.append("\""); } } sb.append("/>"); return sb.toString(); } /** *

Return a String representation of this user.

*/ @Override public String toString() { StringBuilder sb = new StringBuilder("User username=\""); sb.append(Escape.xml(username)); sb.append("\""); if (fullName != null) { sb.append(", fullName=\""); sb.append(Escape.xml(fullName)); sb.append("\""); } synchronized (groups) { if (groups.size() > 0) { sb.append(", groups=\""); StringUtils.join(groups, ',', (x) -> Escape.xml(x.getGroupname()), sb); sb.append("\""); } } synchronized (roles) { if (roles.size() > 0) { sb.append(", roles=\""); StringUtils.join(roles, ',', (x) -> Escape.xml(x.getRolename()), sb); sb.append("\""); } } return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy