forked from External/mage
Added chat mute and user (de)activation actions to the Mage server console.
This commit is contained in:
parent
7c4b40073c
commit
c46f75ac28
18 changed files with 511 additions and 213 deletions
|
|
@ -12,6 +12,7 @@ import com.j256.ormlite.table.TableUtils;
|
|||
import java.io.File;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import mage.cards.repository.CardRepository;
|
||||
import mage.cards.repository.RepositoryUtil;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.shiro.crypto.RandomNumberGenerator;
|
||||
|
|
@ -27,7 +28,7 @@ public enum AuthorizedUserRepository {
|
|||
private static final String JDBC_URL = "jdbc:h2:file:./db/authorized_user.h2;AUTO_SERVER=TRUE";
|
||||
private static final String VERSION_ENTITY_NAME = "authorized_user";
|
||||
// raise this if db structure was changed
|
||||
private static final long DB_VERSION = 1;
|
||||
private static final long DB_VERSION = 2;
|
||||
private static final RandomNumberGenerator rng = new SecureRandomNumberGenerator();
|
||||
|
||||
private Dao<AuthorizedUser, Object> dao;
|
||||
|
|
@ -39,16 +40,10 @@ public enum AuthorizedUserRepository {
|
|||
}
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
boolean obsolete = RepositoryUtil.isDatabaseObsolete(connectionSource, VERSION_ENTITY_NAME, DB_VERSION);
|
||||
|
||||
if (obsolete) {
|
||||
TableUtils.dropTable(connectionSource, AuthorizedUser.class, true);
|
||||
}
|
||||
|
||||
TableUtils.createTableIfNotExists(connectionSource, AuthorizedUser.class);
|
||||
dao = DaoManager.createDao(connectionSource, AuthorizedUser.class);
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error creating authorized_user repository - ", ex);
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error creating / assigning authorized_user repository - ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -87,6 +82,14 @@ public enum AuthorizedUserRepository {
|
|||
return null;
|
||||
}
|
||||
|
||||
public void update(AuthorizedUser authorizedUser) {
|
||||
try {
|
||||
dao.update(authorizedUser);
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error updating authorized_user", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public AuthorizedUser getByEmail(String userName) {
|
||||
try {
|
||||
QueryBuilder<AuthorizedUser, Object> qb = dao.queryBuilder();
|
||||
|
|
@ -112,4 +115,38 @@ public enum AuthorizedUserRepository {
|
|||
Logger.getLogger(AuthorizedUserRepository.class).error("Error closing authorized_user repository - ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public long getDBVersionFromDB() {
|
||||
try {
|
||||
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
|
||||
return RepositoryUtil.getDatabaseVersion(connectionSource, VERSION_ENTITY_NAME);
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(CardRepository.class).error("Error getting DB version from DB - ", ex);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean checkAlterAndMigrateAuthorizedUser() {
|
||||
long currentDBVersion = getDBVersionFromDB();
|
||||
if (currentDBVersion == 1 && DB_VERSION == 2) {
|
||||
return migrateFrom1To2();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean migrateFrom1To2() {
|
||||
try {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).info("Starting " + VERSION_ENTITY_NAME + " DB migration from version 1 to version 2");
|
||||
dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN active BOOLEAN DEFAULT true;");
|
||||
dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN lockedUntil DATETIME;");
|
||||
dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN chatLockedUntil DATETIME;");
|
||||
dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN lastConnection DATETIME;");
|
||||
RepositoryUtil.updateVersion(dao.getConnectionSource(), VERSION_ENTITY_NAME, DB_VERSION);
|
||||
Logger.getLogger(AuthorizedUserRepository.class).info("Migration finished.");
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error while migrating from version 1 to version 2 - ", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue