Skip to content

Commit 8f44d00

Browse files
authored
Delete user related oauth stuff on user deletion too (#19677) (#19680)
Backport (#19677) * delete user related oauth stuff on user deletion too * extend doctor check-db-consistency * make it build for v1.16.x
1 parent 4386eb7 commit 8f44d00

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

models/user.go

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
_ "image/jpeg" // Needed for jpeg support
1414

1515
asymkey_model "code.gitea.io/gitea/models/asymkey"
16+
auth_model "code.gitea.io/gitea/models/auth"
1617
"code.gitea.io/gitea/models/db"
1718
repo_model "code.gitea.io/gitea/models/repo"
1819
user_model "code.gitea.io/gitea/models/user"
@@ -83,6 +84,11 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
8384
}
8485
// ***** END: Follow *****
8586

87+
if _, err := db.GetEngine(ctx).In("grant_id", builder.Select("id").From("oauth2_grant").Where(builder.Eq{"oauth2_grant.user_id": u.ID})).
88+
Delete(&auth_model.OAuth2AuthorizationCode{}); err != nil {
89+
return err
90+
}
91+
8692
if err = deleteBeans(e,
8793
&AccessToken{UID: u.ID},
8894
&Collaboration{UserID: u.ID},
@@ -100,6 +106,8 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
100106
&Collaboration{UserID: u.ID},
101107
&Stopwatch{UserID: u.ID},
102108
&user_model.Setting{UserID: u.ID},
109+
&auth_model.OAuth2Application{UID: u.ID},
110+
&auth_model.OAuth2Grant{UserID: u.ID},
103111
); err != nil {
104112
return fmt.Errorf("deleteBeans: %v", err)
105113
}

modules/doctor/dbconsistency.go

+9
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ func checkDBConsistency(logger log.Logger, autofix bool) error {
186186
// find action without repository
187187
genericOrphanCheck("Action entries without existing repository",
188188
"action", "repository", "action.repo_id=repository.id"),
189+
// find OAuth2Grant without existing user
190+
genericOrphanCheck("Orphaned OAuth2Grant without existing User",
191+
"oauth2_grant", "user", "oauth2_grant.user_id=user.id"),
192+
// find OAuth2Application without existing user
193+
genericOrphanCheck("Orphaned OAuth2Application without existing User",
194+
"oauth2_application", "user", "oauth2_application.uid=user.id"),
195+
// find OAuth2AuthorizationCode without existing OAuth2Grant
196+
genericOrphanCheck("Orphaned OAuth2AuthorizationCode without existing OAuth2Grant",
197+
"oauth2_authorization_code", "oauth2_grant", "oauth2_authorization_code.grant_id=oauth2_grant.id"),
189198
)
190199

191200
for _, c := range consistencyChecks {

0 commit comments

Comments
 (0)