Skip to content

Commit 948516d

Browse files
authored
Merge branch 'main' into main
2 parents af61490 + 59b867d commit 948516d

File tree

23 files changed

+243
-319
lines changed

23 files changed

+243
-319
lines changed

integrations/api_user_org_perm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestUnknowUser(t *testing.T) {
133133

134134
var apiError api.APIError
135135
DecodeJSON(t, resp, &apiError)
136-
assert.Equal(t, "GetUserByName", apiError.Message)
136+
assert.Equal(t, "user redirect does not exist [name: unknow]", apiError.Message)
137137
}
138138

139139
func TestUnknowOrganization(t *testing.T) {

modules/context/context.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ type Context struct {
6767
IsSigned bool
6868
IsBasicAuth bool
6969

70-
Repo *Repository
71-
Org *Organization
70+
ContextUser *user_model.User
71+
Repo *Repository
72+
Org *Organization
7273
}
7374

7475
// TrHTMLEscapeArgs runs Tr but pre-escapes all arguments with html.EscapeString.

modules/context/org.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
5353
var err error
5454
ctx.Org.Organization, err = models.GetOrgByName(orgName)
5555
if err != nil {
56-
if user_model.IsErrUserNotExist(err) {
56+
if models.IsErrOrgNotExist(err) {
5757
redirectUserID, err := user_model.LookupUserRedirect(orgName)
5858
if err == nil {
5959
RedirectToUser(ctx, orgName, redirectUserID)
@@ -68,6 +68,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
6868
return
6969
}
7070
org := ctx.Org.Organization
71+
ctx.ContextUser = org.AsUser()
7172
ctx.Data["Org"] = org
7273

7374
teams, err := org.LoadTeams()

modules/context/repo.go

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
439439
}
440440
}
441441
ctx.Repo.Owner = owner
442+
ctx.ContextUser = owner
442443
ctx.Data["Username"] = ctx.Repo.Owner.Name
443444

444445
// redirect link to wiki

routers/api/v1/admin/org.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/convert"
1616
api "code.gitea.io/gitea/modules/structs"
1717
"code.gitea.io/gitea/modules/web"
18-
"code.gitea.io/gitea/routers/api/v1/user"
1918
"code.gitea.io/gitea/routers/api/v1/utils"
2019
)
2120

@@ -45,11 +44,8 @@ func CreateOrg(ctx *context.APIContext) {
4544
// "$ref": "#/responses/forbidden"
4645
// "422":
4746
// "$ref": "#/responses/validationError"
47+
4848
form := web.GetForm(ctx).(*api.CreateOrgOption)
49-
u := user.GetUserByParams(ctx)
50-
if ctx.Written() {
51-
return
52-
}
5349

5450
visibility := api.VisibleTypePublic
5551
if form.Visibility != "" {
@@ -67,7 +63,7 @@ func CreateOrg(ctx *context.APIContext) {
6763
Visibility: visibility,
6864
}
6965

70-
if err := models.CreateOrganization(org, u); err != nil {
66+
if err := models.CreateOrganization(org, ctx.ContextUser); err != nil {
7167
if user_model.IsErrUserAlreadyExist(err) ||
7268
db.IsErrNameReserved(err) ||
7369
db.IsErrNameCharsNotAllowed(err) ||

routers/api/v1/admin/repo.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
api "code.gitea.io/gitea/modules/structs"
1010
"code.gitea.io/gitea/modules/web"
1111
"code.gitea.io/gitea/routers/api/v1/repo"
12-
"code.gitea.io/gitea/routers/api/v1/user"
1312
)
1413

1514
// CreateRepo api for creating a repository
@@ -42,11 +41,8 @@ func CreateRepo(ctx *context.APIContext) {
4241
// "$ref": "#/responses/error"
4342
// "422":
4443
// "$ref": "#/responses/validationError"
44+
4545
form := web.GetForm(ctx).(*api.CreateRepoOption)
46-
owner := user.GetUserByParams(ctx)
47-
if ctx.Written() {
48-
return
49-
}
5046

51-
repo.CreateUserRepo(ctx, owner, *form)
47+
repo.CreateUserRepo(ctx, ctx.ContextUser, *form)
5248
}

routers/api/v1/admin/user.go

+34-48
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func CreateUser(ctx *context.APIContext) {
7373
// "$ref": "#/responses/forbidden"
7474
// "422":
7575
// "$ref": "#/responses/validationError"
76+
7677
form := web.GetForm(ctx).(*api.CreateUserOption)
7778

7879
u := &user_model.User{
@@ -163,13 +164,10 @@ func EditUser(ctx *context.APIContext) {
163164
// "$ref": "#/responses/forbidden"
164165
// "422":
165166
// "$ref": "#/responses/validationError"
167+
166168
form := web.GetForm(ctx).(*api.EditUserOption)
167-
u := user.GetUserByParams(ctx)
168-
if ctx.Written() {
169-
return
170-
}
171169

172-
parseAuthSource(ctx, u, form.SourceID, form.LoginName)
170+
parseAuthSource(ctx, ctx.ContextUser, form.SourceID, form.LoginName)
173171
if ctx.Written() {
174172
return
175173
}
@@ -193,24 +191,24 @@ func EditUser(ctx *context.APIContext) {
193191
ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned"))
194192
return
195193
}
196-
if u.Salt, err = user_model.GetUserSalt(); err != nil {
194+
if ctx.ContextUser.Salt, err = user_model.GetUserSalt(); err != nil {
197195
ctx.Error(http.StatusInternalServerError, "UpdateUser", err)
198196
return
199197
}
200-
if err = u.SetPassword(form.Password); err != nil {
198+
if err = ctx.ContextUser.SetPassword(form.Password); err != nil {
201199
ctx.InternalServerError(err)
202200
return
203201
}
204202
}
205203

206204
if form.MustChangePassword != nil {
207-
u.MustChangePassword = *form.MustChangePassword
205+
ctx.ContextUser.MustChangePassword = *form.MustChangePassword
208206
}
209207

210-
u.LoginName = form.LoginName
208+
ctx.ContextUser.LoginName = form.LoginName
211209

212210
if form.FullName != nil {
213-
u.FullName = *form.FullName
211+
ctx.ContextUser.FullName = *form.FullName
214212
}
215213
var emailChanged bool
216214
if form.Email != nil {
@@ -225,47 +223,47 @@ func EditUser(ctx *context.APIContext) {
225223
return
226224
}
227225

228-
emailChanged = !strings.EqualFold(u.Email, email)
229-
u.Email = email
226+
emailChanged = !strings.EqualFold(ctx.ContextUser.Email, email)
227+
ctx.ContextUser.Email = email
230228
}
231229
if form.Website != nil {
232-
u.Website = *form.Website
230+
ctx.ContextUser.Website = *form.Website
233231
}
234232
if form.Location != nil {
235-
u.Location = *form.Location
233+
ctx.ContextUser.Location = *form.Location
236234
}
237235
if form.Description != nil {
238-
u.Description = *form.Description
236+
ctx.ContextUser.Description = *form.Description
239237
}
240238
if form.Active != nil {
241-
u.IsActive = *form.Active
239+
ctx.ContextUser.IsActive = *form.Active
242240
}
243241
if len(form.Visibility) != 0 {
244-
u.Visibility = api.VisibilityModes[form.Visibility]
242+
ctx.ContextUser.Visibility = api.VisibilityModes[form.Visibility]
245243
}
246244
if form.Admin != nil {
247-
u.IsAdmin = *form.Admin
245+
ctx.ContextUser.IsAdmin = *form.Admin
248246
}
249247
if form.AllowGitHook != nil {
250-
u.AllowGitHook = *form.AllowGitHook
248+
ctx.ContextUser.AllowGitHook = *form.AllowGitHook
251249
}
252250
if form.AllowImportLocal != nil {
253-
u.AllowImportLocal = *form.AllowImportLocal
251+
ctx.ContextUser.AllowImportLocal = *form.AllowImportLocal
254252
}
255253
if form.MaxRepoCreation != nil {
256-
u.MaxRepoCreation = *form.MaxRepoCreation
254+
ctx.ContextUser.MaxRepoCreation = *form.MaxRepoCreation
257255
}
258256
if form.AllowCreateOrganization != nil {
259-
u.AllowCreateOrganization = *form.AllowCreateOrganization
257+
ctx.ContextUser.AllowCreateOrganization = *form.AllowCreateOrganization
260258
}
261259
if form.ProhibitLogin != nil {
262-
u.ProhibitLogin = *form.ProhibitLogin
260+
ctx.ContextUser.ProhibitLogin = *form.ProhibitLogin
263261
}
264262
if form.Restricted != nil {
265-
u.IsRestricted = *form.Restricted
263+
ctx.ContextUser.IsRestricted = *form.Restricted
266264
}
267265

268-
if err := user_model.UpdateUser(u, emailChanged); err != nil {
266+
if err := user_model.UpdateUser(ctx.ContextUser, emailChanged); err != nil {
269267
if user_model.IsErrEmailAlreadyUsed(err) ||
270268
user_model.IsErrEmailCharIsNotSupported(err) ||
271269
user_model.IsErrEmailInvalid(err) {
@@ -275,9 +273,9 @@ func EditUser(ctx *context.APIContext) {
275273
}
276274
return
277275
}
278-
log.Trace("Account profile updated by admin (%s): %s", ctx.Doer.Name, u.Name)
276+
log.Trace("Account profile updated by admin (%s): %s", ctx.Doer.Name, ctx.ContextUser.Name)
279277

280-
ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.Doer))
278+
ctx.JSON(http.StatusOK, convert.ToUser(ctx.ContextUser, ctx.Doer))
281279
}
282280

283281
// DeleteUser api for deleting a user
@@ -301,17 +299,12 @@ func DeleteUser(ctx *context.APIContext) {
301299
// "422":
302300
// "$ref": "#/responses/validationError"
303301

304-
u := user.GetUserByParams(ctx)
305-
if ctx.Written() {
306-
return
307-
}
308-
309-
if u.IsOrganization() {
310-
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("%s is an organization not a user", u.Name))
302+
if ctx.ContextUser.IsOrganization() {
303+
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("%s is an organization not a user", ctx.ContextUser.Name))
311304
return
312305
}
313306

314-
if err := user_service.DeleteUser(u); err != nil {
307+
if err := user_service.DeleteUser(ctx.ContextUser); err != nil {
315308
if models.IsErrUserOwnRepos(err) ||
316309
models.IsErrUserHasOrgs(err) {
317310
ctx.Error(http.StatusUnprocessableEntity, "", err)
@@ -320,7 +313,7 @@ func DeleteUser(ctx *context.APIContext) {
320313
}
321314
return
322315
}
323-
log.Trace("Account deleted by admin(%s): %s", ctx.Doer.Name, u.Name)
316+
log.Trace("Account deleted by admin(%s): %s", ctx.Doer.Name, ctx.ContextUser.Name)
324317

325318
ctx.Status(http.StatusNoContent)
326319
}
@@ -351,12 +344,10 @@ func CreatePublicKey(ctx *context.APIContext) {
351344
// "$ref": "#/responses/forbidden"
352345
// "422":
353346
// "$ref": "#/responses/validationError"
347+
354348
form := web.GetForm(ctx).(*api.CreateKeyOption)
355-
u := user.GetUserByParams(ctx)
356-
if ctx.Written() {
357-
return
358-
}
359-
user.CreateUserPublicKey(ctx, *form, u.ID)
349+
350+
user.CreateUserPublicKey(ctx, *form, ctx.ContextUser.ID)
360351
}
361352

362353
// DeleteUserPublicKey api for deleting a user's public key
@@ -386,12 +377,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
386377
// "404":
387378
// "$ref": "#/responses/notFound"
388379

389-
u := user.GetUserByParams(ctx)
390-
if ctx.Written() {
391-
return
392-
}
393-
394-
if err := asymkey_service.DeletePublicKey(u, ctx.ParamsInt64(":id")); err != nil {
380+
if err := asymkey_service.DeletePublicKey(ctx.ContextUser, ctx.ParamsInt64(":id")); err != nil {
395381
if asymkey_model.IsErrKeyNotExist(err) {
396382
ctx.NotFound()
397383
} else if asymkey_model.IsErrKeyAccessDenied(err) {
@@ -401,7 +387,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
401387
}
402388
return
403389
}
404-
log.Trace("Key deleted by admin(%s): %s", ctx.Doer.Name, u.Name)
390+
log.Trace("Key deleted by admin(%s): %s", ctx.Doer.Name, ctx.ContextUser.Name)
405391

406392
ctx.Status(http.StatusNoContent)
407393
}

routers/api/v1/api.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import (
8787
"code.gitea.io/gitea/routers/api/v1/settings"
8888
"code.gitea.io/gitea/routers/api/v1/user"
8989
"code.gitea.io/gitea/services/auth"
90+
context_service "code.gitea.io/gitea/services/context"
9091
"code.gitea.io/gitea/services/forms"
9192

9293
_ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
@@ -156,6 +157,7 @@ func repoAssignment() func(ctx *context.APIContext) {
156157
}
157158
}
158159
ctx.Repo.Owner = owner
160+
ctx.ContextUser = owner
159161

160162
// Get repository.
161163
repo, err := repo_model.GetRepositoryByName(owner.ID, repoName)
@@ -441,6 +443,7 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
441443
}
442444
return
443445
}
446+
ctx.ContextUser = ctx.Org.Organization.AsUser()
444447
}
445448

446449
if assignTeam {
@@ -636,7 +639,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
636639
Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
637640
m.Combo("/{id}").Delete(user.DeleteAccessToken)
638641
}, reqBasicOrRevProxyAuth())
639-
})
642+
}, context_service.UserAssignmentAPI())
640643
})
641644

