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.util.Collection; 022import java.util.HashSet; 023import java.util.Set; 024 025/** 026 * Simple POJO implementation of the {@link AuthorizationInfo} interface that stores roles and permissions as internal 027 * attributes. 028 * 029 * @see org.apache.shiro.realm.AuthorizingRealm 030 * @since 0.9 031 */ 032public class SimpleAuthorizationInfo implements AuthorizationInfo { 033 034 /** 035 * The internal roles collection. 036 */ 037 protected Set<String> roles; 038 039 /** 040 * Collection of all string-based permissions associated with the account. 041 */ 042 protected Set<String> stringPermissions; 043 044 /** 045 * Collection of all object-based permissions associated with the account. 046 */ 047 protected Set<Permission> objectPermissions; 048 049 /** 050 * Default no-argument constructor. 051 */ 052 public SimpleAuthorizationInfo() { 053 } 054 055 /** 056 * Creates a new instance with the specified roles and no permissions. 057 * 058 * @param roles the roles assigned to the realm account. 059 */ 060 public SimpleAuthorizationInfo(Set<String> roles) { 061 this.roles = roles; 062 } 063 064 public Set<String> getRoles() { 065 return roles; 066 } 067 068 /** 069 * Sets the roles assigned to the account. 070 * 071 * @param roles the roles assigned to the account. 072 */ 073 public void setRoles(Set<String> roles) { 074 this.roles = roles; 075 } 076 077 /** 078 * Adds (assigns) a role to those associated with the account. If the account doesn't yet have any roles, a 079 * new roles collection (a Set) will be created automatically. 080 * 081 * @param role the role to add to those associated with the account. 082 */ 083 public void addRole(String role) { 084 if (this.roles == null) { 085 this.roles = new HashSet<String>(); 086 } 087 this.roles.add(role); 088 } 089 090 /** 091 * Adds (assigns) multiple roles to those associated with the account. If the account doesn't yet have any roles, a 092 * new roles collection (a Set) will be created automatically. 093 * 094 * @param roles the roles to add to those associated with the account. 095 */ 096 public void addRoles(Collection<String> roles) { 097 if (this.roles == null) { 098 this.roles = new HashSet<String>(); 099 } 100 this.roles.addAll(roles); 101 } 102 103 public Set<String> getStringPermissions() { 104 return stringPermissions; 105 } 106 107 /** 108 * Sets the string-based permissions assigned directly to the account. The permissions set here, in addition to any 109 * {@link #getObjectPermissions() object permissions} constitute the total permissions assigned directly to the 110 * account. 111 * 112 * @param stringPermissions the string-based permissions assigned directly to the account. 113 */ 114 public void setStringPermissions(Set<String> stringPermissions) { 115 this.stringPermissions = stringPermissions; 116 } 117 118 /** 119 * Adds (assigns) a permission to those directly associated with the account. If the account doesn't yet have any 120 * direct permissions, a new permission collection (a Set<String>) will be created automatically. 121 * 122 * @param permission the permission to add to those directly assigned to the account. 123 */ 124 public void addStringPermission(String permission) { 125 if (this.stringPermissions == null) { 126 this.stringPermissions = new HashSet<String>(); 127 } 128 this.stringPermissions.add(permission); 129 } 130 131 /** 132 * Adds (assigns) multiple permissions to those associated directly with the account. If the account doesn't yet 133 * have any string-based permissions, a new permissions collection (a Set<String>) will be created automatically. 134 * 135 * @param permissions the permissions to add to those associated directly with the account. 136 */ 137 public void addStringPermissions(Collection<String> permissions) { 138 if (this.stringPermissions == null) { 139 this.stringPermissions = new HashSet<String>(); 140 } 141 this.stringPermissions.addAll(permissions); 142 } 143 144 public Set<Permission> getObjectPermissions() { 145 return objectPermissions; 146 } 147 148 /** 149 * Sets the object-based permissions assigned directly to the account. The permissions set here, in addition to any 150 * {@link #getStringPermissions() string permissions} constitute the total permissions assigned directly to the 151 * account. 152 * 153 * @param objectPermissions the object-based permissions assigned directly to the account. 154 */ 155 public void setObjectPermissions(Set<Permission> objectPermissions) { 156 this.objectPermissions = objectPermissions; 157 } 158 159 /** 160 * Adds (assigns) a permission to those directly associated with the account. If the account doesn't yet have any 161 * direct permissions, a new permission collection (a Set<{@link Permission Permission}>) will be created automatically. 162 * 163 * @param permission the permission to add to those directly assigned to the account. 164 */ 165 public void addObjectPermission(Permission permission) { 166 if (this.objectPermissions == null) { 167 this.objectPermissions = new HashSet<Permission>(); 168 } 169 this.objectPermissions.add(permission); 170 } 171 172 /** 173 * Adds (assigns) multiple permissions to those associated directly with the account. If the account doesn't yet 174 * have any object-based permissions, a new permissions collection (a Set<{@link Permission Permission}>) 175 * will be created automatically. 176 * 177 * @param permissions the permissions to add to those associated directly with the account. 178 */ 179 public void addObjectPermissions(Collection<Permission> permissions) { 180 if (this.objectPermissions == null) { 181 this.objectPermissions = new HashSet<Permission>(); 182 } 183 this.objectPermissions.addAll(permissions); 184 } 185}