summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/mattermost-server/v5/model/auditconv.go
blob: b3cf60629ae880bc5af7279e466c75ea35cce583 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package model

import (
	"github.com/francoispqt/gojay"
)

// AuditModelTypeConv converts key model types to something better suited for audit output.
func AuditModelTypeConv(val interface{}) (newVal interface{}, converted bool) {
	if val == nil {
		return nil, false
	}
	switch v := val.(type) {
	case *Channel:
		return newAuditChannel(v), true
	case *Team:
		return newAuditTeam(v), true
	case *User:
		return newAuditUser(v), true
	case *Command:
		return newAuditCommand(v), true
	case *CommandArgs:
		return newAuditCommandArgs(v), true
	case *Bot:
		return newAuditBot(v), true
	case *ChannelModerationPatch:
		return newAuditChannelModerationPatch(v), true
	case *Emoji:
		return newAuditEmoji(v), true
	case *FileInfo:
		return newAuditFileInfo(v), true
	case *Group:
		return newAuditGroup(v), true
	case *Job:
		return newAuditJob(v), true
	case *OAuthApp:
		return newAuditOAuthApp(v), true
	case *Post:
		return newAuditPost(v), true
	case *Role:
		return newAuditRole(v), true
	case *Scheme:
		return newAuditScheme(v), true
	case *SchemeRoles:
		return newAuditSchemeRoles(v), true
	case *Session:
		return newAuditSession(v), true
	case *IncomingWebhook:
		return newAuditIncomingWebhook(v), true
	case *OutgoingWebhook:
		return newAuditOutgoingWebhook(v), true
	case *RemoteCluster:
		return newRemoteCluster(v), true
	}
	return val, false
}

type auditChannel struct {
	ID   string
	Name string
	Type string
}

// newAuditChannel creates a simplified representation of Channel for output to audit log.
func newAuditChannel(c *Channel) auditChannel {
	var channel auditChannel
	if c != nil {
		channel.ID = c.Id
		channel.Name = c.Name
		channel.Type = c.Type
	}
	return channel
}

func (c auditChannel) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", c.ID)
	enc.StringKey("name", c.Name)
	enc.StringKey("type", c.Type)
}

func (c auditChannel) IsNil() bool {
	return false
}

type auditTeam struct {
	ID   string
	Name string
	Type string
}

// newAuditTeam creates a simplified representation of Team for output to audit log.
func newAuditTeam(t *Team) auditTeam {
	var team auditTeam
	if t != nil {
		team.ID = t.Id
		team.Name = t.Name
		team.Type = t.Type
	}
	return team
}

func (t auditTeam) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", t.ID)
	enc.StringKey("name", t.Name)
	enc.StringKey("type", t.Type)
}

func (t auditTeam) IsNil() bool {
	return false
}

type auditUser struct {
	ID    string
	Name  string
	Roles string
}

// newAuditUser creates a simplified representation of User for output to audit log.
func newAuditUser(u *User) auditUser {
	var user auditUser
	if u != nil {
		user.ID = u.Id
		user.Name = u.Username
		user.Roles = u.Roles
	}
	return user
}

func (u auditUser) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", u.ID)
	enc.StringKey("name", u.Name)
	enc.StringKey("roles", u.Roles)
}

func (u auditUser) IsNil() bool {
	return false
}

type auditCommand struct {
	ID               string
	CreatorID        string
	TeamID           string
	Trigger          string
	Method           string
	Username         string
	IconURL          string
	AutoComplete     bool
	AutoCompleteDesc string
	AutoCompleteHint string
	DisplayName      string
	Description      string
	URL              string
}

// newAuditCommand creates a simplified representation of Command for output to audit log.
func newAuditCommand(c *Command) auditCommand {
	var cmd auditCommand
	if c != nil {
		cmd.ID = c.Id
		cmd.CreatorID = c.CreatorId
		cmd.TeamID = c.TeamId
		cmd.Trigger = c.Trigger
		cmd.Method = c.Method
		cmd.Username = c.Username
		cmd.IconURL = c.IconURL
		cmd.AutoComplete = c.AutoComplete
		cmd.AutoCompleteDesc = c.AutoCompleteDesc
		cmd.AutoCompleteHint = c.AutoCompleteHint
		cmd.DisplayName = c.DisplayName
		cmd.Description = c.Description
		cmd.URL = c.URL
	}
	return cmd
}

