Search in sources :

Example 41 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class KeycloakApplication method importRealm.

public void importRealm(RealmRepresentation rep, String from) {
    KeycloakSession session = sessionFactory.create();
    boolean exists = false;
    try {
        session.getTransactionManager().begin();
        try {
            RealmManager manager = new RealmManager(session);
            if (rep.getId() != null && manager.getRealm(rep.getId()) != null) {
                ServicesLogger.LOGGER.realmExists(rep.getRealm(), from);
                exists = true;
            }
            if (manager.getRealmByName(rep.getRealm()) != null) {
                ServicesLogger.LOGGER.realmExists(rep.getRealm(), from);
                exists = true;
            }
            if (!exists) {
                RealmModel realm = manager.importRealm(rep);
                ServicesLogger.LOGGER.importedRealm(realm.getName(), from);
            }
            session.getTransactionManager().commit();
        } catch (Throwable t) {
            session.getTransactionManager().rollback();
            if (!exists) {
                ServicesLogger.LOGGER.unableToImportRealm(t, rep.getRealm(), from);
            }
        }
    } finally {
        session.close();
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) KeycloakSession(org.keycloak.models.KeycloakSession) RealmManager(org.keycloak.services.managers.RealmManager)

Example 42 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class AdminConsole method addRealmAccess.

private void addRealmAccess(RealmModel realm, UserModel user, Map<String, Set<String>> realmAdminAccess) {
    RealmManager realmManager = new RealmManager(session);
    ClientModel realmAdminApp = realm.getClientByClientId(realmManager.getRealmAdminClientId(realm));
    getRealmAdminAccess(realm, realmAdminApp, user, realmAdminAccess);
}
Also used : ClientModel(org.keycloak.models.ClientModel) RealmManager(org.keycloak.services.managers.RealmManager)

Example 43 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class LDAPProvidersIntegrationTest method testRemoveImportedUsers.

@Test
public void testRemoveImportedUsers() {
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        UserModel user = session.users().getUserByUsername(ctx.getRealm(), "johnkeycloak");
        Assert.assertEquals(ctx.getLdapModel().getId(), user.getFederationLink());
    });
    adminClient.realm("test").userStorage().removeImportedUsers(ldapModelId);
    testingClient.server().run(session -> {
        RealmManager manager = new RealmManager(session);
        RealmModel appRealm = manager.getRealm("test");
        UserModel user = session.userLocalStorage().getUserByUsername(appRealm, "johnkeycloak");
        Assert.assertNull(user);
    });
}
Also used : CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserModel(org.keycloak.models.UserModel) RealmModel(org.keycloak.models.RealmModel) RealmManager(org.keycloak.services.managers.RealmManager) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 44 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class MgmtPermissions method hasOneAdminRole.

public boolean hasOneAdminRole(RealmModel realm, String... adminRoles) {
    String clientId;
    RealmManager realmManager = new RealmManager(session);
    if (adminsRealm.equals(realmManager.getKeycloakAdminstrationRealm())) {
        clientId = realm.getMasterAdminClient().getClientId();
    } else if (adminsRealm.equals(realm)) {
        clientId = realm.getClientByClientId(realmManager.getRealmAdminClientId(realm)).getClientId();
    } else {
        return false;
    }
    for (String adminRole : adminRoles) {
        if (identity.hasClientRole(clientId, adminRole))
            return true;
    }
    return false;
}
Also used : RealmManager(org.keycloak.services.managers.RealmManager)

Example 45 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class UserSessionProviderOfflineTest method testOnUserRemoved.

@Test
@ModelTest
public void testOnUserRemoved(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR) -> {
        try {
            int started = Time.currentTime();
            AtomicReference<String> userSessionID = new AtomicReference<>();
            KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR1) -> {
                currentSession = sessionUR1;
                RealmModel fooRealm = currentSession.realms().createRealm("foo", "foo");
                fooRealm.setDefaultRole(currentSession.roles().addRealmRole(fooRealm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + fooRealm.getName()));
                fooRealm.setSsoSessionIdleTimeout(1800);
                fooRealm.setSsoSessionMaxLifespan(36000);
                fooRealm.setOfflineSessionIdleTimeout(2592000);
                fooRealm.setOfflineSessionMaxLifespan(5184000);
                fooRealm.addClient("foo-app");
                currentSession.users().addUser(fooRealm, "user3");
                UserSessionModel userSession = currentSession.sessions().createUserSession(fooRealm, currentSession.users().getUserByUsername(fooRealm, "user3"), "user3", "127.0.0.1", "form", true, null, null);
                userSessionID.set(userSession.getId());
                createClientSession(currentSession, fooRealm.getClientByClientId("foo-app"), userSession, "http://redirect", "state");
            });
            KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR2) -> {
                currentSession = sessionUR2;
                // Create offline session
                RealmModel fooRealm = currentSession.realms().getRealm("foo");
                UserSessionModel userSession = currentSession.sessions().getUserSession(fooRealm, userSessionID.get());
                createOfflineSessionIncludeClientSessions(currentSession, userSession);
            });
            KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionUR3) -> {
                currentSession = sessionUR3;
                RealmManager realmMgr = new RealmManager(currentSession);
                RealmModel fooRealm = realmMgr.getRealm("foo");
                UserModel user3 = currentSession.users().getUserByUsername(fooRealm, "user3");
                // Assert session was persisted with both clientSessions
                UserSessionModel offlineSession = currentSession.sessions().getOfflineUserSession(fooRealm, userSessionID.get());
                assertSession(offlineSession, user3, "127.0.0.1", started, started, "foo-app");
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionTearDown) -> {
                currentSession = sessionTearDown;
                RealmManager realmMgr = new RealmManager(currentSession);
                RealmModel fooRealm = realmMgr.getRealm("foo");
                UserModel user3 = currentSession.users().getUserByUsername(fooRealm, "user3");
                // Remove user3
                new UserManager(currentSession).removeUser(fooRealm, user3);
                // Cleanup
                realmMgr = new RealmManager(currentSession);
                realmMgr.removeRealm(realmMgr.getRealm("foo"));
            });
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserSessionModel(org.keycloak.models.UserSessionModel) UserManager(org.keycloak.models.UserManager) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmManager(org.keycloak.services.managers.RealmManager) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Aggregations

RealmManager (org.keycloak.services.managers.RealmManager)47 RealmModel (org.keycloak.models.RealmModel)34 Test (org.junit.Test)19 UserModel (org.keycloak.models.UserModel)17 KeycloakSession (org.keycloak.models.KeycloakSession)16 ClientModel (org.keycloak.models.ClientModel)13 ClientManager (org.keycloak.services.managers.ClientManager)11 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)8 Path (javax.ws.rs.Path)6 UserSessionModel (org.keycloak.models.UserSessionModel)6 UserManager (org.keycloak.models.UserManager)5 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)4 LinkedList (java.util.LinkedList)3 GET (javax.ws.rs.GET)3 NotFoundException (javax.ws.rs.NotFoundException)3 Produces (javax.ws.rs.Produces)3 UserConsentModel (org.keycloak.models.UserConsentModel)3 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)3