Search in sources :

Example 1 with UserManager

use of com.helger.photon.security.user.UserManager in project phoss-smp by phax.

the class V11__MigrateUsersToDB method migrate.

public void migrate(@Nonnull final Context context) throws Exception {
    try (final WebScoped aWS = new WebScoped()) {
        LOGGER.info("Migrating all users to the DB");
        final String sFilename = PhotonSecurityManager.FactoryXML.DIRECTORY_SECURITY + PhotonSecurityManager.FactoryXML.FILENAME_USERS_XML;
        final File aFile = WebFileIO.getDataIO().getFile(sFilename);
        if (aFile.exists()) {
            // Read the XML file
            final UserManager aMgrXML = new UserManager(sFilename);
            final ICommonsList<IUser> aUsers = aMgrXML.getAll();
            if (aUsers.isNotEmpty()) {
                // Create a new JDBC manager
                final UserManagerJDBC aMgrNew = new UserManagerJDBC(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
                for (final IUser aUser : aUsers) {
                    // Don't run the callback here
                    if (aMgrNew.internalCreateNewUser((User) aUser, false, false) == null)
                        LOGGER.error("Failed to migrate user " + aUser + " to DB");
                }
            }
            // Rename to avoid later inconsistencies
            WebFileIO.getDataIO().renameFile(sFilename, sFilename + ".migrated");
            LOGGER.info("Finished migrating all " + aUsers.size() + " users to the DB");
        } else {
            LOGGER.warn("No user file found");
        }
    }
}
Also used : WebScoped(com.helger.web.scope.mgr.WebScoped) IUser(com.helger.photon.security.user.IUser) User(com.helger.photon.security.user.User) UserManager(com.helger.photon.security.user.UserManager) IUser(com.helger.photon.security.user.IUser) UserManagerJDBC(com.helger.photon.jdbc.security.UserManagerJDBC) File(java.io.File) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Aggregations

SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)1 UserManagerJDBC (com.helger.photon.jdbc.security.UserManagerJDBC)1 IUser (com.helger.photon.security.user.IUser)1 User (com.helger.photon.security.user.User)1 UserManager (com.helger.photon.security.user.UserManager)1 WebScoped (com.helger.web.scope.mgr.WebScoped)1 File (java.io.File)1