1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.spring.security;
20
21 import com.fasterxml.jackson.annotation.JsonCreator;
22 import com.fasterxml.jackson.annotation.JsonIgnore;
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import org.apache.commons.lang3.builder.EqualsBuilder;
29 import org.apache.commons.lang3.builder.HashCodeBuilder;
30 import org.apache.commons.lang3.builder.ToStringBuilder;
31 import org.apache.syncope.core.provisioning.api.utils.RealmUtils;
32 import org.springframework.security.core.GrantedAuthority;
33
34 public class SyncopeGrantedAuthority implements GrantedAuthority {
35
36 private static final long serialVersionUID = -5647624636011919735L;
37
38 @JsonProperty
39 private final String entitlement;
40
41 private final Set<String> realms = new TreeSet<>();
42
43 @JsonCreator
44 public SyncopeGrantedAuthority(@JsonProperty("entitlement") final String entitlement) {
45 this.entitlement = entitlement;
46 }
47
48 public SyncopeGrantedAuthority(final String entitlement, final String realm) {
49 this.entitlement = entitlement;
50 this.realms.add(realm);
51 }
52
53 public boolean addRealm(final String newRealm) {
54 return RealmUtils.normalizingAddTo(realms, newRealm);
55 }
56
57 public void addRealms(final Collection<String> newRealms) {
58 newRealms.forEach(this::addRealm);
59 }
60
61 public Set<String> getRealms() {
62 return Collections.unmodifiableSet(realms);
63 }
64
65 @JsonIgnore
66 @Override
67 public String getAuthority() {
68 return entitlement;
69 }
70
71 @Override
72 public int hashCode() {
73 return new HashCodeBuilder().
74 append(entitlement).
75 append(realms).
76 build();
77 }
78
79 @Override
80 public boolean equals(final Object obj) {
81 if (this == obj) {
82 return true;
83 }
84 if (obj == null) {
85 return false;
86 }
87 if (getClass() != obj.getClass()) {
88 return false;
89 }
90 final SyncopeGrantedAuthority other = (SyncopeGrantedAuthority) obj;
91 return new EqualsBuilder().
92 append(entitlement, other.entitlement).
93 append(realms, other.realms).
94 build();
95 }
96
97 @Override
98 public String toString() {
99 return new ToStringBuilder(this).
100 append(entitlement).
101 append(realms).
102 build();
103 }
104 }