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

Matthias Piepkorn
2018-06-17 b8d686069c3249e4bd11eb5eef95f5bd51ea58fb
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
b8d686 72     public static ProtocolMapperModel create(String name, String tokenClaimName, boolean fullPath) {
0ad1a9 73         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
b8d686 74                 "String", PROVIDER_ID);
0ad1a9 75         mapper.getConfig().put(FULL_PATH, Boolean.toString(fullPath));
7f7e0c 76         return mapper;
MP 77     }
78 }