View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.user2.login;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.mycore.common.MCRUserInformation;
27  import org.mycore.user2.MCRRealm;
28  import org.mycore.user2.MCRRealmFactory;
29  import org.mycore.user2.MCRUserAttributeMapper;
30  import org.mycore.user2.annotation.MCRUserAttribute;
31  import org.mycore.user2.annotation.MCRUserAttributeJavaConverter;
32  import org.mycore.user2.utils.MCRRolesConverter;
33  
34  /**
35   * 
36   * @author Ren\u00E9 Adler (eagle)
37   */
38  public class MCRShibbolethUserInformation implements MCRUserInformation {
39      private String userId;
40  
41      private String realmId;
42  
43      private Map<String, Object> attributes;
44  
45      @MCRUserAttribute
46      private String realName;
47  
48      private Set<String> roles = new HashSet<>();
49  
50      public MCRShibbolethUserInformation(String userId, String realmId, Map<String, Object> attributes)
51          throws Exception {
52          this.userId = userId;
53          this.realmId = realmId;
54          this.attributes = attributes;
55  
56          MCRUserAttributeMapper attributeMapper = MCRRealmFactory.getAttributeMapper(this.realmId);
57          if (attributeMapper != null) {
58              attributeMapper.mapAttributes(this, attributes);
59          }
60      }
61  
62      /* (non-Javadoc)
63       * @see org.mycore.common.MCRUserInformation#getUserID()
64       */
65      @Override
66      public String getUserID() {
67          return userId + "@" + realmId;
68      }
69  
70      /* (non-Javadoc)
71       * @see org.mycore.common.MCRUserInformation#isUserInRole(java.lang.String)
72       */
73      @Override
74      public boolean isUserInRole(String role) {
75          return roles.contains(role);
76      }
77  
78      /* (non-Javadoc)
79       * @see org.mycore.common.MCRUserInformation#getUserAttribute(java.lang.String)
80       */
81      @Override
82      public String getUserAttribute(String attribute) {
83          String key;
84          switch (attribute) {
85          case MCRUserInformation.ATT_REAL_NAME:
86              return this.realName;
87          case MCRRealm.USER_INFORMATION_ATTR:
88              return this.realmId;
89          default:
90              key = attribute;
91              break;
92          }
93  
94          Object value = attributes.get(key);
95  
96          return value != null ? value.toString() : null;
97      }
98  
99      // This is used for MCRUserAttributeMapper
100 
101     Collection<String> getRoles() {
102         return roles;
103     }
104 
105     @MCRUserAttribute(name = "roles", separator = ";")
106     @MCRUserAttributeJavaConverter(MCRRolesConverter.class)
107     void setRoles(Collection<String> roles) {
108         this.roles.addAll(roles);
109     }
110 }