diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/team.go | 2 | ||||
-rw-r--r-- | model/user.go | 49 | ||||
-rw-r--r-- | model/user_test.go | 23 | ||||
-rw-r--r-- | model/utils.go | 12 |
4 files changed, 80 insertions, 6 deletions
diff --git a/model/team.go b/model/team.go index 6006f738c..8b4f82830 100644 --- a/model/team.go +++ b/model/team.go @@ -158,7 +158,7 @@ func IsReservedTeamName(s string) bool { func IsValidTeamName(s string) bool { - if !IsValidAlphaNum(s) { + if !IsValidAlphaNum(s, false) { return false } diff --git a/model/user.go b/model/user.go index 05fc96953..fdc519b99 100644 --- a/model/user.go +++ b/model/user.go @@ -13,9 +13,8 @@ import ( ) const ( - ROLE_ADMIN = "admin" + ROLE_TEAM_ADMIN = "admin" ROLE_SYSTEM_ADMIN = "system_admin" - ROLE_SYSTEM_SUPPORT = "system_support" USER_AWAY_TIMEOUT = 5 * 60 * 1000 // 5 minutes USER_OFFLINE_TIMEOUT = 1 * 60 * 1000 // 1 minute USER_OFFLINE = "offline" @@ -272,6 +271,52 @@ func (u *User) GetDisplayName() string { } } +func IsValidRoles(userRoles string) bool { + + roles := strings.Split(userRoles, " ") + + for _, r := range roles { + if !isValidRole(r) { + return false + } + } + + return true +} + +func isValidRole(role string) bool { + if role == "" { + return true + } + + if role == ROLE_TEAM_ADMIN { + return true + } + + if role == ROLE_SYSTEM_ADMIN { + return true + } + + return false +} + +func (u *User) IsInRole(inRole string) bool { + return IsInRole(u.Roles, inRole) +} + +func IsInRole(userRoles string, inRole string) bool { + roles := strings.Split(userRoles, " ") + + for _, r := range roles { + if r == inRole { + return true + } + + } + + return false +} + func (u *User) PreExport() { u.Password = "" u.AuthData = "" diff --git a/model/user_test.go b/model/user_test.go index a3b4be091..d9c1a00b6 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -192,3 +192,26 @@ func TestCleanUsername(t *testing.T) { t.Fatal("didn't clean name properly") } } + +func TestRoles(t *testing.T) { + + if !IsValidRoles("admin") { + t.Fatal() + } + + if IsValidRoles("junk") { + t.Fatal() + } + + if IsInRole("system_admin junk", "admin") { + t.Fatal() + } + + if !IsInRole("system_admin junk", "system_admin") { + t.Fatal() + } + + if IsInRole("admin", "system_admin") { + t.Fatal() + } +} diff --git a/model/utils.go b/model/utils.go index 17d1c6317..d5122e805 100644 --- a/model/utils.go +++ b/model/utils.go @@ -202,7 +202,7 @@ func GetSubDomain(s string) (string, string) { func IsValidChannelIdentifier(s string) bool { - if !IsValidAlphaNum(s) { + if !IsValidAlphaNum(s, true) { return false } @@ -213,10 +213,16 @@ func IsValidChannelIdentifier(s string) bool { return true } +var validAlphaNumUnderscore = regexp.MustCompile(`^[a-z0-9]+([a-z\-\_0-9]+|(__)?)[a-z0-9]+$`) var validAlphaNum = regexp.MustCompile(`^[a-z0-9]+([a-z\-0-9]+|(__)?)[a-z0-9]+$`) -func IsValidAlphaNum(s string) bool { - match := validAlphaNum.MatchString(s) +func IsValidAlphaNum(s string, allowUnderscores bool) bool { + var match bool + if allowUnderscores { + match = validAlphaNumUnderscore.MatchString(s) + } else { + match = validAlphaNum.MatchString(s) + } if !match { return false |