Skip to content

Commit 9defddb

Browse files
lunny6543wxiaoguang
authoredNov 28, 2021
Move more model into models/user (#17826)
* Move more model into models/user * Remove unnecessary comment Co-authored-by: 6543 <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent b1df890 commit 9defddb

File tree

23 files changed

+547
-603
lines changed

23 files changed

+547
-603
lines changed
 

‎models/error.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,46 +1624,6 @@ func (err ErrUploadNotExist) Error() string {
16241624
return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID)
16251625
}
16261626

1627-
// ___________ __ .__ .____ .__ ____ ___
1628-
// \_ _____/__ ____/ |_ ___________ ____ _____ | | | | ____ ____ |__| ____ | | \______ ___________
1629-
// | __)_\ \/ /\ __\/ __ \_ __ \/ \\__ \ | | | | / _ \ / ___\| |/ \ | | / ___// __ \_ __ \
1630-
// | \> < | | \ ___/| | \/ | \/ __ \| |__ | |__( <_> ) /_/ > | | \ | | /\___ \\ ___/| | \/
1631-
// /_______ /__/\_ \ |__| \___ >__| |___| (____ /____/ |_______ \____/\___ /|__|___| / |______//____ >\___ >__|
1632-
// \/ \/ \/ \/ \/ \/ /_____/ \/ \/ \/
1633-
1634-
// ErrExternalLoginUserAlreadyExist represents a "ExternalLoginUserAlreadyExist" kind of error.
1635-
type ErrExternalLoginUserAlreadyExist struct {
1636-
ExternalID string
1637-
UserID int64
1638-
LoginSourceID int64
1639-
}
1640-
1641-
// IsErrExternalLoginUserAlreadyExist checks if an error is a ExternalLoginUserAlreadyExist.
1642-
func IsErrExternalLoginUserAlreadyExist(err error) bool {
1643-
_, ok := err.(ErrExternalLoginUserAlreadyExist)
1644-
return ok
1645-
}
1646-
1647-
func (err ErrExternalLoginUserAlreadyExist) Error() string {
1648-
return fmt.Sprintf("external login user already exists [externalID: %s, userID: %d, loginSourceID: %d]", err.ExternalID, err.UserID, err.LoginSourceID)
1649-
}
1650-
1651-
// ErrExternalLoginUserNotExist represents a "ExternalLoginUserNotExist" kind of error.
1652-
type ErrExternalLoginUserNotExist struct {
1653-
UserID int64
1654-
LoginSourceID int64
1655-
}
1656-
1657-
// IsErrExternalLoginUserNotExist checks if an error is a ExternalLoginUserNotExist.
1658-
func IsErrExternalLoginUserNotExist(err error) bool {
1659-
_, ok := err.(ErrExternalLoginUserNotExist)
1660-
return ok
1661-
}
1662-
1663-
func (err ErrExternalLoginUserNotExist) Error() string {
1664-
return fmt.Sprintf("external login user link does not exists [userID: %d, loginSourceID: %d]", err.UserID, err.LoginSourceID)
1665-
}
1666-
16671627
// .___ ________ .___ .__
16681628
// | | ______ ________ __ ____ \______ \ ____ ______ ____ ____ __| _/____ ____ ____ |__| ____ ______
16691629
// | |/ ___// ___/ | \_/ __ \ | | \_/ __ \\____ \_/ __ \ / \ / __ |/ __ \ / \_/ ___\| |/ __ \ / ___/

‎models/migrate.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,23 @@ func UpdateReviewsMigrationsByType(tp structs.GitServiceType, originalAuthorID s
245245
})
246246
return err
247247
}
248+
249+
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
250+
func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, userID int64) error {
251+
if err := UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
252+
return err
253+
}
254+
255+
if err := UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
256+
return err
257+
}
258+
259+
if err := UpdateReleasesMigrationsByType(tp, externalUserID, userID); err != nil {
260+
return err
261+
}
262+
263+
if err := UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
264+
return err
265+
}
266+
return UpdateReviewsMigrationsByType(tp, externalUserID, userID)
267+
}

‎models/org.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (org *Organization) LoadTeams() ([]*Team, error) {
8181
}
8282

