Satellite add/delete project members by email api (#928)

* Satellite add/delete project members by email api
This commit is contained in:
Yaroslav Vorobiov 2018-12-21 17:41:53 +02:00 committed by GitHub
parent f70b826fd4
commit 16db288c85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 55 deletions

View File

@ -6,7 +6,6 @@ package satelliteql
import (
"github.com/graphql-go/graphql"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"storj.io/storj/pkg/satellite"
"storj.io/storj/pkg/utils"
@ -210,41 +209,26 @@ func rootMutation(service *satellite.Service, types Types) *graphql.Object {
fieldProjectID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
fieldUserID: &graphql.ArgumentConfig{
fieldEmail: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
pID, _ := p.Args[fieldProjectID].(string)
uID, _ := p.Args[fieldUserID].([]interface{})
emails, _ := p.Args[fieldEmail].([]interface{})
projectID, pErr := uuid.Parse(pID)
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())
projectID, err := uuid.Parse(pID)
if err != nil {
return nil, err
}
var addMemberErr errs.Group
for _, userID := range userIDs {
err = service.AddProjectMember(p.Context, *projectID, *userID)
addMemberErr.Add(err)
var userEmails []string
for _, email := range emails {
userEmails = append(userEmails, email.(string))
}
if err = addMemberErr.Err(); err != nil {
err = service.AddProjectMembers(p.Context, *projectID, userEmails)
if err != nil {
return nil, err
}
@ -258,41 +242,26 @@ func rootMutation(service *satellite.Service, types Types) *graphql.Object {
fieldProjectID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
fieldUserID: &graphql.ArgumentConfig{
fieldEmail: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.String)),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
pID, _ := p.Args[fieldProjectID].(string)
uID, _ := p.Args[fieldUserID].([]interface{})
emails, _ := p.Args[fieldEmail].([]interface{})
projectID, pErr := uuid.Parse(pID)
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())
projectID, err := uuid.Parse(pID)
if err != nil {
return nil, err
}
var deleteMemberErr errs.Group
for _, userID := range userIDs {
err = service.DeleteProjectMember(p.Context, *projectID, *userID)
deleteMemberErr.Add(err)
var userEmails []string
for _, email := range emails {
userEmails = append(userEmails, email.(string))
}
if err = deleteMemberErr.Err(); err != nil {
err = service.DeleteProjectMembers(p.Context, *projectID, userEmails)
if err != nil {
return nil, err
}

View File

@ -13,7 +13,6 @@ const (
userType = "user"
userInputType = "userInput"
fieldUserID = "userID"
fieldID = "id"
fieldEmail = "email"
fieldPassword = "password"

View File

@ -277,27 +277,92 @@ func (s *Service) UpdateProject(ctx context.Context, projectID uuid.UUID, descri
return project, nil
}
// AddProjectMember adds User as member of given Project
func (s *Service) AddProjectMember(ctx context.Context, projectID, userID uuid.UUID) (err error) {
// AddProjectMembers adds users by email to given project
func (s *Service) AddProjectMembers(ctx context.Context, projectID uuid.UUID, emails []string) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = GetAuth(ctx)
if err != nil {
return err
}
_, err = s.store.ProjectMembers().Insert(ctx, userID, projectID)
return err
var userIDs []uuid.UUID
var userErr errs.Group
// collect user querying errors
for _, email := range emails {
user, err := s.store.Users().GetByEmail(ctx, email)
if err != nil {
userErr.Add(err)
continue
}
userIDs = append(userIDs, user.ID)
}
if err := userErr.Err(); err != nil {
return err
}
// add project members in transaction scope
tx, err := s.store.BeginTx(ctx)
if err != nil {
return err
}
for _, uID := range userIDs {
_, err := tx.ProjectMembers().Insert(ctx, uID, projectID)
if err != nil {
return errs.Combine(err, tx.Rollback())
}
}
return tx.Commit()
}
// DeleteProjectMember removes user membership for given project
func (s *Service) DeleteProjectMember(ctx context.Context, projectID, userID uuid.UUID) (err error) {
// DeleteProjectMembers removes users by email from given project
func (s *Service) DeleteProjectMembers(ctx context.Context, projectID uuid.UUID, emails []string) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = GetAuth(ctx)
if err != nil {
return err
}
return s.store.ProjectMembers().Delete(ctx, userID, projectID)
var userIDs []uuid.UUID
var userErr errs.Group
// collect user querying errors
for _, email := range emails {
user, err := s.store.Users().GetByEmail(ctx, email)
if err != nil {
userErr.Add(err)
continue
}
userIDs = append(userIDs, user.ID)
}
if err := userErr.Err(); err != nil {
return err
}
// delete project members in transaction scope
tx, err := s.store.BeginTx(ctx)
if err != nil {
return err
}
for _, uID := range userIDs {
err := tx.ProjectMembers().Delete(ctx, uID, projectID)
if err != nil {
return errs.Combine(err, tx.Rollback())
}
}
return tx.Commit()
}
// GetProjectMembers returns ProjectMembers for given Project