summaryrefslogtreecommitdiffstats
path: root/model/scheme.go
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2018-04-20 19:49:13 +0100
committerMartin Kraft <mkraft@users.noreply.github.com>2018-04-20 14:49:13 -0400
commitcd55c44c8fd8f61cdb7cbfb57a588be82c7aa0ab (patch)
tree2979276d03b5bca72b549d7576eab104ceefd495 /model/scheme.go
parent853445dc2ea68f765faa04ad14618b04f1081c43 (diff)
downloadchat-cd55c44c8fd8f61cdb7cbfb57a588be82c7aa0ab.tar.gz
chat-cd55c44c8fd8f61cdb7cbfb57a588be82c7aa0ab.tar.bz2
chat-cd55c44c8fd8f61cdb7cbfb57a588be82c7aa0ab.zip
MM-8796: Full implementation of "Schemes" in Store/Model/App layers. (#8357)
* Add Scheme model and stub store. * Port ChannelStore to be Scheme aware. * Make almost all the API/APP layer work with ChannelSchemes. Only thing still hacky is UpdateChannelMemberRoles(). * Add basic SchemeStore implementation. * Migrate UpdateChannelMemberRoles properly and fix tests. * Update store tests and mocks so they work. * Include creating default roles in Scheme create store function. * Implement role deletion and start scheme deletion. * Only use non-deleted roles for authorization. * Add GetByScheme method to Team store. * Add GetChannelsByScheme. * Update store mocks. * Implement scheme deletion in the store. * Rename is valid function. * Add offset and limit to queries to fetch teams and channels by scheme. * Fix queries. * Implement scheme awareness in Team store and add a migration. * Tidy up ChannelStore mapping functions and add exhaustive unit tests. * Add all missing i18n. * Proper tests for TeamStore internal functions and fix them. * Make additional TeamMember fields nullable. * Make new ChannelMember fields nullable. * Create new nullable columns without defaults. * Make new fields in large tables nullalble. * Fix empty list of TeamMembers. * Deduplicate SQL queries. * Fix spelling. * Fix review comment. * More review fixes. * More review fixes.
Diffstat (limited to 'model/scheme.go')
-rw-r--r--model/scheme.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/model/scheme.go b/model/scheme.go
new file mode 100644
index 000000000..9ad153c73
--- /dev/null
+++ b/model/scheme.go
@@ -0,0 +1,95 @@
+// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package model
+
+import (
+ "encoding/json"
+ "io"
+)
+
+const (
+ SCHEME_NAME_MAX_LENGTH = 64
+ SCHEME_DESCRIPTION_MAX_LENGTH = 1024
+ SCHEME_SCOPE_TEAM = "team"
+ SCHEME_SCOPE_CHANNEL = "channel"
+)
+
+type Scheme struct {
+ Id string `json:"id"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+ CreateAt int64 `json:"create_at"`
+ UpdateAt int64 `json:"update_at"`
+ DeleteAt int64 `json:"delete_at"`
+ Scope string `json:"scope"`
+ DefaultTeamAdminRole string `json:"default_team_admin_role"`
+ DefaultTeamUserRole string `json:"default_team_user_role"`
+ DefaultChannelAdminRole string `json:"default_channel_admin_role"`
+ DefaultChannelUserRole string `json:"default_channel_user_role"`
+}
+
+func (scheme *Scheme) ToJson() string {
+ b, _ := json.Marshal(scheme)
+ return string(b)
+}
+
+func SchemeFromJson(data io.Reader) *Scheme {
+ var scheme *Scheme
+ json.NewDecoder(data).Decode(&scheme)
+ return scheme
+}
+
+func (scheme *Scheme) IsValid() bool {
+ if len(scheme.Id) != 26 {
+ return false
+ }
+
+ return scheme.IsValidForCreate()
+}
+
+func (scheme *Scheme) IsValidForCreate() bool {
+ if len(scheme.Name) == 0 || len(scheme.Name) > SCHEME_NAME_MAX_LENGTH {
+ return false
+ }
+
+ if len(scheme.Description) > SCHEME_DESCRIPTION_MAX_LENGTH {
+ return false
+ }
+
+ switch scheme.Scope {
+ case SCHEME_SCOPE_TEAM, SCHEME_SCOPE_CHANNEL:
+ default:
+ return false
+ }
+
+ if !IsValidRoleName(scheme.DefaultChannelAdminRole) {
+ return false
+ }
+
+ if !IsValidRoleName(scheme.DefaultChannelUserRole) {
+ return false
+ }
+
+ if scheme.Scope == SCHEME_SCOPE_TEAM {
+ if !IsValidRoleName(scheme.DefaultTeamAdminRole) {
+ return false
+ }
+
+ if !IsValidRoleName(scheme.DefaultTeamUserRole) {
+ return false
+ }
+ }
+
+ if scheme.Scope == SCHEME_SCOPE_CHANNEL {
+ if len(scheme.DefaultTeamAdminRole) != 0 {
+ return false
+ }
+
+ if len(scheme.DefaultTeamUserRole) != 0 {
+ return false
+ }
+ }
+
+ return true
+}