progressing
This commit is contained in:
		| @@ -61,6 +61,22 @@ public class KeycloakRepository { | ||||
| 	 * @return true or false | ||||
| 	 */ | ||||
| 	public boolean register(String login, String password, String email) { | ||||
| 		UserRepresentation user = getUserRepresentation(login, password, email); | ||||
| 		UsersResource resource = getUsersResource(keycloakUrl, keycloakRealm, keycloakAdminName, keycloakAdminPassword, keycloakClientId); | ||||
| 		boolean result = register(resource, user); | ||||
| 		sendVerificationLink(login, resource); | ||||
| 		return result; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * generate a user representation | ||||
| 	 * | ||||
| 	 * @param login the login | ||||
| 	 * @param password the password | ||||
| 	 * @param email the email | ||||
| 	 * @return the user representation | ||||
| 	 */ | ||||
| 	protected UserRepresentation getUserRepresentation(String login, String password, String email) { | ||||
| 		CredentialRepresentation passwordCredentials = new CredentialRepresentation(); | ||||
| 		passwordCredentials.setTemporary(false); | ||||
| 		passwordCredentials.setType(CredentialRepresentation.PASSWORD); | ||||
| @@ -72,25 +88,33 @@ public class KeycloakRepository { | ||||
| 		user.setCredentials(Collections.singletonList(passwordCredentials)); | ||||
| 		user.setEnabled(true); | ||||
|  | ||||
| 		UsersResource instance = getInstance(); | ||||
| 		Response response = instance.create(user); | ||||
| 		boolean result = Status.OK.equals(response.getStatusInfo()); | ||||
| 		sendVerificationLink(login); | ||||
| 		return result; | ||||
| 		return user; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * register a user in keycloak | ||||
| 	 * | ||||
| 	 * @param resource the resource | ||||
| 	 * @param user the user | ||||
| 	 * @return true or false | ||||
| 	 */ | ||||
| 	protected boolean register(UsersResource resource, UserRepresentation user) { | ||||
| 		Response response = resource.create(user); | ||||
| 		return Status.OK.equals(response.getStatusInfo()); | ||||
| 	} | ||||
|  | ||||
| 	public Client getClient() { | ||||
| 		return new ResteasyClientBuilderImpl().connectionPoolSize(10).build(); | ||||
| 	} | ||||
|  | ||||
| 	public KeycloakBuilder getKeycloak() { | ||||
| 		return KeycloakBuilder.builder().serverUrl(keycloakUrl).realm(keycloakRealm).grantType(OAuth2Constants.PASSWORD) | ||||
| 				.username(keycloakAdminName).password(keycloakAdminPassword).clientId(keycloakClientId) | ||||
| 	public KeycloakBuilder getKeycloak(String url, String realm, String admin, String password, String clientId) { | ||||
| 		return KeycloakBuilder.builder().serverUrl(url).realm(realm).grantType(OAuth2Constants.PASSWORD) | ||||
| 				.username(admin).password(password).clientId(clientId) | ||||
| 				.resteasyClient(getClient()); | ||||
| 	} | ||||
|  | ||||
| 	public UsersResource getInstance() { | ||||
| 		return getKeycloak().build().realm(keycloakRealm).users(); | ||||
| 	public UsersResource getUsersResource(String url, String realm, String admin, String password, String clientId) { | ||||
| 		return getKeycloak(url, realm, admin, password, clientId).build().realm(realm).users(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -98,8 +122,7 @@ public class KeycloakRepository { | ||||
| 	 * | ||||
| 	 * @param userId the ID of the user | ||||
| 	 */ | ||||
| 	public void sendVerificationLink(String userId) { | ||||
| 		UsersResource usersResource = getInstance(); | ||||
| 	public void sendVerificationLink(String userId, UsersResource usersResource) { | ||||
| 		usersResource.get(userId).sendVerifyEmail(); | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user