Showing the changes to java/pkg/sun/security/acl/AclEntryImpl.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,181 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | +package sun.security.acl; | |
| 26 | + | |
| 27 | +import java.util.*; | |
| 28 | +import java.io.*; | |
| 29 | +import java.security.Principal; | |
| 30 | +import java.security.acl.*; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * This is a class that describes one entry that associates users | |
| 34 | + * or groups with permissions in the ACL. | |
| 35 | + * The entry may be used as a way of granting or denying permissions. | |
| 36 | + * @author Satish Dharmaraj | |
| 37 | + */ | |
| 38 | +public class AclEntryImpl implements AclEntry { | |
| 39 | + private Principal user = null; | |
| 40 | + private Vector<Permission> permissionSet = new Vector<>(10, 10); | |
| 41 | + private boolean negative = false; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Construct an ACL entry that associates a user with permissions | |
| 45 | + * in the ACL. | |
| 46 | + * @param user The user that is associated with this entry. | |
| 47 | + */ | |
| 48 | + public AclEntryImpl(Principal user) { | |
| 49 | + this.user = user; | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Construct a null ACL entry | |
| 54 | + */ | |
| 55 | + public AclEntryImpl() { | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Sets the principal in the entity. If a group or a | |
| 60 | + * principal had already been set, a false value is | |
| 61 | + * returned, otherwise a true value is returned. | |
| 62 | + * @param user The user that is associated with this entry. | |
| 63 | + * @return true if the principal is set, false if there is | |
| 64 | + * one already. | |
| 65 | + */ | |
| 66 | + public boolean setPrincipal(Principal user) { | |
| 67 | + if (this.user != null) | |
| 68 | + return false; | |
| 69 | + this.user = user; | |
| 70 | + return true; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * This method sets the ACL to have negative permissions. | |
| 75 | + * That is the user or group is denied the permission set | |
| 76 | + * specified in the entry. | |
| 77 | + */ | |
| 78 | + public void setNegativePermissions() { | |
| 79 | + negative = true; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Returns true if this is a negative ACL. | |
| 84 | + */ | |
| 85 | + public boolean isNegative() { | |
| 86 | + return negative; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * A principal or a group can be associated with multiple | |
| 91 | + * permissions. This method adds a permission to the ACL entry. | |
| 92 | + * @param permission The permission to be associated with | |
| 93 | + * the principal or the group in the entry. | |
| 94 | + * @return true if the permission was added, false if the | |
| 95 | + * permission was already part of the permission set. | |
| 96 | + */ | |
| 97 | + public boolean addPermission(Permission permission) { | |
| 98 | + | |
| 99 | + if (permissionSet.contains(permission)) | |
| 100 | + return false; | |
| 101 | + | |
| 102 | + permissionSet.addElement(permission); | |
| 103 | + | |
| 104 | + return true; | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * The method disassociates the permission from the Principal | |
| 109 | + * or the Group in this ACL entry. | |
| 110 | + * @param permission The permission to be disassociated with | |
| 111 | + * the principal or the group in the entry. | |
| 112 | + * @return true if the permission is removed, false if the | |
| 113 | + * permission is not part of the permission set. | |
| 114 | + */ | |
| 115 | + public boolean removePermission(Permission permission) { | |
| 116 | + return permissionSet.removeElement(permission); | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Checks if the passed permission is part of the allowed | |
| 121 | + * permission set in this entry. | |
| 122 | + * @param permission The permission that has to be part of | |
| 123 | + * the permission set in the entry. | |
| 124 | + * @return true if the permission passed is part of the | |
| 125 | + * permission set in the entry, false otherwise. | |
| 126 | + */ | |
| 127 | + public boolean checkPermission(Permission permission) { | |
| 128 | + return permissionSet.contains(permission); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * return an enumeration of the permissions in this ACL entry. | |
| 133 | + */ | |
| 134 | + public Enumeration<Permission> permissions() { | |
| 135 | + return permissionSet.elements(); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Return a string representation of the contents of the ACL entry. | |
| 140 | + */ | |
| 141 | + public String toString() { | |
| 142 | + StringBuffer s = new StringBuffer(); | |
| 143 | + if (negative) | |
| 144 | + s.append("-"); | |
| 145 | + else | |
| 146 | + s.append("+"); | |
| 147 | + if (user instanceof Group) | |
| 148 | + s.append("Group."); | |
| 149 | + else | |
| 150 | + s.append("User."); | |
| 151 | + s.append(user + "="); | |
| 152 | + Enumeration<Permission> e = permissions(); | |
| 153 | + while(e.hasMoreElements()) { | |
| 154 | + Permission p = e.nextElement(); | |
| 155 | + s.append(p); | |
| 156 | + if (e.hasMoreElements()) | |
| 157 | + s.append(","); | |
| 158 | + } | |
| 159 | + return new String(s); | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Clones an AclEntry. | |
| 164 | + */ | |
| 165 | + public synchronized Object clone() { | |
| 166 | + AclEntryImpl cloned; | |
| 167 | + cloned = new AclEntryImpl(user); | |
| 168 | + cloned.permissionSet = (Vector<Permission>) permissionSet.clone(); | |
| 169 | + cloned.negative = negative; | |
| 170 | + return cloned; | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Return the Principal associated in this ACL entry. | |
| 175 | + * The method returns null if the entry uses a group | |
| 176 | + * instead of a principal. | |
| 177 | + */ | |
| 178 | + public Principal getPrincipal() { | |
| 179 | + return user; | |
| 180 | + } | |
| 181 | +} | |