func (cmd auditCommand) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", cmd.ID)
	enc.StringKey("creator_id", cmd.CreatorID)
	enc.StringKey("team_id", cmd.TeamID)
	enc.StringKey("trigger", cmd.Trigger)
	enc.StringKey("method", cmd.Method)
	enc.StringKey("username", cmd.Username)
	enc.StringKey("icon_url", cmd.IconURL)
	enc.BoolKey("auto_complete", cmd.AutoComplete)
	enc.StringKey("auto_complete_desc", cmd.AutoCompleteDesc)
	enc.StringKey("auto_complete_hint", cmd.AutoCompleteHint)
	enc.StringKey("display", cmd.DisplayName)
	enc.StringKey("desc", cmd.Description)
	enc.StringKey("url", cmd.URL)
}

func (cmd auditCommand) IsNil() bool {
	return false
}

type auditCommandArgs struct {
	ChannelID string
	TeamID    string
	TriggerID string
	Command   string
}

// newAuditCommandArgs creates a simplified representation of CommandArgs for output to audit log.
func newAuditCommandArgs(ca *CommandArgs) auditCommandArgs {
	var cmdargs auditCommandArgs
	if ca != nil {
		cmdargs.ChannelID = ca.ChannelId
		cmdargs.TeamID = ca.TeamId
		cmdargs.TriggerID = ca.TriggerId
		cmdargs.Command = ca.Command
	}
	return cmdargs
}

func (ca auditCommandArgs) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("channel_id", ca.ChannelID)
	enc.StringKey("team_id", ca.TriggerID)
	enc.StringKey("trigger_id", ca.TeamID)
	enc.StringKey("command", ca.Command)
}

func (ca auditCommandArgs) IsNil() bool {
	return false
}

type auditBot struct {
	UserID      string
	Username    string
	Displayname string
}

// newAuditBot creates a simplified representation of Bot for output to audit log.
func newAuditBot(b *Bot) auditBot {
	var bot auditBot
	if b != nil {
		bot.UserID = b.UserId
		bot.Username = b.Username
		bot.Displayname = b.DisplayName
	}
	return bot
}

func (b auditBot) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("user_id", b.UserID)
	enc.StringKey("username", b.Username)
	enc.StringKey("display", b.Displayname)
}

func (b auditBot) IsNil() bool {
	return false
}

type auditChannelModerationPatch struct {
	Name        string
	RoleGuests  bool
	RoleMembers bool
}

// newAuditChannelModerationPatch creates a simplified representation of ChannelModerationPatch for output to audit log.
func newAuditChannelModerationPatch(p *ChannelModerationPatch) auditChannelModerationPatch {
	var patch auditChannelModerationPatch
	if p != nil {
		if p.Name != nil {
			patch.Name = *p.Name
		}
		if p.Roles.Guests != nil {
			patch.RoleGuests = *p.Roles.Guests
		}
		if p.Roles.Members != nil {
			patch.RoleMembers = *p.Roles.Members
		}
	}
	return patch
}

func (p auditChannelModerationPatch) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("name", p.Name)
	enc.BoolKey("role_guests", p.RoleGuests)
	enc.BoolKey("role_members", p.RoleMembers)
}

func (p auditChannelModerationPatch) IsNil() bool {
	return false
}

type auditEmoji struct {
	ID   string
	Name string
}

// newAuditEmoji creates a simplified representation of Emoji for output to audit log.
func newAuditEmoji(e *Emoji) auditEmoji {
	var emoji auditEmoji
	if e != nil {
		emoji.ID = e.Id
		emoji.Name = e.Name
	}
	return emoji
}

func (e auditEmoji) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", e.ID)
	enc.StringKey("name", e.Name)
}

func (e auditEmoji) IsNil() bool {
	return false
}

type auditFileInfo struct {
	ID        string
	PostID    string
	Path      string
	Name      string
	Extension string
	Size      int64
}

// newAuditFileInfo creates a simplified representation of FileInfo for output to audit log.
func newAuditFileInfo(f *FileInfo) auditFileInfo {
	var fi auditFileInfo
	if f != nil {
		fi.ID = f.Id
		fi.PostID = f.PostId
		fi.Path = f.Path
		fi.Name = f.Name
		fi.Extension = f.Extension
		fi.Size = f.Size
	}
	return fi
}

func (fi auditFileInfo) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", fi.ID)
	enc.StringKey("post_id", fi.PostID)
	enc.StringKey("path", fi.Path)
	enc.StringKey("name", fi.Name)
	enc.StringKey("ext", fi.Extension)
	enc.Int64Key("size", fi.Size)
}

func (fi auditFileInfo) IsNil() bool {
	return false
}

type auditGroup struct {
	ID          string
	Name        string
	DisplayName string
	Description string
}

// newAuditGroup creates a simplified representation of Group for output to audit log.
func newAuditGroup(g *Group) auditGroup {
	var group auditGroup
	if g != nil {
		group.ID = g.Id
		if g.Name == nil {
			group.Name = ""
		} else {
			group.Name = *g.Name
		}
		group.DisplayName = g.DisplayName
		group.Description = g.Description
	}
	return group
}

