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.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 }