github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/users/utils.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package users 5 6 import ( 7 "strings" 8 9 "github.com/masterhung0112/hk_server/v5/model" 10 ) 11 12 func maxInt(a, b int) int { 13 if a > b { 14 return a 15 } 16 return b 17 } 18 19 func (us *UserService) IsFirstUserAccount() bool { 20 cachedSessions, err := us.sessionCache.Len() 21 if err != nil { 22 return false 23 } 24 if cachedSessions == 0 { 25 count, err := us.store.Count(model.UserCountOptions{IncludeDeleted: true}) 26 if err != nil { 27 return false 28 } 29 if count <= 0 { 30 return true 31 } 32 } 33 34 return false 35 } 36 37 // CheckUserDomain checks that a user's email domain matches a list of space-delimited domains as a string. 38 func CheckUserDomain(user *model.User, domains string) bool { 39 return CheckEmailDomain(user.Email, domains) 40 } 41 42 // CheckEmailDomain checks that an email domain matches a list of space-delimited domains as a string. 43 func CheckEmailDomain(email string, domains string) bool { 44 if domains == "" { 45 return true 46 } 47 48 domainArray := strings.Fields(strings.TrimSpace(strings.ToLower(strings.Replace(strings.Replace(domains, "@", " ", -1), ",", " ", -1)))) 49 50 for _, d := range domainArray { 51 if strings.HasSuffix(strings.ToLower(email), "@"+d) { 52 return true 53 } 54 } 55 56 return false 57 } 58 59 func (us *UserService) sanitizeProfiles(users []*model.User, asAdmin bool) []*model.User { 60 for _, u := range users { 61 us.SanitizeProfile(u, asAdmin) 62 } 63 64 return users 65 } 66 67 func (us *UserService) SanitizeProfile(user *model.User, asAdmin bool) { 68 options := us.GetSanitizeOptions(asAdmin) 69 70 user.SanitizeProfile(options) 71 } 72 73 func (us *UserService) GetSanitizeOptions(asAdmin bool) map[string]bool { 74 options := us.config().GetSanitizeOptions() 75 if asAdmin { 76 options["email"] = true 77 options["fullname"] = true 78 options["authservice"] = true 79 } 80 return options 81 } 82 83 // IsUsernameTaken checks if the username is already used by another user. Return false if the username is invalid. 84 func (us *UserService) IsUsernameTaken(name string) bool { 85 if !model.IsValidUsername(name) { 86 return false 87 } 88 89 if _, err := us.store.GetByUsername(name); err != nil { 90 return false 91 } 92 93 return true 94 }