Skip to content

Commit 8ca0e22

Browse files
Gustedjpraetjolheisertechknowlogickdelvh
committed
Add username check to doctor (go-gitea#20140)
* Add username check to doctor - Add a new breaking change detector to Gitea's doctor, which checks if all users still have a valid username according to Gitea. Given from time-to-time we need to make changes, either due to new routes or due to security, it's for a instance's admin to check if all users still have a valid username. * Fix extra argument * Apply suggestions from code review Co-authored-by: Jimmy Praet <[email protected]> * Apply suggestions from code review Co-authored-by: delvh <[email protected]> Co-authored-by: Jimmy Praet <[email protected]> Co-authored-by: John Olheiser <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent 5ce8fdb commit 8ca0e22

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

modules/doctor/breaking.go

+30
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ func checkUserEmail(ctx context.Context, logger log.Logger, _ bool) error {
5858
return nil
5959
}
6060

61+
// From time to time Gitea makes changes to the reserved usernames and which symbols
62+
// are allowed for various reasons. This check helps with detecting users that, according
63+
// to our reserved names, don't have a valid username.
64+
func checkUserName(ctx context.Context, logger log.Logger, _ bool) error {
65+
var invalidUserCount int64
66+
if err := iterateUserAccounts(ctx, func(u *user.User) error {
67+
if err := user.IsUsableUsername(u.Name); err != nil {
68+
invalidUserCount++
69+
logger.Warn("User[id=%d] does not have a valid username: %v", u.ID, err)
70+
}
71+
return nil
72+
}); err != nil {
73+
return fmt.Errorf("iterateUserAccounts: %v", err)
74+
}
75+
76+
if invalidUserCount == 0 {
77+
logger.Info("All users have a valid username.")
78+
} else {
79+
logger.Warn("%d user(s) have a non-valid username.", invalidUserCount)
80+
}
81+
return nil
82+
}
83+
6184
func init() {
6285
Register(&Check{
6386
Title: "Check if users has an valid email address",
@@ -66,4 +89,11 @@ func init() {
6689
Run: checkUserEmail,
6790
Priority: 9,
6891
})
92+
Register(&Check{
93+
Title: "Check if users have a valid username",
94+
Name: "check-user-names",
95+
IsDefault: false,
96+
Run: checkUserName,
97+
Priority: 9,
98+
})
6999
}

0 commit comments

Comments
 (0)