Skip to content

Commit 9b58f7c

Browse files
refactor group creation such that we know the owner ID ahead of time, bailing out if both ownerID and parentGroupID are < 1
1 parent 202196d commit 9b58f7c

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

routers/api/v1/group/group.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package group
22

33
import (
4+
"fmt"
45
"net/http"
56
"strings"
67

@@ -12,12 +13,22 @@ import (
1213
group_service "code.gitea.io/gitea/services/group"
1314
)
1415

15-
func createCommonGroup(ctx *context.APIContext, parentGroupID int64) (*api.Group, error) {
16+
func createCommonGroup(ctx *context.APIContext, parentGroupID, ownerID int64) (*api.Group, error) {
17+
if ownerID < 1 {
18+
if parentGroupID < 1 {
19+
return nil, fmt.Errorf("cannot determine new group's owner")
20+
}
21+
npg, err := group_model.GetGroupByID(ctx, parentGroupID)
22+
if err != nil {
23+
return nil, err
24+
}
25+
ownerID = npg.OwnerID
26+
}
1627
form := web.GetForm(ctx).(*api.NewGroupOption)
1728
group := &group_model.Group{
1829
Name: form.Name,
1930
Description: form.Description,
20-
OwnerID: ctx.Org.Organization.ID,
31+
OwnerID: ownerID,
2132
LowerName: strings.ToLower(form.Name),
2233
Visibility: form.Visibility,
2334
ParentGroupID: parentGroupID,
@@ -45,6 +56,7 @@ func NewGroup(ctx *context.APIContext) {
4556
// required: true
4657
// - name: body
4758
// in: body
59+
// required: true
4860
// schema:
4961
// "$ref": "#/definitions/CreateGroupOption"
5062
// responses:
@@ -54,7 +66,7 @@ func NewGroup(ctx *context.APIContext) {
5466
// "$ref": "#/responses/notFound"
5567
// "422":
5668
// "$ref": "#/responses/validationError"
57-
ag, err := createCommonGroup(ctx, 0)
69+
ag, err := createCommonGroup(ctx, 0, ctx.Org.Organization.ID)
5870
if err != nil {
5971
ctx.APIErrorInternal(err)
6072
return
@@ -80,6 +92,7 @@ func NewSubGroup(ctx *context.APIContext) {
8092
// required: true
8193
// - name: body
8294
// in: body
95+
// required: true
8396
// schema:
8497
// "$ref": "#/definitions/CreateGroupOption"
8598
// responses:
@@ -94,7 +107,7 @@ func NewSubGroup(ctx *context.APIContext) {
94107
err error
95108
)
96109
gid := ctx.PathParamInt64("group_id")
97-
group, err = createCommonGroup(ctx, gid)
110+
group, err = createCommonGroup(ctx, gid, 0)
98111
if err != nil {
99112
ctx.APIErrorInternal(err)
100113
return
@@ -120,6 +133,7 @@ func MoveGroup(ctx *context.APIContext) {
120133
// required: true
121134
// - name: body
122135
// in: body
136+
// required: true
123137
// schema:
124138
// "$ref": "#/definitions/MoveGroupOption"
125139
// responses:
@@ -185,6 +199,7 @@ func EditGroup(ctx *context.APIContext) {
185199
// required: true
186200
// - name: body
187201
// in: body
202+
// required: true
188203
// schema:
189204
// "$ref": "#/definitions/EditGroupOption"
190205
// responses:
@@ -245,10 +260,6 @@ func GetGroup(ctx *context.APIContext) {
245260
// type: integer
246261
// format: int64
247262
// required: true
248-
// - name: body
249-
// in: body
250-
// schema:
251-
// "$ref": "#/definitions/EditGroupOption"
252263
// responses:
253264
// "200":
254265
// "$ref": "#/responses/Group"
@@ -288,11 +299,6 @@ func DeleteGroup(ctx *context.APIContext) {
288299
// produces:
289300
// - application/json
290301
// parameters:
291-
// - name: owner
292-
// in: path
293-
// description: owner of the group to delete
294-
// type: string
295-
// required: true
296302
// - name: group_id
297303
// in: path
298304
// description: id of the group to delete

0 commit comments

Comments
 (0)