Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use middleware to load org #4208

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions server/api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,9 @@
// @Param perPage query int false "for response pagination, max items per page" default(50)
func GetOrgAgents(c *gin.Context) {
_store := store.FromContext(c)
org := session.Org(c)

Check warning on line 295 in server/api/agent.go

View check run for this annotation

Codecov / codecov/patch

server/api/agent.go#L295

Added line #L295 was not covered by tests

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

agents, err := _store.AgentListForOrg(orgID, session.Pagination(c))
agents, err := _store.AgentListForOrg(org.ID, session.Pagination(c))

Check warning on line 297 in server/api/agent.go

View check run for this annotation

Codecov / codecov/patch

server/api/agent.go#L297

Added line #L297 was not covered by tests
if err != nil {
c.String(http.StatusInternalServerError, "Error getting agent list. %s", err)
return
Expand All @@ -321,12 +316,7 @@
// @Param agent body Agent true "the agent's updated data"
func PatchOrgAgent(c *gin.Context) {
_store := store.FromContext(c)

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Invalid organization ID")
return
}
org := session.Org(c)

Check warning on line 319 in server/api/agent.go

View check run for this annotation

Codecov / codecov/patch

server/api/agent.go#L319

Added line #L319 was not covered by tests

agentID, err := strconv.ParseInt(c.Param("agent_id"), 10, 64)
if err != nil {
Expand All @@ -340,7 +330,7 @@
return
}

if agent.OrgID != orgID {
if agent.OrgID != org.ID {

Check warning on line 333 in server/api/agent.go

View check run for this annotation

Codecov / codecov/patch

server/api/agent.go#L333

Added line #L333 was not covered by tests
c.String(http.StatusBadRequest, "Agent does not belong to this organization")
return
}
Expand Down Expand Up @@ -378,12 +368,7 @@
// @Param agent_id path int true "the agent's id"
func DeleteOrgAgent(c *gin.Context) {
_store := store.FromContext(c)

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Invalid organization ID")
return
}
org := session.Org(c)

Check warning on line 371 in server/api/agent.go

View check run for this annotation

Codecov / codecov/patch

server/api/agent.go#L371

Added line #L371 was not covered by tests

agentID, err := strconv.ParseInt(c.Param("agent_id"), 10, 64)
if err != nil {
Expand All @@ -397,7 +382,7 @@
return
}

if agent.OrgID != orgID {
if agent.OrgID != org.ID {

Check warning on line 385 in server/api/agent.go

View check run for this annotation

Codecov / codecov/patch

server/api/agent.go#L385

Added line #L385 was not covered by tests
c.String(http.StatusBadRequest, "Agent does not belong to this organization")
return
}
Expand Down
42 changes: 5 additions & 37 deletions server/api/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import (
"net/http"
"strconv"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -58,20 +57,7 @@
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
// @Param org_id path string true "the organization's id"
func GetOrg(c *gin.Context) {
_store := store.FromContext(c)

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

org, err := _store.OrgGet(orgID)
if err != nil {
handleDBError(c, err)
return
}

org := session.Org(c)

Check warning on line 60 in server/api/org.go

View check run for this annotation

Codecov / codecov/patch

server/api/org.go#L60

Added line #L60 was not covered by tests
c.JSON(http.StatusOK, org)
}

Expand All @@ -86,7 +72,7 @@
// @Param org_id path string true "the organization's id"
func GetOrgPermissions(c *gin.Context) {
user := session.User(c)
_store := store.FromContext(c)
org := session.Org(c)

Check warning on line 75 in server/api/org.go

View check run for this annotation

Codecov / codecov/patch

server/api/org.go#L75

Added line #L75 was not covered by tests

_forge, err := server.Config.Services.Manager.ForgeFromUser(user)
if err != nil {
Expand All @@ -95,23 +81,11 @@
return
}

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

if user == nil {
c.JSON(http.StatusOK, &model.OrgPerm{})
return
}

org, err := _store.OrgGet(orgID)
if err != nil {
c.String(http.StatusInternalServerError, "Error getting org %d. %s", orgID, err)
return
}

if (org.IsUser && org.Name == user.Login) || (user.Admin && !org.IsUser) {
c.JSON(http.StatusOK, &model.OrgPerm{
Member: true,
Expand All @@ -125,7 +99,7 @@

perm, err := server.Config.Services.Membership.Get(c, _forge, user, org.Name)
if err != nil {
c.String(http.StatusInternalServerError, "Error getting membership for %d. %s", orgID, err)
c.String(http.StatusInternalServerError, "Error getting membership for %d. %s", org.ID, err)

Check warning on line 102 in server/api/org.go

View check run for this annotation

Codecov / codecov/patch

server/api/org.go#L102

Added line #L102 was not covered by tests
return
}

Expand Down Expand Up @@ -200,15 +174,9 @@
// @Param id path string true "the org's id"
func DeleteOrg(c *gin.Context) {
_store := store.FromContext(c)
org := session.Org(c)

Check warning on line 177 in server/api/org.go

View check run for this annotation

Codecov / codecov/patch

server/api/org.go#L177

Added line #L177 was not covered by tests

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

err = _store.OrgDelete(orgID)
if err != nil {
if err := _store.OrgDelete(org.ID); err != nil {

Check warning on line 179 in server/api/org.go

View check run for this annotation

Codecov / codecov/patch

server/api/org.go#L179

Added line #L179 was not covered by tests
handleDBError(c, err)
return
}
Expand Down
61 changes: 19 additions & 42 deletions server/api/org_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"

Expand All @@ -36,16 +35,11 @@
// @Param org_id path string true "the org's id"
// @Param registry path string true "the registry's address"
func GetOrgRegistry(c *gin.Context) {
org := session.Org(c)

Check warning on line 38 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L38

Added line #L38 was not covered by tests
addr := c.Param("registry")

orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

registryService := server.Config.Services.Manager.RegistryService()
registry, err := registryService.OrgRegistryFind(orgID, addr)
registry, err := registryService.OrgRegistryFind(org.ID, addr)

Check warning on line 42 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L42

Added line #L42 was not covered by tests
if err != nil {
handleDBError(c, err)
return
Expand All @@ -65,16 +59,12 @@
// @Param page query int false "for response pagination, page offset number" default(1)
// @Param perPage query int false "for response pagination, max items per page" default(50)
func GetOrgRegistryList(c *gin.Context) {
orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}
org := session.Org(c)

Check warning on line 62 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L62

Added line #L62 was not covered by tests

registryService := server.Config.Services.Manager.RegistryService()
list, err := registryService.OrgRegistryList(orgID, session.Pagination(c))
list, err := registryService.OrgRegistryList(org.ID, session.Pagination(c))

Check warning on line 65 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L65

Added line #L65 was not covered by tests
if err != nil {
c.String(http.StatusInternalServerError, "Error getting registry list for %q. %s", orgID, err)
c.String(http.StatusInternalServerError, "Error getting registry list for %q. %s", org.ID, err)

Check warning on line 67 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L67

Added line #L67 was not covered by tests
return
}
// copy the registry detail to remove the sensitive
Expand All @@ -96,31 +86,27 @@
// @Param org_id path string true "the org's id"
// @Param registryData body Registry true "the new registry"
func PostOrgRegistry(c *gin.Context) {
orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}
org := session.Org(c)

Check warning on line 89 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L89

Added line #L89 was not covered by tests

in := new(model.Registry)
if err := c.Bind(in); err != nil {
c.String(http.StatusBadRequest, "Error parsing org %q registry. %s", orgID, err)
c.String(http.StatusBadRequest, "Error parsing org %q registry. %s", org.ID, err)

Check warning on line 93 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L93

Added line #L93 was not covered by tests
return
}
registry := &model.Registry{
OrgID: orgID,
OrgID: org.ID,

Check warning on line 97 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L97

Added line #L97 was not covered by tests
Address: in.Address,
Username: in.Username,
Password: in.Password,
}
if err := registry.Validate(); err != nil {
c.String(http.StatusUnprocessableEntity, "Error inserting org %q registry. %s", orgID, err)
c.String(http.StatusUnprocessableEntity, "Error inserting org %q registry. %s", org.ID, err)

Check warning on line 103 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L103

Added line #L103 was not covered by tests
return
}

registryService := server.Config.Services.Manager.RegistryService()
if err := registryService.OrgRegistryCreate(orgID, registry); err != nil {
c.String(http.StatusInternalServerError, "Error inserting org %q registry %q. %s", orgID, in.Address, err)
if err := registryService.OrgRegistryCreate(org.ID, registry); err != nil {
c.String(http.StatusInternalServerError, "Error inserting org %q registry %q. %s", org.ID, in.Address, err)

Check warning on line 109 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L108-L109

Added lines #L108 - L109 were not covered by tests
return
}
c.JSON(http.StatusOK, registry.Copy())
Expand All @@ -138,22 +124,17 @@
// @Param registry path string true "the registry's name"
// @Param registryData body Registry true "the update registry data"
func PatchOrgRegistry(c *gin.Context) {
org := session.Org(c)

Check warning on line 127 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L127

Added line #L127 was not covered by tests
addr := c.Param("registry")
orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

in := new(model.Registry)
err = c.Bind(in)
if err != nil {
if err := c.Bind(in); err != nil {

Check warning on line 131 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L131

Added line #L131 was not covered by tests
c.String(http.StatusBadRequest, "Error parsing registry. %s", err)
return
}

registryService := server.Config.Services.Manager.RegistryService()
registry, err := registryService.OrgRegistryFind(orgID, addr)
registry, err := registryService.OrgRegistryFind(org.ID, addr)

Check warning on line 137 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L137

Added line #L137 was not covered by tests
if err != nil {
handleDBError(c, err)
return
Expand All @@ -169,12 +150,12 @@
}

if err := registry.Validate(); err != nil {
c.String(http.StatusUnprocessableEntity, "Error updating org %q registry. %s", orgID, err)
c.String(http.StatusUnprocessableEntity, "Error updating org %q registry. %s", org.ID, err)

Check warning on line 153 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L153

Added line #L153 was not covered by tests
return
}

if err := registryService.OrgRegistryUpdate(orgID, registry); err != nil {
c.String(http.StatusInternalServerError, "Error updating org %q registry %q. %s", orgID, in.Address, err)
if err := registryService.OrgRegistryUpdate(org.ID, registry); err != nil {
c.String(http.StatusInternalServerError, "Error updating org %q registry %q. %s", org.ID, in.Address, err)

Check warning on line 158 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L157-L158

Added lines #L157 - L158 were not covered by tests
return
}
c.JSON(http.StatusOK, registry.Copy())
Expand All @@ -191,15 +172,11 @@
// @Param org_id path string true "the org's id"
// @Param registry path string true "the registry's name"
func DeleteOrgRegistry(c *gin.Context) {
org := session.Org(c)

Check warning on line 175 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L175

Added line #L175 was not covered by tests
addr := c.Param("registry")
orgID, err := strconv.ParseInt(c.Param("org_id"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing org id. %s", err)
return
}

registryService := server.Config.Services.Manager.RegistryService()
if err := registryService.OrgRegistryDelete(orgID, addr); err != nil {
if err := registryService.OrgRegistryDelete(org.ID, addr); err != nil {

Check warning on line 179 in server/api/org_registry.go

View check run for this annotation

Codecov / codecov/patch

server/api/org_registry.go#L179

Added line #L179 was not covered by tests
handleDBError(c, err)
return
}
Expand Down
Loading