8383
// GetMembers returns all members of organization.
84-
func (org *Organization) GetMembers() (UserList, map[int64]bool, error) {
84+
func (org *Organization) GetMembers() (user_model.UserList, map[int64]bool, error) {
8585
return FindOrgMembers(&FindOrgMembersOpts{
8686
OrgID: org.ID,
8787
})
@@ -149,7 +149,7 @@ func CountOrgMembers(opts *FindOrgMembersOpts) (int64, error) {
149149
}
150150

151151
// FindOrgMembers loads organization members according conditions
152-
func FindOrgMembers(opts *FindOrgMembersOpts) (UserList, map[int64]bool, error) {
152+
func FindOrgMembers(opts *FindOrgMembersOpts) (user_model.UserList, map[int64]bool, error) {
153153
ous, err := GetOrgUsersByOrgID(opts)
154154
if err != nil {
155155
return nil, nil, err
@@ -162,7 +162,7 @@ func FindOrgMembers(opts *FindOrgMembersOpts) (UserList, map[int64]bool, error)
162162
idsIsPublic[ou.UID] = ou.IsPublic
163163
}
164164

165-
users, err := GetUsersByIDs(ids)
165+
users, err := user_model.GetUsersByIDs(ids)
166166
if err != nil {
167167
return nil, nil, err
168168
}

‎models/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
260260
}
261261

262262
// ***** START: ExternalLoginUser *****
263-
if err = removeAllAccountLinks(e, u); err != nil {
263+
if err = user_model.RemoveAllAccountLinks(ctx, u); err != nil {
264264
return fmt.Errorf("ExternalLoginUser: %v", err)
265265
}
266266
// ***** END: ExternalLoginUser *****

‎models/user/email_address.go

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/models/db"
16+
"code.gitea.io/gitea/modules/base"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
19+
"code.gitea.io/gitea/modules/util"
1820

1921
"xorm.io/builder"
2022
)
@@ -275,3 +277,247 @@ func DeleteInactiveEmailAddresses(ctx context.Context) error {
275277
Delete(new(EmailAddress))
276278
return err
277279
}
280+
281+
// ActivateEmail activates the email address to given user.
282+
func ActivateEmail(email *EmailAddress) error {
283+
ctx, committer, err := db.TxContext()
284+
if err != nil {
285+
return err
286+
}
287+
defer committer.Close()
288+
if err := updateActivation(db.GetEngine(ctx), email, true); err != nil {
289+
return err
290+
}
291+
return committer.Commit()
292+
}
293+
294+
func updateActivation(e db.Engine, email *EmailAddress, activate bool) error {
295+
user, err := GetUserByIDEngine(e, email.UID)
296+
if err != nil {
297+
return err
298+
}
299+
if user.Rands, err = GetUserSalt(); err != nil {
300+
return err
301+
}
302+
email.IsActivated = activate
303+
if _, err := e.ID(email.ID).Cols("is_activated").Update(email); err != nil {
304+
return err
305+
}
306+
return UpdateUserColsEngine(e, user, "rands")
307+
}
308+
309+
// MakeEmailPrimary sets primary email address of given user.
310+
func MakeEmailPrimary(email *EmailAddress) error {
311+
has, err := db.GetEngine(db.DefaultContext).Get(email)
312+
if err != nil {
313+
return err
314+
} else if !has {
315+
return ErrEmailAddressNotExist{Email: email.Email}
316+
}
317+
318+
if !email.IsActivated {
319+
return ErrEmailNotActivated
320+
}
321+
322+
user := &User{}
323+
has, err = db.GetEngine(db.DefaultContext).ID(email.UID).Get(user)
324+
if err != nil {
325+
return err
326+
} else if !has {
327+
return ErrUserNotExist{
328+
UID: email.UID,
329+
Name: "",
330+
KeyID: 0,
331+
}
332+
}
333+
334+
ctx, committer, err := db.TxContext()
335+
if err != nil {
336+
return err
337+
}
338+
defer committer.Close()
339+
sess := db.GetEngine(ctx)
340+
341+
// 1. Update user table
342+
user.Email = email.Email
343+
if _, err = sess.ID(user.ID).Cols("email").Update(user); err != nil {
344+
return err
345+
}
346+
347+
// 2. Update old primary email
348+
if _, err = sess.Where("uid=? AND is_primary=?", email.UID, true).Cols("is_primary").Update(&EmailAddress{
349+
IsPrimary: false,
350+
}); err != nil {
351+
return err
352+
}
353+
354+
// 3. update new primary email
355+
email.IsPrimary = true
356+
if _, err = sess.ID(email.ID).Cols("is_primary").Update(email); err != nil {
357+
return err
358+
}
359+
360+
return committer.Commit()
361+
}
362+
363+
// VerifyActiveEmailCode verifies active email code when active account
364+
func VerifyActiveEmailCode(code, email string) *EmailAddress {
365+
minutes := setting.Service.ActiveCodeLives
366+
367+
if user := GetVerifyUser(code); user != nil {
368+
// time limit code
369+
prefix := code[:base.TimeLimitCodeLength]
370+
data := fmt.Sprintf("%d%s%s%s%s", user.ID, email, user.LowerName, user.Passwd, user.Rands)
371+
372+
if base.VerifyTimeLimitCode(data, minutes, prefix) {
373+
emailAddress := &EmailAddress{UID: user.ID, Email: email}
374+
if has, _ := db.GetEngine(db.DefaultContext).Get(emailAddress); has {
375+
return emailAddress
376+
}
377+
}
378+
}
379+
return nil
380+
}
381+
382+
// SearchEmailOrderBy is used to sort the results from SearchEmails()
383+
type SearchEmailOrderBy string
384+
385+
func (s SearchEmailOrderBy) String() string {
386+
return string(s)
387+
}
388+
389+
// Strings for sorting result
390+
const (
391+
SearchEmailOrderByEmail SearchEmailOrderBy = "email_address.lower_email ASC, email_address.is_primary DESC, email_address.id ASC"
392+
SearchEmailOrderByEmailReverse SearchEmailOrderBy = "email_address.lower_email DESC, email_address.is_primary ASC, email_address.id DESC"
393+
SearchEmailOrderByName SearchEmailOrderBy = "`user`.lower_name ASC, email_address.is_primary DESC, email_address.id ASC"
394+
SearchEmailOrderByNameReverse SearchEmailOrderBy = "`user`.lower_name DESC, email_address.is_primary ASC, email_address.id DESC"
395+
)
396+
397+
// SearchEmailOptions are options to search e-mail addresses for the admin panel
398+
type SearchEmailOptions struct {
399+
db.ListOptions
400+
Keyword string
401+
SortType SearchEmailOrderBy
402+
IsPrimary util.OptionalBool
403+
IsActivated util.OptionalBool
404+
}
405+
406+
// SearchEmailResult is an e-mail address found in the user or email_address table
407+
type SearchEmailResult struct {
408+
UID int64
409+
Email string
410+
IsActivated bool
411+
IsPrimary bool
412+
// From User
413+
Name string
414+
FullName string
415+
}
416+
417+
// SearchEmails takes options i.e. keyword and part of email name to search,
418+
// it returns results in given range and number of total results.
419+
func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error) {
420+
var cond builder.Cond = builder.Eq{"`user`.`type`": UserTypeIndividual}
421+
if len(opts.Keyword) > 0 {
422+
likeStr := "%" + strings.ToLower(opts.Keyword) + "%"
423+
cond = cond.And(builder.Or(
424+
builder.Like{"lower(`user`.full_name)", likeStr},
425+
builder.Like{"`user`.lower_name", likeStr},
426+
builder.Like{"email_address.lower_email", likeStr},
427+
))
428+
}
429+
430+
switch {
431+
case opts.IsPrimary.IsTrue():
432+
cond = cond.And(builder.Eq{"email_address.is_primary": true})
433+
case opts.IsPrimary.IsFalse():
434+
cond = cond.And(builder.Eq{"email_address.is_primary": false})
435+
}
436+
437+
switch {
438+
case opts.IsActivated.IsTrue():
439+
cond = cond.And(builder.Eq{"email_address.is_activated": true})
440+
case opts.IsActivated.IsFalse():
441+
cond = cond.And(builder.Eq{"email_address.is_activated": false})
442+
}
443+
444+
count, err := db.GetEngine(db.DefaultContext).Join("INNER", "`user`", "`user`.ID = email_address.uid").
445+
Where(cond).Count(new(EmailAddress))
446+
if err != nil {
447+
return nil, 0, fmt.Errorf("Count: %v", err)
448+
}
449+
450+
orderby := opts.SortType.String()
451+
if orderby == "" {
452+
orderby = SearchEmailOrderByEmail.String()
453+
}
454+
455+
opts.SetDefaultValues()
456+
457+
emails := make([]*SearchEmailResult, 0, opts.PageSize)
458+
err = db.GetEngine(db.DefaultContext).Table("email_address").
459+
Select("email_address.*, `user`.name, `user`.full_name").
460+
Join("INNER", "`user`", "`user`.ID = email_address.uid").
461+
Where(cond).
462+
OrderBy(orderby).
463+
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
464+
Find(&emails)
465+
466+
return emails, count, err
467+
}
468+
469+
// ActivateUserEmail will change the activated state of an email address,
470+
// either primary or secondary (all in the email_address table)
471+
func ActivateUserEmail(userID int64, email string, activate bool) (err error) {
472+
ctx, committer, err := db.TxContext()
473+
if err != nil {
474+
return err
475+
}
476+
defer committer.Close()
477+
sess := db.GetEngine(ctx)
478+
479+
// Activate/deactivate a user's secondary email address
480+
// First check if there's another user active with the same address
481+
addr := EmailAddress{UID: userID, LowerEmail: strings.ToLower(email)}
482+
if has, err := sess.Get(&addr); err != nil {
483+
return err
484+
} else if !has {
485+
return fmt.Errorf("no such email: %d (%s)", userID, email)
486+
}
487+
if addr.IsActivated == activate {
488+
// Already in the desired state; no action
489+
return nil
490+
}
491+
if activate {
492+
if used, err := IsEmailActive(ctx, email, addr.ID); err != nil {
493+
return fmt.Errorf("unable to check isEmailActive() for %s: %v", email, err)
494+
} else if used {
495+
return ErrEmailAlreadyUsed{Email: email}
496+
}
497+
}
498+
if err = updateActivation(sess, &addr, activate); err != nil {
499+
return fmt.Errorf("unable to updateActivation() for %d:%s: %w", addr.ID, addr.Email, err)
500+
}
501+
502+
// Activate/deactivate a user's primary email address and account
503+
if addr.IsPrimary {
504+
user := User{ID: userID, Email: email}
505+
if has, err := sess.Get(&user); err != nil {
506+
return err
507+
} else if !has {
508+
return fmt.Errorf("no user with ID: %d and Email: %s", userID, email)
509+
}
510+
// The user's activation state should be synchronized with the primary email
511+
if user.IsActive != activate {
512+
user.IsActive = activate
513+
if user.Rands, err = GetUserSalt(); err != nil {
514+
return fmt.Errorf("unable to generate salt: %v", err)
515+
}
516+
if err = UpdateUserColsEngine(sess, &user, "is_active", "rands"); err != nil {
517+
return fmt.Errorf("unable to updateUserCols() for user ID: %d: %v", userID, err)
518+
}
519+
}
520+
}
521+
522+
return committer.Commit()
523+
}

‎models/user/email_address_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models/db"
1111
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/modules/util"
1213

1314
"github.com/stretchr/testify/assert"
1415
)
@@ -130,3 +131,124 @@ func TestDeleteEmailAddresses(t *testing.T) {
130131
err := DeleteEmailAddresses(emails)
131132
assert.Error(t, err)
132133
}
134+
135+
func TestMakeEmailPrimary(t *testing.T) {
136+
assert.NoError(t, unittest.PrepareTestDatabase())
137+
138+
email := &EmailAddress{
139+
Email: "user567890@example.com",
140+
}
141+
err := MakeEmailPrimary(email)
142+
assert.Error(t, err)
143+
assert.EqualError(t, err, ErrEmailAddressNotExist{Email: email.Email}.Error())
144+
145+
email = &EmailAddress{
146+
Email: "user11@example.com",
147+
}
148+
err = MakeEmailPrimary(email)
149+
assert.Error(t, err)
150+
assert.EqualError(t, err, ErrEmailNotActivated.Error())
151+
152+
email = &EmailAddress{
153+
Email: "user9999999@example.com",
154+
}
155+
err = MakeEmailPrimary(email)
156+
assert.Error(t, err)
157+
assert.True(t, IsErrUserNotExist(err))
158+
159+
email = &EmailAddress{
160+
Email: "user101@example.com",
161+
}
162+
err = MakeEmailPrimary(email)
163+
assert.NoError(t, err)
164+
165+
user, _ := GetUserByID(int64(10))
166+
assert.Equal(t, "user101@example.com", user.Email)
167+
}
168+
169+
func TestActivate(t *testing.T) {
170+
assert.NoError(t, unittest.PrepareTestDatabase())
171+
172+
email := &EmailAddress{
173+
ID: int64(1),
174+
UID: int64(1),
175+
Email: "user11@example.com",
176+
}
177+
assert.NoError(t, ActivateEmail(email))
178+
179+
emails, _ := GetEmailAddresses(int64(1))
180+
assert.Len(t, emails, 3)
181+
assert.True(t, emails[0].IsActivated)
182+
assert.True(t, emails[0].IsPrimary)
183+
assert.False(t, emails[1].IsPrimary)
184+
assert.True(t, emails[2].IsActivated)
185+
assert.False(t, emails[2].IsPrimary)
186+
}
187+
188+
func TestListEmails(t *testing.T) {
189+
assert.NoError(t, unittest.PrepareTestDatabase())
190+
191+
// Must find all users and their emails
192+
opts := &SearchEmailOptions{
193+
ListOptions: db.ListOptions{
194+
PageSize: 10000,
195+
},
196+
}
197+
emails, count, err := SearchEmails(opts)
198+
assert.NoError(t, err)
199+
assert.NotEqual(t, int64(0), count)
200+
assert.True(t, count > 5)
201+
202+
contains := func(match func(s *SearchEmailResult) bool) bool {
203+
for _, v := range emails {
204+
if match(v) {
205+
return true
206+
}
207+
}
208+
return false
209+
}
210+
211+
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 18 }))
212+
// 'user3' is an organization
213+
assert.False(t, contains(func(s *SearchEmailResult) bool { return s.UID == 3 }))
214+
215+
// Must find no records
216+
opts = &SearchEmailOptions{Keyword: "NOTFOUND"}
217+
emails, count, err = SearchEmails(opts)
218+
assert.NoError(t, err)
219+
assert.Equal(t, int64(0), count)
220+
221+
// Must find users 'user2', 'user28', etc.
222+
opts = &SearchEmailOptions{Keyword: "user2"}
223+
emails, count, err = SearchEmails(opts)
224+
assert.NoError(t, err)
225+
assert.NotEqual(t, int64(0), count)
226+
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 2 }))
227+
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 27 }))
228+
229+
// Must find only primary addresses (i.e. from the `user` table)
230+
opts = &SearchEmailOptions{IsPrimary: util.OptionalBoolTrue}
231+
emails, _, err = SearchEmails(opts)
232+
assert.NoError(t, err)
233+
assert.True(t, contains(func(s *SearchEmailResult) bool { return s.IsPrimary }))
234+
assert.False(t, contains(func(s *SearchEmailResult) bool { return !s.IsPrimary }))
235+
236+
// Must find only inactive addresses (i.e. not validated)
237+
opts = &SearchEmailOptions{IsActivated: util.OptionalBoolFalse}
238+
emails, _, err = SearchEmails(opts)
239+
assert.NoError(t, err)
240+
assert.True(t, contains(func(s *SearchEmailResult) bool { return !s.IsActivated }))
241+
assert.False(t, contains(func(s *SearchEmailResult) bool { return s.IsActivated }))
242+
243+
// Must find more than one page, but retrieve only one
244+
opts = &SearchEmailOptions{
245+
ListOptions: db.ListOptions{
246+
PageSize: 5,
247+
Page: 1,
248+
},
249+
}
250+
emails, count, err = SearchEmails(opts)
251+
assert.NoError(t, err)
252+
assert.Len(t, emails, 5)
253+
assert.Greater(t, count, int64(len(emails)))
254+
}

