refactor group creation such that we know the owner ID ahead of time, bailing out if both ownerID and parentGroupID are < 1

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧
2026-04-02 20:00:53 -04:00
parent 52d17c410a
commit 60dc00a165
+19 -13
View File
@@ -1,6 +1,7 @@
package group package group
import ( import (
"fmt"
"net/http" "net/http"
"strings" "strings"
@@ -12,12 +13,22 @@ import (
group_service "code.gitea.io/gitea/services/group" group_service "code.gitea.io/gitea/services/group"
) )
func createCommonGroup(ctx *context.APIContext, parentGroupID int64) (*api.Group, error) { func createCommonGroup(ctx *context.APIContext, parentGroupID, ownerID int64) (*api.Group, error) {
if ownerID < 1 {
if parentGroupID < 1 {
return nil, fmt.Errorf("cannot determine new group's owner")
}
npg, err := group_model.GetGroupByID(ctx, parentGroupID)
if err != nil {
return nil, err
}
ownerID = npg.OwnerID
}
form := web.GetForm(ctx).(*api.NewGroupOption) form := web.GetForm(ctx).(*api.NewGroupOption)
group := &group_model.Group{ group := &group_model.Group{
Name: form.Name, Name: form.Name,
Description: form.Description, Description: form.Description,
OwnerID: ctx.Org.Organization.ID, OwnerID: ownerID,
LowerName: strings.ToLower(form.Name), LowerName: strings.ToLower(form.Name),
Visibility: form.Visibility, Visibility: form.Visibility,
ParentGroupID: parentGroupID, ParentGroupID: parentGroupID,
@@ -45,6 +56,7 @@ func NewGroup(ctx *context.APIContext) {
// required: true // required: true
// - name: body // - name: body
// in: body // in: body
// required: true
// schema: // schema:
// "$ref": "#/definitions/CreateGroupOption" // "$ref": "#/definitions/CreateGroupOption"
// responses: // responses:
@@ -54,7 +66,7 @@ func NewGroup(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
// "422": // "422":
// "$ref": "#/responses/validationError" // "$ref": "#/responses/validationError"
ag, err := createCommonGroup(ctx, 0) ag, err := createCommonGroup(ctx, 0, ctx.Org.Organization.ID)
if err != nil { if err != nil {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
return return
@@ -80,6 +92,7 @@ func NewSubGroup(ctx *context.APIContext) {
// required: true // required: true
// - name: body // - name: body
// in: body // in: body
// required: true
// schema: // schema:
// "$ref": "#/definitions/CreateGroupOption" // "$ref": "#/definitions/CreateGroupOption"
// responses: // responses:
@@ -94,7 +107,7 @@ func NewSubGroup(ctx *context.APIContext) {
err error err error
) )
gid := ctx.PathParamInt64("group_id") gid := ctx.PathParamInt64("group_id")
group, err = createCommonGroup(ctx, gid) group, err = createCommonGroup(ctx, gid, 0)
if err != nil { if err != nil {
ctx.APIErrorInternal(err) ctx.APIErrorInternal(err)
return return
@@ -120,6 +133,7 @@ func MoveGroup(ctx *context.APIContext) {
// required: true // required: true
// - name: body // - name: body
// in: body // in: body
// required: true
// schema: // schema:
// "$ref": "#/definitions/MoveGroupOption" // "$ref": "#/definitions/MoveGroupOption"
// responses: // responses:
@@ -185,6 +199,7 @@ func EditGroup(ctx *context.APIContext) {
// required: true // required: true
// - name: body // - name: body
// in: body // in: body
// required: true
// schema: // schema:
// "$ref": "#/definitions/EditGroupOption" // "$ref": "#/definitions/EditGroupOption"
// responses: // responses:
@@ -245,10 +260,6 @@ func GetGroup(ctx *context.APIContext) {
// type: integer // type: integer
// format: int64 // format: int64
// required: true // required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/EditGroupOption"
// responses: // responses:
// "200": // "200":
// "$ref": "#/responses/Group" // "$ref": "#/responses/Group"
@@ -288,11 +299,6 @@ func DeleteGroup(ctx *context.APIContext) {
// produces: // produces:
// - application/json // - application/json
// parameters: // parameters:
// - name: owner
// in: path
// description: owner of the group to delete
// type: string
// required: true
// - name: group_id // - name: group_id
// in: path // in: path
// description: id of the group to delete // description: id of the group to delete