2023-08-20 10:56:13 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Player Analytics (Plan).
|
|
|
|
*
|
|
|
|
* Plan is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License v3 as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Plan is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package com.djrapitops.plan.storage.database.transactions.patches;
|
|
|
|
|
|
|
|
import com.djrapitops.plan.exceptions.database.DBOpException;
|
2024-03-11 19:52:41 +01:00
|
|
|
import com.djrapitops.plan.storage.database.sql.building.Sql;
|
2023-10-21 17:18:31 +02:00
|
|
|
import com.djrapitops.plan.storage.database.sql.tables.webuser.SecurityTable;
|
|
|
|
import com.djrapitops.plan.storage.database.sql.tables.webuser.WebGroupTable;
|
2023-08-20 10:56:13 +02:00
|
|
|
|
|
|
|
import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces permission_level with group_id to plan_security table.
|
|
|
|
*
|
|
|
|
* @author AuroraLS3
|
|
|
|
*/
|
|
|
|
public class SecurityTableGroupPatch extends Patch {
|
|
|
|
|
|
|
|
private final String tempTableName;
|
|
|
|
private final String tableName;
|
|
|
|
|
|
|
|
public SecurityTableGroupPatch() {
|
|
|
|
tableName = SecurityTable.TABLE_NAME;
|
|
|
|
tempTableName = "temp_security";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasBeenApplied() {
|
|
|
|
return hasColumn(tableName, SecurityTable.GROUP_ID)
|
|
|
|
&& !hasColumn(tableName, "permission_level")
|
|
|
|
&& !hasTable(tempTableName); // If this table exists the patch has failed to finish.
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void applyPatch() {
|
|
|
|
try {
|
2023-09-23 21:24:34 +02:00
|
|
|
boolean hasIdColumn = hasColumn(tableName, SecurityTable.ID)
|
|
|
|
// Either temp table doesn't exist or it has id column so one can be selected.
|
|
|
|
&& !hasTable(tempTableName) || hasColumn(tempTableName, SecurityTable.ID);
|
|
|
|
if (hasIdColumn) {
|
|
|
|
dropForeignKeys(tableName);
|
|
|
|
ensureNoForeignKeyConstraints(tableName);
|
|
|
|
}
|
|
|
|
|
2023-08-20 10:56:13 +02:00
|
|
|
tempOldTable();
|
|
|
|
dropTable(tableName);
|
|
|
|
execute(SecurityTable.createTableSQL(dbType));
|
|
|
|
|
|
|
|
execute("INSERT INTO " + tableName + " (" +
|
2023-09-23 21:24:34 +02:00
|
|
|
(hasIdColumn ? SecurityTable.ID + ',' : "") +
|
2023-08-20 10:56:13 +02:00
|
|
|
SecurityTable.USERNAME + ',' +
|
|
|
|
SecurityTable.LINKED_TO + ',' +
|
|
|
|
SecurityTable.SALT_PASSWORD_HASH + ',' +
|
|
|
|
SecurityTable.GROUP_ID +
|
|
|
|
") " + SELECT +
|
2023-09-23 21:24:34 +02:00
|
|
|
(hasIdColumn ? SecurityTable.ID + ',' : "") +
|
2023-08-20 10:56:13 +02:00
|
|
|
SecurityTable.USERNAME + ',' +
|
|
|
|
SecurityTable.LINKED_TO + ',' +
|
|
|
|
SecurityTable.SALT_PASSWORD_HASH + ',' +
|
2024-03-11 19:52:41 +01:00
|
|
|
"(" + SELECT + WebGroupTable.ID + FROM + WebGroupTable.TABLE_NAME + WHERE + WebGroupTable.NAME + "=" + Sql.concat(dbType, "'legacy_level_'", "permission_level") + ")" +
|
2023-08-20 10:56:13 +02:00
|
|
|
FROM + tempTableName
|
|
|
|
);
|
|
|
|
|
|
|
|
dropTable(tempTableName);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new DBOpException(SecurityTableGroupPatch.class.getSimpleName() + " failed.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void tempOldTable() {
|
|
|
|
if (!hasTable(tempTableName)) {
|
|
|
|
renameTable(tableName, tempTableName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|