Debian Codegraph @

src/main/org/h2/security/auth/impl/StaticRolesMapper.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: Alessandro Ventura
5 */
6 package org.h2.security.auth.impl;
7
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.HashSet;
11
12 import org.h2.api.UserToRolesMapper;
13 import org.h2.security.auth.AuthenticationException;
14 import org.h2.security.auth.AuthenticationInfo;
15 import org.h2.security.auth.ConfigProperties;
16
17 /**
18 * Assign static roles to authenticated users
19 * <p>
20 * Configuration parameters:
21 * </p>
22 * <ul>
23 * <li>roles role list separated by comma</li>
24 * </ul>
25 *
26 */
27 public class StaticRolesMapper implements UserToRolesMapper {
28
29 private Collection<String> roles;
30
31 public StaticRolesMapper() {
32 }
33
34 public StaticRolesMapper(String... roles) {
35 this.roles=Arrays.asList(roles);
36 }
37
38 @Override
39 public void configure(ConfigProperties configProperties) {
40 String rolesString=configProperties.getStringValue("roles", "");
41 if (rolesString!=null) {
42 roles = new HashSet<>(Arrays.asList(rolesString.split(",")));
43 }
44 }
45
46 @Override
47 public Collection<String> mapUserToRoles(AuthenticationInfo authenticationInfo) throws AuthenticationException {
48 return roles;
49 }
50
51 }