Search in sources :

Example 1 with SMPDBExecutor

use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.

the class V12__MigrateUserGroupsToDB method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all user groups to the DB");
        final String sFilename = PhotonSecurityManager.FactoryXML.DIRECTORY_SECURITY + PhotonSecurityManager.FactoryXML.FILENAME_USERGROUPS_XML;
        final File aFile = WebFileIO.getDataIO().getFile(sFilename);
        if (aFile.exists()) {
            final IUserManager aUserMgr = new UserManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
            final IRoleManager aRoleMgr = new RoleManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
            final UserGroupManager aMgrXML = new UserGroupManager(sFilename, aUserMgr, aRoleMgr);
            final ICommonsList<IUserGroup> aUserGroups = aMgrXML.getAll();
            if (aUserGroups.isNotEmpty()) {
                final UserGroupManagerJDBC aMgrNew = new UserGroupManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER, aUserMgr, aRoleMgr);
                for (final IUserGroup aUserGroup : aUserGroups) {
                    // Don't run the callback here
                    if (aMgrNew.internalCreateNewUserGroup((UserGroup) aUserGroup, false, false) == null)
                        LOGGER.error("Failed to migrate user group " + aUserGroup + " to DB");
                }
            }
            // Rename to avoid later inconsistencies
            WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
            LOGGER.info("Finished migrating all " + aUserGroups.size() + " user groups to the DB");
        } else {
            LOGGER.warn("No user group file found");
        }
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) UserGroupManager(com.helger.photon.security.usergroup.UserGroupManager) IUserManager(com.helger.photon.security.user.IUserManager) IUserGroup(com.helger.photon.security.usergroup.IUserGroup) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) UserGroup(com.helger.photon.security.usergroup.UserGroup) IUserGroup(com.helger.photon.security.usergroup.IUserGroup) IRoleManager(com.helger.photon.security.role.IRoleManager) UserGroupManagerJDBC(com.helger.photon.jdbc.security.UserGroupManagerJDBC) UserManagerJDBC(com.helger.photon.jdbc.security.UserManagerJDBC) RoleManagerJDBC(com.helger.photon.jdbc.security.RoleManagerJDBC) File(java.io.File)

Example 2 with SMPDBExecutor

use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.

the class SMPManagerProviderSQL method beforeInitManagers.

@Override
public void beforeInitManagers() {
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    // Set the special PhotonSecurityManager factory
    // Must be before Flyway, so that auditing of Flyway actions (may) work
    PhotonSecurityManagerFactoryJDBC.install(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
    // Flyway migration is enabled by default
    if (aCF.getAsBoolean(SMPJDBCConfiguration.CONFIG_SMP_FLYWAY_ENABLED, true))
        FlywayMigrator.Singleton.INSTANCE.runFlyway(m_eDBType);
    else
        LOGGER.warn("Flyway Migration is disabled according to the configuration item " + SMPJDBCConfiguration.CONFIG_SMP_FLYWAY_ENABLED);
    // Register this here, so that the SMPMetaManager is available
    DBExecutor.setConnectionStatusChangeCallback((eOld, eNew) -> {
        // false: don't trigger callback, because the source is DBExecutor
        SMPMetaManager.getInstance().setBackendConnectionState(eNew, false);
    });
    // Allow communicating in the other direction as well
    SMPMetaManager.getInstance().setBackendConnectionStateChangeCallback(eNew -> DBExecutor.resetConnectionEstablished());
}
Also used : ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Example 3 with SMPDBExecutor

use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.

the class V15__MigrateDBUsersToPhotonUsers method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all old DB users to ph-oton users");
        final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
        // Old JDBC user manager
        final SMPUserManagerJDBC aSQLUserMgr = new SMPUserManagerJDBC(SMPDBExecutor::new);
        final ICommonsList<DBUser> aSQLUsers = aSQLUserMgr.getAllUsers();
        LOGGER.info("Found " + aSQLUsers.size() + " DB user to migrate");
        final ICommonsOrderedMap<String, String> aCreatedMappings = new CommonsLinkedHashMap<>();
        // New JDBC user manager
        final IUserManager aPhotonUserMgr = PhotonSecurityManager.getUserMgr();
        for (final DBUser aSQLUser : aSQLUsers) {
            final DBUser aDBUser = aSQLUser;
            IUser aPhotonUser = null;
            int nIndex = 0;
            while (true) {
                final String sUserName = aDBUser.getUserName() + (nIndex > 0 ? Integer.toString(nIndex) : "");
                // The suffix "@example.org" is added to make it an email-address
                final String sEmailAddress = sUserName + "@example.org";
                aPhotonUser = aPhotonUserMgr.createNewUser(sEmailAddress, sEmailAddress, aDBUser.getPassword(), null, sUserName, null, CSMPServer.DEFAULT_LOCALE, null, false);
                if (aPhotonUser != null) {
                    // New user was successfully created
                    break;
                }
                // User name already taken
                ++nIndex;
                if (nIndex > 1000) {
                    // Avoid endless loop
                    throw new IllegalStateException("Too many iterations mapping the DB user '" + aDBUser.getUserName() + "' to a ph-oton user");
                }
            }
            aCreatedMappings.put(aDBUser.getUserName(), aPhotonUser.getID());
            LOGGER.info("Mapped DB user '" + aDBUser.getUserName() + "' to ph-oton user " + aPhotonUser.getID());
        }
        // Update the ownership in "smp_ownership"
        // Remove the table "smp_user"
        aSQLUserMgr.updateOwnershipsAndKillUsers(aCreatedMappings);
        if (XMLMapHandler.writeMap(aCreatedMappings, new FileSystemResource(WebFileIO.getDataIO().getFile("migrations/db-photon-user-mapping-" + eDBType.getID() + ".xml"))).isFailure())
            LOGGER.error("Failed to store mapping of DB users to ph-oton users as XML");
        LOGGER.info("Finished migrating all DB users to ph-oton users");
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) CommonsLinkedHashMap(com.helger.commons.collection.impl.CommonsLinkedHashMap) IUserManager(com.helger.photon.security.user.IUserManager) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) IUser(com.helger.photon.security.user.IUser) DBUser(com.helger.phoss.smp.backend.sql.domain.DBUser) EDatabaseType(com.helger.phoss.smp.backend.sql.EDatabaseType)

