summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/acl/OwnerImpl.java
blob: 3f47a1dd8a0e8e0ed4709e206c383a3b89de0db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.acl;

import java.security.Principal;
import java.security.acl.Group;
import java.security.acl.LastOwnerException;
import java.security.acl.NotOwnerException;
import java.security.acl.Owner;
import java.util.Enumeration;

/**
 * Class implementing the Owner interface. The
 * initial owner principal is configured as
 * part of the constructor.
 * 
 * @author Satish Dharmaraj
 */
public class OwnerImpl implements Owner {
    private Group ownerGroup;

    public OwnerImpl(Principal owner) {
        ownerGroup = new GroupImpl("AclOwners");
        ownerGroup.addMember(owner);
    }

    /**
     * Adds an owner. Owners can modify ACL contents and can disassociate
     * ACLs from the objects they protect in the AclConfig interface.
     * The caller principal must be a part of the owners list of the ACL in
     * order to invoke this method. The initial owner is configured
     * at ACL construction time.
     * 
     * @param caller the principal who is invoking this method.
     * @param owner The owner that should be added to the owners list.
     * @return true if success, false if already an owner.
     * @exception NotOwnerException if the caller principal is not on
     *                the owners list of the Acl.
     */
    public synchronized boolean addOwner(Principal caller, Principal owner)
            throws NotOwnerException {
        if (!isOwner(caller))
            throw new NotOwnerException();

        ownerGroup.addMember(owner);
        return false;
    }

    /**
     * Delete owner. If this is the last owner in the ACL, an exception is
     * raised.
     * The caller principal must be a part of the owners list of the ACL in
     * order to invoke this method.
     * 
     * @param caller the principal who is invoking this method.
     * @param owner The owner to be removed from the owners list.
     * @return true if the owner is removed, false if the owner is not part
     *         of the owners list.
     * @exception NotOwnerException if the caller principal is not on
     *                the owners list of the Acl.
     * @exception LastOwnerException if there is only one owner left in the group, then
     *                deleteOwner would leave the ACL owner-less. This exception is raised in such a case.
     */
    public synchronized boolean deleteOwner(Principal caller, Principal owner)
            throws NotOwnerException, LastOwnerException {
        if (!isOwner(caller))
            throw new NotOwnerException();

        Enumeration<? extends Principal> e = ownerGroup.members();
        //
        // check if there is atleast 2 members left.
        //
        e.nextElement(); // consume next element
        if (e.hasMoreElements())
            return ownerGroup.removeMember(owner);
        else
            throw new LastOwnerException();

    }

    /**
     * returns if the given principal belongs to the owner list.
     * 
     * @param owner The owner to check if part of the owners list
     * @return true if the passed principal is in the owner list, false if not.
     */
    public synchronized boolean isOwner(Principal owner) {
        return ownerGroup.isMember(owner);
    }
}