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-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/satellite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Mutation is graphql request that modifies data
|
|
|
|
Mutation = "mutation"
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
createUserMutation = "createUser"
|
|
|
|
updateAccountMutation = "updateAccount"
|
|
|
|
deleteAccountMutation = "deleteAccount"
|
|
|
|
changePasswordMutation = "changePassword"
|
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-27 15:30:15 +00:00
|
|
|
addProjectMembersMutation = "addProjectMembers"
|
|
|
|
deleteProjectMembersMutation = "deleteProjectMembers"
|
|
|
|
|
|
|
|
createAPIKeyMutation = "createAPIKey"
|
|
|
|
deleteAPIKeyMutation = "deleteAPIKey"
|
2018-12-06 14:40:32 +00:00
|
|
|
|
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-12-24 12:52:52 +00:00
|
|
|
updateAccountMutation: &graphql.Field{
|
2018-11-28 10:31:15 +00:00
|
|
|
Type: types.User(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.UserInput()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-12-11 15:54:45 +00:00
|
|
|
input, _ := p.Args[input].(map[string]interface{})
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
auth, err := satellite.GetAuth(p.Context)
|
2018-11-28 10:31:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
info := fillUserInfo(&auth.User, input)
|
2018-11-28 10:31:15 +00:00
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
err = service.UpdateAccount(p.Context, info)
|
2018-11-28 12:30:38 +00:00
|
|
|
if err != nil {
|
2018-12-24 12:52:52 +00:00
|
|
|
return nil, err
|
2018-11-28 10:31:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return auth.User, nil
|
2018-11-28 10:31:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-24 12:52:52 +00:00
|
|
|
changePasswordMutation: &graphql.Field{
|
2018-12-10 15:57:06 +00:00
|
|
|
Type: types.User(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
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-24 12:52:52 +00:00
|
|
|
pass, _ := p.Args[fieldPassword].(string)
|
|
|
|
newPass, _ := p.Args[fieldNewPassword].(string)
|
|
|
|
|
|
|
|
auth, err := satellite.GetAuth(p.Context)
|
2018-12-10 15:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
err = service.ChangePassword(p.Context, pass, newPass)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-11 15:54:45 +00:00
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return auth.User, nil
|
2018-12-10 15:57:06 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-24 12:52:52 +00:00
|
|
|
deleteAccountMutation: &graphql.Field{
|
2018-11-27 14:20:58 +00:00
|
|
|
Type: types.User(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
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-24 12:52:52 +00:00
|
|
|
password, _ := p.Args[fieldPassword].(string)
|
|
|
|
|
|
|
|
auth, err := satellite.GetAuth(p.Context)
|
2018-11-27 14:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
err = service.DeleteAccount(p.Context, password)
|
2018-11-27 14:20:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:52:52 +00:00
|
|
|
return auth.User, nil
|
2018-11-27 14:20:58 +00:00
|
|
|
},
|
|
|
|
},
|
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
|
2018-12-27 15:30:15 +00:00
|
|
|
addProjectMembersMutation: &graphql.Field{
|
2018-12-06 14:40:32 +00:00
|
|
|
Type: types.Project(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldProjectID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2018-12-21 15:41:53 +00:00
|
|
|
fieldEmail: &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-21 15:41:53 +00:00
|
|
|
emails, _ := p.Args[fieldEmail].([]interface{})
|
2018-12-06 14:40:32 +00:00
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
projectID, err := uuid.Parse(pID)
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
var userEmails []string
|
|
|
|
for _, email := range emails {
|
|
|
|
userEmails = append(userEmails, email.(string))
|
2018-12-20 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
err = service.AddProjectMembers(p.Context, *projectID, userEmails)
|
|
|
|
if err != nil {
|
2018-12-20 15:36:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.GetProject(p.Context, *projectID)
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// delete user membership for given project
|
2018-12-27 15:30:15 +00:00
|
|
|
deleteProjectMembersMutation: &graphql.Field{
|
2018-12-06 14:40:32 +00:00
|
|
|
Type: types.Project(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldProjectID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2018-12-21 15:41:53 +00:00
|
|
|
fieldEmail: &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-21 15:41:53 +00:00
|
|
|
emails, _ := p.Args[fieldEmail].([]interface{})
|
2018-12-06 14:40:32 +00:00
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
projectID, err := uuid.Parse(pID)
|
2018-12-06 14:40:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
var userEmails []string
|
|
|
|
for _, email := range emails {
|
|
|
|
userEmails = append(userEmails, email.(string))
|
2018-12-20 15:36:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 15:41:53 +00:00
|
|
|
err = service.DeleteProjectMembers(p.Context, *projectID, userEmails)
|
|
|
|
if err != nil {
|
2018-12-20 15:36:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.GetProject(p.Context, *projectID)
|
2018-12-06 14:40:32 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-27 15:30:15 +00:00
|
|
|
// creates new api key
|
|
|
|
createAPIKeyMutation: &graphql.Field{
|
|
|
|
Type: types.CreateAPIKey(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldProjectID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
fieldName: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
projectID, _ := p.Args[fieldProjectID].(string)
|
|
|
|
name, _ := p.Args[fieldName].(string)
|
|
|
|
|
|
|
|
pID, err := uuid.Parse(projectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, key, err := service.CreateAPIKey(p.Context, *pID, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return createAPIKey{
|
|
|
|
Key: key,
|
|
|
|
KeyInfo: info,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// deletes api key
|
|
|
|
deleteAPIKeyMutation: &graphql.Field{
|
|
|
|
Type: types.APIKeyInfo(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
keyID, _ := p.Args[fieldID].(string)
|
|
|
|
|
|
|
|
id, err := uuid.Parse(keyID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := service.GetAPIKeyInfo(p.Context, *id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = service.DeleteAPIKey(p.Context, *id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|