/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server;

import org.bodington.server.realm.Permission;

/**
 * Class to be thrown when an operation is attempted that the user doesn't
 * have permission to perform.
 * @author buckett
 */
public class PermissionDeniedException extends BuildingServerException
{
    private Permission[] permissions;

    public PermissionDeniedException(Permission permission)
    {
        super("Permission Denied");
        if (permission == null)
            throw new IllegalArgumentException("permission can't be null");
        this.permissions = new Permission[]{permission};
    }
    
    public PermissionDeniedException(Permission[] permissions)
    {
        super("Permission Denied");
        if (permissions == null)
            throw new IllegalArgumentException("permissions[] can't be null");
        if (permissions.length < 1)
            throw new IllegalArgumentException("permissions[].length must be 1 or more");
        this.permissions = permissions;
    }

    public Permission[] getPermissions()
    {
        return this.permissions;
    }
    
    public String getPermission()
    {
        StringBuffer out = new StringBuffer();
        out.append(permissions[0]);
        for (int i = 1; i < permissions.length; i++)
            out.append(" or "+ permissions[i]);
        return out.toString();
    }
}
