Skip to content

Commit b3fcc37

Browse files
committed
Fix NULL passwordhash in view, and migration code cleanup
1 parent bb951bc commit b3fcc37

11 files changed

+255
-314
lines changed

Common/Migrations/20240319160003_DbCleanup.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
1414
// ### CUSTOM SQL BEGIN ###
1515

1616
// Update the password hashes prefix BEFORE dropping the column
17-
migrationBuilder.Sql("UPDATE users SET password = CONCAT('pbkdf2:', substring(password from 5)) WHERE password LIKE 'USER$%'");
18-
migrationBuilder.Sql("UPDATE users SET password = CONCAT('bcrypt:', password) WHERE password NOT LIKE 'pbkdf2:%'");
17+
migrationBuilder.Sql("UPDATE users SET password = CONCAT('pbkdf2:', substring(password from 5)) WHERE password LIKE 'USER$%';");
18+
migrationBuilder.Sql("UPDATE users SET password = CONCAT('bcrypt:', password) WHERE password NOT LIKE 'pbkdf2:%';");
1919

2020
// #### CUSTOM SQL END ####
2121

@@ -77,12 +77,12 @@ protected override void Down(MigrationBuilder migrationBuilder)
7777
// ### CUSTOM SQL BEGIN ###
7878

7979
// Populate the password_encryption column BEFORE updating the password hashes prefix
80-
migrationBuilder.Sql("UPDATE users SET password_encryption = 'pbkdf2' WHERE password LIKE 'pbkdf2:%'");
81-
migrationBuilder.Sql("UPDATE users SET password_encryption = 'bcrypt_enhanced' WHERE password LIKE 'bcrypt:%'");
80+
migrationBuilder.Sql("UPDATE users SET password_encryption = 'pbkdf2' WHERE password LIKE 'pbkdf2:%';");
81+
migrationBuilder.Sql("UPDATE users SET password_encryption = 'bcrypt_enhanced' WHERE password LIKE 'bcrypt:%';");
8282

8383
// Update the password hashes prefix AFTER updating the password_encryption column
84-
migrationBuilder.Sql("UPDATE users SET password = SUBSTRING(password FROM 8) WHERE password LIKE 'pbkdf2:%'");
85-
migrationBuilder.Sql("UPDATE users SET password = SUBSTRING(password FROM 8) WHERE password LIKE 'bcrypt:%'");
84+
migrationBuilder.Sql("UPDATE users SET password = SUBSTRING(password FROM 8) WHERE password LIKE 'pbkdf2:%';");
85+
migrationBuilder.Sql("UPDATE users SET password = SUBSTRING(password FROM 8) WHERE password LIKE 'bcrypt:%';");
8686

8787
// #### CUSTOM SQL END ####
8888
}

Common/Migrations/20241029221207_AddShareRequests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public partial class AddShareRequests : Migration
1212
protected override void Up(MigrationBuilder migrationBuilder)
1313
{
1414
migrationBuilder.Sql(
15-
"ALTER TABLE password_resets ALTER COLUMN used_on TYPE timestamp with time zone USING (CASE WHEN used_on IS NOT NULL THEN CURRENT_TIMESTAMP ELSE NULL END)");
15+
"ALTER TABLE password_resets ALTER COLUMN used_on TYPE timestamp with time zone USING (CASE WHEN used_on IS NOT NULL THEN CURRENT_TIMESTAMP ELSE NULL END);");
1616

1717
migrationBuilder.CreateTable(
1818
name: "share_requests",
@@ -97,7 +97,7 @@ protected override void Down(MigrationBuilder migrationBuilder)
9797
name: "share_requests");
9898

9999
migrationBuilder.Sql(
100-
"ALTER TABLE password_resets ALTER COLUMN used_on TYPE time with time zone USING used_on::time with time zone");
100+
"ALTER TABLE password_resets ALTER COLUMN used_on TYPE time with time zone USING used_on::time with time zone;");
101101

