1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.shiro.authz;
20
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Set;
24
25 /**
26 * Simple POJO implementation of the {@link AuthorizationInfo} interface that stores roles and permissions as internal
27 * attributes.
28 *
29 * @see org.apache.shiro.realm.AuthorizingRealm
30 * @since 0.9
31 */
32 public class SimpleAuthorizationInfo implements AuthorizationInfo {
33
34 /**
35 * The internal roles collection.
36 */
37 protected Set<String> roles;
38
39 /**
40 * Collection of all string-based permissions associated with the account.
41 */
42 protected Set<String> stringPermissions;
43
44 /**
45 * Collection of all object-based permissions associated with the account.
46 */
47 protected Set<Permission> objectPermissions;
48
49 /**
50 * Default no-argument constructor.
51 */
52 public SimpleAuthorizationInfo() {
53 }
54
55 /**
56 * Creates a new instance with the specified roles and no permissions.
57 *
58 * @param roles the roles assigned to the realm account.
59 */
60 public SimpleAuthorizationInfo(Set<String> roles) {
61 this.roles = roles;
62 }
63
64 public Set<String> getRoles() {
65 return roles;
66 }
67
68 /**
69 * Sets the roles assigned to the account.
70 *
71 * @param roles the roles assigned to the account.
72 */
73 public void setRoles(Set<String> roles) {
74 this.roles = roles;
75 }
76
77 /**
78 * Adds (assigns) a role to those associated with the account. If the account doesn't yet have any roles, a
79 * new roles collection (a Set) will be created automatically.
80 *
81 * @param role the role to add to those associated with the account.
82 */
83 public void addRole(String role) {
84 if (this.roles == null) {
85 this.roles = new HashSet<String>();
86 }
87 this.roles.add(role);
88 }
89
90 /**
91 * Adds (assigns) multiple roles to those associated with the account. If the account doesn't yet have any roles, a
92 * new roles collection (a Set) will be created automatically.
93 *
94 * @param roles the roles to add to those associated with the account.
95 */
96 public void addRoles(Collection<String> roles) {
97 if (this.roles == null) {
98 this.roles = new HashSet<String>();
99 }
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 }