2019-02-28 14:52:35 +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.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/db"
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/configstore/action"
|
|
|
|
"agola.io/agola/internal/services/configstore/readdb"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
|
|
|
"agola.io/agola/services/configstore/types"
|
2019-02-28 14:52:35 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-02-28 14:52:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-02-28 14:52:35 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewOrgHandler(log zerolog.Logger, readDB *readdb.ReadDB) *OrgHandler {
|
|
|
|
return &OrgHandler{log: log, readDB: readDB}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-28 14:52:35 +00:00
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
orgRef := vars["orgref"]
|
2019-02-28 14:52:35 +00:00
|
|
|
|
|
|
|
var org *types.Organization
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-02-28 14:52:35 +00:00
|
|
|
var err error
|
2019-05-03 09:07:53 +00:00
|
|
|
org, err = h.readDB.GetOrg(tx, orgRef)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-28 14:52:35 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if org == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrNotExist, errors.Errorf("org %q doesn't exist", orgRef)))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, org); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateOrgHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateOrgHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateOrgHandler {
|
|
|
|
return &CreateOrgHandler{log: log, ah: ah}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req types.Organization
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
org, err := h.ah.CreateOrg(ctx, &req)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, org); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteOrgHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteOrgHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteOrgHandler {
|
|
|
|
return &DeleteOrgHandler{log: log, ah: ah}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteOrgHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2019-05-03 09:07:53 +00:00
|
|
|
orgRef := vars["orgref"]
|
2019-02-28 14:52:35 +00:00
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
err := h.ah.DeleteOrg(ctx, orgRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 15:05:13 +00:00
|
|
|
return
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 10:29:25 +00:00
|
|
|
}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultOrgsLimit = 10
|
|
|
|
MaxOrgsLimit = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrgsHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-02-28 14:52:35 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewOrgsHandler(log zerolog.Logger, readDB *readdb.ReadDB) *OrgsHandler {
|
|
|
|
return &OrgsHandler{log: log, readDB: readDB}
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-28 14:52:35 +00:00
|
|
|
query := r.URL.Query()
|
|
|
|
|
|
|
|
limitS := query.Get("limit")
|
|
|
|
limit := DefaultOrgsLimit
|
|
|
|
if limitS != "" {
|
|
|
|
var err error
|
|
|
|
limit, err = strconv.Atoi(limitS)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Wrapf(err, "cannot parse limit")))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if limit < 0 {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, errors.Errorf("limit must be greater or equal than 0")))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limit > MaxOrgsLimit {
|
|
|
|
limit = MaxOrgsLimit
|
|
|
|
}
|
|
|
|
asc := false
|
|
|
|
if _, ok := query["asc"]; ok {
|
|
|
|
asc = true
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.Get("start")
|
|
|
|
|
|
|
|
var orgs []*types.Organization
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-02-28 14:52:35 +00:00
|
|
|
var err error
|
|
|
|
orgs, err = h.readDB.GetOrgs(tx, start, limit, asc)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-28 14:52:35 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, orgs); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 14:47:22 +00:00
|
|
|
|
|
|
|
type AddOrgMemberHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-09 14:47:22 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewAddOrgMemberHandler(log zerolog.Logger, ah *action.ActionHandler) *AddOrgMemberHandler {
|
|
|
|
return &AddOrgMemberHandler{log: log, ah: ah}
|
2019-05-09 14:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AddOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
orgRef := vars["orgref"]
|
|
|
|
userRef := vars["userref"]
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req csapitypes.AddOrgMemberRequest
|
2019-05-09 14:47:22 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-09 14:47:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
org, err := h.ah.AddOrgMember(ctx, orgRef, userRef, req.Role)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 14:47:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, org); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 14:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 15:05:13 +00:00
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
type RemoveOrgMemberHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-09 15:05:13 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewRemoveOrgMemberHandler(log zerolog.Logger, ah *action.ActionHandler) *RemoveOrgMemberHandler {
|
|
|
|
return &RemoveOrgMemberHandler{log: log, ah: ah}
|
2019-05-09 15:05:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
func (h *RemoveOrgMemberHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-05-09 15:05:13 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
orgRef := vars["orgref"]
|
|
|
|
userRef := vars["userref"]
|
|
|
|
|
2019-05-14 09:20:09 +00:00
|
|
|
err := h.ah.RemoveOrgMember(ctx, orgRef, userRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 15:05:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 15:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-14 10:57:53 +00:00
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func orgMemberResponse(orgUser *action.OrgMemberResponse) *csapitypes.OrgMemberResponse {
|
|
|
|
return &csapitypes.OrgMemberResponse{
|
2019-05-14 10:57:53 +00:00
|
|
|
User: orgUser.User,
|
|
|
|
Role: orgUser.Role,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type OrgMembersHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-14 10:57:53 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewOrgMembersHandler(log zerolog.Logger, ah *action.ActionHandler) *OrgMembersHandler {
|
|
|
|
return &OrgMembersHandler{log: log, ah: ah}
|
2019-05-14 10:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OrgMembersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
orgRef := vars["orgref"]
|
|
|
|
|
|
|
|
orgUsers, err := h.ah.GetOrgMembers(ctx, orgRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-14 10:57:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
res := make([]*csapitypes.OrgMemberResponse, len(orgUsers))
|
2019-05-14 10:57:53 +00:00
|
|
|
for i, orgUser := range orgUsers {
|
|
|
|
res[i] = orgMemberResponse(orgUser)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-14 10:57:53 +00:00
|
|
|
}
|
|
|
|
}
|