Example 4 with SMPDBExecutor

use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.

the class V5__MigrateTransportProfilesToDB method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all transport profiles to the DB");
        final String sFilename = "transportprofiles.xml";
        final File aFile = WebFileIO.getDataIO().getFile(sFilename);
        if (aFile.exists()) {
            final SMPTransportProfileManagerXML aMgrXML = new SMPTransportProfileManagerXML(sFilename);
            final ICommonsList<ISMPTransportProfile> aTransportProfiles = aMgrXML.getAll();
            if (aTransportProfiles.isNotEmpty()) {
                final SMPTransportProfileManagerJDBC aMgrNew = new SMPTransportProfileManagerJDBC(SMPDBExecutor::new);
                for (final ISMPTransportProfile aTransportProfile : aTransportProfiles) if (aMgrNew.createSMPTransportProfile(aTransportProfile.getID(), aTransportProfile.getName(), aTransportProfile.isDeprecated()) == null)
                    LOGGER.error("Failed to migrate " + aTransportProfile + " to DB");
            }
            // Rename to avoid later inconsistencies
            WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
            LOGGER.info("Finished migrating all " + aTransportProfiles.size() + " transport profiles to the DB");
        } else {
            LOGGER.info("No transport profile file found");
        }
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) ISMPTransportProfile(com.helger.peppol.smp.ISMPTransportProfile) SMPTransportProfileManagerJDBC(com.helger.phoss.smp.backend.sql.mgr.SMPTransportProfileManagerJDBC) SMPTransportProfileManagerXML(com.helger.phoss.smp.domain.transportprofile.SMPTransportProfileManagerXML) File(java.io.File) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Example 5 with SMPDBExecutor

use of com.helger.phoss.smp.backend.sql.SMPDBExecutor in project phoss-smp by phax.

the class SMPIDFactoryJDBC method readAndUpdateIDCounter.

@Override
protected long readAndUpdateIDCounter(@Nonnegative final int nReserveCount) {
    final MutableLong aReadValue = new MutableLong(0);
    final DBExecutor aExecutor = new SMPDBExecutor();
    aExecutor.performInTransaction(() -> {
        // Read existing value
        final String sExistingValue = SMPSettingsManagerJDBC.getSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID);
        final long nRead = StringParser.parseLong(sExistingValue, m_nInitialCount);
        aReadValue.set(nRead);
        // Write new value
        final long nNewValue = nRead + nReserveCount;
        SMPSettingsManagerJDBC.setSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID, Long.toString(nNewValue));
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Updated SQL ID from " + sExistingValue + " to " + nNewValue);
    });
    return aReadValue.longValue();
}
Also used : MutableLong(com.helger.commons.mutable.MutableLong) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Aggregations

SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)8 WebScoped (com.helger.web.scope.mgr.WebScoped)6 File (java.io.File)5 RoleManagerJDBC (com.helger.photon.jdbc.security.RoleManagerJDBC)2 UserManagerJDBC (com.helger.photon.jdbc.security.UserManagerJDBC)2 IUser (com.helger.photon.security.user.IUser)2 IUserManager (com.helger.photon.security.user.IUserManager)2 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 MutableLong (com.helger.commons.mutable.MutableLong)1 DBExecutor (com.helger.db.jdbc.executor.DBExecutor)1 ISMPTransportProfile (com.helger.peppol.smp.ISMPTransportProfile)1 EDatabaseType (com.helger.phoss.smp.backend.sql.EDatabaseType)1 DBUser (com.helger.phoss.smp.backend.sql.domain.DBUser)1 SMPSettingsManagerJDBC (com.helger.phoss.smp.backend.sql.mgr.SMPSettingsManagerJDBC)1 SMPTransportProfileManagerJDBC (com.helger.phoss.smp.backend.sql.mgr.SMPTransportProfileManagerJDBC)1 SMPTransportProfileManagerXML (com.helger.phoss.smp.domain.transportprofile.SMPTransportProfileManagerXML)1 ISMPSettings (com.helger.phoss.smp.settings.ISMPSettings)1 SMPSettingsManagerXML (com.helger.phoss.smp.settings.SMPSettingsManagerXML)1 UserGroupManagerJDBC (com.helger.photon.jdbc.security.UserGroupManagerJDBC)1