Update sqlite migration

Change allowance check on /admin/whois
This commit is contained in:
Till Faelligen 2022-02-11 18:04:03 +01:00
parent b5c8fc922c
commit 6fe71d92bb
5 changed files with 24 additions and 43 deletions

View file

@ -47,7 +47,8 @@ func GetAdminWhois(
req *http.Request, userAPI api.UserInternalAPI, device *api.Device,
userID string,
) util.JSONResponse {
if device.AccountType != api.AccountTypeAdmin && userID != device.UserID {
allowed := device.AccountType == api.AccountTypeAdmin || userID == device.UserID
if !allowed {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden("userID does not match the current user"),

View file

@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS account_accounts (
-- If the account is currently active
is_deactivated BOOLEAN DEFAULT FALSE,
-- The account_type (user = 1, guest = 2, admin = 3, appservice = 4)
account_type INT DEFAULT 2
account_type SMALLINT
-- TODO:
-- upgraded_ts, devices, any email reset stuff?
);

View file

@ -12,13 +12,18 @@ func LoadAddAccountType(m *sqlutil.Migrations) {
}
func UpAddAccountType(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE account_accounts ADD COLUMN IF NOT EXISTS account_type INT DEFAULT 2;")
_, err := tx.Exec("ALTER TABLE account_accounts ADD COLUMN IF NOT EXISTS account_type SMALLINT;")
if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err)
return fmt.Errorf("failed to add column: %w", err)
}
_, err = tx.Exec("UPDATE account_accounts SET account_type = 1 WHERE appservice_id = '';")
if err != nil {
return fmt.Errorf("failed to update user accounts: %w", err)
}
_, err = tx.Exec("UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> '';")
if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err)
return fmt.Errorf("failed to update appservice accounts upgrade: %w", err)
}
return nil
}

View file

@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS account_accounts (
-- If the account is currently active
is_deactivated BOOLEAN DEFAULT 0,
-- The account_type (user = 1, guest = 2, admin = 3, appservice = 4)
account_type INTEGER DEFAULT 2
account_type INTEGER
-- TODO:
-- upgraded_ts, devices, any email reset stuff?
);

View file

@ -18,49 +18,24 @@ func LoadAddAccountType(m *sqlutil.Migrations) {
}
func UpAddAccountType(tx *sql.Tx) error {
_, err := tx.Exec(`
ALTER TABLE account_accounts RENAME TO account_accounts_tmp;
CREATE TABLE account_accounts (
localpart TEXT NOT NULL PRIMARY KEY,
created_ts BIGINT NOT NULL,
password_hash TEXT,
appservice_id TEXT,
is_deactivated BOOLEAN DEFAULT 0,
account_type INTEGER DEFAULT 2
);
INSERT INTO account_accounts (
localpart, created_ts, password_hash, appservice_id
) SELECT
localpart, created_ts, password_hash, appservice_id
FROM account_accounts_tmp;
UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> '';
DROP TABLE account_accounts_tmp;`)
// initially set every account to useracount, change appserver accounts afterwards
_, err := tx.Exec(`ALTER TABLE account_accounts ADD COLUMN account_type INTEGER;`)
if err != nil {
return fmt.Errorf("failed to execute upgrade: %w", err)
return fmt.Errorf("failed to add column: %w", err)
}
_, err = tx.Exec(`UPDATE account_accounts SET account_type = 1 WHERE appservice_id = ''`)
if err != nil {
return fmt.Errorf("failed to update user accounts: %w", err)
}
_, err = tx.Exec(`UPDATE account_accounts SET account_type = 4 WHERE appservice_id <> ''`)
if err != nil {
return fmt.Errorf("failed to update appservice accounts upgrade: %w", err)
}
return nil
}
func DownAddAccountType(tx *sql.Tx) error {
_, err := tx.Exec(`
ALTER TABLE account_accounts RENAME TO account_accounts_tmp;
CREATE TABLE account_accounts (
localpart TEXT NOT NULL PRIMARY KEY,
created_ts BIGINT NOT NULL,
password_hash TEXT,
appservice_id TEXT,
is_deactivated BOOLEAN DEFAULT 0
);
INSERT
INTO account_accounts (
localpart, created_ts, password_hash, appservice_id
) SELECT
localpart, created_ts, password_hash, appservice_id
FROM account_accounts_tmp
;
DROP TABLE account_accounts_tmp;`)
_, err := tx.Exec(`ALTER TABLE account_accounts DROP COLUMN account_type;`)
if err != nil {
return fmt.Errorf("failed to execute downgrade: %w", err)
}