chore: Migrate user role to new column (#6721)
* Migration * Double write * Backfill script * Simplify for self-hosted * flip * migration
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
IsDate,
|
||||
AllowNull,
|
||||
AfterUpdate,
|
||||
BeforeSave,
|
||||
} from "sequelize-typescript";
|
||||
import { UserPreferenceDefaults } from "@shared/constants";
|
||||
import { languages } from "@shared/i18n";
|
||||
@@ -144,6 +145,10 @@ class User extends ParanoidModel<
|
||||
@Column
|
||||
isViewer: boolean;
|
||||
|
||||
@Default(UserRole.Member)
|
||||
@Column(DataType.ENUM(...Object.values(UserRole)))
|
||||
role: UserRole;
|
||||
|
||||
@Column(DataType.BLOB)
|
||||
@Encrypted
|
||||
get jwtSecret() {
|
||||
@@ -623,6 +628,20 @@ class User extends ParanoidModel<
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Temporary hook to double write role while we transition to the new field.
|
||||
*/
|
||||
@BeforeSave
|
||||
static doubleWriteRole = async (model: User) => {
|
||||
if (model.isAdmin) {
|
||||
model.role = UserRole.Admin;
|
||||
} else if (model.isViewer) {
|
||||
model.role = UserRole.Viewer;
|
||||
} else {
|
||||
model.role = UserRole.Member;
|
||||
}
|
||||
};
|
||||
|
||||
@BeforeCreate
|
||||
static setRandomJwtSecret = (model: User) => {
|
||||
model.jwtSecret = crypto.randomBytes(64).toString("hex");
|
||||
|
||||
Reference in New Issue
Block a user