github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/auditconv.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import "github.com/francoispqt/gojay" 7 8 // AuditModelTypeConv converts key model types to something better suited for audit output. 9 func AuditModelTypeConv(val interface{}) (newVal interface{}, converted bool) { 10 if val == nil { 11 return nil, false 12 } 13 switch v := val.(type) { 14 case *Channel: 15 return newAuditChannel(v), true 16 case *Team: 17 return newAuditTeam(v), true 18 case *User: 19 return newAuditUser(v), true 20 case *Command: 21 return newAuditCommand(v), true 22 case *CommandArgs: 23 return newAuditCommandArgs(v), true 24 case *Bot: 25 return newAuditBot(v), true 26 case *ChannelModerationPatch: 27 return newAuditChannelModerationPatch(v), true 28 case *Emoji: 29 return newAuditEmoji(v), true 30 case *FileInfo: 31 return newAuditFileInfo(v), true 32 case *Group: 33 return newAuditGroup(v), true 34 case *Job: 35 return newAuditJob(v), true 36 case *OAuthApp: 37 return newAuditOAuthApp(v), true 38 case *Post: 39 return newAuditPost(v), true 40 case *Role: 41 return newAuditRole(v), true 42 case *Scheme: 43 return newAuditScheme(v), true 44 case *SchemeRoles: 45 return newAuditSchemeRoles(v), true 46 case *Session: 47 return newAuditSession(v), true 48 case *IncomingWebhook: 49 return newAuditIncomingWebhook(v), true 50 case *OutgoingWebhook: 51 return newAuditOutgoingWebhook(v), true 52 } 53 return val, false 54 } 55 56 type auditChannel struct { 57 ID string 58 Name string 59 Type string 60 } 61 62 // newAuditChannel creates a simplified representation of Channel for output to audit log. 63 func newAuditChannel(c *Channel) auditChannel { 64 var channel auditChannel 65 if c != nil { 66 channel.ID = c.Id 67 channel.Name = c.Name 68 channel.Type = c.Type 69 } 70 return channel 71 } 72 73 func (c auditChannel) MarshalJSONObject(enc *gojay.Encoder) { 74 enc.StringKey("id", c.ID) 75 enc.StringKey("name", c.Name) 76 enc.StringKey("type", c.Type) 77 } 78 79 func (c auditChannel) IsNil() bool { 80 return false 81 } 82 83 type auditTeam struct { 84 ID string 85 Name string 86 Type string 87 } 88 89 // newAuditTeam creates a simplified representation of Team for output to audit log. 90 func newAuditTeam(t *Team) auditTeam { 91 var team auditTeam 92 if t != nil { 93 team.ID = t.Id 94 team.Name = t.Name 95 team.Type = t.Type 96 } 97 return team 98 } 99 100 func (t auditTeam) MarshalJSONObject(enc *gojay.Encoder) { 101 enc.StringKey("id", t.ID) 102 enc.StringKey("name", t.Name) 103 enc.StringKey("type", t.Type) 104 } 105 106 func (t auditTeam) IsNil() bool { 107 return false 108 } 109 110 type auditUser struct { 111 ID string 112 Name string 113 Roles string 114 } 115 116 // newAuditUser creates a simplified representation of User for output to audit log. 117 func newAuditUser(u *User) auditUser { 118 var user auditUser 119 if u != nil { 120 user.ID = u.Id 121 user.Name = u.Username 122 user.Roles = u.Roles 123 } 124 return user 125 } 126 127 func (u auditUser) MarshalJSONObject(enc *gojay.Encoder) { 128 enc.StringKey("id", u.ID) 129 enc.StringKey("name", u.Name) 130 enc.StringKey("roles", u.Roles) 131 } 132 133 func (u auditUser) IsNil() bool { 134 return false 135 } 136 137 type auditCommand struct { 138 ID string 139 CreatorID string 140 TeamID string 141 Trigger string 142 Method string 143 Username string 144 IconURL string 145 AutoComplete bool 146 AutoCompleteDesc string 147 AutoCompleteHint string 148 DisplayName string 149 Description string 150 URL string 151 } 152 153 // newAuditCommand creates a simplified representation of Command for output to audit log. 154 func newAuditCommand(c *Command) auditCommand { 155 var cmd auditCommand 156 if c != nil { 157 cmd.ID = c.Id 158 cmd.CreatorID = c.CreatorId 159 cmd.TeamID = c.TeamId 160 cmd.Trigger = c.Trigger 161 cmd.Method = c.Method 162 cmd.Username = c.Username 163 cmd.IconURL = c.IconURL 164 cmd.AutoComplete = c.AutoComplete 165 cmd.AutoCompleteDesc = c.AutoCompleteDesc 166 cmd.AutoCompleteHint = c.AutoCompleteHint 167 cmd.DisplayName = c.DisplayName 168 cmd.Description = c.Description 169 cmd.URL = c.URL 170 } 171 return cmd 172 } 173 174 func (cmd auditCommand) MarshalJSONObject(enc *gojay.Encoder) { 175 enc.StringKey("id", cmd.ID) 176 enc.StringKey("creator_id", cmd.CreatorID) 177 enc.StringKey("team_id", cmd.TeamID) 178 enc.StringKey("trigger", cmd.Trigger) 179 enc.StringKey("method", cmd.Method) 180 enc.StringKey("username", cmd.Username) 181 enc.StringKey("icon_url", cmd.IconURL) 182 enc.BoolKey("auto_complete", cmd.AutoComplete) 183 enc.StringKey("auto_complete_desc", cmd.AutoCompleteDesc) 184 enc.StringKey("auto_complete_hint", cmd.AutoCompleteHint) 185 enc.StringKey("display", cmd.DisplayName) 186 enc.StringKey("desc", cmd.Description) 187 enc.StringKey("url", cmd.URL) 188 } 189 190 func (cmd auditCommand) IsNil() bool { 191 return false 192 } 193 194 type auditCommandArgs struct { 195 ChannelID string 196 TeamID string 197 TriggerID string 198 Command string 199 } 200 201 // newAuditCommandArgs creates a simplified representation of CommandArgs for output to audit log. 202 func newAuditCommandArgs(ca *CommandArgs) auditCommandArgs { 203 var cmdargs auditCommandArgs 204 if ca != nil { 205 cmdargs.ChannelID = ca.ChannelId 206 cmdargs.TeamID = ca.TeamId 207 cmdargs.TriggerID = ca.TriggerId 208 cmdargs.Command = ca.Command 209 } 210 return cmdargs 211 } 212 213 func (ca auditCommandArgs) MarshalJSONObject(enc *gojay.Encoder) { 214 enc.StringKey("channel_id", ca.ChannelID) 215 enc.StringKey("team_id", ca.TriggerID) 216 enc.StringKey("trigger_id", ca.TeamID) 217 enc.StringKey("command", ca.Command) 218 } 219 220 func (ca auditCommandArgs) IsNil() bool { 221 return false 222 } 223 224 type auditBot struct { 225 UserID string 226 Username string 227 Displayname string 228 } 229 230 // newAuditBot creates a simplified representation of Bot for output to audit log. 231 func newAuditBot(b *Bot) auditBot { 232 var bot auditBot 233 if b != nil { 234 bot.UserID = b.UserId 235 bot.Username = b.Username 236 bot.Displayname = b.DisplayName 237 } 238 return bot 239 } 240 241 func (b auditBot) MarshalJSONObject(enc *gojay.Encoder) { 242 enc.StringKey("user_id", b.UserID) 243 enc.StringKey("username", b.Username) 244 enc.StringKey("display", b.Displayname) 245 } 246 247 func (b auditBot) IsNil() bool { 248 return false 249 } 250 251 type auditChannelModerationPatch struct { 252 Name string 253 RoleGuests bool 254 RoleMembers bool 255 } 256 257 // newAuditChannelModerationPatch creates a simplified representation of ChannelModerationPatch for output to audit log. 258 func newAuditChannelModerationPatch(p *ChannelModerationPatch) auditChannelModerationPatch { 259 var patch auditChannelModerationPatch 260 if p != nil { 261 if p.Name != nil { 262 patch.Name = *p.Name 263 } 264 if p.Roles.Guests != nil { 265 patch.RoleGuests = *p.Roles.Guests 266 } 267 if p.Roles.Members != nil { 268 patch.RoleMembers = *p.Roles.Members 269 } 270 } 271 return patch 272 } 273 274 func (p auditChannelModerationPatch) MarshalJSONObject(enc *gojay.Encoder) { 275 enc.StringKey("name", p.Name) 276 enc.BoolKey("role_guests", p.RoleGuests) 277 enc.BoolKey("role_members", p.RoleMembers) 278 } 279 280 func (p auditChannelModerationPatch) IsNil() bool { 281 return false 282 } 283 284 type auditEmoji struct { 285 ID string 286 Name string 287 } 288 289 // newAuditEmoji creates a simplified representation of Emoji for output to audit log. 290 func newAuditEmoji(e *Emoji) auditEmoji { 291 var emoji auditEmoji 292 if e != nil { 293 emoji.ID = e.Id 294 emoji.Name = e.Name 295 } 296 return emoji 297 } 298 299 func (e auditEmoji) MarshalJSONObject(enc *gojay.Encoder) { 300 enc.StringKey("id", e.ID) 301 enc.StringKey("name", e.Name) 302 } 303 304 func (e auditEmoji) IsNil() bool { 305 return false 306 } 307 308 type auditFileInfo struct { 309 ID string 310 PostID string 311 Path string 312 Name string 313 Extension string 314 Size int64 315 } 316 317 // newAuditFileInfo creates a simplified representation of FileInfo for output to audit log. 318 func newAuditFileInfo(f *FileInfo) auditFileInfo { 319 var fi auditFileInfo 320 if f != nil { 321 fi.ID = f.Id 322 fi.PostID = f.PostId 323 fi.Path = f.Path 324 fi.Name = f.Name 325 fi.Extension = f.Extension 326 fi.Size = f.Size 327 } 328 return fi 329 } 330 331 func (fi auditFileInfo) MarshalJSONObject(enc *gojay.Encoder) { 332 enc.StringKey("id", fi.ID) 333 enc.StringKey("post_id", fi.PostID) 334 enc.StringKey("path", fi.Path) 335 enc.StringKey("name", fi.Name) 336 enc.StringKey("ext", fi.Extension) 337 enc.Int64Key("size", fi.Size) 338 } 339 340 func (fi auditFileInfo) IsNil() bool { 341 return false 342 } 343 344 type auditGroup struct { 345 ID string 346 Name string 347 DisplayName string 348 Description string 349 } 350 351 // newAuditGroup creates a simplified representation of Group for output to audit log. 352 func newAuditGroup(g *Group) auditGroup { 353 var group auditGroup 354 if g != nil { 355 group.ID = g.Id 356 if g.Name == nil { 357 group.Name = "" 358 } else { 359 group.Name = *g.Name 360 } 361 group.DisplayName = g.DisplayName 362 group.Description = g.Description 363 } 364 return group 365 } 366 367 func (g auditGroup) MarshalJSONObject(enc *gojay.Encoder) { 368 enc.StringKey("id", g.ID) 369 enc.StringKey("name", g.Name) 370 enc.StringKey("display", g.DisplayName) 371 enc.StringKey("desc", g.Description) 372 } 373 374 func (g auditGroup) IsNil() bool { 375 return false 376 } 377 378 type auditJob struct { 379 ID string 380 Type string 381 Priority int64 382 StartAt int64 383 } 384 385 // newAuditJob creates a simplified representation of Job for output to audit log. 386 func newAuditJob(j *Job) auditJob { 387 var job auditJob 388 if j != nil { 389 job.ID = j.Id 390 job.Type = j.Type 391 job.Priority = j.Priority 392 job.StartAt = j.StartAt 393 } 394 return job 395 } 396 397 func (j auditJob) MarshalJSONObject(enc *gojay.Encoder) { 398 enc.StringKey("id", j.ID) 399 enc.StringKey("type", j.Type) 400 enc.Int64Key("priority", j.Priority) 401 enc.Int64Key("start_at", j.StartAt) 402 } 403 404 func (j auditJob) IsNil() bool { 405 return false 406 } 407 408 type auditOAuthApp struct { 409 ID string 410 CreatorID string 411 Name string 412 Description string 413 IsTrusted bool 414 } 415 416 // newAuditOAuthApp creates a simplified representation of OAuthApp for output to audit log. 417 func newAuditOAuthApp(o *OAuthApp) auditOAuthApp { 418 var oauth auditOAuthApp 419 if o != nil { 420 oauth.ID = o.Id 421 oauth.CreatorID = o.CreatorId 422 oauth.Name = o.Name 423 oauth.Description = o.Description 424 oauth.IsTrusted = o.IsTrusted 425 } 426 return oauth 427 } 428 429 func (o auditOAuthApp) MarshalJSONObject(enc *gojay.Encoder) { 430 enc.StringKey("id", o.ID) 431 enc.StringKey("creator_id", o.CreatorID) 432 enc.StringKey("name", o.Name) 433 enc.StringKey("desc", o.Description) 434 enc.BoolKey("trusted", o.IsTrusted) 435 } 436 437 func (o auditOAuthApp) IsNil() bool { 438 return false 439 } 440 441 type auditPost struct { 442 ID string 443 ChannelID string 444 Type string 445 IsPinned bool 446 } 447 448 // newAuditPost creates a simplified representation of Post for output to audit log. 449 func newAuditPost(p *Post) auditPost { 450 var post auditPost 451 if p != nil { 452 post.ID = p.Id 453 post.ChannelID = p.ChannelId 454 post.Type = p.Type 455 post.IsPinned = p.IsPinned 456 } 457 return post 458 } 459 460 func (p auditPost) MarshalJSONObject(enc *gojay.Encoder) { 461 enc.StringKey("id", p.ID) 462 enc.StringKey("channel_id", p.ChannelID) 463 enc.StringKey("type", p.Type) 464 enc.BoolKey("pinned", p.IsPinned) 465 } 466 467 func (p auditPost) IsNil() bool { 468 return false 469 } 470 471 type auditRole struct { 472 ID string 473 Name string 474 DisplayName string 475 Permissions []string 476 SchemeManaged bool 477 BuiltIn bool 478 } 479 480 // newAuditRole creates a simplified representation of Role for output to audit log. 481 func newAuditRole(r *Role) auditRole { 482 var role auditRole 483 if r != nil { 484 role.ID = r.Id 485 role.Name = r.Name 486 role.DisplayName = r.DisplayName 487 role.Permissions = append(role.Permissions, r.Permissions...) 488 role.SchemeManaged = r.SchemeManaged 489 role.BuiltIn = r.BuiltIn 490 } 491 return role 492 } 493 494 func (r auditRole) MarshalJSONObject(enc *gojay.Encoder) { 495 enc.StringKey("id", r.ID) 496 enc.StringKey("name", r.Name) 497 enc.StringKey("display", r.DisplayName) 498 enc.SliceStringKey("perms", r.Permissions) 499 enc.BoolKey("schemeManaged", r.SchemeManaged) 500 enc.BoolKey("builtin", r.BuiltIn) 501 } 502 503 func (r auditRole) IsNil() bool { 504 return false 505 } 506 507 type auditScheme struct { 508 ID string 509 Name string 510 DisplayName string 511 Scope string 512 } 513 514 // newAuditScheme creates a simplified representation of Scheme for output to audit log. 515 func newAuditScheme(s *Scheme) auditScheme { 516 var scheme auditScheme 517 if s != nil { 518 scheme.ID = s.Id 519 scheme.Name = s.Name 520 scheme.DisplayName = s.DisplayName 521 scheme.Scope = s.Scope 522 } 523 return scheme 524 } 525 526 func (s auditScheme) MarshalJSONObject(enc *gojay.Encoder) { 527 enc.StringKey("id", s.ID) 528 enc.StringKey("name", s.Name) 529 enc.StringKey("display", s.DisplayName) 530 enc.StringKey("scope", s.Scope) 531 } 532 533 func (s auditScheme) IsNil() bool { 534 return false 535 } 536 537 type auditSchemeRoles struct { 538 SchemeAdmin bool 539 SchemeUser bool 540 SchemeGuest bool 541 } 542 543 // newAuditSchemeRoles creates a simplified representation of SchemeRoles for output to audit log. 544 func newAuditSchemeRoles(s *SchemeRoles) auditSchemeRoles { 545 var roles auditSchemeRoles 546 if s != nil { 547 roles.SchemeAdmin = s.SchemeAdmin 548 roles.SchemeUser = s.SchemeUser 549 roles.SchemeGuest = s.SchemeGuest 550 } 551 return roles 552 } 553 554 func (s auditSchemeRoles) MarshalJSONObject(enc *gojay.Encoder) { 555 enc.BoolKey("admin", s.SchemeAdmin) 556 enc.BoolKey("user", s.SchemeUser) 557 enc.BoolKey("guest", s.SchemeGuest) 558 } 559 560 func (s auditSchemeRoles) IsNil() bool { 561 return false 562 } 563 564 type auditSession struct { 565 ID string 566 UserId string 567 DeviceId string 568 } 569 570 // newAuditSession creates a simplified representation of Session for output to audit log. 571 func newAuditSession(s *Session) auditSession { 572 var session auditSession 573 if s != nil { 574 session.ID = s.Id 575 session.UserId = s.UserId 576 session.DeviceId = s.DeviceId 577 } 578 return session 579 } 580 581 func (s auditSession) MarshalJSONObject(enc *gojay.Encoder) { 582 enc.StringKey("id", s.ID) 583 enc.StringKey("user_id", s.UserId) 584 enc.StringKey("device_id", s.DeviceId) 585 } 586 587 func (s auditSession) IsNil() bool { 588 return false 589 } 590 591 type auditIncomingWebhook struct { 592 ID string 593 ChannelID string 594 TeamId string 595 DisplayName string 596 Description string 597 } 598 599 // newAuditIncomingWebhook creates a simplified representation of IncomingWebhook for output to audit log. 600 func newAuditIncomingWebhook(h *IncomingWebhook) auditIncomingWebhook { 601 var hook auditIncomingWebhook 602 if h != nil { 603 hook.ID = h.Id 604 hook.ChannelID = h.ChannelId 605 hook.TeamId = h.TeamId 606 hook.DisplayName = h.DisplayName 607 hook.Description = h.Description 608 } 609 return hook 610 } 611 612 func (h auditIncomingWebhook) MarshalJSONObject(enc *gojay.Encoder) { 613 enc.StringKey("id", h.ID) 614 enc.StringKey("channel_id", h.ChannelID) 615 enc.StringKey("team_id", h.TeamId) 616 enc.StringKey("display", h.DisplayName) 617 enc.StringKey("desc", h.Description) 618 } 619 620 func (h auditIncomingWebhook) IsNil() bool { 621 return false 622 } 623 624 type auditOutgoingWebhook struct { 625 ID string 626 ChannelID string 627 TeamID string 628 TriggerWords StringArray 629 TriggerWhen int 630 DisplayName string 631 Description string 632 ContentType string 633 Username string 634 } 635 636 // newAuditOutgoingWebhook creates a simplified representation of OutgoingWebhook for output to audit log. 637 func newAuditOutgoingWebhook(h *OutgoingWebhook) auditOutgoingWebhook { 638 var hook auditOutgoingWebhook 639 if h != nil { 640 hook.ID = h.Id 641 hook.ChannelID = h.ChannelId 642 hook.TeamID = h.TeamId 643 hook.TriggerWords = h.TriggerWords 644 hook.TriggerWhen = h.TriggerWhen 645 hook.DisplayName = h.DisplayName 646 hook.Description = h.Description 647 hook.ContentType = h.ContentType 648 hook.Username = h.Username 649 } 650 return hook 651 } 652 653 func (h auditOutgoingWebhook) MarshalJSONObject(enc *gojay.Encoder) { 654 enc.StringKey("id", h.ID) 655 enc.StringKey("channel_id", h.ChannelID) 656 enc.StringKey("team_id", h.TeamID) 657 enc.SliceStringKey("trigger_words", h.TriggerWords) 658 enc.IntKey("trigger_when", h.TriggerWhen) 659 enc.StringKey("display", h.DisplayName) 660 enc.StringKey("desc", h.Description) 661 enc.StringKey("content_type", h.ContentType) 662 enc.StringKey("username", h.Username) 663 } 664 665 func (h auditOutgoingWebhook) IsNil() bool { 666 return false 667 }