compliance.go 2.21 KB
Newer Older
1
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
2 3 4 5 6 7 8
// See License.txt for license information.

package app

import (
	"io/ioutil"

Chris's avatar
Chris committed
9 10
	"net/http"

Christopher Speller's avatar
Christopher Speller committed
11
	"github.com/mattermost/mattermost-server/model"
12 13
)

Chris's avatar
Chris committed
14
func (a *App) GetComplianceReports(page, perPage int) (model.Compliances, *model.AppError) {
15
	if license := a.License(); !*a.Config().ComplianceSettings.Enable || license == nil || !*license.Features.Compliance {
16
		return nil, model.NewAppError("GetComplianceReports", "ent.compliance.licence_disable.app_error", nil, "", http.StatusNotImplemented)
17 18
	}

19 20
	result := <-a.Srv.Store.Compliance().GetAll(page*perPage, perPage)
	if result.Err != nil {
21 22
		return nil, result.Err
	}
23
	return result.Data.(model.Compliances), nil
24 25
}

Chris's avatar
Chris committed
26
func (a *App) SaveComplianceReport(job *model.Compliance) (*model.Compliance, *model.AppError) {
27
	if license := a.License(); !*a.Config().ComplianceSettings.Enable || license == nil || !*license.Features.Compliance || a.Compliance == nil {
28
		return nil, model.NewAppError("saveComplianceReport", "ent.compliance.licence_disable.app_error", nil, "", http.StatusNotImplemented)
29 30 31 32
	}

	job.Type = model.COMPLIANCE_TYPE_ADHOC

33 34
	result := <-a.Srv.Store.Compliance().Save(job)
	if result.Err != nil {
35 36 37
		return nil, result.Err
	}

38 39 40 41 42
	job = result.Data.(*model.Compliance)
	a.Go(func() {
		a.Compliance.RunComplianceJob(job)
	})

43 44 45
	return job, nil
}

Chris's avatar
Chris committed
46
func (a *App) GetComplianceReport(reportId string) (*model.Compliance, *model.AppError) {
47
	if license := a.License(); !*a.Config().ComplianceSettings.Enable || license == nil || !*license.Features.Compliance || a.Compliance == nil {
48
		return nil, model.NewAppError("downloadComplianceReport", "ent.compliance.licence_disable.app_error", nil, "", http.StatusNotImplemented)
49 50
	}

51 52
	result := <-a.Srv.Store.Compliance().Get(reportId)
	if result.Err != nil {
53 54
		return nil, result.Err
	}
55
	return result.Data.(*model.Compliance), nil
56 57
}

Chris's avatar
Chris committed
58
func (a *App) GetComplianceFile(job *model.Compliance) ([]byte, *model.AppError) {
59 60
	f, err := ioutil.ReadFile(*a.Config().ComplianceSettings.Directory + "compliance/" + job.JobName() + ".zip")
	if err != nil {
61
		return nil, model.NewAppError("readFile", "api.file.read_file.reading_local.app_error", nil, err.Error(), http.StatusNotImplemented)
62
	}
63
	return f, nil
64
}