2019-05-03 10:47:22 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
package action
|
2019-05-03 10:47:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-05-09 14:47:22 +00:00
|
|
|
"fmt"
|
2019-05-03 12:24:18 +00:00
|
|
|
"time"
|
2019-05-03 10:47:22 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/datamanager"
|
|
|
|
"agola.io/agola/internal/db"
|
|
|
|
"agola.io/agola/internal/services/configstore/readdb"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
"agola.io/agola/services/configstore/types"
|
2019-05-03 10:47:22 +00:00
|
|
|
|
2022-02-21 08:40:18 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-03 10:47:22 +00:00
|
|
|
)
|
|
|
|
|
2019-05-14 10:57:53 +00:00
|
|
|
type OrgMemberResponse struct {
|
|
|
|
User *types.User
|
|
|
|
Role types.MemberRole
|
|
|
|
}
|
|
|
|
|
|
|
|
func orgMemberResponse(orgUser *readdb.OrgUser) *OrgMemberResponse {
|
|
|
|
return &OrgMemberResponse{
|
|
|
|
User: orgUser.User,
|
|
|
|
Role: orgUser.Role,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) ([]*OrgMemberResponse, error) {
|
|
|
|
var orgUsers []*readdb.OrgUser
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-14 10:57:53 +00:00
|
|
|
var err error
|
|
|
|
org, err := h.readDB.GetOrg(tx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if org == nil {
|
2019-11-06 12:29:42 +00:00
|
|
|
return util.NewErrNotExist(errors.Errorf("org %q doesn't exist", orgRef))
|
2019-05-14 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orgUsers, err = h.readDB.GetOrgUsers(tx, org.ID)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*OrgMemberResponse, len(orgUsers))
|
|
|
|
for i, orgUser := range orgUsers {
|
|
|
|
res[i] = orgMemberResponse(orgUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func (h *ActionHandler) CreateOrg(ctx context.Context, org *types.Organization) (*types.Organization, error) {
|
2019-05-03 10:47:22 +00:00
|
|
|
if org.Name == "" {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("organization name required"))
|
|
|
|
}
|
|
|
|
if !util.ValidateName(org.Name) {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid organization name %q", org.Name))
|
|
|
|
}
|
2019-05-14 08:56:17 +00:00
|
|
|
if !types.IsValidVisibility(org.Visibility) {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid organization visibility"))
|
|
|
|
}
|
2019-05-03 10:47:22 +00:00
|
|
|
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
// changegroup is the org name
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("orgname-" + org.Name)}
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
var err error
|
2019-05-03 21:35:25 +00:00
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check duplicate org name
|
2019-05-09 14:47:22 +00:00
|
|
|
o, err := h.readDB.GetOrgByName(tx, org.Name)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-09 14:47:22 +00:00
|
|
|
if o != nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("org %q already exists", o.Name))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
2019-05-03 12:24:18 +00:00
|
|
|
|
|
|
|
if org.CreatorUserID != "" {
|
2019-05-03 21:35:25 +00:00
|
|
|
user, err := h.readDB.GetUser(tx, org.CreatorUserID)
|
2019-05-03 12:24:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("creator user %q doesn't exist", org.CreatorUserID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 10:47:22 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:40:07 +00:00
|
|
|
actions := []*datamanager.Action{}
|
|
|
|
|
2022-02-21 08:40:18 +00:00
|
|
|
org.ID = uuid.Must(uuid.NewV4()).String()
|
2019-05-03 12:24:18 +00:00
|
|
|
org.CreatedAt = time.Now()
|
2019-05-03 10:47:22 +00:00
|
|
|
orgj, err := json.Marshal(org)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to marshal org: %w", err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
2019-05-03 15:40:07 +00:00
|
|
|
actions = append(actions, &datamanager.Action{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeOrg),
|
|
|
|
ID: org.ID,
|
|
|
|
Data: orgj,
|
|
|
|
})
|
|
|
|
|
|
|
|
if org.CreatorUserID != "" {
|
|
|
|
// add the creator as org member with role owner
|
|
|
|
orgmember := &types.OrganizationMember{
|
2022-02-21 08:40:18 +00:00
|
|
|
ID: uuid.Must(uuid.NewV4()).String(),
|
2019-05-03 15:40:07 +00:00
|
|
|
OrganizationID: org.ID,
|
|
|
|
UserID: org.CreatorUserID,
|
|
|
|
MemberRole: types.MemberRoleOwner,
|
|
|
|
}
|
|
|
|
orgmemberj, err := json.Marshal(orgmember)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to marshal project group: %w", err)
|
2019-05-03 15:40:07 +00:00
|
|
|
}
|
|
|
|
actions = append(actions, &datamanager.Action{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeOrgMember),
|
|
|
|
ID: orgmember.ID,
|
|
|
|
Data: orgmemberj,
|
|
|
|
})
|
|
|
|
}
|
2019-05-03 10:47:22 +00:00
|
|
|
|
2019-05-14 08:56:17 +00:00
|
|
|
// create root org project group
|
2019-05-03 10:47:22 +00:00
|
|
|
pg := &types.ProjectGroup{
|
2022-02-21 08:40:18 +00:00
|
|
|
ID: uuid.Must(uuid.NewV4()).String(),
|
2019-05-14 08:56:17 +00:00
|
|
|
// use same org visibility
|
|
|
|
Visibility: org.Visibility,
|
2019-05-03 10:47:22 +00:00
|
|
|
Parent: types.Parent{
|
|
|
|
Type: types.ConfigTypeOrg,
|
|
|
|
ID: org.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pgj, err := json.Marshal(pg)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to marshal project group: %w", err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
2019-05-03 15:40:07 +00:00
|
|
|
actions = append(actions, &datamanager.Action{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeProjectGroup),
|
|
|
|
ID: pg.ID,
|
|
|
|
Data: pgj,
|
|
|
|
})
|
2019-05-03 10:47:22 +00:00
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2019-05-03 10:47:22 +00:00
|
|
|
return org, err
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
var org *types.Organization
|
|
|
|
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-03 10:47:22 +00:00
|
|
|
var err error
|
|
|
|
// check org existance
|
2019-05-03 21:35:25 +00:00
|
|
|
org, err = h.readDB.GetOrgByName(tx, orgRef)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if org == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("org %q doesn't exist", orgRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
// changegroup is the org id
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("orgid-" + org.ID)}
|
2019-05-03 21:35:25 +00:00
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(sgotti) delete all project groups, projects etc...
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypeDelete,
|
|
|
|
DataType: string(types.ConfigTypeOrg),
|
|
|
|
ID: org.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2019-05-03 10:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-05-09 14:47:22 +00:00
|
|
|
|
|
|
|
// AddOrgMember add/updates an org member.
|
|
|
|
// TODO(sgotti) handle invitation when implemented
|
|
|
|
func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string, role types.MemberRole) (*types.OrganizationMember, error) {
|
|
|
|
if !types.IsValidMemberRole(role) {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid role %q", role))
|
|
|
|
}
|
|
|
|
|
|
|
|
var org *types.Organization
|
|
|
|
var user *types.User
|
|
|
|
var orgmember *types.OrganizationMember
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-09 14:47:22 +00:00
|
|
|
var err error
|
|
|
|
// check existing org
|
|
|
|
org, err = h.readDB.GetOrg(tx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if org == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("org %q doesn't exists", orgRef))
|
|
|
|
}
|
|
|
|
// check existing user
|
|
|
|
user, err = h.readDB.GetUser(tx, userRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("user %q doesn't exists", userRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch org member if it already exist
|
|
|
|
orgmember, err = h.readDB.GetOrgMemberByOrgUserID(tx, org.ID, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cgNames := []string{util.EncodeSha256Hex(fmt.Sprintf("orgmember-%s-%s", org.ID, user.ID))}
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update if role changed
|
|
|
|
if orgmember != nil {
|
|
|
|
if orgmember.MemberRole == role {
|
|
|
|
return orgmember, nil
|
|
|
|
}
|
|
|
|
orgmember.MemberRole = role
|
|
|
|
} else {
|
|
|
|
orgmember = &types.OrganizationMember{
|
2022-02-21 08:40:18 +00:00
|
|
|
ID: uuid.Must(uuid.NewV4()).String(),
|
2019-05-09 14:47:22 +00:00
|
|
|
OrganizationID: org.ID,
|
|
|
|
UserID: user.ID,
|
|
|
|
MemberRole: role,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actions := []*datamanager.Action{}
|
|
|
|
orgmemberj, err := json.Marshal(orgmember)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to marshal project group: %w", err)
|
2019-05-09 14:47:22 +00:00
|
|
|
}
|
|
|
|
actions = append(actions, &datamanager.Action{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeOrgMember),
|
|
|
|
ID: orgmember.ID,
|
|
|
|
Data: orgmemberj,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
|
|
|
return orgmember, err
|
|
|
|
}
|
2019-05-09 15:05:13 +00:00
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
// RemoveOrgMember removes an org member.
|
|
|
|
func (h *ActionHandler) RemoveOrgMember(ctx context.Context, orgRef, userRef string) error {
|
2019-05-09 15:05:13 +00:00
|
|
|
var org *types.Organization
|
|
|
|
var user *types.User
|
|
|
|
var orgmember *types.OrganizationMember
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-09 15:05:13 +00:00
|
|
|
var err error
|
|
|
|
// check existing org
|
|
|
|
org, err = h.readDB.GetOrg(tx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if org == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("org %q doesn't exists", orgRef))
|
|
|
|
}
|
|
|
|
// check existing user
|
|
|
|
user, err = h.readDB.GetUser(tx, userRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("user %q doesn't exists", userRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that org member exists
|
|
|
|
orgmember, err = h.readDB.GetOrgMemberByOrgUserID(tx, org.ID, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if orgmember == nil {
|
|
|
|
return util.NewErrBadRequest(errors.Errorf("orgmember for org %q, user %q doesn't exists", orgRef, userRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
cgNames := []string{util.EncodeSha256Hex(fmt.Sprintf("orgmember-%s-%s", org.ID, user.ID))}
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
actions := []*datamanager.Action{}
|
|
|
|
actions = append(actions, &datamanager.Action{
|
|
|
|
ActionType: datamanager.ActionTypeDelete,
|
|
|
|
DataType: string(types.ConfigTypeOrgMember),
|
|
|
|
ID: orgmember.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
|
|
|
return err
|
|
|
|
}
|