102102
}
103103
}

Common/Migrations/20241031153812_AddAdminUsersView.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenShock.Common.Migrations
88
/// <inheritdoc />
99
public partial class AddAdminUsersView : Migration
1010
{
11-
public const string AdminUsersViewCreateQuery =
11+
public const string Query_Create_AdminUsersView =
1212
"""
1313
CREATE VIEW admin_users_view AS
1414
SELECT
@@ -33,21 +33,21 @@ CREATE VIEW admin_users_view AS
3333
users u;
3434
""";
3535

36-
public const string AdminUsersViewDropQuery =
36+
public const string Query_Drop_AdminUsersView =
3737
"""
38-
DROP VIEW admin_users_view
38+
DROP VIEW admin_users_view;
3939
""";
4040

4141
/// <inheritdoc />
4242
protected override void Up(MigrationBuilder migrationBuilder)
4343
{
44-
migrationBuilder.Sql(AdminUsersViewCreateQuery);
44+
migrationBuilder.Sql(Query_Create_AdminUsersView);
4545
}
4646

4747
/// <inheritdoc />
4848
protected override void Down(MigrationBuilder migrationBuilder)
4949
{
50-
migrationBuilder.Sql(AdminUsersViewDropQuery);
50+
migrationBuilder.Sql(Query_Drop_AdminUsersView);
5151
}
5252
}
5353
}