func (g auditGroup) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", g.ID)
	enc.StringKey("name", g.Name)
	enc.StringKey("display", g.DisplayName)
	enc.StringKey("desc", g.Description)
}

func (g auditGroup) IsNil() bool {
	return false
}

type auditJob struct {
	ID       string
	Type     string
	Priority int64
	StartAt  int64
}

// newAuditJob creates a simplified representation of Job for output to audit log.
func newAuditJob(j *Job) auditJob {
	var job auditJob
	if j != nil {
		job.ID = j.Id
		job.Type = j.Type
		job.Priority = j.Priority
		job.StartAt = j.StartAt
	}
	return job
}

func (j auditJob) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", j.ID)
	enc.StringKey("type", j.Type)
	enc.Int64Key("priority", j.Priority)
	enc.Int64Key("start_at", j.StartAt)
}

func (j auditJob) IsNil() bool {
	return false
}

type auditOAuthApp struct {
	ID          string
	CreatorID   string
	Name        string
	Description string
	IsTrusted   bool
}

// newAuditOAuthApp creates a simplified representation of OAuthApp for output to audit log.
func newAuditOAuthApp(o *OAuthApp) auditOAuthApp {
	var oauth auditOAuthApp
	if o != nil {
		oauth.ID = o.Id
		oauth.CreatorID = o.CreatorId
		oauth.Name = o.Name
		oauth.Description = o.Description
		oauth.IsTrusted = o.IsTrusted
	}
	return oauth
}

func (o auditOAuthApp) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", o.ID)
	enc.StringKey("creator_id", o.CreatorID)
	enc.StringKey("name", o.Name)
	enc.StringKey("desc", o.Description)
	enc.BoolKey("trusted", o.IsTrusted)
}

func (o auditOAuthApp) IsNil() bool {
	return false
}

type auditPost struct {
	ID        string
	ChannelID string
	Type      string
	IsPinned  bool
}

// newAuditPost creates a simplified representation of Post for output to audit log.
func newAuditPost(p *Post) auditPost {
	var post auditPost
	if p != nil {
		post.ID = p.Id
		post.ChannelID = p.ChannelId
		post.Type = p.Type
		post.IsPinned = p.IsPinned
	}
	return post
}

func (p auditPost) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", p.ID)
	enc.StringKey("channel_id", p.ChannelID)
	enc.StringKey("type", p.Type)
	enc.BoolKey("pinned", p.IsPinned)
}

func (p auditPost) IsNil() bool {
	return false
}

type auditRole struct {
	ID            string
	Name          string
	DisplayName   string
	Permissions   []string
	SchemeManaged bool
	BuiltIn       bool
}

// newAuditRole creates a simplified representation of Role for output to audit log.
func newAuditRole(r *Role) auditRole {
	var role auditRole
	if r != nil {
		role.ID = r.Id
		role.Name = r.Name
		role.DisplayName = r.DisplayName
		role.Permissions = append(role.Permissions, r.Permissions...)
		role.SchemeManaged = r.SchemeManaged
		role.BuiltIn = r.BuiltIn
	}
	return role
}

func (r auditRole) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", r.ID)
	enc.StringKey("name", r.Name)
	enc.StringKey("display", r.DisplayName)
	enc.SliceStringKey("perms", r.Permissions)
	enc.BoolKey("schemeManaged", r.SchemeManaged)
	enc.BoolKey("builtin", r.BuiltIn)
}

func (r auditRole) IsNil() bool {
	return false
}

type auditScheme struct {
	ID          string
	Name        string
	DisplayName string
	Scope       string
}

// newAuditScheme creates a simplified representation of Scheme for output to audit log.
func newAuditScheme(s *Scheme) auditScheme {
	var scheme auditScheme
	if s != nil {
		scheme.ID = s.Id
		scheme.Name = s.Name
		scheme.DisplayName = s.DisplayName
		scheme.Scope = s.Scope
	}
	return scheme
}

func (s auditScheme) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", s.ID)
	enc.StringKey("name", s.Name)
	enc.StringKey("display", s.DisplayName)
	enc.StringKey("scope", s.Scope)
}

func (s auditScheme) IsNil() bool {
	return false
}

type auditSchemeRoles struct {
	SchemeAdmin bool
	SchemeUser  bool
	SchemeGuest bool
}

// newAuditSchemeRoles creates a simplified representation of SchemeRoles for output to audit log.
func newAuditSchemeRoles(s *SchemeRoles) auditSchemeRoles {
	var roles auditSchemeRoles
	if s != nil {
		roles.SchemeAdmin = s.SchemeAdmin
		roles.SchemeUser = s.SchemeUser
		roles.SchemeGuest = s.SchemeGuest
	}
	return roles
}

