2020-05-18 21:37:18 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2020-05-18 21:37:18 +01:00
|
|
|
"net/http"
|
2022-04-19 14:07:44 +01:00
|
|
|
"strconv"
|
2020-05-18 21:37:18 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (server *Server) addUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2020-05-18 21:37:18 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to read body",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var input console.CreateUser
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &input)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to unmarshal request",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:03:28 +01:00
|
|
|
user := console.CreateUser{
|
2021-10-26 14:30:19 +01:00
|
|
|
Email: input.Email,
|
|
|
|
FullName: input.FullName,
|
|
|
|
Password: input.Password,
|
|
|
|
SignupPromoCode: input.SignupPromoCode,
|
2021-10-01 13:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = user.IsValid()
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "user data is not valid",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-16 13:17:30 +01:00
|
|
|
existingUser, err := server.db.Console().Users().GetByEmail(ctx, input.Email)
|
|
|
|
if err != nil && !errors.Is(sql.ErrNoRows, err) {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to check for user email",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-16 13:17:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if existingUser != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, fmt.Sprintf("user with email already exists %s", input.Email),
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusConflict)
|
2020-07-16 13:17:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:37:18 +01:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(input.Password), 0)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to save password hash",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userID, err := uuid.New()
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to create UUID",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-27 21:13:01 +01:00
|
|
|
newUser, err := server.db.Console().Users().Insert(ctx, &console.User{
|
2021-11-01 15:27:32 +00:00
|
|
|
ID: userID,
|
|
|
|
FullName: user.FullName,
|
|
|
|
ShortName: user.ShortName,
|
|
|
|
Email: user.Email,
|
|
|
|
PasswordHash: hash,
|
2022-05-27 21:13:01 +01:00
|
|
|
ProjectLimit: server.console.DefaultProjectLimit,
|
|
|
|
ProjectStorageLimit: server.console.UsageLimits.Storage.Free.Int64(),
|
|
|
|
ProjectBandwidthLimit: server.console.UsageLimits.Bandwidth.Free.Int64(),
|
2021-10-26 14:30:19 +01:00
|
|
|
SignupPromoCode: user.SignupPromoCode,
|
2020-05-18 21:37:18 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to insert user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
2020-07-16 12:58:40 +01:00
|
|
|
|
2022-05-27 21:13:01 +01:00
|
|
|
_, err = server.payments.Setup(ctx, newUser.ID, newUser.Email, newUser.SignupPromoCode)
|
2020-07-16 12:58:40 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to create payment account for user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-16 12:58:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
// Set User Status to be activated, as we manually created it
|
2022-05-27 21:13:01 +01:00
|
|
|
newUser.Status = console.Active
|
2022-06-01 22:15:37 +01:00
|
|
|
err = server.db.Console().Users().Update(ctx, userID, console.UpdateUserRequest{
|
|
|
|
Status: &newUser.Status,
|
|
|
|
})
|
2020-05-18 21:37:18 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to activate user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-27 21:13:01 +01:00
|
|
|
data, err := json.Marshal(newUser)
|
2020-05-18 21:37:18 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "json encoding failed",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONData(w, http.StatusOK, data)
|
2020-05-18 21:37:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) userInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "user-email missing",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-10-07 11:42:25 +01:00
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusNotFound)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to get user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
user.PasswordHash = nil
|
|
|
|
|
2022-06-22 11:09:30 +01:00
|
|
|
projects, err := server.db.Console().Projects().GetOwn(ctx, user.ID)
|
2020-05-18 21:37:18 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to get user projects",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
2020-10-02 23:40:15 +01:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
FullName string `json:"fullName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
ProjectLimit int `json:"projectLimit"`
|
2020-05-18 21:37:18 +01:00
|
|
|
}
|
|
|
|
type Project struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
OwnerID uuid.UUID `json:"ownerId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var output struct {
|
2021-08-27 01:51:26 +01:00
|
|
|
User User `json:"user"`
|
|
|
|
Projects []Project `json:"projects"`
|
2020-05-18 21:37:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
output.User = User{
|
2020-10-02 23:40:15 +01:00
|
|
|
ID: user.ID,
|
|
|
|
FullName: user.FullName,
|
|
|
|
Email: user.Email,
|
|
|
|
ProjectLimit: user.ProjectLimit,
|
2020-05-18 21:37:18 +01:00
|
|
|
}
|
|
|
|
for _, p := range projects {
|
|
|
|
output.Projects = append(output.Projects, Project{
|
|
|
|
ID: p.ID,
|
|
|
|
Name: p.Name,
|
|
|
|
Description: p.Description,
|
|
|
|
OwnerID: p.OwnerID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(output)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "json encoding failed",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-18 21:37:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONData(w, http.StatusOK, data)
|
2020-05-18 21:37:18 +01:00
|
|
|
}
|
2020-05-20 10:20:04 +01:00
|
|
|
|
2023-02-02 22:58:17 +00:00
|
|
|
func (server *Server) userLimits(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
|
|
|
sendJSONError(w, "user-email missing",
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
|
|
|
"", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to get user",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.PasswordHash = nil
|
|
|
|
|
|
|
|
var limits struct {
|
|
|
|
Storage int64 `json:"storage"`
|
|
|
|
Bandwidth int64 `json:"bandwidth"`
|
|
|
|
Segment int64 `json:"maxSegments"`
|
|
|
|
}
|
|
|
|
|
|
|
|
limits.Storage = user.ProjectStorageLimit
|
|
|
|
limits.Bandwidth = user.ProjectBandwidthLimit
|
|
|
|
limits.Segment = user.ProjectSegmentLimit
|
|
|
|
|
|
|
|
data, err := json.Marshal(limits)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "json encoding failed",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendJSONData(w, http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:20:04 +01:00
|
|
|
func (server *Server) updateUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "user-email missing",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusBadRequest)
|
2020-05-20 10:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-10-07 11:42:25 +01:00
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusNotFound)
|
2020-05-20 10:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to get user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-20 10:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2020-05-20 10:20:04 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to read body",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-20 10:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:07:44 +01:00
|
|
|
type UserWithPaidTier struct {
|
|
|
|
console.User
|
|
|
|
PaidTierStr string `json:"paidTierStr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var input UserWithPaidTier
|
2020-05-20 10:20:04 +01:00
|
|
|
|
|
|
|
err = json.Unmarshal(body, &input)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to unmarshal request",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusBadRequest)
|
2020-05-20 10:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest := console.UpdateUserRequest{}
|
|
|
|
|
2020-05-20 10:20:04 +01:00
|
|
|
if input.FullName != "" {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.FullName = &input.FullName
|
2020-05-20 10:20:04 +01:00
|
|
|
}
|
|
|
|
if input.ShortName != "" {
|
2022-06-01 22:15:37 +01:00
|
|
|
shortNamePtr := &input.ShortName
|
|
|
|
updateRequest.ShortName = &shortNamePtr
|
2020-05-20 10:20:04 +01:00
|
|
|
}
|
|
|
|
if input.Email != "" {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.Email = &input.Email
|
2020-05-20 10:20:04 +01:00
|
|
|
}
|
|
|
|
if len(input.PasswordHash) > 0 {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.PasswordHash = input.PasswordHash
|
2020-05-20 10:20:04 +01:00
|
|
|
}
|
2020-10-02 23:40:15 +01:00
|
|
|
if input.ProjectLimit > 0 {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.ProjectLimit = &input.ProjectLimit
|
2020-10-02 23:40:15 +01:00
|
|
|
}
|
2022-04-19 14:07:44 +01:00
|
|
|
if input.ProjectStorageLimit > 0 {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.ProjectStorageLimit = &input.ProjectStorageLimit
|
2022-04-19 14:07:44 +01:00
|
|
|
}
|
|
|
|
if input.ProjectBandwidthLimit > 0 {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.ProjectBandwidthLimit = &input.ProjectBandwidthLimit
|
2022-04-19 14:07:44 +01:00
|
|
|
}
|
|
|
|
if input.ProjectSegmentLimit > 0 {
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.ProjectSegmentLimit = &input.ProjectSegmentLimit
|
2022-04-19 14:07:44 +01:00
|
|
|
}
|
|
|
|
if input.PaidTierStr != "" {
|
|
|
|
status, err := strconv.ParseBool(input.PaidTierStr)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to parse paid tier status",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
updateRequest.PaidTier = &status
|
2022-04-19 14:07:44 +01:00
|
|
|
}
|
2020-05-20 10:20:04 +01:00
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
err = server.db.Console().Users().Update(ctx, user.ID, updateRequest)
|
2020-05-20 10:20:04 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to update user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-05-20 10:20:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 22:31:40 +01:00
|
|
|
|
2023-02-02 22:58:17 +00:00
|
|
|
// updateLimits updates user limits and all project limits for that user (future and existing).
|
|
|
|
func (server *Server) updateLimits(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
|
|
|
sendJSONError(w, "user-email missing",
|
|
|
|
"", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
|
|
|
"", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to get user",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to read body",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
console.User
|
|
|
|
}
|
|
|
|
|
|
|
|
var input User
|
|
|
|
|
|
|
|
err = json.Unmarshal(body, &input)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to unmarshal request",
|
|
|
|
err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRequest := console.UpdateUserRequest{}
|
|
|
|
|
|
|
|
if input.ProjectStorageLimit > 0 {
|
|
|
|
updateRequest.ProjectStorageLimit = &input.ProjectStorageLimit
|
|
|
|
}
|
|
|
|
if input.ProjectBandwidthLimit > 0 {
|
|
|
|
updateRequest.ProjectBandwidthLimit = &input.ProjectBandwidthLimit
|
|
|
|
}
|
|
|
|
if input.ProjectSegmentLimit > 0 {
|
|
|
|
updateRequest.ProjectSegmentLimit = &input.ProjectSegmentLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
userLimits := console.UsageLimits{
|
|
|
|
Storage: *updateRequest.ProjectStorageLimit,
|
|
|
|
Bandwidth: *updateRequest.ProjectBandwidthLimit,
|
|
|
|
Segment: *updateRequest.ProjectSegmentLimit,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.db.Console().Users().UpdateUserProjectLimits(ctx, user.ID, userLimits)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to update user limits",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
userProjects, err := server.db.Console().Projects().GetOwn(ctx, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to get user's projects",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range userProjects {
|
|
|
|
err = server.db.Console().Projects().UpdateUsageLimits(ctx, p.ID, userLimits)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to update project limits",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-27 23:04:12 +01:00
|
|
|
func (server *Server) disableUserMFA(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
|
|
|
sendJSONError(w, "user-email missing", "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
|
|
|
"", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to get user details",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.MFAEnabled = false
|
|
|
|
user.MFASecretKey = ""
|
2022-06-01 22:15:37 +01:00
|
|
|
mfaSecretKeyPtr := &user.MFASecretKey
|
|
|
|
var mfaRecoveryCodes []string
|
2022-05-27 23:04:12 +01:00
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
err = server.db.Console().Users().Update(ctx, user.ID, console.UpdateUserRequest{
|
|
|
|
MFAEnabled: &user.MFAEnabled,
|
|
|
|
MFASecretKey: &mfaSecretKeyPtr,
|
|
|
|
MFARecoveryCodes: &mfaRecoveryCodes,
|
|
|
|
})
|
2022-05-27 23:04:12 +01:00
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to disable mfa",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2022-12-16 15:17:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) freezeUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
|
|
|
sendJSONError(w, "user-email missing", "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
|
|
|
"", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendJSONError(w, "failed to get user details",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.freezeAccounts.FreezeUser(ctx, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
sendJSONError(w, "failed to freeze user",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) unfreezeUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
|
|
|
sendJSONError(w, "user-email missing", "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
|
|
|
"", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendJSONError(w, "failed to get user details",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = server.freezeAccounts.UnfreezeUser(ctx, u.ID); err != nil {
|
|
|
|
sendJSONError(w, "failed to unfreeze user",
|
|
|
|
err.Error(), http.StatusInternalServerError)
|
2022-05-27 23:04:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 22:31:40 +01:00
|
|
|
func (server *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userEmail, ok := vars["useremail"]
|
|
|
|
if !ok {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "user-email missing", "", http.StatusBadRequest)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := server.db.Console().Users().GetByEmail(ctx, userEmail)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-10-07 11:42:25 +01:00
|
|
|
sendJSONError(w, fmt.Sprintf("user with email %q does not exist", userEmail),
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusNotFound)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "failed to get user details",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure user has no own projects any longer
|
|
|
|
projects, err := server.db.Console().Projects().GetByUserID(ctx, user.ID)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to list projects",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(projects) > 0 {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "some projects still exist",
|
2020-08-13 13:40:05 +01:00
|
|
|
fmt.Sprintf("%v", projects), http.StatusConflict)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete memberships in foreign projects
|
|
|
|
members, err := server.db.Console().ProjectMembers().GetByMemberID(ctx, user.ID)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to search for user project memberships",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(members) > 0 {
|
|
|
|
for _, project := range members {
|
|
|
|
err := server.db.Console().ProjectMembers().Delete(ctx, user.ID, project.ProjectID)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to delete user project membership",
|
2020-08-13 13:40:05 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure no unpaid invoices exist.
|
2021-10-18 23:18:18 +01:00
|
|
|
invoices, err := server.payments.Invoices().List(ctx, user.ID)
|
2020-07-06 22:31:40 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to list user invoices",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(invoices) > 0 {
|
|
|
|
for _, invoice := range invoices {
|
2020-12-23 15:40:44 +00:00
|
|
|
if invoice.Status == "draft" || invoice.Status == "open" {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "user has unpaid/pending invoices",
|
2020-08-13 13:40:05 +01:00
|
|
|
"", http.StatusConflict)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:58:40 +01:00
|
|
|
hasItems, err := server.payments.Invoices().CheckPendingItems(ctx, user.ID)
|
2020-07-06 22:31:40 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to list pending invoice items",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if hasItems {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "user has pending invoice items",
|
2020-08-05 14:13:11 +01:00
|
|
|
"", http.StatusConflict)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
emptyName := ""
|
|
|
|
emptyNamePtr := &emptyName
|
|
|
|
deactivatedEmail := fmt.Sprintf("deactivated+%s@storj.io", user.ID.String())
|
|
|
|
status := console.Deleted
|
2020-07-06 22:31:40 +01:00
|
|
|
|
2022-06-01 22:15:37 +01:00
|
|
|
err = server.db.Console().Users().Update(ctx, user.ID, console.UpdateUserRequest{
|
|
|
|
FullName: &emptyName,
|
|
|
|
ShortName: &emptyNamePtr,
|
|
|
|
Email: &deactivatedEmail,
|
|
|
|
Status: &status,
|
|
|
|
})
|
2020-07-06 22:31:40 +01:00
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to delete user",
|
2020-08-05 14:13:11 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
2020-07-06 22:31:40 +01:00
|
|
|
return
|
|
|
|
}
|
2020-08-19 13:43:56 +01:00
|
|
|
|
|
|
|
err = server.payments.CreditCards().RemoveAll(ctx, user.ID)
|
|
|
|
if err != nil {
|
2021-10-01 12:50:21 +01:00
|
|
|
sendJSONError(w, "unable to delete credit card(s) from stripe account",
|
2020-08-19 13:43:56 +01:00
|
|
|
err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2020-07-06 22:31:40 +01:00
|
|
|
}
|