Common/Migrations/20241105235041_RestrictFieldLengths.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
1818
""");
1919

2020
// We need to drop the view to modify the target table
21-
migrationBuilder.Sql(AddAdminUsersView.AdminUsersViewDropQuery);
21+
migrationBuilder.Sql(AddAdminUsersView.Query_Drop_AdminUsersView);
2222

2323
migrationBuilder.AlterColumn<string>(
2424
name: "old_name",
@@ -161,14 +161,14 @@ protected override void Up(MigrationBuilder migrationBuilder)
161161
oldType: "character varying");
162162

163163
// Re-Create the view
164-
migrationBuilder.Sql(AddAdminUsersView.AdminUsersViewCreateQuery);
164+
migrationBuilder.Sql(AddAdminUsersView.Query_Create_AdminUsersView);
165165
}
166166

167167
/// <inheritdoc />
168168
protected override void Down(MigrationBuilder migrationBuilder)
169169
{
170170
// We need to drop the view to modify the target table
171-
migrationBuilder.Sql(AddAdminUsersView.AdminUsersViewDropQuery);
171+
migrationBuilder.Sql(AddAdminUsersView.Query_Drop_AdminUsersView);
172172

173173
migrationBuilder.AlterColumn<string>(
174174
name: "old_name",
@@ -311,7 +311,7 @@ protected override void Down(MigrationBuilder migrationBuilder)
311311
oldMaxLength: 40);
312312

313313
// Re-Create the view
314-
migrationBuilder.Sql(AddAdminUsersView.AdminUsersViewCreateQuery);
314+
migrationBuilder.Sql(AddAdminUsersView.Query_Create_AdminUsersView);
315315
}
316316
}
317317
}

Common/Migrations/20241122214013_Fix Petrainer998DR RFIDs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ UPDATE shockers
1616
SET
1717
rf_id = ((rf_id)::bit(32) << 1)::integer
1818
WHERE
19-
model = 'petrainer998DR'
19+
model = 'petrainer998DR';
2020
""",
2121
true
2222
);
@@ -31,7 +31,7 @@ UPDATE shockers
3131
SET
3232
rf_id = ((rf_id)::bit(32) >> 1)::integer
3333
WHERE
34-
model = 'petrainer998DR'
34+
model = 'petrainer998DR';
3535
""",
3636
true
3737
);

Common/Migrations/20250203224107_RanksToRoles.cs

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,39 @@ namespace OpenShock.Common.Migrations
77
/// <inheritdoc />
88
public partial class RanksToRoles : Migration
99
{
10+
public const string Query_Create_AdminUsersView =
11+
"""
12+
CREATE VIEW admin_users_view AS
13+
SELECT
14+
u.id,
15+
u.name,
16+
u.email,
17+
SPLIT_PART(u.password_hash, ':', 1) AS password_hash_type,
18+
u.created_at,
19+
u.email_activated,
20+
u.roles,
21+
(SELECT COUNT(*) FROM api_tokens ato WHERE ato.user_id = u.id) AS api_token_count,
22+
(SELECT COUNT(*) FROM password_resets pre WHERE pre.user_id = u.id) AS password_reset_count,
23+
(SELECT COUNT(*) FROM shocker_shares ssh WHERE ssh.shared_with = u.id) AS shocker_share_count,
24+
(SELECT COUNT(*) FROM shocker_shares_links ssl WHERE ssl.owner_id = u.id) AS shocker_share_link_count,
25+
(SELECT COUNT(*) FROM users_email_changes uec WHERE uec.user_id = u.id) AS email_change_request_count,
26+
(SELECT COUNT(*) FROM users_name_changes unc WHERE unc.user_id = u.id) AS name_change_request_count,
27+
(SELECT COUNT(*) FROM users_activation uac WHERE uac.user_id = u.id) AS user_activation_count,
28+
(SELECT COUNT(*) FROM devices dev WHERE dev.owner = u.id) AS device_count,
29+
(SELECT COUNT(*) FROM devices dev JOIN shockers sck ON dev.id = sck.device WHERE dev.owner = u.id) AS shocker_count,
30+
(SELECT COUNT(*) FROM devices dev JOIN shockers sck ON dev.id = sck.device JOIN shocker_control_logs scl ON scl.shocker_id = sck.id WHERE dev.owner = u.id) AS shocker_control_log_count
31+
FROM
32+
users u;
33+
""";
34+
1035
/// <inheritdoc />
1136
protected override void Up(MigrationBuilder migrationBuilder)
1237
{
38+
// Drop the view temporarily to modify the underlying table
39+
migrationBuilder.Sql(AddAdminUsersView.Query_Drop_AdminUsersView);
40+
1341
migrationBuilder.Sql(
1442
"""
15-
-- Drop the view temporarily to modify the underlying table
16-
DROP VIEW admin_users_view;
17-
1843
-- Add the roles column as a text array to replace the rank enum
1944
ALTER TABLE users ADD roles text[] NOT NULL DEFAULT ARRAY[]::text[];
2045
@@ -40,41 +65,21 @@ UPDATE users
4065
-- Update the roles column to use the new role_type enum array
4166
ALTER TABLE users ALTER COLUMN roles SET DEFAULT ARRAY[]::role_type[];
4267
ALTER TABLE users ALTER COLUMN roles TYPE role_type[] USING CAST(roles as role_type[]);
43-
44-
-- Recreate the admin_users_view to reflect the new roles structure
45-
CREATE VIEW admin_users_view AS
46-
SELECT
47-
u.id,
48-
u.name,
49-
u.email,
50-
SPLIT_PART(u.password_hash, ':', 1) AS password_hash_type,
51-
u.created_at,
52-
u.email_activated,
53-
u.roles,
54-
(SELECT COUNT(*) FROM api_tokens ato WHERE ato.user_id = u.id) AS api_token_count,
55-
(SELECT COUNT(*) FROM password_resets pre WHERE pre.user_id = u.id) AS password_reset_count,
56-
(SELECT COUNT(*) FROM shocker_shares ssh WHERE ssh.shared_with = u.id) AS shocker_share_count,
57-
(SELECT COUNT(*) FROM shocker_shares_links ssl WHERE ssl.owner_id = u.id) AS shocker_share_link_count,
58-
(SELECT COUNT(*) FROM users_email_changes uec WHERE uec.user_id = u.id) AS email_change_request_count,
59-
(SELECT COUNT(*) FROM users_name_changes unc WHERE unc.user_id = u.id) AS name_change_request_count,
60-
(SELECT COUNT(*) FROM users_activation uac WHERE uac.user_id = u.id) AS user_activation_count,
61-
(SELECT COUNT(*) FROM devices dev WHERE dev.owner = u.id) AS device_count,
62-
(SELECT COUNT(*) FROM devices dev JOIN shockers sck ON dev.id = sck.device WHERE dev.owner = u.id) AS shocker_count,
63-
(SELECT COUNT(*) FROM devices dev JOIN shockers sck ON dev.id = sck.device JOIN shocker_control_logs scl ON scl.shocker_id = sck.id WHERE dev.owner = u.id) AS shocker_control_log_count
64-
FROM
65-
users u;
6668
"""
6769
);
70+
71+
// Recreate the admin_users_view to reflect the new roles structure
72+
migrationBuilder.Sql(Query_Create_AdminUsersView);
6873
}
6974

7075
/// <inheritdoc />
7176
protected override void Down(MigrationBuilder migrationBuilder)
7277
{
78+
// Drop the view temporarily to modify the underlying table
79+
migrationBuilder.Sql(AddAdminUsersView.Query_Drop_AdminUsersView);
80+
7381
migrationBuilder.Sql(
7482
"""
75-
-- Drop the view temporarily to modify the underlying table
76-
DROP VIEW admin_users_view;
77-
7883
-- Add the rank column back as a temporary nullable text column
7984
ALTER TABLE users ADD rank text;
8085
@@ -100,31 +105,11 @@ UPDATE users
100105
-- Change the rank column back to a non-nullable rank_type enum
101106
ALTER TABLE users ALTER COLUMN rank TYPE rank_type USING CAST(rank as rank_type);
102107
ALTER TABLE users ALTER COLUMN rank SET NOT NULL;
103-
104-
-- Recreate the admin_users_view to restore the original structure
105-
CREATE VIEW admin_users_view AS
106-
SELECT
107-
u.id,
108-
u.name,
109-
u.email,
110-
SPLIT_PART(u.password_hash, ':', 1) AS password_hash_type,
111-
u.created_at,
112-
u.email_activated,
113-
u.rank,
114-
(SELECT COUNT(*) FROM api_tokens ato WHERE ato.user_id = u.id) AS api_token_count,
115-
(SELECT COUNT(*) FROM password_resets pre WHERE pre.user_id = u.id) AS password_reset_count,
116-
(SELECT COUNT(*) FROM shocker_shares ssh WHERE ssh.shared_with = u.id) AS shocker_share_count,
117-
(SELECT COUNT(*) FROM shocker_shares_links ssl WHERE ssl.owner_id = u.id) AS shocker_share_link_count,
118-
(SELECT COUNT(*) FROM users_email_changes uec WHERE uec.user_id = u.id) AS email_change_request_count,
119-
(SELECT COUNT(*) FROM users_name_changes unc WHERE unc.user_id = u.id) AS name_change_request_count,
120-
(SELECT COUNT(*) FROM users_activation uac WHERE uac.user_id = u.id) AS user_activation_count,
121-
(SELECT COUNT(*) FROM devices dev WHERE dev.owner = u.id) AS device_count,
122-
(SELECT COUNT(*) FROM devices dev JOIN shockers sck ON dev.id = sck.device WHERE dev.owner = u.id) AS shocker_count,
123-
(SELECT COUNT(*) FROM devices dev JOIN shockers sck ON dev.id = sck.device JOIN shocker_control_logs scl ON scl.shocker_id = sck.id WHERE dev.owner = u.id) AS shocker_control_log_count
124-
FROM
125-
users u;
126108
"""
127109
);
110+
111+
// Recreate the admin_users_view to restore the original structure
112+
migrationBuilder.Sql(AddAdminUsersView.Query_Create_AdminUsersView);
128113
}
129114
}
130115
}

0 commit comments

Comments
 (0)