Skip to content

Commit b93b534

Browse files
committed
some improvements
1 parent c782b08 commit b93b534

File tree

16 files changed

+60
-64
lines changed

16 files changed

+60
-64
lines changed

models/notification.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
269269

270270
return err
271271
}
272-
if issue.IsPull && !checkUnitUser(ctx, issue.Repo, user, unit.TypePullRequests) {
272+
if issue.IsPull && !checkRepoUnitUser(ctx, issue.Repo, user, unit.TypePullRequests) {
273273
continue
274274
}
275-
if !issue.IsPull && !checkUnitUser(ctx, issue.Repo, user, unit.TypeIssues) {
275+
if !issue.IsPull && !checkRepoUnitUser(ctx, issue.Repo, user, unit.TypeIssues) {
276276
continue
277277
}
278278

models/org_team.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ func removeTeamMember(ctx context.Context, team *Team, userID int64) error {
909909
}
910910

911911
// Remove issue assignments from now unaccessible
912-
if err := reconsiderIssueAssignees(ctx, repo, userID); err != nil {
912+
if err := reconsiderRepoIssuesAssignee(ctx, repo, userID); err != nil {
913913
return err
914914
}
915915
}

models/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ func NewRepoContext() {
126126
admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
127127
}
128128

129-
// CheckUnitUser check whether user could visit the unit of this repository
130-
func CheckUnitUser(repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
131-
return checkUnitUser(db.DefaultContext, repo, user, unitType)
129+
// CheckRepoUnitUser check whether user could visit the unit of this repository
130+
func CheckRepoUnitUser(repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
131+
return checkRepoUnitUser(db.DefaultContext, repo, user, unitType)
132132
}
133133

134-
func checkUnitUser(ctx context.Context, repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
134+
func checkRepoUnitUser(ctx context.Context, repo *repo_model.Repository, user *user_model.User, unitType unit.Type) bool {
135135
if user.IsAdmin {
136136
return true
137137
}

models/repo/repo_unit.go

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package repo
66

77
import (
8-
"encoding/binary"
98
"fmt"
109

1110
"code.gitea.io/gitea/models/db"
@@ -46,41 +45,12 @@ func init() {
4645
db.RegisterModel(new(RepoUnit))
4746
}
4847

49-
// JSONUnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
50-
// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
51-
func JSONUnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
52-
err := json.Unmarshal(bs, v)
53-
if err != nil {
54-
ok := true
55-
rs := []byte{}
56-
temp := make([]byte, 2)
57-
for _, rn := range string(bs) {
58-
if rn > 0xffff {
59-
ok = false
60-
break
61-
}
62-
binary.LittleEndian.PutUint16(temp, uint16(rn))
63-
rs = append(rs, temp...)
64-
}
65-
if ok {
66-
if len(rs) > 1 && rs[0] == 0xff && rs[1] == 0xfe {
67-
rs = rs[2:]
68-
}
69-
err = json.Unmarshal(rs, v)
70-
}
71-
}
72-
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
73-
err = json.Unmarshal(bs[2:], v)
74-
}
75-
return err
76-
}
77-
7848
// UnitConfig describes common unit config
7949
type UnitConfig struct{}
8050

8151
// FromDB fills up a UnitConfig from serialized format.
8252
func (cfg *UnitConfig) FromDB(bs []byte) error {
83-
return JSONUnmarshalHandleDoubleEncode(bs, &cfg)
53+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
8454
}
8555

8656
// ToDB exports a UnitConfig to a serialized format.
@@ -95,7 +65,7 @@ type ExternalUncycloConfig struct {
9565

9666
// FromDB fills up a ExternalUncycloConfig from serialized format.
9767
func (cfg *ExternalUncycloConfig) FromDB(bs []byte) error {
98-
return JSONUnmarshalHandleDoubleEncode(bs, &cfg)
68+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
9969
}
10070

10171
// ToDB exports a ExternalUncycloConfig to a serialized format.
@@ -112,7 +82,7 @@ type ExternalTrackerConfig struct {
11282

11383
// FromDB fills up a ExternalTrackerConfig from serialized format.
11484
func (cfg *ExternalTrackerConfig) FromDB(bs []byte) error {
115-
return JSONUnmarshalHandleDoubleEncode(bs, &cfg)
85+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
11686
}
11787

11888
// ToDB exports a ExternalTrackerConfig to a serialized format.
@@ -129,7 +99,7 @@ type IssuesConfig struct {
12999

130100
// FromDB fills up a IssuesConfig from serialized format.
131101
func (cfg *IssuesConfig) FromDB(bs []byte) error {
132-
return JSONUnmarshalHandleDoubleEncode(bs, &cfg)
102+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
133103
}
134104

135105
// ToDB exports a IssuesConfig to a serialized format.
@@ -152,7 +122,7 @@ type PullRequestsConfig struct {
152122

153123
// FromDB fills up a PullRequestsConfig from serialized format.
154124
func (cfg *PullRequestsConfig) FromDB(bs []byte) error {
155-
return JSONUnmarshalHandleDoubleEncode(bs, &cfg)
125+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
156126
}
157127

158128
// ToDB exports a PullRequestsConfig to a serialized format.

models/repo_collaboration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ func DeleteCollaboration(repo *repo_model.Repository, uid int64) (err error) {
224224
}
225225

226226
// Unassign a user from any issue (s)he has been assigned to in the repository
227-
if err := reconsiderIssueAssignees(ctx, repo, uid); err != nil {
227+
if err := reconsiderRepoIssuesAssignee(ctx, repo, uid); err != nil {
228228
return err
229229
}
230230

231231
return committer.Commit()
232232
}
233233

234-
func reconsiderIssueAssignees(ctx context.Context, repo *repo_model.Repository, uid int64) error {
234+
func reconsiderRepoIssuesAssignee(ctx context.Context, repo *repo_model.Repository, uid int64) error {
235235
user, err := user_model.GetUserByIDEngine(db.GetEngine(ctx), uid)
236236
if err != nil {
237237
return err

modules/doctor/fix16961.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models/db"
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
"code.gitea.io/gitea/models/unit"
14+
"code.gitea.io/gitea/modules/json"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/timeutil"
1617

@@ -37,7 +38,7 @@ func parseBool16961(bs []byte) (bool, error) {
3738
}
3839

3940
func fixUnitConfig16961(bs []byte, cfg *repo_model.UnitConfig) (fixed bool, err error) {
40-
err = repo_model.JSONUnmarshalHandleDoubleEncode(bs, &cfg)
41+
err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
4142
if err == nil {
4243
return
4344
}
@@ -51,7 +52,7 @@ func fixUnitConfig16961(bs []byte, cfg *repo_model.UnitConfig) (fixed bool, err
5152
}
5253

5354
func fixExternalUncycloConfig16961(bs []byte, cfg *repo_model.ExternalUncycloConfig) (fixed bool, err error) {
54-
err = repo_model.JSONUnmarshalHandleDoubleEncode(bs, &cfg)
55+
err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
5556
if err == nil {
5657
return
5758
}
@@ -67,7 +68,7 @@ func fixExternalUncycloConfig16961(bs []byte, cfg *repo_model.ExternalUncycloConfig) (
6768
}
6869

6970
func fixExternalTrackerConfig16961(bs []byte, cfg *repo_model.ExternalTrackerConfig) (fixed bool, err error) {
70-
err = repo_model.JSONUnmarshalHandleDoubleEncode(bs, &cfg)
71+
err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
7172
if err == nil {
7273
return
7374
}
@@ -92,7 +93,7 @@ func fixExternalTrackerConfig16961(bs []byte, cfg *repo_model.ExternalTrackerCon
9293
}
9394

9495
func fixPullRequestsConfig16961(bs []byte, cfg *repo_model.PullRequestsConfig) (fixed bool, err error) {
95-
err = repo_model.JSONUnmarshalHandleDoubleEncode(bs, &cfg)
96+
err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
9697
if err == nil {
9798
return
9899
}
@@ -174,7 +175,7 @@ func fixPullRequestsConfig16961(bs []byte, cfg *repo_model.PullRequestsConfig) (
174175
}
175176

176177
func fixIssuesConfig16961(bs []byte, cfg *repo_model.IssuesConfig) (fixed bool, err error) {
177-
err = repo_model.JSONUnmarshalHandleDoubleEncode(bs, &cfg)
178+
err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
178179
if err == nil {
179180
return
180181
}

modules/json/json.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package json
66

77
import (
88
"bytes"
9+
"encoding/binary"
910
"encoding/json"
1011
"io"
1112

@@ -140,3 +141,32 @@ func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
140141
func Valid(data []byte) bool {
141142
return json.Valid(data)
142143
}
144+
145+
// UnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
146+
// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
147+
func UnmarshalHandleDoubleEncode(bs []byte, v interface{}) error {
148+
err := json.Unmarshal(bs, v)
149+
if err != nil {
150+
ok := true
151+
rs := []byte{}
152+
temp := make([]byte, 2)
153+
for _, rn := range string(bs) {
154+
if rn > 0xffff {
155+
ok = false
156+
break
157+
}
158+
binary.LittleEndian.PutUint16(temp, uint16(rn))
159+
rs = append(rs, temp...)
160+
}
161+
if ok {
162+
if len(rs) > 1 && rs[0] == 0xff && rs[1] == 0xfe {
163+
rs = rs[2:]
164+
}
165+
err = json.Unmarshal(rs, v)
166+
}
167+
}
168+
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
169+
err = json.Unmarshal(bs[2:], v)
170+
}
171+
return err
172+
}

routers/web/explore/code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func Code(ctx *context.Context) {
7979
var rightRepoMap = make(map[int64]*repo_model.Repository, len(repoMaps))
8080
repoIDs = make([]int64, 0, len(repoMaps))
8181
for id, repo := range repoMaps {
82-
if models.CheckUnitUser(repo, ctx.User, unit.TypeCode) {
82+
if models.CheckRepoUnitUser(repo, ctx.User, unit.TypeCode) {
8383
rightRepoMap[id] = repo
8484
repoIDs = append(repoIDs, id)
8585
}

routers/web/repo/compare.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
440440
if rootRepo != nil &&
441441
rootRepo.ID != ci.HeadRepo.ID &&
442442
rootRepo.ID != baseRepo.ID {
443-
canRead := models.CheckUnitUser(rootRepo, ctx.User, unit.TypeCode)
443+
canRead := models.CheckRepoUnitUser(rootRepo, ctx.User, unit.TypeCode)
444444
if canRead {
445445
ctx.Data["RootRepo"] = rootRepo
446446
if !fileOnly {
@@ -465,7 +465,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
465465
ownForkRepo.ID != ci.HeadRepo.ID &&
466466
ownForkRepo.ID != baseRepo.ID &&
467467
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
468-
canRead := models.CheckUnitUser(ownForkRepo, ctx.User, unit.TypeCode)
468+
canRead := models.CheckRepoUnitUser(ownForkRepo, ctx.User, unit.TypeCode)
469469
if canRead {
470470
ctx.Data["OwnForkRepo"] = ownForkRepo
471471
if !fileOnly {

routers/web/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func Create(ctx *context.Context) {
147147
templateID := ctx.FormInt64("template_id")
148148
if templateID > 0 {
149149
templateRepo, err := repo_model.GetRepositoryByID(templateID)
150-
if err == nil && models.CheckUnitUser(templateRepo, ctxUser, unit.TypeCode) {
150+
if err == nil && models.CheckRepoUnitUser(templateRepo, ctxUser, unit.TypeCode) {
151151
ctx.Data["repo_template"] = templateID
152152
ctx.Data["repo_template_name"] = templateRepo.Name
153153
}

services/auth/source/ldap/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"code.gitea.io/gitea/models/login"
11-
repo_model "code.gitea.io/gitea/models/repo"
1211
"code.gitea.io/gitea/modules/json"
1312
"code.gitea.io/gitea/modules/secret"
1413
"code.gitea.io/gitea/modules/setting"
@@ -62,7 +61,7 @@ type Source struct {
6261

6362
// FromDB fills up a LDAPConfig from serialized format.
6463
func (source *Source) FromDB(bs []byte) error {
65-
err := repo_model.JSONUnmarshalHandleDoubleEncode(bs, &source)
64+
err := json.UnmarshalHandleDoubleEncode(bs, &source)
6665
if err != nil {
6766
return err
6867
}

services/auth/source/oauth2/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package oauth2
66

77
import (
88
"code.gitea.io/gitea/models/login"
9-
repo_model "code.gitea.io/gitea/models/repo"
109
"code.gitea.io/gitea/modules/json"
1110
)
1211

@@ -33,7 +32,7 @@ type Source struct {
3332

3433
// FromDB fills up an OAuth2Config from serialized format.
3534
func (source *Source) FromDB(bs []byte) error {
36-
return repo_model.JSONUnmarshalHandleDoubleEncode(bs, &source)
35+
return json.UnmarshalHandleDoubleEncode(bs, &source)
3736
}
3837

3938
// ToDB exports an SMTPConfig to a serialized format.

services/auth/source/pam/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package pam
66

77
import (
88
"code.gitea.io/gitea/models/login"
9-
repo_model "code.gitea.io/gitea/models/repo"
109
"code.gitea.io/gitea/modules/json"
1110
)
1211

@@ -29,7 +28,7 @@ type Source struct {
2928

3029
// FromDB fills up a PAMConfig from serialized format.
3130
func (source *Source) FromDB(bs []byte) error {
32-
return repo_model.JSONUnmarshalHandleDoubleEncode(bs, &source)
31+
return json.UnmarshalHandleDoubleEncode(bs, &source)
3332
}
3433

3534
// ToDB exports a PAMConfig to a serialized format.

services/auth/source/smtp/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package smtp
66

77
import (
88
"code.gitea.io/gitea/models/login"
9-
repo_model "code.gitea.io/gitea/models/repo"
109
"code.gitea.io/gitea/modules/json"
1110
)
1211

@@ -35,7 +34,7 @@ type Source struct {
3534

3635
// FromDB fills up an SMTPConfig from serialized format.
3736
func (source *Source) FromDB(bs []byte) error {
38-
return repo_model.JSONUnmarshalHandleDoubleEncode(bs, &source)
37+
return json.UnmarshalHandleDoubleEncode(bs, &source)
3938
}
4039

4140
// ToDB exports an SMTPConfig to a serialized format.

services/auth/source/sspi/source.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package sspi
66

77
import (
88
"code.gitea.io/gitea/models/login"
9-
repo_model "code.gitea.io/gitea/models/repo"
109
"code.gitea.io/gitea/modules/json"
1110
)
1211

@@ -28,7 +27,7 @@ type Source struct {
2827

2928
// FromDB fills up an SSPIConfig from serialized format.
3029
func (cfg *Source) FromDB(bs []byte) error {
31-
return repo_model.JSONUnmarshalHandleDoubleEncode(bs, &cfg)
30+
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
3231
}
3332

3433
// ToDB exports an SSPIConfig to a serialized format.

services/mailer/mail_issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, vi
139139
visited[user.ID] = true
140140

141141
// test if this user is allowed to see the issue/pull
142-
if !models.CheckUnitUser(ctx.Issue.Repo, user, checkUnit) {
142+
if !models.CheckRepoUnitUser(ctx.Issue.Repo, user, checkUnit) {
143143
continue
144144
}
145145

0 commit comments

Comments
 (0)