GroupManagerProxy.java 3.21 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
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
package org.jivesoftware.messenger.auth.spi;

import org.jivesoftware.util.BasicResultFilter;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.messenger.Entity;
import org.jivesoftware.messenger.auth.*;
import org.jivesoftware.messenger.user.spi.UserGroupIteratorProxy;
import java.util.Iterator;

/**
 * Protection proxy for the GroupManager class. It restricts access to
 * protected methods by throwing UnauthorizedExceptions when necessary.
 *
 * @author Iain Shigeoka
 * @see org.jivesoftware.messenger.auth.GroupManager
 */
public class GroupManagerProxy implements GroupManager {

    private GroupManager groupManager;
    private AuthToken auth;
    private Permissions permissions;

    /**
     * Creates a new GroupManagerProxy.
     */
    public GroupManagerProxy(GroupManager groupManager, AuthToken auth, Permissions permissions) {
        this.groupManager = groupManager;
        this.auth = auth;
        this.permissions = permissions;
    }

    public Group createGroup(String name)
            throws UnauthorizedException, GroupAlreadyExistsException {
        if (permissions.hasPermission(Permissions.SYSTEM_ADMIN)) {
            Group group = groupManager.createGroup(name);
            return new GroupProxy(group, auth, permissions);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public Group getGroup(long groupID) throws GroupNotFoundException {
        Group group = groupManager.getGroup(groupID);
        Permissions groupPermissions = group.getPermissions(auth);
        Permissions newPermissions = new Permissions(permissions, groupPermissions);

        return new GroupProxy(group, auth, newPermissions);
    }

    public Group getGroup(String name) throws GroupNotFoundException {
        Group group = groupManager.getGroup(name);
        Permissions groupPermissions = group.getPermissions(auth);
        Permissions newPermissions =
                new Permissions(permissions, groupPermissions);
        return new GroupProxy(group, auth, newPermissions);
    }

    public void deleteGroup(Group group) throws UnauthorizedException {
        if (permissions.hasPermission(Permissions.SYSTEM_ADMIN)) {
            groupManager.deleteGroup(group);
        }
        else {
            throw new UnauthorizedException();
        }
    }

    public int getGroupCount() {
        return groupManager.getGroupCount();
    }

    public Iterator getGroups() {
        Iterator iterator = groupManager.getGroups();
        return new UserGroupIteratorProxy(JiveConstants.GROUP, iterator, auth, permissions);
    }

    public Iterator getGroups(BasicResultFilter filter) {
        Iterator iterator = groupManager.getGroups(filter);
        return new UserGroupIteratorProxy(JiveConstants.GROUP, iterator, auth, permissions);
    }

    public Iterator getGroups(Entity entity) {
        Iterator iterator = groupManager.getGroups(entity);
        return new UserGroupIteratorProxy(JiveConstants.GROUP, iterator, auth, permissions);
    }
}