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"
|
|
|
|
"io/ioutil"
|
|
|
|
"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()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
ProjectLimit: server.config.ConsoleConfig.DefaultProjectLimit,
|
|
|
|
ProjectStorageLimit: server.config.ConsoleConfig.UsageLimits.Storage.Free.Int64(),
|
|
|
|
ProjectBandwidthLimit: server.config.ConsoleConfig.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
|
|
|
|
2021-10-26 14:30:19 +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
|
2020-05-18 21:37:18 +01:00
|
|
|
newuser.Status = console.Active
|
|
|
|
newuser.PasswordHash = nil
|
|
|
|
err = server.db.Console().Users().Update(ctx, newuser)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(newuser)
|
|
|
|
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
|
|
|
|
|
|
|
|
projects, err := server.db.Console().Projects().GetByUserID(ctx, user.ID)
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.FullName != "" {
|
|
|
|
user.FullName = input.FullName
|
|
|
|
}
|
|
|
|
if input.ShortName != "" {
|
|
|
|
user.ShortName = input.ShortName
|
|
|
|
}
|
|
|
|
if input.Email != "" {
|
|
|
|
user.Email = input.Email
|
|
|
|
}
|
|
|
|
if !input.PartnerID.IsZero() {
|
|
|
|
user.PartnerID = input.PartnerID
|
|
|
|
}
|
|
|
|
if len(input.PasswordHash) > 0 {
|
|
|
|
user.PasswordHash = input.PasswordHash
|
|
|
|
}
|
2020-10-02 23:40:15 +01:00
|
|
|
if input.ProjectLimit > 0 {
|
|
|
|
user.ProjectLimit = input.ProjectLimit
|
|
|
|
}
|
2022-04-19 14:07:44 +01:00
|
|
|
if input.ProjectStorageLimit > 0 {
|
|
|
|
user.ProjectStorageLimit = input.ProjectStorageLimit
|
|
|
|
}
|
|
|
|
if input.ProjectBandwidthLimit > 0 {
|
|
|
|
user.ProjectBandwidthLimit = input.ProjectBandwidthLimit
|
|
|
|
}
|
|
|
|
if input.ProjectSegmentLimit > 0 {
|
|
|
|
user.ProjectSegmentLimit = input.ProjectSegmentLimit
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
user.PaidTier = status
|
|
|
|
}
|
2020-05-20 10:20:04 +01:00
|
|
|
|
|
|
|
err = server.db.Console().Users().Update(ctx, user)
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
userInfo := &console.User{
|
|
|
|
ID: user.ID,
|
|
|
|
FullName: "",
|
|
|
|
ShortName: "",
|
|
|
|
Email: fmt.Sprintf("deactivated+%s@storj.io", user.ID.String()),
|
|
|
|
Status: console.Deleted,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.db.Console().Users().Update(ctx, userInfo)
|
|
|
|
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
|
|
|
}
|