2019-04-08 09:35:45 +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:48:49 +00:00
|
|
|
package action
|
2019-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/types"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-04-08 09:35:45 +00:00
|
|
|
)
|
|
|
|
|
2019-05-05 12:36:14 +00:00
|
|
|
func (h *ActionHandler) GetOrg(ctx context.Context, orgRef string) (*types.Organization, error) {
|
|
|
|
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return org, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetOrgsRequest struct {
|
|
|
|
Start string
|
|
|
|
Limit int
|
|
|
|
Asc bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) GetOrgs(ctx context.Context, req *GetOrgsRequest) ([]*types.Organization, error) {
|
|
|
|
orgs, resp, err := h.configstoreClient.GetOrgs(ctx, req.Start, req.Limit, req.Asc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
return orgs, nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 10:58:29 +00:00
|
|
|
type OrgMembersResponse struct {
|
|
|
|
Organization *types.Organization
|
|
|
|
Members []*OrgMemberResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgMemberResponse struct {
|
|
|
|
User *types.User
|
|
|
|
Role types.MemberRole
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) GetOrgMembers(ctx context.Context, orgRef string) (*OrgMembersResponse, error) {
|
|
|
|
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
orgMembers, resp, err := h.configstoreClient.GetOrgMembers(ctx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &OrgMembersResponse{
|
|
|
|
Organization: org,
|
|
|
|
Members: make([]*OrgMemberResponse, len(orgMembers)),
|
|
|
|
}
|
|
|
|
for i, orgMember := range orgMembers {
|
|
|
|
res.Members[i] = &OrgMemberResponse{
|
|
|
|
User: orgMember.User,
|
|
|
|
Role: orgMember.Role,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
type CreateOrgRequest struct {
|
2019-05-14 08:56:17 +00:00
|
|
|
Name string
|
|
|
|
Visibility types.Visibility
|
2019-05-03 12:24:18 +00:00
|
|
|
|
|
|
|
CreatorUserID string
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) {
|
2019-05-03 21:19:23 +00:00
|
|
|
if !h.IsUserLoggedOrAdmin(ctx) {
|
|
|
|
return nil, errors.Errorf("user not logged in")
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if req.Name == "" {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("organization name required"))
|
|
|
|
}
|
|
|
|
if !util.ValidateName(req.Name) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid organization name %q", req.Name))
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
org := &types.Organization{
|
2019-05-14 08:56:17 +00:00
|
|
|
Name: req.Name,
|
|
|
|
Visibility: req.Visibility,
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-05-03 12:24:18 +00:00
|
|
|
if req.CreatorUserID != "" {
|
|
|
|
org.CreatorUserID = req.CreatorUserID
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("creating organization")
|
|
|
|
org, resp, err := h.configstoreClient.CreateOrg(ctx, org)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to create organization: %w", ErrFromRemote(resp, err))
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-05-03 21:48:49 +00:00
|
|
|
h.log.Infof("organization %s created, ID: %s", org.Name, org.ID)
|
2019-04-08 09:35:45 +00:00
|
|
|
|
|
|
|
return org, nil
|
|
|
|
}
|
2019-05-05 12:36:14 +00:00
|
|
|
|
|
|
|
func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error {
|
2019-05-03 21:19:23 +00:00
|
|
|
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isOrgOwner, err := h.IsOrgOwner(ctx, org.ID)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to determine ownership: %w", err)
|
2019-05-03 21:19:23 +00:00
|
|
|
}
|
|
|
|
if !isOrgOwner {
|
|
|
|
return util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = h.configstoreClient.DeleteOrg(ctx, orgRef)
|
2019-05-05 12:36:14 +00:00
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return errors.Errorf("failed to delete org: %w", ErrFromRemote(resp, err))
|
2019-05-05 12:36:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-09 14:47:22 +00:00
|
|
|
|
|
|
|
type AddOrgMemberResponse struct {
|
|
|
|
OrganizationMember *types.OrganizationMember
|
|
|
|
Org *types.Organization
|
|
|
|
User *types.User
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string, role types.MemberRole) (*AddOrgMemberResponse, error) {
|
|
|
|
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isOrgOwner, err := h.IsOrgOwner(ctx, org.ID)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("failed to determine ownership: %w", err)
|
2019-05-09 14:47:22 +00:00
|
|
|
}
|
|
|
|
if !isOrgOwner {
|
|
|
|
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
|
|
|
orgmember, resp, err := h.configstoreClient.AddOrgMember(ctx, orgRef, userRef, role)
|
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return nil, errors.Errorf("failed to add/update organization member: %w", ErrFromRemote(resp, err))
|
2019-05-09 14:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &AddOrgMemberResponse{
|
|
|
|
OrganizationMember: orgmember,
|
|
|
|
Org: org,
|
|
|
|
User: user,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-05-09 15:05:13 +00:00
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
func (h *ActionHandler) RemoveOrgMember(ctx context.Context, orgRef, userRef string) error {
|
2019-05-09 15:05:13 +00:00
|
|
|
org, resp, err := h.configstoreClient.GetOrg(ctx, orgRef)
|
|
|
|
if err != nil {
|
|
|
|
return ErrFromRemote(resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isOrgOwner, err := h.IsOrgOwner(ctx, org.ID)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to determine ownership: %w", err)
|
2019-05-09 15:05:13 +00:00
|
|
|
}
|
|
|
|
if !isOrgOwner {
|
|
|
|
return util.NewErrForbidden(errors.Errorf("user not authorized"))
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
resp, err = h.configstoreClient.RemoveOrgMember(ctx, orgRef, userRef)
|
2019-05-09 15:05:13 +00:00
|
|
|
if err != nil {
|
2019-05-23 10:59:11 +00:00
|
|
|
return errors.Errorf("failed to remove organization member: %w", ErrFromRemote(resp, err))
|
2019-05-09 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|