本文整理汇总了Java中org.keycloak.representations.idm.UserRepresentation类的典型用法代码示例。如果您正苦于以下问题:Java UserRepresentation类的具体用法?Java UserRepresentation怎么用?Java UserRepresentation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UserRepresentation类属于org.keycloak.representations.idm包,在下文中一共展示了UserRepresentation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUserPassword
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
public void setUserPassword(String realmId, String username, String newPassword) {
List<UserRepresentation> retrieveUserList = client.realm(realmId).users().search(username,
null,
null,
null,
0, 1);
if (!retrieveUserList.isEmpty()) {
UserResource retrievedUser = client.realm(realmId).users().get(retrieveUserList.get(0).getId());
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue(newPassword);
credential.setTemporary(false);
retrievedUser.resetPassword(credential);
// Remove the UPDATE_PASSWORD required action
UserRepresentation userRepresentation = retrievedUser.toRepresentation();
userRepresentation.getRequiredActions().remove("UPDATE_PASSWORD");
retrievedUser.update(userRepresentation);
} else {
throw new RuntimeException("Requested user not found");
}
}
示例2: createUser
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public String createUser(String username, String password, String firstname, String lastname, String email, List<String> rolenames) {
if (username.equals("user")) {
throw new UnsupportedOperationException("Dont't do that! (Sso rest api doesn't create user with username 'user' properly)");
}
CredentialRepresentation cr = new CredentialRepresentation();
cr.setType(CredentialRepresentation.PASSWORD);
cr.setValue(password);
cr.setTemporary(false);
UserRepresentation ur = new UserRepresentation();
ur.setUsername(username);
ur.setCredentials(Arrays.asList(cr));
ur.setFirstName(firstname);
ur.setLastName(lastname);
ur.setEmail(email);
ur.setEnabled(true);
Response response = client.realm(realmName).users().create(ur);
response.close();
String userId = getUserId(username);
client.realm(realmName).users().get(userId).resetPassword(cr);
if (rolenames != null && rolenames.size() > 0) {
addRealmRolesToUser(userId, rolenames);
}
return userId;
}
示例3: getEmailVerifiedUpdatedFlag
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public String getEmailVerifiedUpdatedFlag(String userId) {
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation user = resource.toRepresentation();
Map<String, List<String>> map = user.getAttributes();
List<String> list = map.get(JsonKey.EMAIL_VERIFIED_UPDATED);
if (!list.isEmpty()) {
return list.get(0);
} else {
return "";
}
}
示例4: getLastLoginTime
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public String getLastLoginTime(String userId) {
String lastLoginTime = null;
try {
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation ur = resource.toRepresentation();
Map<String, List<String>> map = ur.getAttributes();
if (map == null) {
map = new HashMap<>();
}
List<String> list = map.get(JsonKey.LAST_LOGIN_TIME);
if (list != null && !list.isEmpty()) {
lastLoginTime = list.get(0);
}
} catch (Exception e) {
ProjectLogger.log(e.getMessage(), e);
}
return lastLoginTime;
}
示例5: setEmailVerifiedTrue
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public String setEmailVerifiedTrue(String userId) {
try {
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation ur = resource.toRepresentation();
ur.setEmailVerified(true);
if (isNotNull(resource)) {
try {
resource.update(ur);
} catch (Exception ex) {
ProjectLogger.log(ex.getMessage(), ex);
throw new ProjectCommonException(ResponseCode.invalidUsrData.getErrorCode(),
ResponseCode.invalidUsrData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
}
} catch (Exception e) {
ProjectLogger.log(e.getMessage(), e);
throw new ProjectCommonException(ResponseCode.invalidUsrData.getErrorCode(),
ResponseCode.invalidUsrData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
return JsonKey.SUCCESS;
}
示例6: setEmailVerifiedAsFalse
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public String setEmailVerifiedAsFalse(String userId) {
try {
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation ur = resource.toRepresentation();
ur.setEmailVerified(false);
if (isNotNull(resource)) {
try {
resource.update(ur);
} catch (Exception ex) {
ProjectLogger.log(ex.getMessage(), ex);
throw new ProjectCommonException(ResponseCode.invalidUsrData.getErrorCode(),
ResponseCode.invalidUsrData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
}
} catch (Exception e) {
ProjectLogger.log(e.getMessage(), e);
throw new ProjectCommonException(ResponseCode.invalidUsrData.getErrorCode(),
ResponseCode.invalidUsrData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
return JsonKey.SUCCESS;
}
示例7: checkUserExistence
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
/**
* Check the existence of user with email.
*
* @param email email of the user
* @throws IOException
*/
public void checkUserExistence(String email) throws IOException, DuplicatedKeycloakEntry{
// First try: Find the user by searching for the username field
List<UserRepresentation> users = getProjectUserRealm().users().search(email, null, null, null, -1, -1);
String errMsg = "";
// If we found one, it already has the user
if (users.size()>0){
throw new DuplicatedKeycloakEntry("User with username: " +email + " already exists.", errMsg);
}
// Second try: Find the user by searching for the email field
users = getProjectUserRealm().users().search(null, null, null, email, -1, -1);
// If we found one, it already has the user
if (users.size()>0){
throw new DuplicatedKeycloakEntry("User with email: " +email + " already exists.", errMsg);
}
}
示例8: deleteUser
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
/**
* Delete a user from Keycloak
*
* @param email email of the user to delete
*/
public void deleteUser(String email) {
// First try: Find the user by searching for the username
List<UserRepresentation> users = getProjectUserRealm().users().search(email, null, null, null, -1, -1);
// If we found one, delete it
if (!users.isEmpty()) {
getProjectUserRealm().users().get(users.get(0).getId()).remove();
return ;
}
// Second try: Find the user by searching for the email
users = getProjectUserRealm().users().search(null, null, null, email, -1, -1);
// If we found one, delete it
if (!users.isEmpty()) {
getProjectUserRealm().users().get(users.get(0).getId()).remove();
return ;
}
}
示例9: createRealm
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public void createRealm(String realmName, String realmAdminUser) {
final RealmRepresentation newRealm = new RealmRepresentation();
newRealm.setRealm(realmName);
newRealm.setEnabled(true);
newRealm.setPasswordPolicy("hashAlgorithm(scramsha1)");
final UserRepresentation newUser = new UserRepresentation();
newUser.setUsername(realmAdminUser);
newUser.setEnabled(true);
newUser.setClientRoles(Collections.singletonMap("realm-management", Collections.singletonList("manage-users")));
newRealm.setUsers(Collections.singletonList(newUser));
try (CloseableKeycloak wrapper = new CloseableKeycloak(params)) {
wrapper.get().realms().create(newRealm);
}
}
示例10: update
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public User update(User entity) throws SecurityManagementException {
checkNotNull("entity",
entity);
consumeRealm(realmResource -> {
UsersResource usersResource = realmResource.users();
UserResource userResource = getUserResource(usersResource,
entity.getIdentifier());
if (userResource == null) {
throw new UserNotFoundException(entity.getIdentifier());
}
UserRepresentation userRepresentation = new UserRepresentation();
fillUserRepresentationAttributes(entity,
userRepresentation);
ClientResponse response = (ClientResponse) userResource.update(userRepresentation);
handleResponse(response);
});
return entity;
}
示例11: getUserRepresentations
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
private List<UserRepresentation> getUserRepresentations(String pattern,
int start,
int size) {
List<UserRepresentation> result = null;
if (isEmpty(pattern)) {
result = userRepresentations.subList(start,
start + size);
} else {
UserResource userResource = getUser(userResources,
pattern);
if (userResource != null) {
result = new ArrayList<UserRepresentation>(1);
result.add(userResource.toRepresentation());
}
}
return result;
}
示例12: updateUserDetails
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public void updateUserDetails(User user) {
UserRepresentation ur = client.realm(realmName).users().get(user.id).toRepresentation();
ur.setFirstName(user.firstName);
ur.setLastName(user.lastName);
ur.setEmail(user.email);
client.realm(realmName).users().get(user.id).update(ur);
}
示例13: deactivateUser
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
/**
* Method to deactivate the user on basis of user id.
*
* @param request Map
* @return boolean true if success otherwise false .
*/
@Override
public String deactivateUser(Map<String, Object> request) {
try {
Keycloak keycloak = KeyCloakConnectionProvider.getConnection();
String userId = (String) request.get(JsonKey.USER_ID);
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation ur = resource.toRepresentation();
ur.setEnabled(false);
if (isNotNull(resource)) {
try {
resource.update(ur);
} catch (Exception ex) {
ProjectLogger.log(ex.getMessage(), ex);
throw new ProjectCommonException(ResponseCode.invalidUsrData.getErrorCode(),
ResponseCode.invalidUsrData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
}
} catch (Exception e) {
ProjectLogger.log(e.getMessage(), e);
throw new ProjectCommonException(ResponseCode.invalidUsrData.getErrorCode(),
ResponseCode.invalidUsrData.getErrorMessage(),
ResponseCode.CLIENT_ERROR.getResponseCode());
}
return JsonKey.SUCCESS;
}
示例14: setEmailVerifiedUpdatedFlag
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public void setEmailVerifiedUpdatedFlag(String userId, String flag) {
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation user = resource.toRepresentation();
Map<String, List<String>> map = user.getAttributes();
List<String> list = new ArrayList<>();
list.add(flag);
if (map == null) {
map = new HashMap<>();
}
map.put(JsonKey.EMAIL_VERIFIED_UPDATED, list);
user.setAttributes(map);
resource.update(user);
}
示例15: addUserLoginTime
import org.keycloak.representations.idm.UserRepresentation; //导入依赖的package包/类
@Override
public boolean addUserLoginTime(String userId) {
boolean response = true;
try {
UserResource resource =
keycloak.realm(KeyCloakConnectionProvider.SSO_REALM).users().get(userId);
UserRepresentation ur = resource.toRepresentation();
Map<String, List<String>> map = ur.getAttributes();
List<String> list = new ArrayList<>();
if (map == null) {
map = new HashMap<>();
}
List<String> currentLogTime = map.get(JsonKey.CURRENT_LOGIN_TIME);
if (currentLogTime == null || currentLogTime.isEmpty()) {
currentLogTime = new ArrayList<>();
currentLogTime.add(Long.toString(System.currentTimeMillis()));
} else {
list.add(currentLogTime.get(0));
currentLogTime.clear();
currentLogTime.add(0, Long.toString(System.currentTimeMillis()));
}
map.put(JsonKey.CURRENT_LOGIN_TIME, currentLogTime);
map.put(JsonKey.LAST_LOGIN_TIME, list);
ur.setAttributes(map);
resource.update(ur);
} catch (Exception e) {
ProjectLogger.log(e.getMessage(), e);
response = false;
}
return response;
}