Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/acl/AclEntryImpl.java
38830 views
/*1* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.security.acl;2526import java.util.*;27import java.security.Principal;28import java.security.acl.*;2930/**31* This is a class that describes one entry that associates users32* or groups with permissions in the ACL.33* The entry may be used as a way of granting or denying permissions.34* @author Satish Dharmaraj35*/36public class AclEntryImpl implements AclEntry {37private Principal user = null;38private Vector<Permission> permissionSet = new Vector<>(10, 10);39private boolean negative = false;4041/**42* Construct an ACL entry that associates a user with permissions43* in the ACL.44* @param user The user that is associated with this entry.45*/46public AclEntryImpl(Principal user) {47this.user = user;48}4950/**51* Construct a null ACL entry52*/53public AclEntryImpl() {54}5556/**57* Sets the principal in the entity. If a group or a58* principal had already been set, a false value is59* returned, otherwise a true value is returned.60* @param user The user that is associated with this entry.61* @return true if the principal is set, false if there is62* one already.63*/64public boolean setPrincipal(Principal user) {65if (this.user != null)66return false;67this.user = user;68return true;69}7071/**72* This method sets the ACL to have negative permissions.73* That is the user or group is denied the permission set74* specified in the entry.75*/76public void setNegativePermissions() {77negative = true;78}7980/**81* Returns true if this is a negative ACL.82*/83public boolean isNegative() {84return negative;85}8687/**88* A principal or a group can be associated with multiple89* permissions. This method adds a permission to the ACL entry.90* @param permission The permission to be associated with91* the principal or the group in the entry.92* @return true if the permission was added, false if the93* permission was already part of the permission set.94*/95public boolean addPermission(Permission permission) {9697if (permissionSet.contains(permission))98return false;99100permissionSet.addElement(permission);101102return true;103}104105/**106* The method disassociates the permission from the Principal107* or the Group in this ACL entry.108* @param permission The permission to be disassociated with109* the principal or the group in the entry.110* @return true if the permission is removed, false if the111* permission is not part of the permission set.112*/113public boolean removePermission(Permission permission) {114return permissionSet.removeElement(permission);115}116117/**118* Checks if the passed permission is part of the allowed119* permission set in this entry.120* @param permission The permission that has to be part of121* the permission set in the entry.122* @return true if the permission passed is part of the123* permission set in the entry, false otherwise.124*/125public boolean checkPermission(Permission permission) {126return permissionSet.contains(permission);127}128129/**130* return an enumeration of the permissions in this ACL entry.131*/132public Enumeration<Permission> permissions() {133return permissionSet.elements();134}135136/**137* Return a string representation of the contents of the ACL entry.138*/139public String toString() {140StringBuffer s = new StringBuffer();141if (negative)142s.append("-");143else144s.append("+");145if (user instanceof Group)146s.append("Group.");147else148s.append("User.");149s.append(user + "=");150Enumeration<Permission> e = permissions();151while(e.hasMoreElements()) {152Permission p = e.nextElement();153s.append(p);154if (e.hasMoreElements())155s.append(",");156}157return new String(s);158}159160/**161* Clones an AclEntry.162*/163@SuppressWarnings("unchecked") // Safe casts assuming clone() works correctly164public synchronized Object clone() {165AclEntryImpl cloned;166cloned = new AclEntryImpl(user);167cloned.permissionSet = (Vector<Permission>) permissionSet.clone();168cloned.negative = negative;169return cloned;170}171172/**173* Return the Principal associated in this ACL entry.174* The method returns null if the entry uses a group175* instead of a principal.176*/177public Principal getPrincipal() {178return user;179}180}181182183