‎models/external_login_user.go renamed to ‎models/user/external_login_user.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,53 @@
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

5-
package models
5+
package user
66

77
import (
8+
"context"
9+
"fmt"
810
"time"
911

1012
"code.gitea.io/gitea/models/db"
1113
"code.gitea.io/gitea/models/login"
12-
user_model "code.gitea.io/gitea/models/user"
13-
"code.gitea.io/gitea/modules/structs"
1414

1515
"github.com/markbates/goth"
1616
"xorm.io/builder"
1717
)
1818

19+
// ErrExternalLoginUserAlreadyExist represents a "ExternalLoginUserAlreadyExist" kind of error.
20+
type ErrExternalLoginUserAlreadyExist struct {
21+
ExternalID string
22+
UserID int64
23+
LoginSourceID int64
24+
}
25+
26+
// IsErrExternalLoginUserAlreadyExist checks if an error is a ExternalLoginUserAlreadyExist.
27+
func IsErrExternalLoginUserAlreadyExist(err error) bool {
28+
_, ok := err.(ErrExternalLoginUserAlreadyExist)
29+
return ok
30+
}
31+
32+
func (err ErrExternalLoginUserAlreadyExist) Error() string {
33+
return fmt.Sprintf("external login user already exists [externalID: %s, userID: %d, loginSourceID: %d]", err.ExternalID, err.UserID, err.LoginSourceID)
34+
}
35+
36+
// ErrExternalLoginUserNotExist represents a "ExternalLoginUserNotExist" kind of error.
37+
type ErrExternalLoginUserNotExist struct {
38+
UserID int64
39+
LoginSourceID int64
40+
}
41+
42+
// IsErrExternalLoginUserNotExist checks if an error is a ExternalLoginUserNotExist.
43+
func IsErrExternalLoginUserNotExist(err error) bool {
44+
_, ok := err.(ErrExternalLoginUserNotExist)
45+
return ok
46+
}
47+
48+
func (err ErrExternalLoginUserNotExist) Error() string {
49+
return fmt.Sprintf("external login user link does not exists [userID: %d, loginSourceID: %d]", err.UserID, err.LoginSourceID)
50+
}
51+
1952
// ExternalLoginUser makes the connecting between some existing user and additional external login sources
2053
type ExternalLoginUser struct {
2154
ExternalID string `xorm:"pk NOT NULL"`
@@ -47,7 +80,7 @@ func GetExternalLogin(externalLoginUser *ExternalLoginUser) (bool, error) {
4780
}
4881

4982
// ListAccountLinks returns a map with the ExternalLoginUser and its LoginSource
50-
func ListAccountLinks(user *user_model.User) ([]*ExternalLoginUser, error) {
83+
func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) {
5184
externalAccounts := make([]*ExternalLoginUser, 0, 5)
5285
err := db.GetEngine(db.DefaultContext).Where("user_id=?", user.ID).
5386
Desc("login_source_id").
@@ -60,7 +93,7 @@ func ListAccountLinks(user *user_model.User) ([]*ExternalLoginUser, error) {
6093
}
6194

6295
// LinkExternalToUser link the external user to the user
63-
func LinkExternalToUser(user *user_model.User, externalLoginUser *ExternalLoginUser) error {
96+
func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error {
6497
has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID).
6598
NoAutoCondition().
6699
Exist(externalLoginUser)
@@ -75,7 +108,7 @@ func LinkExternalToUser(user *user_model.User, externalLoginUser *ExternalLoginU
75108
}
76109

77110
// RemoveAccountLink will remove all external login sources for the given user
78-
func RemoveAccountLink(user *user_model.User, loginSourceID int64) (int64, error) {
111+
func RemoveAccountLink(user *User, loginSourceID int64) (int64, error) {
79112
deleted, err := db.GetEngine(db.DefaultContext).Delete(&ExternalLoginUser{UserID: user.ID, LoginSourceID: loginSourceID})
80113
if err != nil {
81114
return deleted, err
@@ -86,9 +119,9 @@ func RemoveAccountLink(user *user_model.User, loginSourceID int64) (int64, error
86119
return deleted, err
87120
}
88121

89-
// removeAllAccountLinks will remove all external login sources for the given user
90-
func removeAllAccountLinks(e db.Engine, user *user_model.User) error {
91-
_, err := e.Delete(&ExternalLoginUser{UserID: user.ID})
122+
// RemoveAllAccountLinks will remove all external login sources for the given user
123+
func RemoveAllAccountLinks(ctx context.Context, user *User) error {
124+
_, err := db.GetEngine(ctx).Delete(&ExternalLoginUser{UserID: user.ID})
92125
return err
93126
}
94127

@@ -107,7 +140,7 @@ func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
107140
}
108141

109142
// UpdateExternalUser updates external user's information
110-
func UpdateExternalUser(user *user_model.User, gothUser goth.User) error {
143+
func UpdateExternalUser(user *User, gothUser goth.User) error {
111144
loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
112145
if err != nil {
113146
return err
@@ -172,23 +205,3 @@ func FindExternalUsersByProvider(opts FindExternalUserOptions) ([]ExternalLoginU
172205
}
173206
return users, nil
174207
}
175-
176-
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
177-
func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, userID int64) error {
178-
if err := UpdateIssuesMigrationsByType(tp, externalUserID, userID); err != nil {
179-
return err
180-
}
181-
182-
if err := UpdateCommentsMigrationsByType(tp, externalUserID, userID); err != nil {
183-
return err
184-
}
185-
186-
if err := UpdateReleasesMigrationsByType(tp, externalUserID, userID); err != nil {
187-
return err
188-
}
189-
190-
if err := UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
191-
return err
192-
}
193-
return UpdateReviewsMigrationsByType(tp, externalUserID, userID)
194-
}

‎models/user/list.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package user
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/login"
12+
)
13+
14+
// UserList is a list of user.
15+
// This type provide valuable methods to retrieve information for a group of users efficiently.
16+
type UserList []*User //revive:disable-line:exported
17+
18+
// GetUserIDs returns a slice of user's id
19+
func (users UserList) GetUserIDs() []int64 {
20+
userIDs := make([]int64, len(users))
21+
for _, user := range users {
22+
userIDs = append(userIDs, user.ID) // Considering that user id are unique in the list
23+
}
24+
return userIDs
25+
}
26+
27+
// GetTwoFaStatus return state of 2FA enrollement
28+
func (users UserList) GetTwoFaStatus() map[int64]bool {
29+
results := make(map[int64]bool, len(users))
30+
for _, user := range users {
31+
results[user.ID] = false // Set default to false
32+
}
33+
tokenMaps, err := users.loadTwoFactorStatus(db.GetEngine(db.DefaultContext))
34+
if err == nil {
35+
for _, token := range tokenMaps {
36+
results[token.UID] = true
37+
}
38+
}
39+
40+
return results
41+
}
42+
43+
func (users UserList) loadTwoFactorStatus(e db.Engine) (map[int64]*login.TwoFactor, error) {
44+
if len(users) == 0 {
45+
return nil, nil
46+
}
47+
48+
userIDs := users.GetUserIDs()
49+
tokenMaps := make(map[int64]*login.TwoFactor, len(userIDs))
50+
err := e.
51+
In("uid", userIDs).
52+
Find(&tokenMaps)
53+
if err != nil {
54+
return nil, fmt.Errorf("find two factor: %v", err)
55+
}
56+
return tokenMaps, nil
57+
}
58+
59+
// GetUsersByIDs returns all resolved users from a list of Ids.
60+
func GetUsersByIDs(ids []int64) (UserList, error) {
61+
ous := make([]*User, 0, len(ids))
62+
if len(ids) == 0 {
63+
return ous, nil
64+
}
65+
err := db.GetEngine(db.DefaultContext).In("id", ids).
66+
Asc("name").
67+
Find(&ous)
68+
return ous, err
69+
}

‎models/user_email.go

Lines changed: 0 additions & 262 deletions
This file was deleted.

‎models/user_email_test.go

Lines changed: 0 additions & 137 deletions
This file was deleted.

‎models/userlist.go

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,17 @@ import (
88
"fmt"
99

1010
"code.gitea.io/gitea/models/db"
11-
"code.gitea.io/gitea/models/login"
1211
user_model "code.gitea.io/gitea/models/user"
1312
"code.gitea.io/gitea/modules/log"
1413
)
1514

16-
// UserList is a list of user.
17-
// This type provide valuable methods to retrieve information for a group of users efficiently.
18-
type UserList []*user_model.User
19-
20-
func (users UserList) getUserIDs() []int64 {
21-
userIDs := make([]int64, len(users))
22-
for _, user := range users {
23-
userIDs = append(userIDs, user.ID) // Considering that user id are unique in the list
24-
}
25-
return userIDs
26-
}
27-
2815
// IsUserOrgOwner returns true if user is in the owner team of given organization.
29-
func (users UserList) IsUserOrgOwner(orgID int64) map[int64]bool {
16+
func IsUserOrgOwner(users user_model.UserList, orgID int64) map[int64]bool {
3017
results := make(map[int64]bool, len(users))
3118
for _, user := range users {
3219
results[user.ID] = false // Set default to false
3320
}
34-
ownerMaps, err := users.loadOrganizationOwners(db.GetEngine(db.DefaultContext), orgID)
21+
ownerMaps, err := loadOrganizationOwners(db.GetEngine(db.DefaultContext), users, orgID)
3522
if err == nil {
3623
for _, owner := range ownerMaps {
3724
results[owner.UID] = true
@@ -40,7 +27,7 @@ func (users UserList) IsUserOrgOwner(orgID int64) map[int64]bool {
4027
return results
4128
}
4229

43-
func (users UserList) loadOrganizationOwners(e db.Engine, orgID int64) (map[int64]*TeamUser, error) {
30+
func loadOrganizationOwners(e db.Engine, users user_model.UserList, orgID int64) (map[int64]*TeamUser, error) {
4431
if len(users) == 0 {
4532
return nil, nil
4633
}
@@ -53,7 +40,7 @@ func (users UserList) loadOrganizationOwners(e db.Engine, orgID int64) (map[int6
5340
return nil, err
5441
}
5542

56-
userIDs := users.getUserIDs()
43+
userIDs := users.GetUserIDs()
5744
ownerMaps := make(map[int64]*TeamUser)
5845
err = e.In("uid", userIDs).
5946
And("org_id=?", orgID).
@@ -64,47 +51,3 @@ func (users UserList) loadOrganizationOwners(e db.Engine, orgID int64) (map[int6
6451
}
6552
return ownerMaps, nil
6653
}
67-
68-
// GetTwoFaStatus return state of 2FA enrollement
69-
func (users UserList) GetTwoFaStatus() map[int64]bool {
70-
results := make(map[int64]bool, len(users))
71-
for _, user := range users {
72-
results[user.ID] = false // Set default to false
73-
}
74-
tokenMaps, err := users.loadTwoFactorStatus(db.GetEngine(db.DefaultContext))
75-
if err == nil {
76-
for _, token := range tokenMaps {
77-
results[token.UID] = true
78-
}
79-
}
80-
81-
return results
82-
}
83-
84-
func (users UserList) loadTwoFactorStatus(e db.Engine) (map[int64]*login.TwoFactor, error) {
85-
if len(users) == 0 {
86-
return nil, nil
87-
}
88-
89-
userIDs := users.getUserIDs()
90-
tokenMaps := make(map[int64]*login.TwoFactor, len(userIDs))
91-
err := e.
92-
In("uid", userIDs).
93-
Find(&tokenMaps)
94-
if err != nil {
95-
return nil, fmt.Errorf("find two factor: %v", err)
96-
}
97-
return tokenMaps, nil
98-
}
99-
100-
// GetUsersByIDs returns all resolved users from a list of Ids.
101-
func GetUsersByIDs(ids []int64) (UserList, error) {
102-
ous := make([]*user_model.User, 0, len(ids))
103-
if len(ids) == 0 {
104-
return ous, nil
105-
}
106-
err := db.GetEngine(db.DefaultContext).In("id", ids).
107-
Asc("name").
108-
Find(&ous)
109-
return ous, err
110-
}

‎models/userlist_test.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,5 @@ func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bo
6464
assert.NoError(t, err)
6565
members, _, err := org.GetMembers()
6666
assert.NoError(t, err)
67-
assert.Equal(t, expected, members.IsUserOrgOwner(orgID))
68-
}
69-
70-
func TestUserListIsTwoFaEnrolled(t *testing.T) {
71-
assert.NoError(t, unittest.PrepareTestDatabase())
72-
tt := []struct {
73-
orgid int64
74-
expected map[int64]bool
75-
}{
76-
{3, map[int64]bool{2: false, 4: false, 28: false}},
77-
{6, map[int64]bool{5: false, 28: false}},
78-
{7, map[int64]bool{5: false}},
79-
{25, map[int64]bool{24: true}},
80-
{22, map[int64]bool{}},
81-
}
82-
for _, v := range tt {
83-
t.Run(fmt.Sprintf("IsTwoFaEnrolledOfOrdIg%d", v.orgid), func(t *testing.T) {
84-
testUserListIsTwoFaEnrolled(t, v.orgid, v.expected)
85-
})
86-
}
87-
}
88-
89-
func testUserListIsTwoFaEnrolled(t *testing.T, orgID int64, expected map[int64]bool) {
90-
org, err := GetOrgByID(orgID)
91-
assert.NoError(t, err)
92-
members, _, err := org.GetMembers()
93-
assert.NoError(t, err)
94-
assert.Equal(t, expected, members.GetTwoFaStatus())
67+
assert.Equal(t, expected, IsUserOrgOwner(members, orgID))
9568
}

‎routers/api/v1/repo/issue_subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func GetIssueSubscribers(ctx *context.APIContext) {
273273
userIDs = append(userIDs, iw.UserID)
274274
}
275275

276-
users, err := models.GetUsersByIDs(userIDs)
276+
users, err := user_model.GetUsersByIDs(userIDs)
277277
if err != nil {
278278
ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
279279
return

‎routers/web/admin/emails.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"net/url"
1111

12-
"code.gitea.io/gitea/models"
1312
"code.gitea.io/gitea/models/db"
1413
user_model "code.gitea.io/gitea/models/user"
1514
"code.gitea.io/gitea/modules/base"
@@ -29,7 +28,7 @@ func Emails(ctx *context.Context) {
2928
ctx.Data["PageIsAdmin"] = true
3029
ctx.Data["PageIsAdminEmails"] = true
3130

32-
opts := &models.SearchEmailOptions{
31+
opts := &user_model.SearchEmailOptions{
3332
ListOptions: db.ListOptions{
3433
PageSize: setting.UI.Admin.UserPagingNum,
3534
Page: ctx.FormInt("page"),
@@ -41,31 +40,31 @@ func Emails(ctx *context.Context) {
4140
}
4241

4342
type ActiveEmail struct {
44-
models.SearchEmailResult
43+
user_model.SearchEmailResult
4544
CanChange bool
4645
}
4746

4847
var (
49-
baseEmails []*models.SearchEmailResult
48+
baseEmails []*user_model.SearchEmailResult
5049
emails []ActiveEmail
5150
count int64
5251
err error
53-
orderBy models.SearchEmailOrderBy
52+
orderBy user_model.SearchEmailOrderBy
5453
)
5554

5655
ctx.Data["SortType"] = ctx.FormString("sort")
5756
switch ctx.FormString("sort") {
5857
case "email":
59-
orderBy = models.SearchEmailOrderByEmail
58+
orderBy = user_model.SearchEmailOrderByEmail
6059
case "reverseemail":
61-
orderBy = models.SearchEmailOrderByEmailReverse
60+
orderBy = user_model.SearchEmailOrderByEmailReverse
6261
case "username":
63-
orderBy = models.SearchEmailOrderByName
62+
orderBy = user_model.SearchEmailOrderByName
6463
case "reverseusername":
65-
orderBy = models.SearchEmailOrderByNameReverse
64+
orderBy = user_model.SearchEmailOrderByNameReverse
6665
default:
6766
ctx.Data["SortType"] = "email"
68-
orderBy = models.SearchEmailOrderByEmail
67+
orderBy = user_model.SearchEmailOrderByEmail
6968
}
7069

7170
opts.Keyword = ctx.FormTrim("q")
@@ -78,7 +77,7 @@ func Emails(ctx *context.Context) {
7877
}
7978

8079
if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) {
81-
baseEmails, count, err = models.SearchEmails(opts)
80+
baseEmails, count, err = user_model.SearchEmails(opts)
8281
if err != nil {
8382
ctx.ServerError("SearchEmails", err)
8483
return
@@ -127,7 +126,7 @@ func ActivateEmail(ctx *context.Context) {
127126

128127
log.Info("Changing activation for User ID: %d, email: %s, primary: %v to %v", uid, email, primary, activate)
129128

130-
if err := models.ActivateUserEmail(uid, email, activate); err != nil {
129+
if err := user_model.ActivateUserEmail(uid, email, activate); err != nil {
131130
log.Error("ActivateUserEmail(%v,%v,%v): %v", uid, email, activate, err)
132131
if user_model.IsErrEmailAlreadyUsed(err) {
133132
ctx.Flash.Error(ctx.Tr("admin.emails.duplicate_active"))

‎routers/web/explore/user.go

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

11-
"code.gitea.io/gitea/models"
1211
"code.gitea.io/gitea/models/db"
1312
user_model "code.gitea.io/gitea/models/user"
1413
"code.gitea.io/gitea/modules/base"
@@ -79,7 +78,7 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
7978
ctx.Data["Keyword"] = opts.Keyword
8079
ctx.Data["Total"] = count
8180
ctx.Data["Users"] = users
82-
ctx.Data["UsersTwoFaStatus"] = models.UserList(users).GetTwoFaStatus()
81+
ctx.Data["UsersTwoFaStatus"] = user_model.UserList(users).GetTwoFaStatus()
8382
ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
8483
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
8584

‎routers/web/org/members.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func Members(ctx *context.Context) {
6262
ctx.Data["Page"] = pager
6363
ctx.Data["Members"] = members
6464
ctx.Data["MembersIsPublicMember"] = membersIsPublic
65-
ctx.Data["MembersIsUserOrgOwner"] = members.IsUserOrgOwner(org.ID)
65+
ctx.Data["MembersIsUserOrgOwner"] = models.IsUserOrgOwner(members, org.ID)
6666
ctx.Data["MembersTwoFaStatus"] = members.GetTwoFaStatus()
6767

6868
ctx.HTML(http.StatusOK, tplMembers)

‎routers/web/user/auth.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/http"
1313
"strings"
1414

15-
"code.gitea.io/gitea/models"
1615
"code.gitea.io/gitea/models/db"
1716
"code.gitea.io/gitea/models/login"
1817
user_model "code.gitea.io/gitea/models/user"
@@ -781,7 +780,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *user_mode
781780
}
782781

783782
// update external user information
784-
if err := models.UpdateExternalUser(u, gothUser); err != nil {
783+
if err := user_model.UpdateExternalUser(u, gothUser); err != nil {
785784
log.Error("UpdateExternalUser failed: %v", err)
786785
}
787786

@@ -844,11 +843,11 @@ func oAuth2UserLoginCallback(loginSource *login.Source, request *http.Request, r
844843
}
845844

846845
// search in external linked users
847-
externalLoginUser := &models.ExternalLoginUser{
846+
externalLoginUser := &user_model.ExternalLoginUser{
848847
ExternalID: gothUser.UserID,
849848
LoginSourceID: loginSource.ID,
850849
}
851-
hasUser, err = models.GetExternalLogin(externalLoginUser)
850+
hasUser, err = user_model.GetExternalLogin(externalLoginUser)
852851
if err != nil {
853852
return nil, goth.User{}, err
854853
}
@@ -1355,7 +1354,7 @@ func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.
13551354

13561355
// update external user information
13571356
if gothUser != nil {
1358-
if err := models.UpdateExternalUser(u, *gothUser); err != nil {
1357+
if err := user_model.UpdateExternalUser(u, *gothUser); err != nil {
13591358
log.Error("UpdateExternalUser failed: %v", err)
13601359
}
13611360
}
@@ -1477,7 +1476,7 @@ func handleAccountActivation(ctx *context.Context, user *user_model.User) {
14771476
return
14781477
}
14791478

1480-
if err := models.ActivateUserEmail(user.ID, user.Email, true); err != nil {
1479+
if err := user_model.ActivateUserEmail(user.ID, user.Email, true); err != nil {
14811480
log.Error("Unable to activate email for user: %-v with email: %s: %v", user, user.Email, err)
14821481
ctx.ServerError("ActivateUserEmail", err)
14831482
return
@@ -1505,8 +1504,8 @@ func ActivateEmail(ctx *context.Context) {
15051504
emailStr := ctx.FormString("email")
15061505

15071506
// Verify code.
1508-
if email := models.VerifyActiveEmailCode(code, emailStr); email != nil {
1509-
if err := models.ActivateEmail(email); err != nil {
1507+
if email := user_model.VerifyActiveEmailCode(code, emailStr); email != nil {
1508+
if err := user_model.ActivateEmail(email); err != nil {
15101509
ctx.ServerError("ActivateEmail", err)
15111510
}
15121511

‎routers/web/user/setting/account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func EmailPost(ctx *context.Context) {
9494

9595
// Make emailaddress primary.
9696
if ctx.FormString("_method") == "PRIMARY" {
97-
if err := models.MakeEmailPrimary(&user_model.EmailAddress{ID: ctx.FormInt64("id")}); err != nil {
97+
if err := user_model.MakeEmailPrimary(&user_model.EmailAddress{ID: ctx.FormInt64("id")}); err != nil {
9898
ctx.ServerError("MakeEmailPrimary", err)
9999
return
100100
}

‎routers/web/user/setting/security.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func DeleteAccountLink(ctx *context.Context) {
4343
if id <= 0 {
4444
ctx.Flash.Error("Account link id is not given")
4545
} else {
46-
if _, err := models.RemoveAccountLink(ctx.User, id); err != nil {
46+
if _, err := user_model.RemoveAccountLink(ctx.User, id); err != nil {
4747
ctx.Flash.Error("RemoveAccountLink: " + err.Error())
4848
} else {
4949
ctx.Flash.Success(ctx.Tr("settings.remove_account_link_success"))
@@ -76,7 +76,7 @@ func loadSecurityData(ctx *context.Context) {
7676
}
7777
ctx.Data["Tokens"] = tokens
7878

79-
accountLinks, err := models.ListAccountLinks(ctx.User)
79+
accountLinks, err := user_model.ListAccountLinks(ctx.User)
8080
if err != nil {
8181
ctx.ServerError("ListAccountLinks", err)
8282
return

‎services/auth/login_source.go

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

77
import (
8-
"code.gitea.io/gitea/models"
98
"code.gitea.io/gitea/models/db"
109
"code.gitea.io/gitea/models/login"
1110
user_model "code.gitea.io/gitea/models/user"
@@ -22,7 +21,7 @@ func DeleteLoginSource(source *login.Source) error {
2221
}
2322
}
2423

25-
count, err = db.GetEngine(db.DefaultContext).Count(&models.ExternalLoginUser{LoginSourceID: source.ID})
24+
count, err = db.GetEngine(db.DefaultContext).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID})
2625
if err != nil {
2726
return err
2827
} else if count > 0 {

‎services/externalaccount/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func LinkAccountToUser(user *user_model.User, gothUser goth.User) error {
2222
return err
2323
}
2424

25-
externalLoginUser := &models.ExternalLoginUser{
25+
externalLoginUser := &user_model.ExternalLoginUser{
2626
ExternalID: gothUser.UserID,
2727
UserID: user.ID,
2828
LoginSourceID: loginSource.ID,
@@ -42,7 +42,7 @@ func LinkAccountToUser(user *user_model.User, gothUser goth.User) error {
4242
ExpiresAt: gothUser.ExpiresAt,
4343
}
4444

45-
if err := models.LinkExternalToUser(user, externalLoginUser); err != nil {
45+
if err := user_model.LinkExternalToUser(user, externalLoginUser); err != nil {
4646
return err
4747
}
4848

‎services/migrations/gitea_uploader.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
260260
tp := g.gitServiceType.Name()
261261
if !ok && tp != "" {
262262
var err error
263-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", release.PublisherID))
263+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", release.PublisherID))
264264
if err != nil {
265265
log.Error("GetUserIDByExternalUserID: %v", err)
266266
}
@@ -400,7 +400,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
400400
tp := g.gitServiceType.Name()
401401
if !ok && tp != "" {
402402
var err error
403-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", issue.PosterID))
403+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", issue.PosterID))
404404
if err != nil {
405405
log.Error("GetUserIDByExternalUserID: %v", err)
406406
}
@@ -425,7 +425,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
425425
userid, ok := g.userMap[reaction.UserID]
426426
if !ok && tp != "" {
427427
var err error
428-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
428+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
429429
if err != nil {
430430
log.Error("GetUserIDByExternalUserID: %v", err)
431431
}
@@ -483,7 +483,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
483483
tp := g.gitServiceType.Name()
484484
if !ok && tp != "" {
485485
var err error
486-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", comment.PosterID))
486+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", comment.PosterID))
487487
if err != nil {
488488
log.Error("GetUserIDByExternalUserID: %v", err)
489489
}
@@ -520,7 +520,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
520520
userid, ok := g.userMap[reaction.UserID]
521521
if !ok && tp != "" {
522522
var err error
523-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
523+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
524524
if err != nil {
525525
log.Error("GetUserIDByExternalUserID: %v", err)
526526
}
@@ -564,7 +564,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
564564
tp := g.gitServiceType.Name()
565565
if !ok && tp != "" {
566566
var err error
567-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
567+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
568568
if err != nil {
569569
log.Error("GetUserIDByExternalUserID: %v", err)
570570
}
@@ -744,7 +744,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
744744
userid, ok := g.userMap[pr.PosterID]
745745
if !ok && tp != "" {
746746
var err error
747-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
747+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
748748
if err != nil {
749749
log.Error("GetUserIDByExternalUserID: %v", err)
750750
}
@@ -766,7 +766,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
766766
userid, ok := g.userMap[reaction.UserID]
767767
if !ok && tp != "" {
768768
var err error
769-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
769+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
770770
if err != nil {
771771
log.Error("GetUserIDByExternalUserID: %v", err)
772772
}
@@ -850,7 +850,7 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
850850
tp := g.gitServiceType.Name()
851851
if !ok && tp != "" {
852852
var err error
853-
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", review.ReviewerID))
853+
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", review.ReviewerID))
854854
if err != nil {
855855
log.Error("GetUserIDByExternalUserID: %v", err)
856856
}

‎services/migrations/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/models/db"
12+
user_model "code.gitea.io/gitea/models/user"
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/structs"
1415
)
@@ -45,7 +46,7 @@ func updateMigrationPosterIDByGitService(ctx context.Context, tp structs.GitServ
4546
default:
4647
}
4748

48-
users, err := models.FindExternalUsersByProvider(models.FindExternalUserOptions{
49+
users, err := user_model.FindExternalUsersByProvider(user_model.FindExternalUserOptions{
4950
Provider: provider,
5051
Start: start,
5152
Limit: batchSize,

0 commit comments

Comments
 (0)
Please sign in to comment.