storj/satellite/console/consoleweb/consoleql/mutation.go

394 lines
11 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleql
import (
"fmt"
"github.com/graphql-go/graphql"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"storj.io/storj/internal/post"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/mailservice"
)
const (
// Mutation is graphql request that modifies data
Mutation = "mutation"
2019-01-24 16:26:36 +00:00
// CreateUserMutation is a user creation mutation name
CreateUserMutation = "createUser"
// UpdateAccountMutation is a mutation name for account updating
UpdateAccountMutation = "updateAccount"
// DeleteAccountMutation is a mutation name for account deletion
DeleteAccountMutation = "deleteAccount"
// ChangePasswordMutation is a mutation name for password changing
ChangePasswordMutation = "changePassword"
// CreateProjectMutation is a mutation name for project creation
CreateProjectMutation = "createProject"
// DeleteProjectMutation is a mutation name for project deletion
DeleteProjectMutation = "deleteProject"
// UpdateProjectDescriptionMutation is a mutation name for project updating
UpdateProjectDescriptionMutation = "updateProjectDescription"
// AddProjectMembersMutation is a mutation name for adding new project members
AddProjectMembersMutation = "addProjectMembers"
// DeleteProjectMembersMutation is a mutation name for deleting project members
DeleteProjectMembersMutation = "deleteProjectMembers"
// CreateAPIKeyMutation is a mutation name for api key creation
CreateAPIKeyMutation = "createAPIKey"
// DeleteAPIKeysMutation is a mutation name for api key deleting
DeleteAPIKeysMutation = "deleteAPIKeys"
2019-01-24 16:26:36 +00:00
// InputArg is argument name for all input types
InputArg = "input"
// FieldProjectID is field name for projectID
FieldProjectID = "projectID"
// FieldNewPassword is a field name for new password
FieldNewPassword = "newPassword"
)
// rootMutation creates mutation for graphql populated by AccountsClient
func rootMutation(service *console.Service, mailService *mailservice.Service, types Types) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: Mutation,
Fields: graphql.Fields{
2019-01-24 16:26:36 +00:00
CreateUserMutation: &graphql.Field{
Type: types.User(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
InputArg: &graphql.ArgumentConfig{
2018-11-21 15:51:43 +00:00
Type: graphql.NewNonNull(types.UserInput()),
},
},
2018-11-21 15:51:43 +00:00
// creates user and company from input params and returns userID if succeed
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
input, _ := p.Args[InputArg].(map[string]interface{})
createUser := fromMapCreateUser(input)
user, err := service.CreateUser(p.Context, createUser)
if err != nil {
return nil, err
}
token, err := service.GenerateActivationToken(p.Context, user.ID, user.Email)
if err != nil {
return user, err
}
rootObject := p.Info.RootValue.(map[string]interface{})
link := rootObject["origin"].(string) + rootObject[ActivationPath].(string) + token
err = mailService.SendRendered(
p.Context,
[]post.Address{{Address: user.Email, Name: user.FirstName}},
&AccountActivationEmail{
ActivationLink: link,
},
)
if err != nil {
return user, err
}
return user, nil
},
},
2019-01-24 16:26:36 +00:00
UpdateAccountMutation: &graphql.Field{
2018-11-28 10:31:15 +00:00
Type: types.User(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
InputArg: &graphql.ArgumentConfig{
2018-11-28 10:31:15 +00:00
Type: graphql.NewNonNull(types.UserInput()),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
input, _ := p.Args[InputArg].(map[string]interface{})
auth, err := console.GetAuth(p.Context)
2018-11-28 10:31:15 +00:00
if err != nil {
return nil, err
}
info := fillUserInfo(&auth.User, input)
2018-11-28 10:31:15 +00:00
err = service.UpdateAccount(p.Context, info)
2018-11-28 12:30:38 +00:00
if err != nil {
return nil, err
2018-11-28 10:31:15 +00:00
}
return auth.User, nil
2018-11-28 10:31:15 +00:00
},
},
2019-01-24 16:26:36 +00:00
ChangePasswordMutation: &graphql.Field{
Type: types.User(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldPassword: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
2019-01-24 16:26:36 +00:00
FieldNewPassword: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
pass, _ := p.Args[FieldPassword].(string)
newPass, _ := p.Args[FieldNewPassword].(string)
auth, err := console.GetAuth(p.Context)
if err != nil {
return nil, err
}
err = service.ChangePassword(p.Context, pass, newPass)
if err != nil {
return nil, err
}
return auth.User, nil
},
},
2019-01-24 16:26:36 +00:00
DeleteAccountMutation: &graphql.Field{
Type: types.User(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldPassword: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
password, _ := p.Args[FieldPassword].(string)
auth, err := console.GetAuth(p.Context)
if err != nil {
return nil, err
}
err = service.DeleteAccount(p.Context, password)
if err != nil {
return nil, err
}
return auth.User, nil
},
},
// creates project from input params
2019-01-24 16:26:36 +00:00
CreateProjectMutation: &graphql.Field{
Type: types.Project(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
InputArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(types.ProjectInput()),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
var projectInput = fromMapProjectInfo(p.Args[InputArg].(map[string]interface{}))
return service.CreateProject(p.Context, projectInput)
},
},
// deletes project by id, taken from input params
2019-01-24 16:26:36 +00:00
DeleteProjectMutation: &graphql.Field{
2019-01-08 14:54:35 +00:00
Type: types.Project(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
inputID := p.Args[FieldID].(string)
projectID, err := uuid.Parse(inputID)
if err != nil {
return nil, err
}
2019-01-08 14:54:35 +00:00
project, err := service.GetProject(p.Context, *projectID)
if err != nil {
return nil, err
}
if err = service.DeleteProject(p.Context, project.ID); err != nil {
return nil, err
}
return project, nil
},
},
// updates project description
2019-01-24 16:26:36 +00:00
UpdateProjectDescriptionMutation: &graphql.Field{
2019-01-08 14:54:35 +00:00
Type: types.Project(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
2019-01-24 16:26:36 +00:00
FieldDescription: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
description := p.Args[FieldDescription].(string)
2019-01-24 16:26:36 +00:00
inputID := p.Args[FieldID].(string)
projectID, err := uuid.Parse(inputID)
if err != nil {
return nil, err
}
return service.UpdateProject(p.Context, *projectID, description)
},
},
// add user as member of given project
2019-01-24 16:26:36 +00:00
AddProjectMembersMutation: &graphql.Field{
Type: types.Project(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldProjectID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
2019-01-24 16:26:36 +00:00
FieldEmail: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
pID, _ := p.Args[FieldProjectID].(string)
emails, _ := p.Args[FieldEmail].([]interface{})
projectID, err := uuid.Parse(pID)
if err != nil {
return nil, err
}
var userEmails []string
for _, email := range emails {
userEmails = append(userEmails, email.(string))
}
project, err := service.GetProject(p.Context, *projectID)
if err != nil {
return nil, err
}
users, err := service.AddProjectMembers(p.Context, *projectID, userEmails)
if err != nil {
return nil, err
}
var emailErr errs.Group
for _, user := range users {
err = mailService.SendRendered(
p.Context,
[]post.Address{{Address: user.Email, Name: fmt.Sprintf("%s %s", user.FirstName, user.LastName)}},
&ProjectInvitationEmail{
UserName: user.FirstName,
ProjectName: user.LastName,
},
)
if err != nil {
emailErr.Add(err)
}
}
return project, emailErr.Err()
},
},
// delete user membership for given project
2019-01-24 16:26:36 +00:00
DeleteProjectMembersMutation: &graphql.Field{
Type: types.Project(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldProjectID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
2019-01-24 16:26:36 +00:00
FieldEmail: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
pID, _ := p.Args[FieldProjectID].(string)
emails, _ := p.Args[FieldEmail].([]interface{})
projectID, err := uuid.Parse(pID)
if err != nil {
return nil, err
}
var userEmails []string
for _, email := range emails {
userEmails = append(userEmails, email.(string))
}
err = service.DeleteProjectMembers(p.Context, *projectID, userEmails)
if err != nil {
return nil, err
}
return service.GetProject(p.Context, *projectID)
},
},
2018-12-27 15:30:15 +00:00
// creates new api key
2019-01-24 16:26:36 +00:00
CreateAPIKeyMutation: &graphql.Field{
2018-12-27 15:30:15 +00:00
Type: types.CreateAPIKey(),
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldProjectID: &graphql.ArgumentConfig{
2018-12-27 15:30:15 +00:00
Type: graphql.NewNonNull(graphql.String),
},
2019-01-24 16:26:36 +00:00
FieldName: &graphql.ArgumentConfig{
2018-12-27 15:30:15 +00:00
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
projectID, _ := p.Args[FieldProjectID].(string)
name, _ := p.Args[FieldName].(string)
2018-12-27 15:30:15 +00:00
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
DeleteAPIKeysMutation: &graphql.Field{
Type: graphql.NewList(types.APIKeyInfo()),
2018-12-27 15:30:15 +00:00
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
2018-12-27 15:30:15 +00:00
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
paramKeysID, _ := p.Args[FieldID].([]interface{})
var keyIds []uuid.UUID
var keys []console.APIKeyInfo
for _, id := range paramKeysID {
keyID, err := uuid.Parse(id.(string))
if err != nil {
return nil, err
}
key, err := service.GetAPIKeyInfo(p.Context, *keyID)
if err != nil {
return nil, err
}
keyIds = append(keyIds, *keyID)
keys = append(keys, *key)
2018-12-27 15:30:15 +00:00
}
err := service.DeleteAPIKeys(p.Context, keyIds)
2018-12-27 15:30:15 +00:00
if err != nil {
return nil, err
}
return keys, nil
2018-12-27 15:30:15 +00:00
},
},
},
})
}