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 module hunt.shiro.authz.SimpleAuthorizationInfo;
20 
21 import hunt.shiro.authz.AuthorizationInfo;
22 import hunt.shiro.authz.permission.Permission;
23 
24 import hunt.collection;
25 
26 /**
27  * Simple POJO implementation of the {@link AuthorizationInfo} interface that stores roles and permissions as internal
28  * attributes.
29  *
30  * @see hunt.shiro.realm.AuthorizingRealm
31  */
32 class SimpleAuthorizationInfo : 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     this() {
53     }
54 
55     /**
56      * Creates a new instance with the specified roles and no permissions.
57      * @param roles the roles assigned to the realm account.
58      */
59      this(Set!(string) roles) {
60         this.roles = roles;
61     }
62 
63     Set!(string) getRoles() {
64         return roles;
65     }
66 
67     /**
68      * Sets the roles assigned to the account.
69      * @param roles the roles assigned to the account.
70      */
71     void setRoles(Set!(string) roles) {
72         this.roles = roles;
73     }
74 
75     /**
76      * Adds (assigns) a role to those associated with the account.  If the account doesn't yet have any roles, a
77      * new roles collection (a Set) will be created automatically.
78      * @param role the role to add to those associated with the account.
79      */
80      void addRole(string role) {
81         if (this.roles  is null) {
82             this.roles = new HashSet!(string)();
83         }
84         this.roles.add(role);
85     }
86 
87     /**
88      * Adds (assigns) multiple roles to those associated with the account.  If the account doesn't yet have any roles, a
89      * new roles collection (a Set) will be created automatically.
90      * @param roles the roles to add to those associated with the account.
91      */
92      void addRoles(Collection!(string) roles) {
93         if (this.roles  is null) {
94             this.roles = new HashSet!(string)();
95         }
96         this.roles.addAll(roles);
97     }
98 
99     void addRoles(string[] roles) {
100         if (this.roles  is null) {
101             this.roles = new HashSet!(string)();
102         }
103         this.roles.addAll(roles);
104     }
105 
106      Set!(string) getStringPermissions() {
107         return stringPermissions;
108     }
109 
110     /**
111      * Sets the string-based permissions assigned directly to the account.  The permissions set here, in addition to any
112      * {@link #getObjectPermissions() object permissions} constitute the total permissions assigned directly to the
113      * account.
114      *
115      * @param stringPermissions the string-based permissions assigned directly to the account.
116      */
117      void setStringPermissions(Set!(string) stringPermissions) {
118         this.stringPermissions = stringPermissions;
119     }
120 
121     /**
122      * Adds (assigns) a permission to those directly associated with the account.  If the account doesn't yet have any
123      * direct permissions, a new permission collection (a Set<string>) will be created automatically.
124      * @param permission the permission to add to those directly assigned to the account.
125      */
126      void addStringPermission(string permission) {
127         if (this.stringPermissions  is null) {
128             this.stringPermissions = new HashSet!(string)();
129         }
130         this.stringPermissions.add(permission);
131     }
132 
133     /**
134      * Adds (assigns) multiple permissions to those associated directly with the account.  If the account doesn't yet
135      * have any string-based permissions, a  new permissions collection (a Set<string>) will be created automatically.
136      * @param permissions the permissions to add to those associated directly with the account.
137      */
138      void addStringPermissions(Collection!(string) permissions) {
139         if (this.stringPermissions  is null) {
140             this.stringPermissions = new HashSet!(string)();
141         }
142         this.stringPermissions.addAll(permissions);
143     }
144 
145     void addStringPermissions(string[] permissions) {
146         if (this.stringPermissions  is null) {
147             this.stringPermissions = new HashSet!(string)();
148         }
149         this.stringPermissions.addAll(permissions);
150     }
151 
152      Set!(Permission) getObjectPermissions() {
153         return objectPermissions;
154     }
155 
156     /**
157      * Sets the object-based permissions assigned directly to the account.  The permissions set here, in addition to any
158      * {@link #getStringPermissions() string permissions} constitute the total permissions assigned directly to the
159      * account.
160      *
161      * @param objectPermissions the object-based permissions assigned directly to the account.
162      */
163      void setObjectPermissions(Set!(Permission) objectPermissions) {
164         this.objectPermissions = objectPermissions;
165     }
166 
167     /**
168      * Adds (assigns) a permission to those directly associated with the account.  If the account doesn't yet have any
169      * direct permissions, a new permission collection (a Set<{@link Permission Permission}>) will be created automatically.
170      * @param permission the permission to add to those directly assigned to the account.
171      */
172      void addObjectPermission(Permission permission) {
173         if (this.objectPermissions  is null) {
174             this.objectPermissions = new HashSet!(Permission)();
175         }
176         this.objectPermissions.add(permission);
177     }
178 
179     /**
180      * Adds (assigns) multiple permissions to those associated directly with the account.  If the account doesn't yet
181      * have any object-based permissions, a  new permissions collection (a Set<{@link Permission Permission}>)
182      * will be created automatically.
183      * @param permissions the permissions to add to those associated directly with the account.
184      */
185      void addObjectPermissions(Collection!(Permission) permissions) {
186         if (this.objectPermissions  is null) {
187             this.objectPermissions = new HashSet!(Permission)();
188         }
189         this.objectPermissions.addAll(permissions);
190     }
191 }