View Javadoc
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.io.Serializable;
22  import java.util.Collection;
23  import java.util.LinkedHashSet;
24  import java.util.Set;
25  
26  /**
27   * A simple representation of a security role that has a name and a collection of permissions.  This object can be
28   * used internally by Realms to maintain authorization state.
29   *
30   * @since 0.2
31   */
32  public class SimpleRole implements Serializable {
33  
34      protected String name = null;
35      protected Set<Permission> permissions;
36  
37      public SimpleRole() {
38      }
39  
40      public SimpleRole(String name) {
41          setName(name);
42      }
43  
44      public SimpleRole(String name, Set<Permission> permissions) {
45          setName(name);
46          setPermissions(permissions);
47      }
48  
49      public String getName() {
50          return name;
51      }
52  
53      public void setName(String name) {
54          this.name = name;
55      }
56  
57      public Set<Permission> getPermissions() {
58          return permissions;
59      }
60  
61      public void setPermissions(Set<Permission> permissions) {
62          this.permissions = permissions;
63      }
64  
65      public void add(Permission permission) {
66          Set<Permission> permissions = getPermissions();
67          if (permissions == null) {
68              permissions = new LinkedHashSet<Permission>();
69              setPermissions(permissions);
70          }
71          permissions.add(permission);
72      }
73  
74      public void addAll(Collection<Permission> perms) {
75          if (perms != null && !perms.isEmpty()) {
76              Set<Permission> permissions = getPermissions();
77              if (permissions == null) {
78                  permissions = new LinkedHashSet<Permission>(perms.size());
79                  setPermissions(permissions);
80              }
81              permissions.addAll(perms);
82          }
83      }
84  
85      public boolean isPermitted(Permission p) {
86          Collection<Permission> perms = getPermissions();
87          if (perms != null && !perms.isEmpty()) {
88              for (Permission perm : perms) {
89                  if (perm.implies(p)) {
90                      return true;
91                  }
92              }
93          }
94          return false;
95      }
96  
97      public int hashCode() {
98          return (getName() != null ? getName().hashCode() : 0);
99      }
100 
101     public boolean equals(Object o) {
102         if (o == this) {
103             return true;
104         }
105         if (o instanceof SimpleRole) {
106             SimpleRole../../../../org/apache/shiro/authz/SimpleRole.html#SimpleRole">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 }