mirror of https://github.com/jacekkow/keycloak-protocol-cas

Matthias Piepkorn
2017-07-26 f75caf002c2014cd1dd875225417f2a5de1af9d0
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.mappers;
MP 2
513246 3 import org.keycloak.models.GroupModel;
7f7e0c 4 import org.keycloak.models.ProtocolMapperModel;
513246 5 import org.keycloak.models.UserSessionModel;
MP 6 import org.keycloak.models.utils.ModelToRepresentation;
7f7e0c 7 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
MP 8 import org.keycloak.provider.ProviderConfigProperty;
9
0ad1a9 10 import java.util.ArrayList;
MP 11 import java.util.LinkedList;
12 import java.util.List;
13 import java.util.Map;
7f7e0c 14
MP 15 public class GroupMembershipMapper extends AbstractCASProtocolMapper {
16     private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
17
0ad1a9 18     private static final String FULL_PATH = "full.path";
MP 19
7f7e0c 20     static {
MP 21         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
22         ProviderConfigProperty property1 = new ProviderConfigProperty();
0ad1a9 23         property1.setName(FULL_PATH);
7f7e0c 24         property1.setLabel("Full group path");
MP 25         property1.setType(ProviderConfigProperty.BOOLEAN_TYPE);
26         property1.setDefaultValue("true");
27         property1.setHelpText("Include full path to group i.e. /top/level1/level2, false will just specify the group name");
28         configProperties.add(property1);
29     }
30
31     public static final String PROVIDER_ID = "cas-group-membership-mapper";
32
33
34     @Override
35     public List<ProviderConfigProperty> getConfigProperties() {
36         return configProperties;
37     }
38
39     @Override
40     public String getId() {
41         return PROVIDER_ID;
42     }
43
44     @Override
45     public String getDisplayType() {
46         return "Group Membership";
47     }
48
49     @Override
50     public String getHelpText() {
51         return "Map user group membership";
52     }
53
513246 54     @Override
MP 55     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
56         List<String> membership = new LinkedList<>();
57         boolean fullPath = useFullPath(mappingModel);
58         for (GroupModel group : userSession.getUser().getGroups()) {
59             if (fullPath) {
60                 membership.add(ModelToRepresentation.buildGroupPath(group));
61             } else {
62                 membership.add(group.getName());
63             }
64         }
0ad1a9 65         setPlainAttribute(attributes, mappingModel, membership);
513246 66     }
MP 67
7f7e0c 68     public static boolean useFullPath(ProtocolMapperModel mappingModel) {
0ad1a9 69         return "true".equals(mappingModel.getConfig().get(FULL_PATH));
7f7e0c 70     }
MP 71
72     public static ProtocolMapperModel create(String name, String tokenClaimName,
73                                              boolean consentRequired, String consentText, boolean fullPath) {
0ad1a9 74         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
MP 75                 "String", consentRequired, consentText, PROVIDER_ID);
76         mapper.getConfig().put(FULL_PATH, Boolean.toString(fullPath));
7f7e0c 77         return mapper;
MP 78     }
79 }