2022-09-13 13:59:15 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
2023-06-28 14:06:32 +01:00
|
|
|
"context"
|
2022-09-13 13:59:15 +01:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2023-08-07 14:16:04 +01:00
|
|
|
"strconv"
|
2023-07-10 05:28:18 +01:00
|
|
|
"strings"
|
2023-06-01 08:27:09 +01:00
|
|
|
"time"
|
2022-09-13 13:59:15 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2022-09-13 13:59:15 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Projects is an api controller that exposes projects related functionality.
|
|
|
|
type Projects struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProjects is a constructor for api analytics controller.
|
|
|
|
func NewProjects(log *zap.Logger, service *console.Service) *Projects {
|
|
|
|
return &Projects{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:44:03 +01:00
|
|
|
// GetUserProjects returns the user's projects.
|
|
|
|
func (p *Projects) GetUserProjects(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
projects, err := p.service.GetUsersProjects(ctx)
|
|
|
|
if err != nil {
|
2023-08-07 14:16:04 +01:00
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:44:03 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-07 14:16:04 +01:00
|
|
|
response := make([]console.ProjectInfo, 0)
|
2023-08-02 22:44:03 +01:00
|
|
|
for _, project := range projects {
|
2023-08-07 14:16:04 +01:00
|
|
|
response = append(response, project.GetMinimal())
|
2023-08-02 22:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 14:16:04 +01:00
|
|
|
// GetPagedProjects returns paged projects for a user.
|
|
|
|
func (p *Projects) GetPagedProjects(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitParam := query.Get("limit")
|
|
|
|
if limitParam == "" {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'limit' is required"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
limit, err := strconv.ParseUint(limitParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageParam := query.Get("page")
|
|
|
|
if pageParam == "" {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'page' is required"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
page, err := strconv.ParseUint(pageParam, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if page == 0 {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("parameter 'page' can not be 0"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor := console.ProjectsCursor{
|
|
|
|
Limit: int(limit),
|
|
|
|
Page: int(page),
|
|
|
|
}
|
|
|
|
|
|
|
|
projectsPage, err := p.service.GetUsersOwnedProjectsPage(ctx, cursor)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageToSend := console.ProjectInfoPage{
|
|
|
|
Limit: projectsPage.Limit,
|
|
|
|
Offset: projectsPage.Offset,
|
|
|
|
PageCount: projectsPage.PageCount,
|
|
|
|
CurrentPage: projectsPage.CurrentPage,
|
|
|
|
TotalCount: projectsPage.TotalCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, project := range projectsPage.Projects {
|
|
|
|
pageToSend.Projects = append(pageToSend.Projects, project.GetMinimal())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(pageToSend)
|
|
|
|
if err != nil {
|
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:59:15 +01:00
|
|
|
// GetSalt returns the project's salt.
|
|
|
|
func (p *Projects) GetSalt(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
idParam, ok := mux.Vars(r)["id"]
|
|
|
|
if !ok {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing id route param"))
|
2022-09-13 13:59:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2022-09-13 13:59:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
salt, err := p.service.GetSalt(ctx, id)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
2022-09-13 13:59:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b64SaltString := base64.StdEncoding.EncodeToString(salt)
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(b64SaltString)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2022-09-13 13:59:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:13:39 +01:00
|
|
|
// InviteUsers sends invites to a given project(id) to the given users (emails).
|
|
|
|
func (p *Projects) InviteUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
idParam, ok := mux.Vars(r)["id"]
|
|
|
|
if !ok {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing project id route param"))
|
2023-06-07 22:13:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-06-07 22:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Emails []string `json:"emails"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-06-07 22:13:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-10 05:28:18 +01:00
|
|
|
for i, email := range data.Emails {
|
|
|
|
data.Emails[i] = strings.TrimSpace(email)
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:13:39 +01:00
|
|
|
_, err = p.service.InviteProjectMembers(ctx, id, data.Emails)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-07 22:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:44:15 +01:00
|
|
|
// GetInviteLink returns a link to an invitation given project ID and invitee's email.
|
|
|
|
func (p *Projects) GetInviteLink(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
idParam, ok := mux.Vars(r)["id"]
|
|
|
|
if !ok {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing project id route param"))
|
2023-06-21 12:44:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-06-21 12:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
email := r.URL.Query().Get("email")
|
|
|
|
if email == "" {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing email query param"))
|
2023-06-21 12:44:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := p.service.GetInviteLink(ctx, id, email)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-21 12:44:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(link)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-21 12:44:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:27:09 +01:00
|
|
|
// GetUserInvitations returns the user's pending project member invitations.
|
|
|
|
func (p *Projects) GetUserInvitations(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
invites, err := p.service.GetUserProjectInvitations(ctx)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonInvite struct {
|
|
|
|
ProjectID uuid.UUID `json:"projectID"`
|
|
|
|
ProjectName string `json:"projectName"`
|
|
|
|
ProjectDescription string `json:"projectDescription"`
|
|
|
|
InviterEmail string `json:"inviterEmail"`
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]jsonInvite, 0)
|
|
|
|
|
|
|
|
for _, invite := range invites {
|
|
|
|
proj, err := p.service.GetProjectNoAuth(ctx, invite.ProjectID)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respInvite := jsonInvite{
|
|
|
|
ProjectID: proj.PublicID,
|
|
|
|
ProjectName: proj.Name,
|
|
|
|
ProjectDescription: proj.Description,
|
|
|
|
CreatedAt: invite.CreatedAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if invite.InviterID != nil {
|
|
|
|
inviter, err := p.service.GetUser(ctx, *invite.InviterID)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
respInvite.InviterEmail = inviter.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
response = append(response, respInvite)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RespondToInvitation handles accepting or declining a user's project member invitation.
|
|
|
|
func (p *Projects) RespondToInvitation(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
var idParam string
|
|
|
|
|
|
|
|
if idParam, ok = mux.Vars(r)["id"]; !ok {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing project id route param"))
|
2023-06-01 08:27:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Response console.ProjectInvitationResponse `json:"response"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&payload)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.service.RespondToProjectInvitation(ctx, id, payload.Response)
|
|
|
|
if err != nil {
|
|
|
|
status := http.StatusInternalServerError
|
|
|
|
switch {
|
|
|
|
case console.ErrAlreadyMember.Has(err):
|
|
|
|
status = http.StatusConflict
|
|
|
|
case console.ErrProjectInviteInvalid.Has(err):
|
|
|
|
status = http.StatusNotFound
|
|
|
|
case console.ErrValidation.Has(err):
|
|
|
|
status = http.StatusBadRequest
|
|
|
|
}
|
2023-06-28 14:06:32 +01:00
|
|
|
p.serveJSONError(ctx, w, status, err)
|
2023-06-01 08:27:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:59:15 +01:00
|
|
|
// serveJSONError writes JSON error to response output stream.
|
2023-06-28 14:06:32 +01:00
|
|
|
func (p *Projects) serveJSONError(ctx context.Context, w http.ResponseWriter, status int, err error) {
|
|
|
|
web.ServeJSONError(ctx, p.log, w, status, err)
|
2022-09-13 13:59:15 +01:00
|
|
|
}
|