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