1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.authz.permission; |
20 | |
|
21 | |
import org.apache.shiro.util.StringUtils; |
22 | |
|
23 | |
import java.util.Set; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class DomainPermission extends WildcardPermission { |
34 | |
|
35 | |
private String domain; |
36 | |
private Set<String> actions; |
37 | |
private Set<String> targets; |
38 | |
|
39 | |
private static final long serialVersionUID = 1l; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 1 | public DomainPermission() { |
45 | 1 | this.domain = getDomain(getClass()); |
46 | 1 | setParts(getDomain(getClass())); |
47 | 1 | } |
48 | |
|
49 | 2 | public DomainPermission(String actions) { |
50 | 2 | domain = getDomain(getClass()); |
51 | 2 | this.actions = StringUtils.splitToSet(actions, SUBPART_DIVIDER_TOKEN); |
52 | 2 | encodeParts(domain, actions, null); |
53 | 2 | } |
54 | |
|
55 | 2 | public DomainPermission(String actions, String targets) { |
56 | 2 | this.domain = getDomain(getClass()); |
57 | 2 | this.actions = StringUtils.splitToSet(actions, SUBPART_DIVIDER_TOKEN); |
58 | 2 | this.targets = StringUtils.splitToSet(targets, SUBPART_DIVIDER_TOKEN); |
59 | 2 | encodeParts(this.domain, actions, targets); |
60 | 2 | } |
61 | |
|
62 | 0 | protected DomainPermission(Set<String> actions, Set<String> targets) { |
63 | 0 | this.domain = getDomain(getClass()); |
64 | 0 | setParts(domain, actions, targets); |
65 | 0 | } |
66 | |
|
67 | |
private void encodeParts(String domain, String actions, String targets) { |
68 | 4 | if (!StringUtils.hasText(domain)) { |
69 | 0 | throw new IllegalArgumentException("domain argument cannot be null or empty."); |
70 | |
} |
71 | 4 | StringBuilder sb = new StringBuilder(domain); |
72 | |
|
73 | 4 | if (!StringUtils.hasText(actions)) { |
74 | 0 | if (StringUtils.hasText(targets)) { |
75 | 0 | sb.append(PART_DIVIDER_TOKEN).append(WILDCARD_TOKEN); |
76 | |
} |
77 | |
} else { |
78 | 4 | sb.append(PART_DIVIDER_TOKEN).append(actions); |
79 | |
} |
80 | 4 | if (StringUtils.hasText(targets)) { |
81 | 2 | sb.append(PART_DIVIDER_TOKEN).append(targets); |
82 | |
} |
83 | 4 | setParts(sb.toString()); |
84 | 4 | } |
85 | |
|
86 | |
protected void setParts(String domain, Set<String> actions, Set<String> targets) { |
87 | 0 | String actionsString = StringUtils.toDelimitedString(actions, SUBPART_DIVIDER_TOKEN); |
88 | 0 | String targetsString = StringUtils.toDelimitedString(targets, SUBPART_DIVIDER_TOKEN); |
89 | 0 | encodeParts(domain, actionsString, targetsString); |
90 | 0 | this.domain = domain; |
91 | 0 | this.actions = actions; |
92 | 0 | this.targets = targets; |
93 | 0 | } |
94 | |
|
95 | |
protected String getDomain(Class<? extends DomainPermission> clazz) { |
96 | 6 | String domain = clazz.getSimpleName().toLowerCase(); |
97 | |
|
98 | 6 | int index = domain.lastIndexOf("permission"); |
99 | 6 | if (index != -1) { |
100 | 6 | domain = domain.substring(0, index); |
101 | |
} |
102 | 6 | return domain; |
103 | |
} |
104 | |
|
105 | |
public String getDomain() { |
106 | 5 | return domain; |
107 | |
} |
108 | |
|
109 | |
protected void setDomain(String domain) { |
110 | 0 | if (this.domain != null && this.domain.equals(domain)) { |
111 | 0 | return; |
112 | |
} |
113 | 0 | this.domain = domain; |
114 | 0 | setParts(domain, actions, targets); |
115 | 0 | } |
116 | |
|
117 | |
public Set<String> getActions() { |
118 | 5 | return actions; |
119 | |
} |
120 | |
|
121 | |
protected void setActions(Set<String> actions) { |
122 | 0 | if (this.actions != null && this.actions.equals(actions)) { |
123 | 0 | return; |
124 | |
} |
125 | 0 | this.actions = actions; |
126 | 0 | setParts(domain, actions, targets); |
127 | 0 | } |
128 | |
|
129 | |
public Set<String> getTargets() { |
130 | 5 | return targets; |
131 | |
} |
132 | |
|
133 | |
protected void setTargets(Set<String> targets) { |
134 | 0 | this.targets = targets; |
135 | 0 | if (this.targets != null && this.targets.equals(targets)) { |
136 | 0 | return; |
137 | |
} |
138 | 0 | this.targets = targets; |
139 | 0 | setParts(domain, actions, targets); |
140 | 0 | } |
141 | |
} |