Skip to content

Commit 281a038

Browse files
authored
fix admin lost permission caused by #947 (#1753)
2 parents 25d6e2a + 6362462 commit 281a038

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

cmd/serv.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func runServ(c *cli.Context) error {
250250
user.Name, requestedMode, repoPath)
251251
}
252252

253-
if !repo.CheckUnitUser(user.ID, unitType) {
253+
if !repo.CheckUnitUser(user.ID, user.IsAdmin, unitType) {
254254
fail("You do not have allowed for this action",
255255
"User %s does not have allowed access to repository %s 's code",
256256
user.Name, repoPath)

models/repo.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ func (repo *Repository) getUnits(e Engine) (err error) {
330330
}
331331

332332
// CheckUnitUser check whether user could visit the unit of this repository
333-
func (repo *Repository) CheckUnitUser(userID int64, unitType UnitType) bool {
334-
if err := repo.getUnitsByUserID(x, userID); err != nil {
333+
func (repo *Repository) CheckUnitUser(userID int64, isAdmin bool, unitType UnitType) bool {
334+
if err := repo.getUnitsByUserID(x, userID, isAdmin); err != nil {
335335
return false
336336
}
337337

@@ -344,11 +344,11 @@ func (repo *Repository) CheckUnitUser(userID int64, unitType UnitType) bool {
344344
}
345345

346346
// LoadUnitsByUserID loads units according userID's permissions
347-
func (repo *Repository) LoadUnitsByUserID(userID int64) error {
348-
return repo.getUnitsByUserID(x, userID)
347+
func (repo *Repository) LoadUnitsByUserID(userID int64, isAdmin bool) error {
348+
return repo.getUnitsByUserID(x, userID, isAdmin)
349349
}
350350

351-
func (repo *Repository) getUnitsByUserID(e Engine, userID int64) (err error) {
351+
func (repo *Repository) getUnitsByUserID(e Engine, userID int64, isAdmin bool) (err error) {
352352
if repo.Units != nil {
353353
return nil
354354
}
@@ -358,7 +358,7 @@ func (repo *Repository) getUnitsByUserID(e Engine, userID int64) (err error) {
358358
return err
359359
}
360360

361-
if !repo.Owner.IsOrganization() || userID == 0 {
361+
if !repo.Owner.IsOrganization() || userID == 0 || isAdmin {
362362
return nil
363363
}
364364

modules/context/repo.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,16 @@ func RequireRepoWriter() macaron.Handler {
496496
// LoadRepoUnits loads repsitory's units, it should be called after repository and user loaded
497497
func LoadRepoUnits() macaron.Handler {
498498
return func(ctx *Context) {
499+
var isAdmin bool
500+
if ctx.User != nil && ctx.User.IsAdmin {
501+
isAdmin = true
502+
}
503+
499504
var userID int64
500505
if ctx.User != nil {
501506
userID = ctx.User.ID
502507
}
503-
err := ctx.Repo.Repository.LoadUnitsByUserID(userID)
508+
err := ctx.Repo.Repository.LoadUnitsByUserID(userID, isAdmin)
504509
if err != nil {
505510
ctx.Handle(500, "LoadUnitsByUserID", err)
506511
return

routers/repo/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func HTTP(ctx *context.Context) {
206206
}
207207
}
208208

209-
if !repo.CheckUnitUser(authUser.ID, unitType) {
209+
if !repo.CheckUnitUser(authUser.ID, authUser.IsAdmin, unitType) {
210210
ctx.HandleText(http.StatusForbidden, fmt.Sprintf("User %s does not have allowed access to repository %s 's code",
211211
authUser.Name, repo.RepoPath()))
212212
return

0 commit comments

Comments
 (0)