mattermost/server/public/model/compliance.go

142 lines
3.5 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2016-03-14 19:07:58 -04:00
package model
import (
"net/http"
2016-03-14 19:07:58 -04:00
"strings"
"github.com/mattermost/mattermost/server/public/shared/mlog"
2016-03-14 19:07:58 -04:00
)
const (
2021-07-12 14:05:36 -04:00
ComplianceStatusCreated = "created"
ComplianceStatusRunning = "running"
ComplianceStatusFinished = "finished"
ComplianceStatusFailed = "failed"
ComplianceStatusRemoved = "removed"
ComplianceTypeDaily = "daily"
ComplianceTypeAdhoc = "adhoc"
2016-03-14 19:07:58 -04:00
)
type Compliance struct {
Id string `json:"id"`
CreateAt int64 `json:"create_at"`
UserId string `json:"user_id"`
Status string `json:"status"`
Count int `json:"count"`
Desc string `json:"desc"`
Type string `json:"type"`
StartAt int64 `json:"start_at"`
EndAt int64 `json:"end_at"`
Keywords string `json:"keywords"`
Emails string `json:"emails"`
}
func (c *Compliance) Auditable() map[string]any {
return map[string]any{
Audit logging -- convert audit logs to use the new schema (#20526) * Audit logging - new schema added, old schema removed. * fix linter error by running goimports * Address review comments * Address review comments * Example usage of new audit logging API for the updateUserAuth call * fixed unit test on auditing updating user record * Changed the `TestUpdateConfigDiffInAuditRecord` testcase---it failed, because this PR changes how the `meta` field is serialized into the audit log records. * fix linter error * use string constants for record keys * new audit api calls for api4/bot * `Auditable` interface implementations for model classes * New audit calls for channel api * New audit calls for channel_local * renamed receivers for required style reasons * New audit calls for api4/command * renamed receiver * New audit calls for api4/command_local * renamed receiver * fix unit test to reflect changes in the Auditable implementation of the user class * new audit calls for compliance * new audit calls for configs * remove auditRec.addMeta from updateConfig and patchConfig * new audit calls for config_local * new audit calls * new audit calls for ldap, license apis * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * fix linter error * fixed linter error * fixed "user update" test * Don't include all of config when audit logging config changes. Also fix unit test on TestUpdateConfigDiffInAuditRecord * address review comments * Added Auditable() method for UserPatch * Fix duplicative method declaration from merge * Fix styling and API changes issues introduced with merge * Fix broken test Co-authored-by: Daniel Schalla <daniel@schalla.me>
2022-07-14 07:52:46 -04:00
"id": c.Id,
"create_at": c.CreateAt,
"user_id": c.UserId,
"status": c.Status,
"count": c.Count,
"desc": c.Desc,
"type": c.Type,
"start_at": c.StartAt,
"end_at": c.EndAt,
"keywords": c.Keywords,
"emails": c.Emails,
}
}
2016-03-14 19:07:58 -04:00
type Compliances []Compliance
// ComplianceExportCursor is used for paginated iteration of posts
// for compliance export.
// We need to keep track of the last post ID in addition to the last post
// CreateAt to break ties when two posts have the same CreateAt.
type ComplianceExportCursor struct {
LastChannelsQueryPostCreateAt int64
LastChannelsQueryPostID string
ChannelsQueryCompleted bool
LastDirectMessagesQueryPostCreateAt int64
LastDirectMessagesQueryPostID string
DirectMessagesQueryCompleted bool
}
func (c *Compliance) PreSave() {
if c.Id == "" {
c.Id = NewId()
2016-03-14 19:07:58 -04:00
}
if c.Status == "" {
2021-07-12 14:05:36 -04:00
c.Status = ComplianceStatusCreated
2016-03-14 19:07:58 -04:00
}
c.Count = 0
c.Emails = NormalizeEmail(c.Emails)
c.Keywords = strings.ToLower(c.Keywords)
2016-03-14 19:07:58 -04:00
c.CreateAt = GetMillis()
2016-03-14 19:07:58 -04:00
}
func (c *Compliance) DeepCopy() *Compliance {
cCopy := *c
return &cCopy
}
func (c *Compliance) JobName() string {
jobName := c.Type
2021-07-12 14:05:36 -04:00
if c.Type == ComplianceTypeDaily {
jobName += "-" + c.Desc
2016-03-14 19:07:58 -04:00
}
jobName += "-" + c.Id
2016-03-14 19:07:58 -04:00
return jobName
}
func (c *Compliance) IsValid() *AppError {
if !IsValidId(c.Id) {
return NewAppError("Compliance.IsValid", "model.compliance.is_valid.id.app_error", nil, "", http.StatusBadRequest)
2016-03-14 19:07:58 -04:00
}
if c.CreateAt == 0 {
return NewAppError("Compliance.IsValid", "model.compliance.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
2016-03-14 19:07:58 -04:00
}
if len(c.Desc) > 512 || c.Desc == "" {
return NewAppError("Compliance.IsValid", "model.compliance.is_valid.desc.app_error", nil, "", http.StatusBadRequest)
2016-03-14 19:07:58 -04:00
}
if c.StartAt == 0 {
return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_at.app_error", nil, "", http.StatusBadRequest)
2016-03-14 19:07:58 -04:00
}
if c.EndAt == 0 {
return NewAppError("Compliance.IsValid", "model.compliance.is_valid.end_at.app_error", nil, "", http.StatusBadRequest)
2016-03-14 19:07:58 -04:00
}
if c.EndAt <= c.StartAt {
return NewAppError("Compliance.IsValid", "model.compliance.is_valid.start_end_at.app_error", nil, "", http.StatusBadRequest)
2016-03-14 19:07:58 -04:00
}
return nil
}
// LoggerFields returns the logger annotations reflecting the given compliance job metadata.
func (c *Compliance) LoggerFields() []mlog.Field {
if c == nil {
return nil
}
return []mlog.Field{
mlog.String("job_id", c.Id),
mlog.String("job_type", c.Type),
mlog.String("job_name", c.JobName()),
mlog.Millis("job_create_at", c.CreateAt),
}
}