642645
m.Group("/users", func() {
@@ -653,7 +656,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
653656
m.Get("/starred", user.GetStarredRepos)
654657

655658
m.Get("/subscriptions", user.GetWatchedRepos)
656-
})
659+
}, context_service.UserAssignmentAPI())
657660
}, reqToken())
658661

659662
m.Group("/user", func() {
@@ -669,7 +672,11 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
669672
m.Get("/followers", user.ListMyFollowers)
670673
m.Group("/following", func() {
671674
m.Get("", user.ListMyFollowing)
672-
m.Combo("/{username}").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
675+
m.Group("/{username}", func() {
676+
m.Get("", user.CheckMyFollowing)
677+
m.Put("", user.Follow)
678+
m.Delete("", user.Unfollow)
679+
}, context_service.UserAssignmentAPI())
673680
})
674681

675682
m.Group("/keys", func() {
@@ -1005,7 +1012,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
10051012
m.Group("/users/{username}/orgs", func() {
10061013
m.Get("", org.ListUserOrgs)
10071014
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
1008-
})
1015+
}, context_service.UserAssignmentAPI())
10091016
m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create)
10101017
m.Get("/orgs", org.GetAll)
10111018
m.Group("/orgs/{org}", func() {
@@ -1083,7 +1090,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
10831090
m.Get("/orgs", org.ListUserOrgs)
10841091
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
10851092
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
1086-
})
1093+
}, context_service.UserAssignmentAPI())
10871094
})
10881095
m.Group("/unadopted", func() {
10891096
m.Get("", admin.ListUnadoptedRepositories)

0 commit comments

Comments
 (0)