github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/model/group_syncable.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 "fmt" 9 "io" 10 "io/ioutil" 11 "net/http" 12 ) 13 14 type GroupSyncableType string 15 16 const ( 17 GroupSyncableTypeTeam GroupSyncableType = "Team" 18 GroupSyncableTypeChannel GroupSyncableType = "Channel" 19 ) 20 21 func (gst GroupSyncableType) String() string { 22 return string(gst) 23 } 24 25 type GroupSyncable struct { 26 GroupId string `json:"group_id"` 27 28 // SyncableId represents the Id of the model that is being synced with the group, for example a ChannelId or 29 // TeamId. 30 SyncableId string `db:"-" json:"-"` 31 32 CanLeave bool `db:"-" json:"can_leave"` 33 AutoAdd bool `json:"auto_add"` 34 CreateAt int64 `json:"create_at"` 35 DeleteAt int64 `json:"delete_at"` 36 UpdateAt int64 `json:"update_at"` 37 Type GroupSyncableType `db:"-" json:"-"` 38 39 // Values joined in from the associated team and/or channel 40 ChannelDisplayName string `db:"-" json:"-"` 41 TeamDisplayName string `db:"-" json:"-"` 42 TeamType string `db:"-" json:"-"` 43 ChannelType string `db:"-" json:"-"` 44 TeamID string `db:"-" json:"-"` 45 } 46 47 func (syncable *GroupSyncable) IsValid() *AppError { 48 if !IsValidId(syncable.GroupId) { 49 return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.group_id.app_error", nil, "", http.StatusBadRequest) 50 } 51 if !IsValidId(syncable.SyncableId) { 52 return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.syncable_id.app_error", nil, "", http.StatusBadRequest) 53 } 54 // TODO: Add this validation check for phase 2 of LDAP group sync. 55 // if syncable.AutoAdd == false && syncable.CanLeave == false { 56 // return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.invalid_state", nil, "", http.StatusBadRequest) 57 // } 58 return nil 59 } 60 61 func (syncable *GroupSyncable) UnmarshalJSON(b []byte) error { 62 var kvp map[string]interface{} 63 err := json.Unmarshal(b, &kvp) 64 if err != nil { 65 return err 66 } 67 for key, value := range kvp { 68 switch key { 69 case "team_id": 70 syncable.SyncableId = value.(string) 71 syncable.Type = GroupSyncableTypeTeam 72 case "channel_id": 73 syncable.SyncableId = value.(string) 74 syncable.Type = GroupSyncableTypeChannel 75 case "group_id": 76 syncable.GroupId = value.(string) 77 case "can_leave": 78 syncable.CanLeave = value.(bool) 79 case "auto_add": 80 syncable.AutoAdd = value.(bool) 81 default: 82 } 83 } 84 return nil 85 } 86 87 func (syncable *GroupSyncable) MarshalJSON() ([]byte, error) { 88 type Alias GroupSyncable 89 90 switch syncable.Type { 91 case GroupSyncableTypeTeam: 92 return json.Marshal(&struct { 93 TeamID string `json:"team_id"` 94 TeamDisplayName string `json:"team_display_name,omitempty"` 95 TeamType string `json:"team_type,omitempty"` 96 *Alias 97 }{ 98 TeamDisplayName: syncable.TeamDisplayName, 99 TeamType: syncable.TeamType, 100 TeamID: syncable.SyncableId, 101 Alias: (*Alias)(syncable), 102 }) 103 case GroupSyncableTypeChannel: 104 return json.Marshal(&struct { 105 ChannelID string `json:"channel_id"` 106 ChannelDisplayName string `json:"channel_display_name,omitempty"` 107 ChannelType string `json:"channel_type,omitempty"` 108 109 TeamID string `json:"team_id,omitempty"` 110 TeamDisplayName string `json:"team_display_name,omitempty"` 111 TeamType string `json:"team_type,omitempty"` 112 113 *Alias 114 }{ 115 ChannelID: syncable.SyncableId, 116 ChannelDisplayName: syncable.ChannelDisplayName, 117 ChannelType: syncable.ChannelType, 118 119 TeamID: syncable.TeamID, 120 TeamDisplayName: syncable.TeamDisplayName, 121 TeamType: syncable.TeamType, 122 123 Alias: (*Alias)(syncable), 124 }) 125 default: 126 return nil, &json.MarshalerError{ 127 Err: fmt.Errorf("unknown syncable type: %s", syncable.Type), 128 } 129 } 130 } 131 132 type GroupSyncablePatch struct { 133 CanLeave *bool `json:"can_leave"` 134 AutoAdd *bool `json:"auto_add"` 135 } 136 137 func (syncable *GroupSyncable) Patch(patch *GroupSyncablePatch) { 138 // TODO: Add this validation check for phase 2 of LDAP group sync. 139 // if patch.CanLeave != nil { 140 // syncable.CanLeave = *patch.CanLeave 141 // } 142 if patch.AutoAdd != nil { 143 syncable.AutoAdd = *patch.AutoAdd 144 } 145 } 146 147 type UserTeamIDPair struct { 148 UserID string 149 TeamID string 150 } 151 152 type UserChannelIDPair struct { 153 UserID string 154 ChannelID string 155 } 156 157 func GroupSyncableFromJson(data io.Reader) *GroupSyncable { 158 groupSyncable := &GroupSyncable{} 159 bodyBytes, _ := ioutil.ReadAll(data) 160 json.Unmarshal(bodyBytes, groupSyncable) 161 return groupSyncable 162 } 163 164 func GroupSyncablesFromJson(data io.Reader) []*GroupSyncable { 165 groupSyncables := []*GroupSyncable{} 166 bodyBytes, _ := ioutil.ReadAll(data) 167 json.Unmarshal(bodyBytes, &groupSyncables) 168 return groupSyncables 169 }