| 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 |
|
| 11 |
import org.h2.api.UserToRolesMapper; |
| 12 |
import org.h2.security.auth.AuthenticationException; |
| 13 |
import org.h2.security.auth.AuthenticationInfo; |
| 14 |
import org.h2.security.auth.ConfigProperties; |
| 15 |
|
| 16 |
/** |
| 17 |
* Assign to user a role based on realm name |
| 18 |
* |
| 19 |
* * <p> |
| 20 |
* Configuration parameters: |
| 21 |
* </p> |
| 22 |
* <ul> |
| 23 |
* <li> roleNameFormat, optional by default is @{realm}</li> |
| 24 |
* </ul> |
| 25 |
*/ |
| 26 |
public class AssignRealmNameRole implements UserToRolesMapper{ |
| 27 |
|
| 28 |
private String roleNameFormat; |
| 29 |
|
| 30 |
public AssignRealmNameRole() { |
| 31 |
this("@%s"); |
| 32 |
} |
| 33 |
|
| 34 |
public AssignRealmNameRole(String roleNameFormat) { |
| 35 |
this.roleNameFormat = roleNameFormat; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void configure(ConfigProperties configProperties) { |
| 40 |
roleNameFormat=configProperties.getStringValue("roleNameFormat",roleNameFormat); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public Collection<String> mapUserToRoles(AuthenticationInfo authenticationInfo) throws AuthenticationException { |
| 45 |
return Arrays.asList(String.format(roleNameFormat, authenticationInfo.getRealm())); |
| 46 |
} |
| 47 |
|
| 48 |
} |