001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.authorization.strategies.action;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.wicket.Component;
023import org.apache.wicket.authorization.Action;
024import org.apache.wicket.authorization.IAuthorizationStrategy;
025
026
027/**
028 * An authorization strategy which allows the use of a command pattern for users that want to
029 * authorize a variety of different types of actions throughout an application.
030 * 
031 * @author Jonathan Locke
032 * @since Wicket 1.2
033 */
034public class ActionAuthorizationStrategy extends IAuthorizationStrategy.AllowAllAuthorizationStrategy
035{
036        /** Map from Action keys to IActionAuthorizer implementations. */
037        private final Map<Action, IActionAuthorizer> actionAuthorizerForAction = new HashMap<>();
038
039        /**
040         * Adds an action authorizer.
041         * 
042         * @param authorizer
043         *            The action authorizer to add
044         */
045        public void addActionAuthorizer(IActionAuthorizer authorizer)
046        {
047                actionAuthorizerForAction.put(authorizer.getAction(), authorizer);
048        }
049
050        /**
051         * @see org.apache.wicket.authorization.IAuthorizationStrategy#isActionAuthorized(org.apache.wicket.Component,
052         *      org.apache.wicket.authorization.Action)
053         */
054        @Override
055        public boolean isActionAuthorized(Component component, Action action)
056        {
057                IActionAuthorizer authorizer = actionAuthorizerForAction.get(action);
058                if (authorizer != null)
059                {
060                        return authorizer.authorizeAction(component);
061                }
062                return false;
063        }
064}