2020-07-21 18:10:03 +02:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/daddy/internal/model"
|
|
|
|
"github.com/pkg/errors"
|
2020-09-04 10:10:32 +02:00
|
|
|
errs "github.com/pkg/errors"
|
2020-07-21 18:10:03 +02:00
|
|
|
)
|
|
|
|
|
2020-07-22 22:25:03 +02:00
|
|
|
func handleWorkgroups(ctx context.Context, filter *model.WorkgroupsFilter) ([]*model.Workgroup, error) {
|
2020-07-21 18:10:03 +02:00
|
|
|
db, err := getDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := model.NewWorkgroupRepository(db)
|
|
|
|
|
2020-07-22 22:25:03 +02:00
|
|
|
criteria := make([]interface{}, 0)
|
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
if len(filter.Ids) > 0 {
|
|
|
|
criteria = append(criteria, "id in (?)", filter.Ids)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
found, err := repo.FindWorkgroups(ctx, criteria...)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
workgroups := make([]*model.Workgroup, 0)
|
|
|
|
|
|
|
|
for _, wg := range found {
|
|
|
|
authorized, err := isAuthorized(ctx, wg, model.ActionRead)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if authorized {
|
|
|
|
workgroups = append(workgroups, wg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:10:03 +02:00
|
|
|
return workgroups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleJoinWorkgroup(ctx context.Context, rawWorkgroupID string) (*model.Workgroup, error) {
|
2020-09-04 10:10:32 +02:00
|
|
|
user, db, err := getSessionUser(ctx)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
repo := model.NewWorkgroupRepository(db)
|
|
|
|
|
|
|
|
workgroup, err := repo.Find(ctx, rawWorkgroupID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
authorized, err := isAuthorized(ctx, workgroup, model.ActionJoin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
2020-07-21 18:10:03 +02:00
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
if !authorized {
|
|
|
|
return nil, errs.WithStack(ErrForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
workgroup, err = repo.AddUserToWorkgroup(ctx, user.ID, workgroup.ID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return workgroup, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
func handleLeaveWorkgroup(ctx context.Context, workgroupID string) (*model.Workgroup, error) {
|
|
|
|
user, db, err := getSessionUser(ctx)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
repo := model.NewWorkgroupRepository(db)
|
|
|
|
|
|
|
|
workgroup, err := repo.Find(ctx, workgroupID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
authorized, err := isAuthorized(ctx, workgroup, model.ActionLeave)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
2020-07-21 18:10:03 +02:00
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
if !authorized {
|
|
|
|
return nil, errs.WithStack(ErrForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
workgroup, err = repo.RemoveUserFromWorkgroup(ctx, user.ID, workgroup.ID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return workgroup, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCreateWorkgroup(ctx context.Context, changes model.WorkgroupChanges) (*model.Workgroup, error) {
|
2020-09-04 10:10:32 +02:00
|
|
|
authorized, err := isAuthorized(ctx, &model.Workgroup{}, model.ActionCreate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authorized {
|
|
|
|
return nil, errs.WithStack(ErrForbidden)
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:10:03 +02:00
|
|
|
db, err := getDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := model.NewWorkgroupRepository(db)
|
|
|
|
|
|
|
|
workgroup, err := repo.CreateWorkgroup(ctx, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return workgroup, nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
func handleCloseWorkgroup(ctx context.Context, workgroupID string) (*model.Workgroup, error) {
|
2020-07-21 18:10:03 +02:00
|
|
|
db, err := getDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := model.NewWorkgroupRepository(db)
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
workgroup, err := repo.Find(ctx, workgroupID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
authorized, err := isAuthorized(ctx, workgroup, model.ActionClose)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
2020-07-21 18:10:03 +02:00
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
if !authorized {
|
|
|
|
return nil, errs.WithStack(ErrForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
workgroup, err = repo.CloseWorkgroup(ctx, workgroup.ID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
return workgroup, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUpdateWorkgroup(ctx context.Context, workgroupID string, changes model.WorkgroupChanges) (*model.Workgroup, error) {
|
2020-07-21 18:10:03 +02:00
|
|
|
db, err := getDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := model.NewWorkgroupRepository(db)
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
workgroup, err := repo.Find(ctx, workgroupID)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
authorized, err := isAuthorized(ctx, workgroup, model.ActionUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authorized {
|
|
|
|
return nil, errs.WithStack(ErrForbidden)
|
|
|
|
}
|
2020-07-21 18:10:03 +02:00
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
workgroup, err = repo.UpdateWorkgroup(ctx, workgroup.ID, changes)
|
2020-07-21 18:10:03 +02:00
|
|
|
if err != nil {
|
2020-09-04 10:10:32 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2020-07-21 18:10:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 10:10:32 +02:00
|
|
|
return workgroup, nil
|
2020-07-21 18:10:03 +02:00
|
|
|
}
|