001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.shiro.authz;
020
021 import java.io.Serializable;
022 import java.util.Collection;
023 import java.util.LinkedHashSet;
024 import java.util.Set;
025
026 /**
027 * A simple representation of a security role that has a name and a collection of permissions. This object can be
028 * used internally by Realms to maintain authorization state.
029 *
030 * @author Les Hazlewood
031 * @since 0.2
032 */
033 public class SimpleRole implements Serializable {
034
035 protected String name = null;
036 protected Set<Permission> permissions;
037
038 public SimpleRole() {
039 }
040
041 public SimpleRole(String name) {
042 setName(name);
043 }
044
045 public SimpleRole(String name, Set<Permission> permissions) {
046 setName(name);
047 setPermissions(permissions);
048 }
049
050 public String getName() {
051 return name;
052 }
053
054 public void setName(String name) {
055 this.name = name;
056 }
057
058 public Set<Permission> getPermissions() {
059 return permissions;
060 }
061
062 public void setPermissions(Set<Permission> permissions) {
063 this.permissions = permissions;
064 }
065
066 public void add(Permission permission) {
067 Set<Permission> permissions = getPermissions();
068 if (permissions == null) {
069 permissions = new LinkedHashSet<Permission>();
070 setPermissions(permissions);
071 }
072 permissions.add(permission);
073 }
074
075 public void addAll(Collection<Permission> perms) {
076 if (perms != null && !perms.isEmpty()) {
077 Set<Permission> permissions = getPermissions();
078 if (permissions == null) {
079 permissions = new LinkedHashSet<Permission>(perms.size());
080 setPermissions(permissions);
081 }
082 permissions.addAll(perms);
083 }
084 }
085
086 public boolean isPermitted(Permission p) {
087 Collection<Permission> perms = getPermissions();
088 if (perms != null && !perms.isEmpty()) {
089 for (Permission perm : perms) {
090 if (perm.implies(p)) {
091 return true;
092 }
093 }
094 }
095 return false;
096 }
097
098 public int hashCode() {
099 return (getName() != null ? getName().hashCode() : 0);
100 }
101
102 public boolean equals(Object o) {
103 if (o == this) {
104 return true;
105 }
106 if (o instanceof SimpleRole) {
107 SimpleRole sr = (SimpleRole) o;
108 //only check name, since role names should be unique across an entire application:
109 return (getName() != null ? getName().equals(sr.getName()) : sr.getName() == null);
110 }
111 return false;
112 }
113
114 public String toString() {
115 return getName();
116 }
117 }