2022-02-02 02:01:29 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
"github.com/gorilla/mux"
|
2022-02-02 02:01:29 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/services/audit"
|
2022-03-31 20:04:20 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2022-02-02 02:01:29 +02:00
|
|
|
)
|
|
|
|
|
2022-02-02 20:25:06 +02:00
|
|
|
const (
|
|
|
|
archiveExtension = ".boardarchive"
|
|
|
|
)
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
func (a *API) registerAchivesRoutes(r *mux.Router) {
|
|
|
|
// Archive APIs
|
|
|
|
r.HandleFunc("/boards/{boardID}/archive/export", a.sessionRequired(a.handleArchiveExportBoard)).Methods("GET")
|
|
|
|
r.HandleFunc("/teams/{teamID}/archive/import", a.sessionRequired(a.handleArchiveImport)).Methods("POST")
|
|
|
|
r.HandleFunc("/teams/{teamID}/archive/export", a.sessionRequired(a.handleArchiveExportTeam)).Methods("GET")
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
func (a *API) handleArchiveExportBoard(w http.ResponseWriter, r *http.Request) {
|
2022-04-13 22:24:32 +02:00
|
|
|
// swagger:operation GET /boards/{boardID}/archive/export archiveExportBoard
|
2022-02-02 02:01:29 +02:00
|
|
|
//
|
2022-03-22 16:24:34 +02:00
|
|
|
// Exports an archive of all blocks for one boards.
|
2022-02-02 02:01:29 +02:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 16:24:34 +02:00
|
|
|
// - name: boardID
|
2022-02-02 02:01:29 +02:00
|
|
|
// in: path
|
2022-03-22 16:24:34 +02:00
|
|
|
// description: Id of board to export
|
2022-02-02 02:01:29 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// content:
|
|
|
|
// application-octet-stream:
|
|
|
|
// type: string
|
|
|
|
// format: binary
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
boardID := vars["boardID"]
|
2022-04-05 17:00:04 +02:00
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrPermission("access denied to board"))
|
2022-04-05 17:00:04 +02:00
|
|
|
return
|
|
|
|
}
|
2022-03-22 16:24:34 +02:00
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "archiveExportBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("BoardID", boardID)
|
|
|
|
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
2022-02-02 02:01:29 +02:00
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-03-22 16:24:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := model.ExportArchiveOptions{
|
|
|
|
TeamID: board.TeamID,
|
|
|
|
BoardIDs: []string{board.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := fmt.Sprintf("archive-%s%s", time.Now().Format("2006-01-02"), archiveExtension)
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
|
|
|
|
if err := a.app.ExportArchive(w, opts); err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-03-22 16:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
func (a *API) handleArchiveImport(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /teams/{teamID}/archive/import archiveImport
|
2022-03-22 16:24:34 +02:00
|
|
|
//
|
2022-08-10 14:20:42 +02:00
|
|
|
// Import an archive of boards.
|
2022-03-22 16:24:34 +02:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2022-08-10 14:20:42 +02:00
|
|
|
// consumes:
|
|
|
|
// - multipart/form-data
|
2022-03-22 16:24:34 +02:00
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
2022-08-10 14:20:42 +02:00
|
|
|
// description: Team ID
|
2022-03-22 16:24:34 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-08-10 14:20:42 +02:00
|
|
|
// - name: file
|
|
|
|
// in: formData
|
|
|
|
// description: archive file to import
|
|
|
|
// required: true
|
|
|
|
// type: file
|
2022-03-22 16:24:34 +02:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session, _ := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
2022-02-02 02:01:29 +02:00
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrPermission("access denied to create board"))
|
2022-03-22 16:24:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 00:08:58 +02:00
|
|
|
isGuest, err := a.userIsGuest(userID)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-25 00:08:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if isGuest {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrPermission("access denied to create board"))
|
2022-08-25 00:08:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
file, handle, err := r.FormFile(UploadFormFileKey)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "%v", err)
|
|
|
|
return
|
2022-02-02 02:01:29 +02:00
|
|
|
}
|
2022-08-10 14:20:42 +02:00
|
|
|
defer file.Close()
|
2022-02-02 02:01:29 +02:00
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "import", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("filename", handle.Filename)
|
|
|
|
auditRec.AddMeta("size", handle.Size)
|
2022-02-02 02:01:29 +02:00
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
opt := model.ImportArchiveOptions{
|
|
|
|
TeamID: teamID,
|
|
|
|
ModifiedBy: userID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := a.app.ImportArchive(file, opt); err != nil {
|
|
|
|
a.logger.Debug("Error importing archive",
|
|
|
|
mlog.String("team_id", teamID),
|
|
|
|
mlog.Err(err),
|
|
|
|
)
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
2022-02-02 02:01:29 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
2022-02-02 02:01:29 +02:00
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
func (a *API) handleArchiveExportTeam(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /teams/{teamID}/archive/export archiveExportTeam
|
2022-02-02 02:01:29 +02:00
|
|
|
//
|
2022-08-10 14:20:42 +02:00
|
|
|
// Exports an archive of all blocks for all the boards in a team.
|
2022-02-02 02:01:29 +02:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-31 20:04:20 +02:00
|
|
|
// - name: teamID
|
2022-02-02 02:01:29 +02:00
|
|
|
// in: path
|
2022-08-10 14:20:42 +02:00
|
|
|
// description: Id of team
|
2022-02-02 02:01:29 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-08-10 14:20:42 +02:00
|
|
|
// content:
|
|
|
|
// application-octet-stream:
|
|
|
|
// type: string
|
|
|
|
// format: binary
|
2022-02-02 02:01:29 +02:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2022-08-10 14:20:42 +02:00
|
|
|
if a.MattermostAuth {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in plugin mode"))
|
|
|
|
return
|
2022-08-10 14:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
2022-02-02 02:01:29 +02:00
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session, _ := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "archiveExportTeam", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("TeamID", teamID)
|
2022-04-05 17:00:04 +02:00
|
|
|
|
2022-08-25 00:08:58 +02:00
|
|
|
isGuest, err := a.userIsGuest(userID)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-25 00:08:58 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
boards, err := a.app.GetBoardsForUserAndTeam(userID, teamID, !isGuest)
|
2022-02-02 02:01:29 +02:00
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-02-02 02:01:29 +02:00
|
|
|
return
|
|
|
|
}
|
2022-08-10 14:20:42 +02:00
|
|
|
ids := []string{}
|
|
|
|
for _, board := range boards {
|
|
|
|
ids = append(ids, board.ID)
|
|
|
|
}
|
2022-02-02 02:01:29 +02:00
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
opts := model.ExportArchiveOptions{
|
|
|
|
TeamID: teamID,
|
|
|
|
BoardIDs: ids,
|
2022-02-02 02:01:29 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 14:20:42 +02:00
|
|
|
filename := fmt.Sprintf("archive-%s%s", time.Now().Format("2006-01-02"), archiveExtension)
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
|
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
|
|
|
|
if err := a.app.ExportArchive(w, opts); err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-02-02 02:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|