From 041e8867f805c2b92dd623c8d29d56d1967338ec Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Fri, 3 May 2019 14:24:18 +0200 Subject: [PATCH] *: add creatorUserID and cretedAt to organization --- internal/services/configstore/command/org.go | 13 +++++++++++++ internal/services/gateway/api/org.go | 10 +++++++++- internal/services/gateway/command/org.go | 5 +++++ internal/services/gateway/command/project.go | 16 ++++++++++++++++ internal/services/types/types.go | 5 +++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/services/configstore/command/org.go b/internal/services/configstore/command/org.go index e580798..9dfe5da 100644 --- a/internal/services/configstore/command/org.go +++ b/internal/services/configstore/command/org.go @@ -17,6 +17,7 @@ package command import ( "context" "encoding/json" + "time" "github.com/sorintlab/agola/internal/datamanager" "github.com/sorintlab/agola/internal/db" @@ -55,6 +56,17 @@ func (s *CommandHandler) CreateOrg(ctx context.Context, org *types.Organization) if u != nil { return util.NewErrBadRequest(errors.Errorf("org %q already exists", u.Name)) } + + if org.CreatorUserID != "" { + user, err := s.readDB.GetUser(tx, org.CreatorUserID) + if err != nil { + return err + } + if user == nil { + return util.NewErrBadRequest(errors.Errorf("creator user %q doesn't exist", org.CreatorUserID)) + } + } + return nil }) if err != nil { @@ -62,6 +74,7 @@ func (s *CommandHandler) CreateOrg(ctx context.Context, org *types.Organization) } org.ID = uuid.NewV4().String() + org.CreatedAt = time.Now() orgj, err := json.Marshal(org) if err != nil { return nil, errors.Wrapf(err, "failed to marshal org") diff --git a/internal/services/gateway/api/org.go b/internal/services/gateway/api/org.go index 7660b2c..bd56c2d 100644 --- a/internal/services/gateway/api/org.go +++ b/internal/services/gateway/api/org.go @@ -45,6 +45,13 @@ func NewCreateOrgHandler(logger *zap.Logger, ch *command.CommandHandler) *Create func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := r.Context() + var userID string + userIDVal := ctx.Value("userid") + if userIDVal != nil { + userID = userIDVal.(string) + } + h.log.Infof("userID: %q", userID) + var req CreateOrgRequest d := json.NewDecoder(r.Body) if err := d.Decode(&req); err != nil { @@ -53,7 +60,8 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } creq := &command.CreateOrgRequest{ - Name: req.Name, + Name: req.Name, + CreatorUserID: userID, } org, err := h.ch.CreateOrg(ctx, creq) diff --git a/internal/services/gateway/command/org.go b/internal/services/gateway/command/org.go index 0ae647d..4c0d31f 100644 --- a/internal/services/gateway/command/org.go +++ b/internal/services/gateway/command/org.go @@ -25,6 +25,8 @@ import ( type CreateOrgRequest struct { Name string + + CreatorUserID string } func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) { @@ -38,6 +40,9 @@ func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) ( org := &types.Organization{ Name: req.Name, } + if req.CreatorUserID != "" { + org.CreatorUserID = req.CreatorUserID + } c.log.Infof("creating organization") org, resp, err := c.configstoreClient.CreateOrg(ctx, org) diff --git a/internal/services/gateway/command/project.go b/internal/services/gateway/command/project.go index 696365e..b41e3e6 100644 --- a/internal/services/gateway/command/project.go +++ b/internal/services/gateway/command/project.go @@ -17,6 +17,7 @@ package command import ( "context" "fmt" + "net/http" "net/url" "path" @@ -48,6 +49,21 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path")) } + pg, resp, err := c.configstoreClient.GetProjectGroup(ctx, req.ParentID) + if err != nil { + return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project group %q", req.Name)) + } + + projectPath := path.Join(pg.Path, req.Name) + _, resp, err = c.configstoreClient.GetProject(ctx, projectPath) + if err != nil { + if resp != nil && resp.StatusCode != http.StatusNotFound { + return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get project %q", req.Name)) + } + } else { + return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath)) + } + user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID) if err != nil { return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID)) diff --git a/internal/services/types/types.go b/internal/services/types/types.go index 1ae0192..b70ff69 100644 --- a/internal/services/types/types.go +++ b/internal/services/types/types.go @@ -81,6 +81,11 @@ type Organization struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` + + // CreatorUserID is the user id that created the organization. It could be empty + // if the org was created by using the admin user or the user has been removed. + CreatorUserID string `json:"creator_user_id,omitempty"` + CreatedAt time.Time `json:"created_at,omitempty"` } type ProjectGroup struct {