1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
28
29
30
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 sr = (SimpleRole) o;
107
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 }