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.authroles.authorization.strategies.role;
018
019import java.util.Collection;
020import java.util.HashSet;
021
022import org.apache.wicket.util.io.IClusterable;
023import org.apache.wicket.util.lang.Args;
024import org.apache.wicket.util.string.StringList;
025
026
027/**
028 * Utility class for working with roles.
029 * 
030 * @author Eelco Hillenius
031 * @author Jonathan Locke
032 */
033public class Roles extends HashSet<String> implements IClusterable
034{
035        private static final long serialVersionUID = 1L;
036
037        /** USER role (for use in annotations) */
038        public static final String USER = "USER";
039
040        /** ADMIN role (for use in annotations) */
041        public static final String ADMIN = "ADMIN";
042
043        /**
044         * Construct.
045         */
046        public Roles()
047        {
048        }
049
050        /**
051         * Construct.
052         * 
053         * @param roles
054         *            Roles as a comma separated list, like "ADMIN, USER"
055         */
056        public Roles(final String roles)
057        {
058                Args.notNull(roles, "roles");
059                for (final String role : roles.split("\\s*,\\s*"))
060                {
061                        add(role);
062                }
063        }
064
065        /**
066         * Construct.
067         * 
068         * @param roles
069         *            Roles
070         */
071        public Roles(final String[] roles)
072        {
073                Args.notNull(roles, "roles");
074                for (final String role : roles)
075                {
076                        add(role);
077                }
078        }
079
080        public Roles(final Collection<String> roles) {
081                Args.notNull(roles, "roles");
082                addAll(roles);
083        }
084
085        /**
086         * Whether this roles object containes the provided role.
087         * 
088         * @param role
089         *            the role to check
090         * @return true if it contains the role, false otherwise
091         */
092        public boolean hasRole(final String role)
093        {
094                if (role != null)
095                {
096                        return contains(role);
097                }
098                return false;
099        }
100
101        /**
102         * Whether this roles object contains any of the provided roles.
103         * 
104         * @param roles
105         *            the roles to check
106         * @return true if it contains any of the roles, false otherwise
107         */
108        public boolean hasAnyRole(Roles roles)
109        {
110                if (roles != null)
111                {
112                        for (String role : roles)
113                        {
114                                if (hasRole(role))
115                                {
116                                        return true;
117                                }
118                        }
119                }
120                return false;
121        }
122
123        /**
124         * Whether this roles object contains all the provided roles.
125         * 
126         * @param roles
127         *            the roles to check
128         * @return true if it contains all the roles or the provided roles object is null, false
129         *         otherwise
130         */
131        public boolean hasAllRoles(Roles roles)
132        {
133                if (roles != null)
134                {
135                        for (String role : roles)
136                        {
137                                if (!hasRole(role))
138                                {
139                                        return false;
140                                }
141                        }
142                }
143                return true;
144        }
145
146        /**
147         * @see java.lang.Object#toString()
148         */
149        @Override
150        public String toString()
151        {
152                return StringList.valueOf(this).join();
153        }
154}