2022-11-30 23:49:16 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-12-02 18:40:48 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-11-30 23:49:16 +02:00
|
|
|
"net/http"
|
2022-12-02 18:40:48 +02:00
|
|
|
"strconv"
|
2022-11-30 23:49:16 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2022-12-24 06:31:39 +02:00
|
|
|
|
2022-12-31 22:16:22 +02:00
|
|
|
mm_model "github.com/mattermost/mattermost-server/v6/model"
|
2022-12-02 18:40:48 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
complianceDefaultPage = "0"
|
|
|
|
complianceDefaultPerPage = "60"
|
2022-11-30 23:49:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *API) registerComplianceRoutes(r *mux.Router) {
|
|
|
|
// Compliance APIs
|
2022-12-24 06:31:39 +02:00
|
|
|
r.HandleFunc("/admin/boards", a.sessionRequired(a.handleGetBoardsForCompliance)).Methods("GET")
|
|
|
|
r.HandleFunc("/admin/boards_history", a.sessionRequired(a.handleGetBoardsComplianceHistory)).Methods("GET")
|
|
|
|
r.HandleFunc("/admin/blocks_history", a.sessionRequired(a.handleGetBlocksComplianceHistory)).Methods("GET")
|
2022-11-30 23:49:16 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
func (a *API) handleGetBoardsForCompliance(w http.ResponseWriter, r *http.Request) {
|
2022-11-30 23:49:16 +02:00
|
|
|
// TODO(@pinjasaur): swagger
|
|
|
|
|
2022-12-02 18:40:48 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
teamID := query.Get("team_id")
|
|
|
|
strPage := query.Get("page")
|
|
|
|
strPerPage := query.Get("per_page")
|
|
|
|
|
2022-11-30 23:49:16 +02:00
|
|
|
// Valid authorization (`manage_system`)?
|
|
|
|
userID := getUserID(r)
|
2022-12-31 22:16:22 +02:00
|
|
|
if !a.permissions.HasPermissionTo(userID, mm_model.PermissionManageSystem) {
|
2022-11-30 23:49:16 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrUnauthorized("access denied Compliance Export getAllBoards"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid license feature (Compliance)?
|
|
|
|
license := a.app.GetLicense()
|
|
|
|
if license == nil || !(*license.Features.Compliance) {
|
|
|
|
a.errorResponse(w, r, model.NewErrNotImplemented("insufficient license Compliance Export getAllBoards"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-31 22:16:22 +02:00
|
|
|
// check for valid team
|
|
|
|
_, err := a.app.GetTeam(teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("invalid team id: "+teamID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:40:48 +02:00
|
|
|
if strPage == "" {
|
|
|
|
strPage = complianceDefaultPage
|
|
|
|
}
|
|
|
|
if strPerPage == "" {
|
|
|
|
strPerPage = complianceDefaultPerPage
|
|
|
|
}
|
|
|
|
page, err := strconv.Atoi(strPage)
|
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `page` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
perPage, err := strconv.Atoi(strPerPage)
|
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `per_page` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
opts := model.QueryBoardsForComplianceOptions{
|
|
|
|
TeamID: teamID,
|
|
|
|
Page: page,
|
|
|
|
PerPage: perPage,
|
|
|
|
}
|
|
|
|
|
|
|
|
boards, more, err := a.app.GetBoardsForCompliance(opts)
|
2022-12-02 18:40:48 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
a.logger.Debug("GetBoardsForCompliance",
|
2022-12-02 18:40:48 +02:00
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.Int("boardsCount", len(boards)),
|
2022-12-24 06:31:39 +02:00
|
|
|
mlog.Bool("hasNext", more),
|
2022-12-02 18:40:48 +02:00
|
|
|
)
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
response := model.BoardsComplianceResponse{
|
|
|
|
HasNext: more,
|
2022-12-06 00:10:03 +02:00
|
|
|
Results: boards,
|
|
|
|
}
|
2022-12-02 18:40:48 +02:00
|
|
|
data, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2022-11-30 23:49:16 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
func (a *API) handleGetBoardsComplianceHistory(w http.ResponseWriter, r *http.Request) {
|
2022-11-30 23:49:16 +02:00
|
|
|
// TODO(@pinjasaur): swagger
|
|
|
|
|
2022-12-02 18:40:48 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
strModifiedSince := query.Get("modified_since") // required, everything else optional
|
|
|
|
includeDeleted := query.Get("include_deleted") == "true"
|
|
|
|
teamID := query.Get("team_id")
|
|
|
|
strPage := query.Get("page")
|
|
|
|
strPerPage := query.Get("per_page")
|
|
|
|
|
|
|
|
if strModifiedSince == "" {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("`modified_since` parameter required"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:49:16 +02:00
|
|
|
// Valid authorization (`manage_system`)?
|
|
|
|
userID := getUserID(r)
|
2022-12-31 22:16:22 +02:00
|
|
|
if !a.permissions.HasPermissionTo(userID, mm_model.PermissionManageSystem) {
|
2022-11-30 23:49:16 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrUnauthorized("access denied Compliance Export getBoardsHistory"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid license feature (Compliance)?
|
|
|
|
license := a.app.GetLicense()
|
|
|
|
if license == nil || !(*license.Features.Compliance) {
|
|
|
|
a.errorResponse(w, r, model.NewErrNotImplemented("insufficient license Compliance Export getBoardsHistory"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-31 23:33:05 +02:00
|
|
|
// check for valid team
|
|
|
|
_, err := a.app.GetTeam(teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("invalid team id: "+teamID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:40:48 +02:00
|
|
|
if strPage == "" {
|
|
|
|
strPage = complianceDefaultPage
|
|
|
|
}
|
|
|
|
if strPerPage == "" {
|
|
|
|
strPerPage = complianceDefaultPerPage
|
|
|
|
}
|
|
|
|
page, err := strconv.Atoi(strPage)
|
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `page` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
perPage, err := strconv.Atoi(strPerPage)
|
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `per_page` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
2022-12-24 06:31:39 +02:00
|
|
|
modifiedSince, err := strconv.ParseInt(strModifiedSince, 10, 64)
|
2022-12-02 18:40:48 +02:00
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `modified_since` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
opts := model.QueryBoardsComplianceHistoryOptions{
|
|
|
|
ModifiedSince: modifiedSince,
|
|
|
|
IncludeDeleted: includeDeleted,
|
|
|
|
TeamID: teamID,
|
|
|
|
Page: page,
|
|
|
|
PerPage: perPage,
|
|
|
|
}
|
|
|
|
|
|
|
|
boards, more, err := a.app.GetBoardsComplianceHistory(opts)
|
2022-12-02 18:40:48 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
a.logger.Debug("GetBoardsComplianceHistory",
|
2022-12-02 18:40:48 +02:00
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.Int("boardsCount", len(boards)),
|
2022-12-24 06:31:39 +02:00
|
|
|
mlog.Bool("hasNext", more),
|
2022-12-02 18:40:48 +02:00
|
|
|
)
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
response := model.BoardsComplianceHistoryResponse{
|
|
|
|
HasNext: more,
|
2022-12-06 00:10:03 +02:00
|
|
|
Results: boards,
|
2022-12-02 18:40:48 +02:00
|
|
|
}
|
|
|
|
data, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2022-11-30 23:49:16 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
func (a *API) handleGetBlocksComplianceHistory(w http.ResponseWriter, r *http.Request) {
|
2022-11-30 23:49:16 +02:00
|
|
|
// TODO(@pinjasaur): swagger
|
|
|
|
|
2022-12-02 18:40:48 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
strModifiedSince := query.Get("modified_since") // required, everything else optional
|
|
|
|
includeDeleted := query.Get("include_deleted") == "true"
|
|
|
|
teamID := query.Get("team_id")
|
|
|
|
boardID := query.Get("board_id")
|
|
|
|
strPage := query.Get("page")
|
|
|
|
strPerPage := query.Get("per_page")
|
|
|
|
|
|
|
|
if strModifiedSince == "" {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("`modified_since` parameter required"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:49:16 +02:00
|
|
|
// Valid authorization (`manage_system`)?
|
|
|
|
userID := getUserID(r)
|
2022-12-31 22:16:22 +02:00
|
|
|
if !a.permissions.HasPermissionTo(userID, mm_model.PermissionManageSystem) {
|
2022-11-30 23:49:16 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrUnauthorized("access denied Compliance Export getBlocksHistory"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid license feature (Compliance)?
|
|
|
|
license := a.app.GetLicense()
|
|
|
|
if license == nil || !(*license.Features.Compliance) {
|
|
|
|
a.errorResponse(w, r, model.NewErrNotImplemented("insufficient license Compliance Export getBlocksHistory"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-31 23:33:05 +02:00
|
|
|
// check for valid team
|
|
|
|
_, err := a.app.GetTeam(teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("invalid team id: "+teamID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 21:16:46 +02:00
|
|
|
// check for valid team
|
|
|
|
_, err = a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("invalid board id: "+boardID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-02 18:40:48 +02:00
|
|
|
if strPage == "" {
|
|
|
|
strPage = complianceDefaultPage
|
|
|
|
}
|
|
|
|
if strPerPage == "" {
|
|
|
|
strPerPage = complianceDefaultPerPage
|
|
|
|
}
|
|
|
|
page, err := strconv.Atoi(strPage)
|
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `page` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
perPage, err := strconv.Atoi(strPerPage)
|
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `per_page` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
2022-12-24 06:31:39 +02:00
|
|
|
modifiedSince, err := strconv.ParseInt(strModifiedSince, 10, 64)
|
2022-12-02 18:40:48 +02:00
|
|
|
if err != nil {
|
|
|
|
message := fmt.Sprintf("invalid `modified_since` parameter: %s", err)
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
opts := model.QueryBlocksComplianceHistoryOptions{
|
|
|
|
ModifiedSince: modifiedSince,
|
|
|
|
IncludeDeleted: includeDeleted,
|
|
|
|
TeamID: teamID,
|
|
|
|
BoardID: boardID,
|
|
|
|
Page: page,
|
|
|
|
PerPage: perPage,
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks, more, err := a.app.GetBlocksComplianceHistory(opts)
|
2022-12-02 18:40:48 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
a.logger.Debug("GetBlocksComplianceHistory",
|
2022-12-02 18:40:48 +02:00
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.Int("blocksCount", len(blocks)),
|
2022-12-24 06:31:39 +02:00
|
|
|
mlog.Bool("hasNext", more),
|
2022-12-02 18:40:48 +02:00
|
|
|
)
|
|
|
|
|
2022-12-24 06:31:39 +02:00
|
|
|
response := model.BlocksComplianceHistoryResponse{
|
|
|
|
HasNext: more,
|
2022-12-06 00:10:03 +02:00
|
|
|
Results: blocks,
|
2022-12-02 18:40:48 +02:00
|
|
|
}
|
|
|
|
data, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2022-11-30 23:49:16 +02:00
|
|
|
}
|