Search in sources :

Example 1 with DBUser

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

the class SMPUserManagerJDBC method getAllUsers.

@Nonnull
@ReturnsMutableCopy
public ICommonsList<DBUser> getAllUsers() {
    final ICommonsList<DBUser> ret = new CommonsArrayList<>();
    // Plaintext password....
    final ICommonsList<DBResultRow> aDBResult = newExecutor().queryAll("SELECT username, password FROM smp_user");
    if (aDBResult != null)
        for (final DBResultRow aRow : aDBResult) ret.add(new DBUser(aRow.getAsString(0), aRow.getAsString(1)));
    return ret;
}
Also used : DBResultRow(com.helger.db.jdbc.executor.DBResultRow) DBUser(com.helger.phoss.smp.backend.sql.domain.DBUser) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 2 with DBUser

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

DBUser (com.helger.phoss.smp.backend.sql.domain.DBUser)2 ReturnsMutableCopy (com.helger.commons.annotation.ReturnsMutableCopy)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsLinkedHashMap (com.helger.commons.collection.impl.CommonsLinkedHashMap)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 DBResultRow (com.helger.db.jdbc.executor.DBResultRow)1 EDatabaseType (com.helger.phoss.smp.backend.sql.EDatabaseType)1 SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)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 Nonnull (javax.annotation.Nonnull)1