2018-11-15 12:00:08 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
package satelliteql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
2018-11-26 10:47:23 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2018-12-20 15:36:32 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/satellite"
|
2018-12-06 14:40:32 +00:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Mutation is graphql request that modifies data
|
|
|
|
Mutation = "mutation"
|
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
createUserMutation = "createUser"
|
|
|
|
updateUserMutation = "updateUser"
|
|
|
|
deleteUserMutation = "deleteUser"
|
|
|
|
changeUserPasswordMutation = "changeUserPassword"
|
2018-11-27 14:20:58 +00:00
|
|
|
|
2018-11-28 16:20:23 +00:00
|
|
|
createProjectMutation = "createProject"
|
|
|
|
deleteProjectMutation = "deleteProject"
|
|
|
|
updateProjectDescriptionMutation = "updateProjectDescription"
|
2018-11-21 15:51:43 +00:00
|
|
|
|
2018-12-06 14:40:32 +00:00
|
|
|
addProjectMemberMutation = "addProjectMember"
|
|
|
|
deleteProjectMemberMutation = "deleteProjectMember"
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
input = "input"
|
2018-12-06 14:40:32 +00:00
|
|
|
|
|
|
|
fieldProjectID = "projectID"
|
2018-12-10 15:57:06 +00:00
|
|
|
|
|
|
|
fieldNewPassword = "newPassword"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rootMutation creates mutation for graphql populated by AccountsClient
|
|
|
|
func rootMutation(service *satellite.Service, types Types) *graphql.Object {
|
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: Mutation,
|
|
|
|
Fields: graphql.Fields{
|
2018-11-21 15:51:43 +00:00
|
|
|
createUserMutation: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
2018-11-14 10:50:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2018-11-21 15:51:43 +00:00
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.UserInput()),
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-21 15:51:43 +00:00
|
|
|
// creates user and company from input params and returns userID if succeed
|
2018-11-14 10:50:15 +00:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-12-10 15:57:06 +00:00
|
|
|
input, _ := p.Args[input].(map[string]interface{})
|
|
|
|
createUser := fromMapCreateUser(input)
|
2018-11-14 10:50:15 +00:00
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
user, err := service.CreateUser(p.Context, createUser)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
2018-11-21 15:51:43 +00:00
|
|
|
return "", err
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
return user.ID.String(), nil
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-28 10:31:15 +00:00
|
|
|
updateUserMutation: &graphql.Field{
|
|
|
|
Type: types.User(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
2018-12-11 15:54:45 +00:00
|
|
|
Type: graphql.String,
|
2018-11-28 10:31:15 +00:00
|
|
|
},
|
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.UserInput()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-12-11 15:54:45 +00:00
|
|
|
id, err := uuidIDAuthFallback(p, fieldID)
|
2018-11-28 10:31:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:54:45 +00:00
|
|
|
input, _ := p.Args[input].(map[string]interface{})
|
|
|
|
|
|
|
|
user, err := service.GetUser(p.Context, *id)
|
2018-11-28 10:31:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedUser := *user
|
2018-11-28 12:30:38 +00:00
|
|
|
info := fillUserInfo(&updatedUser, input)
|
2018-11-28 10:31:15 +00:00
|
|
|
|
2018-12-11 15:54:45 +00:00
|
|
|
err = service.UpdateUser(p.Context, *id, info)
|
2018-11-28 12:30:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return user, err
|
2018-11-28 10:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &updatedUser, nil
|
|
|
|
},
|
|
|
|
},
|
2018-12-10 15:57:06 +00:00
|
|
|
changeUserPasswordMutation: &graphql.Field{
|
|
|
|
Type: types.User(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
2018-12-11 15:54:45 +00:00
|
|
|
Type: graphql.String,
|
2018-12-10 15:57:06 +00:00
|
|
|
},
|
|
|
|
fieldPassword: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
fieldNewPassword: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-12-11 15:54:45 +00:00
|
|
|
id, err := uuidIDAuthFallback(p, fieldID)
|
2018-12-10 15:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:54:45 +00:00
|
|
|
pass, _ := p.Args[fieldPassword].(string)
|
|
|
|
newPass, _ := p.Args[fieldNewPassword].(string)
|
|
|
|
|
|
|
|
err = service.ChangeUserPassword(p.Context, *id, pass, newPass)
|
|
|
|
user, getErr := service.GetUser(p.Context, *id)
|
2018-12-10 15:57:06 +00:00
|
|
|
return user, utils.CombineErrors(err, getErr)
|
|
|
|
},
|
|
|
|
},
|
2018-11-27 14:20:58 +00:00
|
|
|
deleteUserMutation: &graphql.Field{
|
|
|
|
Type: types.User(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
2018-12-11 15:54:45 +00:00
|
|
|
Type: graphql.String,
|
2018-11-27 14:20:58 +00:00
|
|
|
},
|
2018-12-14 16:14:17 +00:00
|
|
|
fieldPassword: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2018-11-27 14:20:58 +00:00
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-12-11 15:54:45 +00:00
|
|
|
id, err := uuidIDAuthFallback(p, fieldID)
|
2018-11-27 14:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-14 16:14:17 +00:00
|
|
|
password, _ := p.Args[fieldPassword].(string)
|
|
|
|
|
2018-12-11 15:54:45 +00:00
|
|
|
user, err := service.GetUser(p.Context, *id)
|
2018-11-27 14:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-14 16:14:17 +00:00
|
|
|
err = service.DeleteUser(p.Context, *id, password)
|
2018-11-27 14:20:58 +00:00
|
|
|
return user, err
|
|
|
|
},
|
|
|
|
},
|
2018-11-26 10:47:23 +00:00
|
|
|
// creates project from input params
|
|
|
|
createProjectMutation: &graphql.Field{
|
2018-12-06 14:40:32 +00:00
|
|
|
Type: types.Project(),
|
2018-11-26 10:47:23 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.ProjectInput()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
var projectInput = fromMapProjectInfo(p.Args[input].(map[string]interface{}))
|
|
|
|
|
|
|
|
return service.CreateProject(p.Context, projectInput)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// deletes project by id, taken from input params
|
|
|
|
deleteProjectMutation: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
2018-11-28 16:20:23 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
inputID := p.Args[fieldID].(string)
|
|
|
|
projectID, err := uuid.Parse(inputID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, service.DeleteProject(p.Context, *projectID)
|
|
|
|
},
|
|
|
|
},
|
2018-11-28 16:20:23 +00:00
|
|
|
// updates project description
|
|
|
|
updateProjectDescriptionMutation: &graphql.Field{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.String,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
2018-11-28 16:20:23 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
2018-11-28 16:20:23 +00:00
|
|
|
fieldDescription: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-11-28 16:20:23 +00:00
|
|
|
description := p.Args[fieldDescription].(string)
|
2018-11-26 10:47:23 +00:00
|
|
|
|
|
|
|
inputID := p.Args[fieldID].(string)
|
|
|
|
projectID, err := uuid.Parse(inputID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:20:23 +00:00
|
|
|
return service.UpdateProject(p.Context, *projectID, description)
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-06 14:40:32 +00:00
|
|
|
// add user as member of given project
|
|
|
|
addProjectMemberMutation: &graphql.Field{
|
|
|
|
Type: types.Project(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldProjectID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
fieldUserID: &graphql.ArgumentConfig{
|
2018-12-20 15:36:32 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
pID, _ := p.Args[fieldProjectID].(string)
|
2018-12-20 15:36:32 +00:00
|
|
|
uID, _ := p.Args[fieldUserID].([]interface{})
|
2018-12-06 14:40:32 +00:00
|
|
|
|
|
|
|
projectID, pErr := uuid.Parse(pID)
|
|
|
|
|
2018-12-20 15:36:32 +00:00
|
|
|
var userIDs []*uuid.UUID
|
|
|
|
var userErr errs.Group
|
|
|
|
|
|
|
|
for _, userID := range uID {
|
|
|
|
id, err := uuid.Parse(userID.(string))
|
|
|
|
if err != nil {
|
|
|
|
userErr.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs = append(userIDs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := errs.Combine(pErr, userErr.Err())
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-20 15:36:32 +00:00
|
|
|
var addMemberErr errs.Group
|
|
|
|
for _, userID := range userIDs {
|
|
|
|
err = service.AddProjectMember(p.Context, *projectID, *userID)
|
|
|
|
addMemberErr.Add(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = addMemberErr.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.GetProject(p.Context, *projectID)
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// delete user membership for given project
|
|
|
|
deleteProjectMemberMutation: &graphql.Field{
|
|
|
|
Type: types.Project(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldProjectID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
fieldUserID: &graphql.ArgumentConfig{
|
2018-12-20 15:36:32 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
pID, _ := p.Args[fieldProjectID].(string)
|
2018-12-20 15:36:32 +00:00
|
|
|
uID, _ := p.Args[fieldUserID].([]interface{})
|
2018-12-06 14:40:32 +00:00
|
|
|
|
|
|
|
projectID, pErr := uuid.Parse(pID)
|
|
|
|
|
2018-12-20 15:36:32 +00:00
|
|
|
var userIDs []*uuid.UUID
|
|
|
|
var userErr errs.Group
|
|
|
|
|
|
|
|
for _, userID := range uID {
|
|
|
|
id, err := uuid.Parse(userID.(string))
|
|
|
|
if err != nil {
|
|
|
|
userErr.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs = append(userIDs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := errs.Combine(pErr, userErr.Err())
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-20 15:36:32 +00:00
|
|
|
var deleteMemberErr errs.Group
|
|
|
|
for _, userID := range userIDs {
|
|
|
|
err = service.DeleteProjectMember(p.Context, *projectID, *userID)
|
|
|
|
deleteMemberErr.Add(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = deleteMemberErr.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.GetProject(p.Context, *projectID)
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|