diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/user.go | 49 | ||||
-rw-r--r-- | model/user_test.go | 23 |
2 files changed, 70 insertions, 2 deletions
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() + } +} |