*: 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 (
"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")

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) {
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)

View File

@ -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)

View File

@ -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))

View File

@ -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 {