Java Code Examples for org.keycloak.representations.idm.UserRepresentation#setId()

The following examples show how to use org.keycloak.representations.idm.UserRepresentation#setId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UsersInRoleTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeUsersInRoleTestClass() {
    // create a role via admin client
    testRoleRep = new RoleRepresentation("test-role", "", false);
    testRealmResource().roles().create(testRoleRep);

    emptyTestRoleRep = new RoleRepresentation("empty-test-role", "", false);
    testRealmResource().roles().create(emptyTestRoleRep);

    newUser = new UserRepresentation();
    newUser.setUsername("test_user");
    newUser.setEnabled(true);
    newUser.setId(createUserWithAdminClient(testRealmResource(), newUser));

    assignRealmRoles(testRealmResource(), newUser.getId(), testRoleRep.getName());

    userPage.setId(newUser.getId());
}
 
Example 2
Source File: UserInvalidationClusterTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected UserRepresentation createEntity(UserRepresentation user, ContainerInfo node) {
    Response response = users(node).create(user);
    String id = ApiUtil.getCreatedId(response);
    response.close();
    user.setId(id);
    return readEntity(user, node);
}
 
Example 3
Source File: UsersPartialImport.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void create(RealmModel realm, KeycloakSession session, UserRepresentation user) {
    user.setId(KeycloakModelUtils.generateId());
    UserModel userModel = RepresentationToModel.createUser(session, realm, user);
    if (userModel == null) throw new RuntimeException("Unable to create user " + getName(user));
    createdIds.put(getName(user), userModel.getId());
}
 
Example 4
Source File: LoginSettingsTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Test 
public void verifyEmail() {

    MailServer.start();
    MailServer.createEmailAccount(testUser.getEmail(), "password");        
    
    log.info("enabling verify email in login settings");
    loginSettingsPage.form().setVerifyEmailAllowed(true);
    assertTrue(loginSettingsPage.form().isVerifyEmailAllowed());
    loginSettingsPage.form().save();
    assertAlertSuccess();
    log.debug("enabled");

    log.info("configure smtp server in test realm");
    RealmRepresentation testRealmRep = testRealmResource().toRepresentation();
    testRealmRep.setSmtpServer(suiteContext.getSmtpServer());
    testRealmResource().update(testRealmRep);
    
    testAccountPage.navigateTo();
    testRealmLoginPage.form().login(testUser);
    Assert.assertEquals("An email with instructions to verify your email address has been sent to you.", 
            testRealmVerifyEmailPage.getInstructionMessage());
    
    log.info("verified verify email is enabled");
    
    log.info("disabling verify email");
    loginSettingsPage.navigateTo();
    loginSettingsPage.form().setVerifyEmailAllowed(false);
    assertFalse(loginSettingsPage.form().isVerifyEmailAllowed());
    loginSettingsPage.form().save();
    assertAlertSuccess();
    log.debug("disabled");
    
    log.debug("create new test user");
    UserRepresentation newUser = createUserRepresentation("new_user", "[email protected]", "new", "user", true);
    setPasswordFor(newUser, PASSWORD);
    String id = createUserAndResetPasswordWithAdminClient(testRealmResource(), newUser, PASSWORD);
    newUser.setId(id);
    
    log.info("log in as new user");
    testAccountPage.navigateTo();        
    testRealmLoginPage.form().login(newUser);
    assertCurrentUrlStartsWith(testAccountPage);
            
    log.info("verified verify email is disabled");
    
    MailServer.stop();
}
 
Example 5
Source File: ExportUtils.java    From keycloak with Apache License 2.0 4 votes vote down vote up
/**
 * Full export of user data stored in federated storage (including role mappings and credentials)
 *
 * @param id
 * @return fully exported user representation
 */
