mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 08:21:11 +00:00 
			
		
		
		
	[GITEA] add option for banning dots in usernames
Refs: https://codeberg.org/forgejo/forgejo/pulls/676 Author: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Date: Mon Jun 12 13:57:01 2023 +0200 Co-authored-by: Gusted <postmaster@gusted.xyz> (cherry picked from commitfabdda5c6e) (cherry picked from commitd2c7f45621) (cherry picked from commitdfdbaba3d6)
This commit is contained in:
		
					parent
					
						
							
								66e1d3f082
							
						
					
				
			
			
				commit
				
					
						a3cda092b8
					
				
			
		
					 7 changed files with 57 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -92,13 +92,20 @@ func IsValidExternalTrackerURLFormat(uri string) bool {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	validUsernamePattern   = regexp.MustCompile(`^[\da-zA-Z][-.\w]*$`)
 | 
			
		||||
	invalidUsernamePattern = regexp.MustCompile(`[-._]{2,}|[-._]$`) // No consecutive or trailing non-alphanumeric chars
 | 
			
		||||
	validUsernamePatternWithDots    = regexp.MustCompile(`^[\da-zA-Z][-.\w]*$`)
 | 
			
		||||
	validUsernamePatternWithoutDots = regexp.MustCompile(`^[\da-zA-Z][-\w]*$`)
 | 
			
		||||
 | 
			
		||||
	// No consecutive or trailing non-alphanumeric chars, catches both cases
 | 
			
		||||
	invalidUsernamePattern = regexp.MustCompile(`[-._]{2,}|[-._]$`)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// IsValidUsername checks if username is valid
 | 
			
		||||
func IsValidUsername(name string) bool {
 | 
			
		||||
	// It is difficult to find a single pattern that is both readable and effective,
 | 
			
		||||
	// but it's easier to use positive and negative checks.
 | 
			
		||||
	return validUsernamePattern.MatchString(name) && !invalidUsernamePattern.MatchString(name)
 | 
			
		||||
	if setting.Service.AllowDotsInUsernames {
 | 
			
		||||
		return validUsernamePatternWithDots.MatchString(name) && !invalidUsernamePattern.MatchString(name)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return validUsernamePatternWithoutDots.MatchString(name) && !invalidUsernamePattern.MatchString(name)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue