*: add creatorUserID and cretedAt to organization

This commit is contained in:
Simone Gotti 2019-05-03 14:24:18 +02:00
parent c30707528f
commit 041e8867f8
5 changed files with 48 additions and 1 deletions

View File

@ -17,6 +17,7 @@ package command
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"time"
"github.com/sorintlab/agola/internal/datamanager" "github.com/sorintlab/agola/internal/datamanager"
"github.com/sorintlab/agola/internal/db" "github.com/sorintlab/agola/internal/db"
@ -55,6 +56,17 @@ func (s *CommandHandler) CreateOrg(ctx context.Context, org *types.Organization)
if u != nil { if u != nil {
return util.NewErrBadRequest(errors.Errorf("org %q already exists", u.Name)) 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 return nil
}) })
if err != nil { if err != nil {
@ -62,6 +74,7 @@ func (s *CommandHandler) CreateOrg(ctx context.Context, org *types.Organization)
} }
org.ID = uuid.NewV4().String() org.ID = uuid.NewV4().String()
org.CreatedAt = time.Now()
orgj, err := json.Marshal(org) orgj, err := json.Marshal(org)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to marshal org") return nil, errors.Wrapf(err, "failed to marshal org")

View File

@ -45,6 +45,13 @@ func NewCreateOrgHandler(logger *zap.Logger, ch *command.CommandHandler) *Create
func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() 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 var req CreateOrgRequest
d := json.NewDecoder(r.Body) d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil { if err := d.Decode(&req); err != nil {
@ -54,6 +61,7 @@ func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
creq := &command.CreateOrgRequest{ creq := &command.CreateOrgRequest{
Name: req.Name, Name: req.Name,
CreatorUserID: userID,
} }
org, err := h.ch.CreateOrg(ctx, creq) org, err := h.ch.CreateOrg(ctx, creq)

View File

@ -25,6 +25,8 @@ import (
type CreateOrgRequest struct { type CreateOrgRequest struct {
Name string Name string
CreatorUserID string
} }
func (c *CommandHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*types.Organization, error) { 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{ org := &types.Organization{
Name: req.Name, Name: req.Name,
} }
if req.CreatorUserID != "" {
org.CreatorUserID = req.CreatorUserID
}
c.log.Infof("creating organization") c.log.Infof("creating organization")
org, resp, err := c.configstoreClient.CreateOrg(ctx, org) org, resp, err := c.configstoreClient.CreateOrg(ctx, org)

View File

@ -17,6 +17,7 @@ package command
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"net/url" "net/url"
"path" "path"
@ -48,6 +49,21 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
return nil, util.NewErrBadRequest(errors.Errorf("empty remote repo path")) 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) user, resp, err := c.configstoreClient.GetUser(ctx, req.CurrentUserID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID)) return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.CurrentUserID))

View File

@ -81,6 +81,11 @@ type Organization struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Name string `json:"name,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 { type ProjectGroup struct {