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 | 17 | protected String name = null; |
35 | |
protected Set<Permission> permissions; |
36 | |
|
37 | 0 | public SimpleRole() { |
38 | 0 | } |
39 | |
|
40 | 17 | public SimpleRole(String name) { |
41 | 17 | setName(name); |
42 | 17 | } |
43 | |
|
44 | 0 | public SimpleRole(String name, Set<Permission> permissions) { |
45 | 0 | setName(name); |
46 | 0 | setPermissions(permissions); |
47 | 0 | } |
48 | |
|
49 | |
public String getName() { |
50 | 17 | return name; |
51 | |
} |
52 | |
|
53 | |
public void setName(String name) { |
54 | 17 | this.name = name; |
55 | 17 | } |
56 | |
|
57 | |
public Set<Permission> getPermissions() { |
58 | 24 | return permissions; |
59 | |
} |
60 | |
|
61 | |
public void setPermissions(Set<Permission> permissions) { |
62 | 17 | this.permissions = permissions; |
63 | 17 | } |
64 | |
|
65 | |
public void add(Permission permission) { |
66 | 0 | Set<Permission> permissions = getPermissions(); |
67 | 0 | if (permissions == null) { |
68 | 0 | permissions = new LinkedHashSet<Permission>(); |
69 | 0 | setPermissions(permissions); |
70 | |
} |
71 | 0 | permissions.add(permission); |
72 | 0 | } |
73 | |
|
74 | |
public void addAll(Collection<Permission> perms) { |
75 | 0 | if (perms != null && !perms.isEmpty()) { |
76 | 0 | Set<Permission> permissions = getPermissions(); |
77 | 0 | if (permissions == null) { |
78 | 0 | permissions = new LinkedHashSet<Permission>(perms.size()); |
79 | 0 | setPermissions(permissions); |
80 | |
} |
81 | 0 | permissions.addAll(perms); |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
public boolean isPermitted(Permission p) { |
86 | 0 | Collection<Permission> perms = getPermissions(); |
87 | 0 | if (perms != null && !perms.isEmpty()) { |
88 | 0 | for (Permission perm : perms) { |
89 | 0 | if (perm.implies(p)) { |
90 | 0 | return true; |
91 | |
} |
92 | 0 | } |
93 | |
} |
94 | 0 | return false; |
95 | |
} |
96 | |
|
97 | |
public int hashCode() { |
98 | 0 | return (getName() != null ? getName().hashCode() : 0); |
99 | |
} |
100 | |
|
101 | |
public boolean equals(Object o) { |
102 | 0 | if (o == this) { |
103 | 0 | return true; |
104 | |
} |
105 | 0 | if (o instanceof SimpleRole) { |
106 | 0 | SimpleRole sr = (SimpleRole) o; |
107 | |
|
108 | 0 | return (getName() != null ? getName().equals(sr.getName()) : sr.getName() == null); |
109 | |
} |
110 | 0 | return false; |
111 | |
} |
112 | |
|
113 | |
public String toString() { |
114 | 0 | return getName(); |
115 | |
} |
116 | |
} |