Search in sources :

Example 1 with EDatabaseType

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

the class SMPUserManagerJDBC method updateOwnershipsAndKillUsers.

public void updateOwnershipsAndKillUsers(@Nonnull final ICommonsMap<String, String> aOldToNewUserNameMap) {
    ValueEnforcer.notNull(aOldToNewUserNameMap, "OldToNewUserNameMap");
    final DBExecutor aExecutor = newExecutor();
    aExecutor.performInTransaction(() -> {
        // Drop the Foreign Key Constraint - do this all the time
        try {
            final EDatabaseType eDBType = SMPDataSourceSingleton.getDatabaseType();
            switch(eDBType) {
                case MYSQL:
                    aExecutor.executeStatement("ALTER TABLE smp_ownership DROP FOREIGN KEY FK_smp_ownership_username;");
                    break;
                case POSTGRESQL:
                    aExecutor.executeStatement("ALTER TABLE smp_ownership DROP CONSTRAINT FK_smp_ownership_username;");
                    break;
                case ORACLE:
                case DB2:
                    // No such constraint
                    break;
                default:
                    throw new IllegalStateException("The migration code for DB type " + eDBType + " is missing");
            }
        } catch (final RuntimeException ex) {
            LOGGER.warn("Error in droping constraints", ex);
        }
        // Update user names
        for (final Map.Entry<String, String> aEntry : aOldToNewUserNameMap.entrySet()) {
            final String sOld = aEntry.getKey();
            final String sNew = aEntry.getValue();
            aExecutor.insertOrUpdateOrDelete("UPDATE smp_ownership SET username=? WHERE username=?", new ConstantPreparedStatementDataProvider(sNew, sOld));
        }
        try {
            aExecutor.executeStatement("DROP TABLE smp_user;");
        } catch (final RuntimeException ex) {
            LOGGER.warn("Error in droping table smp_user", ex);
        }
    });
}
Also used : DBExecutor(com.helger.db.jdbc.executor.DBExecutor) EDatabaseType(com.helger.phoss.smp.backend.sql.EDatabaseType) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) Map(java.util.Map) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap)

Example 2 with EDatabaseType

use of com.helger.phoss.smp.backend.sql.EDatabaseType 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)

Aggregations

EDatabaseType (com.helger.phoss.smp.backend.sql.EDatabaseType)2 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 ConstantPreparedStatementDataProvider (com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider)1 DBExecutor (com.helger.db.jdbc.executor.DBExecutor)1 SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)1 DBUser (com.helger.phoss.smp.backend.sql.domain.DBUser)1 IUser (com.helger.photon.security.user.IUser)1 IUserManager (com.helger.photon.security.user.IUserManager)1 WebScoped (com.helger.web.scope.mgr.WebScoped)1 Map (java.util.Map)1