2022-08-10 14:20:42 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-14 18:17:33 +02:00
|
|
|
"io"
|
2022-08-10 14:20:42 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/services/audit"
|
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *API) registerTeamsRoutes(r *mux.Router) {
|
|
|
|
// Team APIs
|
|
|
|
r.HandleFunc("/teams", a.sessionRequired(a.handleGetTeams)).Methods("GET")
|
|
|
|
r.HandleFunc("/teams/{teamID}", a.sessionRequired(a.handleGetTeam)).Methods("GET")
|
|
|
|
r.HandleFunc("/teams/{teamID}/users", a.sessionRequired(a.handleGetTeamUsers)).Methods("GET")
|
2023-02-14 18:17:33 +02:00
|
|
|
r.HandleFunc("/teams/{teamID}/users", a.sessionRequired(a.handleGetTeamUsersByID)).Methods("POST")
|
2022-08-10 14:20:42 +02:00
|
|
|
r.HandleFunc("/teams/{teamID}/archive/export", a.sessionRequired(a.handleArchiveExportTeam)).Methods("GET")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetTeams(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /teams getTeams
|
|
|
|
//
|
|
|
|
// Returns information of all the teams
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Team"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
teams, err := a.app.GetTeamsForUser(userID)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getTeams", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("teamCount", len(teams))
|
|
|
|
|
|
|
|
data, err := json.Marshal(teams)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetTeam(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /teams/{teamID} getTeam
|
|
|
|
//
|
|
|
|
// Returns information of the root team
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Team"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
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 team"))
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var team *model.Team
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if a.MattermostAuth {
|
|
|
|
team, err = a.app.GetTeam(teamID)
|
2022-09-13 12:18:40 +02:00
|
|
|
if model.IsErrNotFound(err) {
|
|
|
|
a.errorResponse(w, r, model.NewErrUnauthorized("invalid team"))
|
2022-08-10 14:20:42 +02:00
|
|
|
}
|
2022-09-13 12:18:40 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
team, err = a.app.GetRootTeam()
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getTeam", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("resultTeamID", team.ID)
|
|
|
|
|
|
|
|
data, err := json.Marshal(team)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handlePostTeamRegenerateSignupToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /teams/{teamID}/regenerate_signup_token regenerateSignupToken
|
|
|
|
//
|
|
|
|
// Regenerates the signup token for the root team
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
if a.MattermostAuth {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in plugin mode"))
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
team, err := a.app.GetRootTeam()
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "regenerateSignupToken", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
team.SignupToken = utils.NewID(utils.IDTypeToken)
|
|
|
|
|
2022-09-13 12:18:40 +02:00
|
|
|
if err = a.app.UpsertTeamSignupToken(*team); err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetTeamUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /teams/{teamID}/users getTeamUsers
|
|
|
|
//
|
|
|
|
// Returns team users
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: search
|
|
|
|
// in: query
|
|
|
|
// description: string to filter users list
|
|
|
|
// required: false
|
|
|
|
// type: string
|
2022-09-10 03:56:44 +02:00
|
|
|
// - name: exclude_bots
|
|
|
|
// in: query
|
|
|
|
// description: exclude bot users
|
|
|
|
// required: false
|
|
|
|
// type: boolean
|
2022-08-10 14:20:42 +02:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
query := r.URL.Query()
|
|
|
|
searchQuery := query.Get("search")
|
2022-09-10 03:56:44 +02:00
|
|
|
excludeBots := r.URL.Query().Get("exclude_bots") == True
|
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 team"))
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getUsers", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
asGuestUser := ""
|
|
|
|
if isGuest {
|
|
|
|
asGuestUser = userID
|
|
|
|
}
|
|
|
|
|
2022-09-10 03:56:44 +02:00
|
|
|
users, err := a.app.SearchTeamUsers(teamID, searchQuery, asGuestUser, excludeBots)
|
2022-08-10 14:20:42 +02:00
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(users)
|
|
|
|
if err != nil {
|
2022-09-13 12:18:40 +02:00
|
|
|
a.errorResponse(w, r, err)
|
2022-08-10 14:20:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.AddMeta("userCount", len(users))
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
2023-02-14 18:17:33 +02:00
|
|
|
|
|
|
|
func (a *API) handleGetTeamUsersByID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /teams/{teamID}/users getTeamUsersByID
|
|
|
|
//
|
|
|
|
// Returns a user[]
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: []UserIDs to return
|
|
|
|
// required: true
|
|
|
|
// type: []string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
requestBody, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var userIDs []string
|
|
|
|
if err = json.Unmarshal(requestBody, &userIDs); err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getTeamUsersByID", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r, model.NewErrPermission("access denied to team"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var users []*model.User
|
|
|
|
var error error
|
|
|
|
|
|
|
|
if len(userIDs) == 0 {
|
|
|
|
a.errorResponse(w, r, model.NewErrBadRequest("User IDs are empty"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if userIDs[0] == model.SingleUser {
|
|
|
|
ws, _ := a.app.GetRootTeam()
|
|
|
|
now := utils.GetMillis()
|
|
|
|
user := &model.User{
|
|
|
|
ID: model.SingleUser,
|
|
|
|
Username: model.SingleUser,
|
|
|
|
Email: model.SingleUser,
|
|
|
|
CreateAt: ws.UpdateAt,
|
|
|
|
UpdateAt: now,
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
} else {
|
|
|
|
users, error = a.app.GetUsersList(userIDs)
|
|
|
|
if error != nil {
|
|
|
|
a.errorResponse(w, r, error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, u := range users {
|
|
|
|
if a.permissions.HasPermissionToTeam(u.ID, teamID, model.PermissionManageTeam) {
|
|
|
|
users[i].Permissions = append(users[i].Permissions, model.PermissionManageTeam.Id)
|
|
|
|
}
|
|
|
|
if a.permissions.HasPermissionTo(u.ID, model.PermissionManageSystem) {
|
|
|
|
users[i].Permissions = append(users[i].Permissions, model.PermissionManageSystem.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usersList, err := json.Marshal(users)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonStringResponse(w, http.StatusOK, string(usersList))
|
|
|
|
auditRec.Success()
|
|
|
|
}
|