func (s auditSchemeRoles) MarshalJSONObject(enc *gojay.Encoder) {
	enc.BoolKey("admin", s.SchemeAdmin)
	enc.BoolKey("user", s.SchemeUser)
	enc.BoolKey("guest", s.SchemeGuest)
}

func (s auditSchemeRoles) IsNil() bool {
	return false
}

type auditSession struct {
	ID       string
	UserId   string
	DeviceId string
}

// newAuditSession creates a simplified representation of Session for output to audit log.
func newAuditSession(s *Session) auditSession {
	var session auditSession
	if s != nil {
		session.ID = s.Id
		session.UserId = s.UserId
		session.DeviceId = s.DeviceId
	}
	return session
}

func (s auditSession) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", s.ID)
	enc.StringKey("user_id", s.UserId)
	enc.StringKey("device_id", s.DeviceId)
}

func (s auditSession) IsNil() bool {
	return false
}

type auditIncomingWebhook struct {
	ID          string
	ChannelID   string
	TeamId      string
	DisplayName string
	Description string
}

// newAuditIncomingWebhook creates a simplified representation of IncomingWebhook for output to audit log.
func newAuditIncomingWebhook(h *IncomingWebhook) auditIncomingWebhook {
	var hook auditIncomingWebhook
	if h != nil {
		hook.ID = h.Id
		hook.ChannelID = h.ChannelId
		hook.TeamId = h.TeamId
		hook.DisplayName = h.DisplayName
		hook.Description = h.Description
	}
	return hook
}

func (h auditIncomingWebhook) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", h.ID)
	enc.StringKey("channel_id", h.ChannelID)
	enc.StringKey("team_id", h.TeamId)
	enc.StringKey("display", h.DisplayName)
	enc.StringKey("desc", h.Description)
}

func (h auditIncomingWebhook) IsNil() bool {
	return false
}

type auditOutgoingWebhook struct {
	ID           string
	ChannelID    string
	TeamID       string
	TriggerWords StringArray
	TriggerWhen  int
	DisplayName  string
	Description  string
	ContentType  string
	Username     string
}

// newAuditOutgoingWebhook creates a simplified representation of OutgoingWebhook for output to audit log.
func newAuditOutgoingWebhook(h *OutgoingWebhook) auditOutgoingWebhook {
	var hook auditOutgoingWebhook
	if h != nil {
		hook.ID = h.Id
		hook.ChannelID = h.ChannelId
		hook.TeamID = h.TeamId
		hook.TriggerWords = h.TriggerWords
		hook.TriggerWhen = h.TriggerWhen
		hook.DisplayName = h.DisplayName
		hook.Description = h.Description
		hook.ContentType = h.ContentType
		hook.Username = h.Username
	}
	return hook
}

func (h auditOutgoingWebhook) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("id", h.ID)
	enc.StringKey("channel_id", h.ChannelID)
	enc.StringKey("team_id", h.TeamID)
	enc.SliceStringKey("trigger_words", h.TriggerWords)
	enc.IntKey("trigger_when", h.TriggerWhen)
	enc.StringKey("display", h.DisplayName)
	enc.StringKey("desc", h.Description)
	enc.StringKey("content_type", h.ContentType)
	enc.StringKey("username", h.Username)
}

func (h auditOutgoingWebhook) IsNil() bool {
	return false
}

type auditRemoteCluster struct {
	RemoteId     string
	RemoteTeamId string
	Name         string
	DisplayName  string
	SiteURL      string
	CreateAt     int64
	LastPingAt   int64
	CreatorId    string
}

// newRemoteCluster creates a simplified representation of RemoteCluster for output to audit log.
func newRemoteCluster(r *RemoteCluster) auditRemoteCluster {
	var rc auditRemoteCluster
	if r != nil {
		rc.RemoteId = r.RemoteId
		rc.RemoteTeamId = r.RemoteTeamId
		rc.Name = r.Name
		rc.DisplayName = r.DisplayName
		rc.SiteURL = r.SiteURL
		rc.CreateAt = r.CreateAt
		rc.LastPingAt = r.LastPingAt
		rc.CreatorId = r.CreatorId
	}
	return rc
}

func (r auditRemoteCluster) MarshalJSONObject(enc *gojay.Encoder) {
	enc.StringKey("remote_id", r.RemoteId)
	enc.StringKey("remote_team_id", r.RemoteTeamId)
	enc.StringKey("name", r.Name)
	enc.StringKey("display_name", r.DisplayName)
	enc.StringKey("site_url", r.SiteURL)
	enc.Int64Key("create_at", r.CreateAt)
	enc.Int64Key("last_ping_at", r.LastPingAt)
	enc.StringKey("creator_id", r.CreatorId)
}

func (r auditRemoteCluster) IsNil() bool {
	return false
}