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