2022-08-14 11:48:53 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"net/http"
|
2023-07-21 17:45:32 +00:00
|
|
|
"strings"
|
2022-08-14 11:48:53 +00:00
|
|
|
|
2023-09-02 11:31:10 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-01-14 17:22:06 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-09-02 11:31:10 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/router/middleware/session"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2022-08-14 11:48:53 +00:00
|
|
|
)
|
|
|
|
|
2024-06-20 11:08:54 +00:00
|
|
|
// GetOrgs
|
|
|
|
//
|
|
|
|
// @Summary List organizations
|
|
|
|
// @Description Returns all registered orgs in the system. Requires admin rights.
|
|
|
|
// @Router /orgs [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} Org
|
|
|
|
// @Tags Orgs
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @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 GetOrgs(c *gin.Context) {
|
|
|
|
orgs, err := store.FromContext(c).OrgList(session.Pagination(c))
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Error getting user list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, orgs)
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
// GetOrg
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Get an organization
|
2023-07-21 17:45:32 +00:00
|
|
|
// @Router /orgs/{org_id} [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} Org
|
|
|
|
// @Tags Organization
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2024-06-04 06:30:54 +00:00
|
|
|
// @Param org_id path string true "the organization's id"
|
2023-07-21 17:45:32 +00:00
|
|
|
func GetOrg(c *gin.Context) {
|
2024-10-09 10:05:01 +00:00
|
|
|
org := session.Org(c)
|
2023-07-21 17:45:32 +00:00
|
|
|
c.JSON(http.StatusOK, org)
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetOrgPermissions
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Get the permissions of the currently authenticated user for the given organization
|
2023-07-21 17:45:32 +00:00
|
|
|
// @Router /orgs/{org_id}/permissions [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} OrgPerm
|
|
|
|
// @Tags Organization permissions
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2024-06-04 06:30:54 +00:00
|
|
|
// @Param org_id path string true "the organization's id"
|
2022-08-14 11:48:53 +00:00
|
|
|
func GetOrgPermissions(c *gin.Context) {
|
2023-07-21 17:45:32 +00:00
|
|
|
user := session.User(c)
|
2024-10-09 10:05:01 +00:00
|
|
|
org := session.Org(c)
|
2023-07-21 17:45:32 +00:00
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromUser(user)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from user")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-14 11:48:53 +00:00
|
|
|
if user == nil {
|
|
|
|
c.JSON(http.StatusOK, &model.OrgPerm{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:04:12 +00:00
|
|
|
if (org.IsUser && org.Name == user.Login) || (user.Admin && !org.IsUser) {
|
2023-07-21 17:45:32 +00:00
|
|
|
c.JSON(http.StatusOK, &model.OrgPerm{
|
|
|
|
Member: true,
|
|
|
|
Admin: true,
|
|
|
|
})
|
|
|
|
return
|
2023-08-21 13:04:12 +00:00
|
|
|
} else if org.IsUser {
|
|
|
|
c.JSON(http.StatusOK, &model.OrgPerm{})
|
|
|
|
return
|
2023-07-21 17:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
perm, err := server.Config.Services.Membership.Get(c, _forge, user, org.Name)
|
2023-07-21 17:45:32 +00:00
|
|
|
if err != nil {
|
2024-10-09 10:05:01 +00:00
|
|
|
c.String(http.StatusInternalServerError, "Error getting membership for %d. %s", org.ID, err)
|
2022-08-14 11:48:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, perm)
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
|
|
|
// LookupOrg
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Lookup an organization by full name
|
2023-07-21 17:45:32 +00:00
|
|
|
// @Router /org/lookup/{org_full_name} [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Org
|
|
|
|
// @Tags Organizations
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Param org_full_name path string true "the organizations full name / slug"
|
2023-07-21 17:45:32 +00:00
|
|
|
func LookupOrg(c *gin.Context) {
|
|
|
|
_store := store.FromContext(c)
|
2024-04-16 06:04:55 +00:00
|
|
|
user := session.User(c)
|
|
|
|
_forge, err := server.Config.Services.Manager.ForgeFromUser(user)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Cannot get forge from user")
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
|
|
|
|
orgFullName := strings.TrimLeft(c.Param("org_full_name"), "/")
|
|
|
|
|
|
|
|
org, err := _store.OrgFindByName(orgFullName)
|
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, err)
|
2023-07-21 17:45:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't leak private org infos
|
|
|
|
if org.Private {
|
|
|
|
user := session.User(c)
|
|
|
|
if user == nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.Admin && org.Name != user.Login {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if !user.Admin {
|
2024-04-16 06:04:55 +00:00
|
|
|
perm, err := server.Config.Services.Membership.Get(c, _forge, user, org.Name)
|
2023-07-21 17:45:32 +00:00
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msg("failed to check membership")
|
2023-09-02 11:31:10 +00:00
|
|
|
c.Status(http.StatusInternalServerError)
|
2023-07-21 17:45:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if perm == nil || !perm.Member {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, org)
|
|
|
|
}
|
2024-06-20 11:08:54 +00:00
|
|
|
|
|
|
|
// DeleteOrg
|
|
|
|
//
|
|
|
|
// @Summary Delete an organization
|
|
|
|
// @Description Deletes the given org. Requires admin rights.
|
|
|
|
// @Router /orgs/{id} [delete]
|
|
|
|
// @Produce plain
|
|
|
|
// @Success 204
|
|
|
|
// @Tags Orgs
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param id path string true "the org's id"
|
|
|
|
func DeleteOrg(c *gin.Context) {
|
|
|
|
_store := store.FromContext(c)
|
2024-10-09 10:05:01 +00:00
|
|
|
org := session.Org(c)
|
2024-06-20 11:08:54 +00:00
|
|
|
|
2024-10-09 10:05:01 +00:00
|
|
|
if err := _store.OrgDelete(org.ID); err != nil {
|
2024-06-20 11:08:54 +00:00
|
|
|
handleDBError(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
}
|