github.com/jfrerich/mattermost-server@v5.8.0-rc2+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 func (group *Group) Patch(patch *GroupPatch) { 52 if patch.Name != nil { 53 group.Name = *patch.Name 54 } 55 if patch.DisplayName != nil { 56 group.DisplayName = *patch.DisplayName 57 } 58 if patch.Description != nil { 59 group.Description = *patch.Description 60 } 61 } 62 63 func (group *Group) IsValidForCreate() *AppError { 64 if l := len(group.Name); l == 0 || l > GroupNameMaxLength { 65 return NewAppError("Group.IsValidForCreate", "model.group.name.app_error", map[string]interface{}{"GroupNameMaxLength": GroupNameMaxLength}, "", http.StatusBadRequest) 66 } 67 68 if l := len(group.DisplayName); l == 0 || l > GroupDisplayNameMaxLength { 69 return NewAppError("Group.IsValidForCreate", "model.group.display_name.app_error", map[string]interface{}{"GroupDisplayNameMaxLength": GroupDisplayNameMaxLength}, "", http.StatusBadRequest) 70 } 71 72 if len(group.Description) > GroupDescriptionMaxLength { 73 return NewAppError("Group.IsValidForCreate", "model.group.description.app_error", map[string]interface{}{"GroupDescriptionMaxLength": GroupDescriptionMaxLength}, "", http.StatusBadRequest) 74 } 75 76 isValidSource := false 77 for _, groupSource := range allGroupSources { 78 if group.Source == groupSource { 79 isValidSource = true 80 break 81 } 82 } 83 if !isValidSource { 84 return NewAppError("Group.IsValidForCreate", "model.group.source.app_error", nil, "", http.StatusBadRequest) 85 } 86 87 if len(group.RemoteId) > GroupRemoteIDMaxLength || (len(group.RemoteId) == 0 && group.requiresRemoteId()) { 88 return NewAppError("Group.IsValidForCreate", "model.group.remote_id.app_error", nil, "", http.StatusBadRequest) 89 } 90 91 return nil 92 } 93 94 func (group *Group) requiresRemoteId() bool { 95 for _, groupSource := range groupSourcesRequiringRemoteID { 96 if groupSource == group.Source { 97 return true 98 } 99 } 100 return false 101 } 102 103 func (group *Group) IsValidForUpdate() *AppError { 104 if len(group.Id) != 26 { 105 return NewAppError("Group.IsValidForUpdate", "model.group.id.app_error", nil, "", http.StatusBadRequest) 106 } 107 if group.CreateAt == 0 { 108 return NewAppError("Group.IsValidForUpdate", "model.group.create_at.app_error", nil, "", http.StatusBadRequest) 109 } 110 if group.UpdateAt == 0 { 111 return NewAppError("Group.IsValidForUpdate", "model.group.update_at.app_error", nil, "", http.StatusBadRequest) 112 } 113 if err := group.IsValidForCreate(); err != nil { 114 return err 115 } 116 return nil 117 } 118 119 func GroupFromJson(data io.Reader) *Group { 120 var group *Group 121 json.NewDecoder(data).Decode(&group) 122 return group 123 } 124 125 func GroupsFromJson(data io.Reader) []*Group { 126 var groups []*Group 127 json.NewDecoder(data).Decode(&groups) 128 return groups 129 } 130 131 func GroupPatchFromJson(data io.Reader) *GroupPatch { 132 var groupPatch *GroupPatch 133 json.NewDecoder(data).Decode(&groupPatch) 134 return groupPatch 135 }