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 */ 019package org.apache.shiro.authz; 020 021import java.io.Serializable; 022import java.util.Collection; 023import java.util.LinkedHashSet; 024import 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 * @since 0.2 031 */ 032public class SimpleRole implements Serializable { 033 034 protected String name; 035 protected Set<Permission> permissions; 036 037 public SimpleRole() { 038 } 039 040 public SimpleRole(String name) { 041 setName(name); 042 } 043 044 public SimpleRole(String name, Set<Permission> permissions) { 045 setName(name); 046 setPermissions(permissions); 047 } 048 049 public String getName() { 050 return name; 051 } 052 053 public void setName(String name) { 054 this.name = name; 055 } 056 057 public Set<Permission> getPermissions() { 058 return permissions; 059 } 060 061 public void setPermissions(Set<Permission> permissions) { 062 this.permissions = permissions; 063 } 064 065 public void add(Permission permission) { 066 Set<Permission> permissions = getPermissions(); 067 if (permissions == null) { 068 permissions = new LinkedHashSet<Permission>(); 069 setPermissions(permissions); 070 } 071 permissions.add(permission); 072 } 073 074 public void addAll(Collection<Permission> perms) { 075 if (perms != null && !perms.isEmpty()) { 076 Set<Permission> permissions = getPermissions(); 077 if (permissions == null) { 078 permissions = new LinkedHashSet<Permission>(perms.size()); 079 setPermissions(permissions); 080 } 081 permissions.addAll(perms); 082 } 083 } 084 085 public boolean isPermitted(Permission p) { 086 Collection<Permission> perms = getPermissions(); 087 if (perms != null && !perms.isEmpty()) { 088 for (Permission perm : perms) { 089 if (perm.implies(p)) { 090 return true; 091 } 092 } 093 } 094 return false; 095 } 096 097 public int hashCode() { 098 return (getName() != null ? getName().hashCode() : 0); 099 } 100 101 public boolean equals(Object o) { 102 if (o == this) { 103 return true; 104 } 105 if (o instanceof SimpleRole) { 106 SimpleRole sr = (SimpleRole) o; 107 //only check name, since role names should be unique across an entire application: 108 return (getName() != null ? getName().equals(sr.getName()) : sr.getName() == null); 109 } 110 return false; 111 } 112 113 public String toString() { 114 return getName(); 115 } 116}