public static UserRepresentation exportFederatedUser(KeycloakSession session, RealmModel realm, String id, ExportOptions options) {
    UserRepresentation userRep = new UserRepresentation();
    userRep.setId(id);
    MultivaluedHashMap<String, String> attributes = session.userFederatedStorage().getAttributes(realm, id);
    if (attributes.size() > 0) {
        Map<String, List<String>> attrs = new HashMap<>();
        attrs.putAll(attributes);
        userRep.setAttributes(attrs);
    }

    Set<String> requiredActions = session.userFederatedStorage().getRequiredActions(realm, id);
    if (requiredActions.size() > 0) {
        List<String> actions = new LinkedList<>();
        actions.addAll(requiredActions);
        userRep.setRequiredActions(actions);
    }


    // Social links
    Set<FederatedIdentityModel> socialLinks = session.userFederatedStorage().getFederatedIdentities(id, realm);
    List<FederatedIdentityRepresentation> socialLinkReps = new ArrayList<>();
    for (FederatedIdentityModel socialLink : socialLinks) {
        FederatedIdentityRepresentation socialLinkRep = exportSocialLink(socialLink);
        socialLinkReps.add(socialLinkRep);
    }
    if (socialLinkReps.size() > 0) {
        userRep.setFederatedIdentities(socialLinkReps);
    }

    // Role mappings
    if (options.isGroupsAndRolesIncluded()) {
        Set<RoleModel> roles = session.userFederatedStorage().getRoleMappings(realm, id);
        List<String> realmRoleNames = new ArrayList<>();
        Map<String, List<String>> clientRoleNames = new HashMap<>();
        for (RoleModel role : roles) {
            if (role.getContainer() instanceof RealmModel) {
                realmRoleNames.add(role.getName());
            } else {
                ClientModel client = (ClientModel) role.getContainer();
                String clientId = client.getClientId();
                List<String> currentClientRoles = clientRoleNames.get(clientId);
                if (currentClientRoles == null) {
                    currentClientRoles = new ArrayList<>();
                    clientRoleNames.put(clientId, currentClientRoles);
                }

                currentClientRoles.add(role.getName());
            }
        }

        if (realmRoleNames.size() > 0) {
            userRep.setRealmRoles(realmRoleNames);
        }
        if (clientRoleNames.size() > 0) {
            userRep.setClientRoles(clientRoleNames);
        }
    }

    // Credentials
    List<CredentialModel> creds = session.userFederatedStorage().getStoredCredentials(realm, id);
    List<CredentialRepresentation> credReps = new ArrayList<>();
    for (CredentialModel cred : creds) {
        CredentialRepresentation credRep = exportCredential(cred);
        credReps.add(credRep);
    }
    userRep.setCredentials(credReps);

    // Grants
    List<UserConsentModel> consents = session.users().getConsents(realm, id);
    LinkedList<UserConsentRepresentation> consentReps = new LinkedList<>();
    for (UserConsentModel consent : consents) {
        UserConsentRepresentation consentRep = ModelToRepresentation.toRepresentation(consent);
        consentReps.add(consentRep);
    }
    if (consentReps.size() > 0) {
        userRep.setClientConsents(consentReps);
    }

    // Not Before
    int notBefore = session.userFederatedStorage().getNotBeforeOfUser(realm, userRep.getId());
    userRep.setNotBefore(notBefore);

    if (options.isGroupsAndRolesIncluded()) {
        List<String> groups = new LinkedList<>();
        for (GroupModel group : session.userFederatedStorage().getGroups(realm, id)) {
            groups.add(ModelToRepresentation.buildGroupPath(group));
        }
        userRep.setGroups(groups);
    }
    return userRep;
}
 
Example 6
Source File: UserTest.java    From keycloak with Apache License 2.0 3 votes vote down vote up
@Test
public void testAccessUserFromOtherRealm() {
    RealmRepresentation firstRealm = new RealmRepresentation();

    firstRealm.setRealm("first-realm");

    adminClient.realms().create(firstRealm);

    realm = adminClient.realm(firstRealm.getRealm());
    realmId = realm.toRepresentation().getId();

    UserRepresentation firstUser = new UserRepresentation();

    firstUser.setUsername("first");
    firstUser.setEmail("[email protected]");

    firstUser.setId(createUser(firstUser, false));

    RealmRepresentation secondRealm = new RealmRepresentation();

    secondRealm.setRealm("second-realm");

    adminClient.realms().create(secondRealm);

    adminClient.realm(firstRealm.getRealm()).users().get(firstUser.getId()).update(firstUser);

    try {
        adminClient.realm(secondRealm.getRealm()).users().get(firstUser.getId()).toRepresentation();
        fail("Should not have access to firstUser from another realm");
    } catch (NotFoundException nfe) {
        // ignore
    }
}