package io.liveoak.keycloak.server;

import org.keycloak.models.AdminRoles;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.ClaimMask;
import org.keycloak.models.Config;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.services.managers.ApplicationManager;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.KeycloakApplication;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import java.io.FileNotFoundException;

public class KeycloakServerApplication extends KeycloakApplication {

    static {
        Config.setAdminRealm("liveoak-admin");
        Config.setModelProvider("mongo");
        Config.setThemeDefault("liveoak");
    }

    public KeycloakServerApplication(@Context ServletContext servletContext) throws FileNotFoundException {
        super(servletContext);

        KeycloakSession session = factory.createSession();
        session.getTransaction().begin();
        try {
            configureLiveOakConsole(session, "http://localhost:8080");

            session.getTransaction().commit();
        } finally {
            session.close();
        }
    }

    protected void configureLiveOakConsole(KeycloakSession session, String baseUrl) {
        RealmManager manager = new RealmManager(session);
        RealmModel adminRealm = manager.getRealm(Config.getAdminRealm());

        if (adminRealm.getApplicationByName("console") == null) {
            ApplicationModel consoleApp = new ApplicationManager(manager).createApplication(adminRealm, "console");
            consoleApp.setPublicClient(true);

            consoleApp.addDefaultRole("user");
            consoleApp.addRole("admin");

            consoleApp.setAllowedClaimsMask(ClaimMask.USERNAME);

            consoleApp.addRedirectUri(baseUrl + "/admin");
            consoleApp.addRedirectUri(baseUrl + "/admin/");
            consoleApp.addWebOrigin(baseUrl);
            consoleApp.setBaseUrl(baseUrl + "/admin/");
        }

        RealmModel appsRealm = manager.getRealm("liveoak-apps");

        if (appsRealm == null) {
            RealmManager realmManager = new RealmManager(session);
            RealmModel realm = realmManager.createRealm("liveoak-apps");
            realm.setEnabled(true);
            realm.setRegistrationAllowed(true);
            realm.setSslNotRequired(true);
            realm.addRequiredCredential(CredentialRepresentation.PASSWORD);

            realm.setCentralLoginLifespan(3000);
            realm.setAccessTokenLifespan(60);
            realm.setRefreshTokenLifespan(3600);
            realm.setAccessCodeLifespan(60);
            realm.setAccessCodeLifespanUserAction(300);

            manager.generateRealmKeys(realm);

            adminRealm = manager.getRealm(Config.getAdminRealm());
            ApplicationModel appsRealmApp = adminRealm.getApplicationByName("liveoak-apps-realm");

            UserModel admin = adminRealm.getUser("admin");

            adminRealm.grantRole(admin, appsRealmApp.getRole(AdminRoles.MANAGE_APPLICATIONS));
        }
    }

}

@@ -1587,20 +1587,46 @@
         
-if (
+ApplicationModel consoleApp = 
 adminRea
@@ -1659,16 +1659,41 @@
 onsole%22)
+;%0D%0A        if (consoleApp
  == null
@@ -1709,33 +1709,16 @@
         
-ApplicationModel 
 consoleA
@@ -1850,100 +1850,8 @@
 %0D%0A%0D%0A
-            consoleApp.addDefaultRole(%22user%22);%0D%0A            consoleApp.addRole(%22admin%22);%0D%0A%0D%0A
     
@@ -2130,32 +2130,97 @@
 l + %22/admin/%22);%0D
+%0A%0D%0A            consoleApp.addScope(adminRealm.getRole(%22admin%22));%0D
 %0A        %7D%0D%0A%0D%0A  
@@ -2279,26 +2279,24 @@
 ak-apps%22);%0D%0A
-%0D%0A
         if (
@@ -2970,338 +2970,8 @@
 m);%0D
-%0A%0D%0A            adminRealm = manager.getRealm(Config.getAdminRealm());%0D%0A            ApplicationModel appsRealmApp = adminRealm.getApplicationByName(%22liveoak-apps-realm%22);%0D%0A%0D%0A            UserModel admin = adminRealm.getUser(%22admin%22);%0D%0A%0D%0A            adminRealm.grantRole(admin, appsRealmApp.getRole(AdminRoles.MANAGE_APPLICATIONS));%0D
 %0A   

Add scope on admin role to LO console