github.com/larkox/mattermost-server@v5.11.1+incompatible/model/group.go (about) 1 // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 "net/http" 10 ) 11 12 const ( 13 GroupSourceLdap GroupSource = "ldap" 14 15 GroupNameMaxLength = 64 16 GroupSourceMaxLength = 64 17 GroupDisplayNameMaxLength = 128 18 GroupDescriptionMaxLength = 1024 19 GroupRemoteIDMaxLength = 48 20 ) 21 22 type GroupSource string 23 24 var allGroupSources = []GroupSource{ 25 GroupSourceLdap, 26 } 27 28 var groupSourcesRequiringRemoteID = []GroupSource{ 29 GroupSourceLdap, 30 } 31 32 type Group struct { 33 Id string `json:"id"` 34 Name string `json:"name"` 35 DisplayName string `json:"display_name"` 36 Description string `json:"description"` 37 Source GroupSource `json:"source"` 38 RemoteId string `json:"remote_id"` 39 CreateAt int64 `json:"create_at"` 40 UpdateAt int64 `json:"update_at"` 41 DeleteAt int64 `json:"delete_at"` 42 HasSyncables bool `db:"-" json:"has_syncables"` 43 } 44 45 type GroupPatch struct { 46 Name *string `json:"name"` 47 DisplayName *string `json:"display_name"` 48 Description *string `json:"description"` 49 } 50 51 type GroupSearchOpts struct { 52 Q string 53 IsLinked *bool 54 IsConfigured *bool 55 } 56 57 func (group *Group) Patch(patch *GroupPatch) { 58 if patch.Name != nil { 59 group.Name = *patch.Name 60 } 61 if patch.DisplayName != nil { 62 group.DisplayName = *patch.DisplayName 63 } 64 if patch.Description != nil { 65 group.Description = *patch.Description 66 } 67 } 68 69 func (group *Group) IsValidForCreate() *AppError { 70 if l := len(group.Name); l == 0 || l > GroupNameMaxLength { 71 return NewAppError("Group.IsValidForCreate", "model.group.name.app_error", map[string]interface{}{"GroupNameMaxLength": GroupNameMaxLength}, "", http.StatusBadRequest) 72 } 73 74 if l := len(group.DisplayName); l == 0 || l > GroupDisplayNameMaxLength { 75 return NewAppError("Group.IsValidForCreate", "model.group.display_name.app_error", map[string]interface{}{"GroupDisplayNameMaxLength": GroupDisplayNameMaxLength}, "", http.StatusBadRequest) 76 } 77 78 if len(group.Description) > GroupDescriptionMaxLength { 79 return NewAppError("Group.IsValidForCreate", "model.group.description.app_error", map[string]interface{}{"GroupDescriptionMaxLength": GroupDescriptionMaxLength}, "", http.StatusBadRequest) 80 } 81 82 isValidSource := false 83 for _, groupSource := range allGroupSources { 84 if group.Source == groupSource { 85 isValidSource = true 86 break 87 } 88 } 89 if !isValidSource { 90 return NewAppError("Group.IsValidForCreate", "model.group.source.app_error", nil, "", http.StatusBadRequest) 91 } 92 93 if len(group.RemoteId) > GroupRemoteIDMaxLength || (len(group.RemoteId) == 0 && group.requiresRemoteId()) { 94 return NewAppError("Group.IsValidForCreate", "model.group.remote_id.app_error", nil, "", http.StatusBadRequest) 95 } 96 97 return nil 98 } 99 100 func (group *Group) requiresRemoteId() bool { 101 for _, groupSource := range groupSourcesRequiringRemoteID { 102 if groupSource == group.Source { 103 return true 104 } 105 } 106 return false 107 } 108 109 func (group *Group) IsValidForUpdate() *AppError { 110 if len(group.Id) != 26 { 111 return NewAppError("Group.IsValidForUpdate", "model.group.id.app_error", nil, "", http.StatusBadRequest) 112 } 113 if group.CreateAt == 0 { 114 return NewAppError("Group.IsValidForUpdate", "model.group.create_at.app_error", nil, "", http.StatusBadRequest) 115 } 116 if group.UpdateAt == 0 { 117 return NewAppError("Group.IsValidForUpdate", "model.group.update_at.app_error", nil, "", http.StatusBadRequest) 118 } 119 if err := group.IsValidForCreate(); err != nil { 120 return err 121 } 122 return nil 123 } 124 125 func GroupFromJson(data io.Reader) *Group { 126 var group *Group 127 json.NewDecoder(data).Decode(&group) 128 return group 129 } 130 131 func GroupsFromJson(data io.Reader) []*Group { 132 var groups []*Group 133 json.NewDecoder(data).Decode(&groups) 134 return groups 135 } 136 137 func GroupPatchFromJson(data io.Reader) *GroupPatch { 138 var groupPatch *GroupPatch 139 json.NewDecoder(data).Decode(&groupPatch) 140 return groupPatch 141 }