summaryrefslogtreecommitdiffstats
path: root/services/user
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-24 17:49:20 +0800
committerGitHub <noreply@github.com>2021-11-24 17:49:20 +0800
commita666829a37be6f9fd98f9e7dd1767c420f7f3b32 (patch)
tree9ab1434b759a8a2cb275a83149903a823851e309 /services/user
parent4e7ca946da2a2642a62f114825129bf5d7ed9196 (diff)
downloadgitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.tar.gz
gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.zip
Move user related model into models/user (#17781)
* Move user related model into models/user * Fix lint for windows * Fix windows lint * Fix windows lint * Move some tests in models * Merge
Diffstat (limited to 'services/user')
-rw-r--r--services/user/user.go12
-rw-r--r--services/user/user_test.go23
2 files changed, 18 insertions, 17 deletions
diff --git a/services/user/user.go b/services/user/user.go
index 0578f70b27..6df9eec131 100644
--- a/services/user/user.go
+++ b/services/user/user.go
@@ -25,7 +25,7 @@ import (
// DeleteUser completely and permanently deletes everything of a user,
// but issues/comments/pulls will be kept and shown as someone has been deleted,
// unless the user is younger than USER_DELETE_WITH_COMMENTS_MAX_DAYS.
-func DeleteUser(u *models.User) error {
+func DeleteUser(u *user_model.User) error {
if u.IsOrganization() {
return fmt.Errorf("%s is an organization not a user", u.Name)
}
@@ -72,7 +72,7 @@ func DeleteUser(u *models.User) error {
// Note: There are something just cannot be roll back,
// so just keep error logs of those operations.
- path := models.UserPath(u.Name)
+ path := user_model.UserPath(u.Name)
if err := util.RemoveAll(path); err != nil {
err = fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
_ = admin_model.CreateNotice(db.DefaultContext, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
@@ -93,7 +93,7 @@ func DeleteUser(u *models.User) error {
// DeleteInactiveUsers deletes all inactive users and email addresses.
func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) error {
- users, err := models.GetInactiveUsers(ctx, olderThan)
+ users, err := user_model.GetInactiveUsers(ctx, olderThan)
if err != nil {
return err
}
@@ -118,7 +118,7 @@ func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) error {
}
// UploadAvatar saves custom avatar for user.
-func UploadAvatar(u *models.User, data []byte) error {
+func UploadAvatar(u *user_model.User, data []byte) error {
m, err := avatar.Prepare(data)
if err != nil {
return err
@@ -136,7 +136,7 @@ func UploadAvatar(u *models.User, data []byte) error {
// Otherwise, if any of the users delete his avatar
// Other users will lose their avatars too.
u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data)))))
- if err = models.UpdateUserCols(ctx, u, "use_custom_avatar", "avatar"); err != nil {
+ if err = user_model.UpdateUserCols(ctx, u, "use_custom_avatar", "avatar"); err != nil {
return fmt.Errorf("updateUser: %v", err)
}
@@ -153,7 +153,7 @@ func UploadAvatar(u *models.User, data []byte) error {
}
// DeleteAvatar deletes the user's custom avatar.
-func DeleteAvatar(u *models.User) error {
+func DeleteAvatar(u *user_model.User) error {
aPath := u.CustomAvatarRelativePath()
log.Trace("DeleteAvatar[%d]: %s", u.ID, aPath)
if len(u.Avatar) > 0 {
diff --git a/services/user/user_test.go b/services/user/user_test.go
index 9162273fae..e18b760308 100644
--- a/services/user/user_test.go
+++ b/services/user/user_test.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
@@ -23,7 +24,7 @@ func TestMain(m *testing.M) {
func TestDeleteUser(t *testing.T) {
test := func(userID int64) {
assert.NoError(t, unittest.PrepareTestDatabase())
- user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User)
+ user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}).(*user_model.User)
ownedRepos := make([]*models.Repository, 0, 10)
assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&ownedRepos, &models.Repository{OwnerID: userID}))
@@ -43,20 +44,20 @@ func TestDeleteUser(t *testing.T) {
}
}
assert.NoError(t, DeleteUser(user))
- unittest.AssertNotExistsBean(t, &models.User{ID: userID})
- unittest.CheckConsistencyFor(t, &models.User{}, &models.Repository{})
+ unittest.AssertNotExistsBean(t, &user_model.User{ID: userID})
+ unittest.CheckConsistencyFor(t, &user_model.User{}, &models.Repository{})
}
test(2)
test(4)
test(8)
test(11)
- org := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User)
+ org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3}).(*user_model.User)
assert.Error(t, DeleteUser(org))
}
func TestCreateUser(t *testing.T) {
- user := &models.User{
+ user := &user_model.User{
Name: "GiteaBot",
Email: "GiteaBot@gitea.io",
Passwd: ";p['////..-++']",
@@ -65,7 +66,7 @@ func TestCreateUser(t *testing.T) {
MustChangePassword: false,
}
- assert.NoError(t, models.CreateUser(user))
+ assert.NoError(t, user_model.CreateUser(user))
assert.NoError(t, DeleteUser(user))
}
@@ -77,11 +78,11 @@ func TestCreateUser_Issue5882(t *testing.T) {
passwd := ".//.;1;;//.,-=_"
tt := []struct {
- user *models.User
+ user *user_model.User
disableOrgCreation bool
}{
- {&models.User{Name: "GiteaBot", Email: "GiteaBot@gitea.io", Passwd: passwd, MustChangePassword: false}, false},
- {&models.User{Name: "GiteaBot2", Email: "GiteaBot2@gitea.io", Passwd: passwd, MustChangePassword: false}, true},
+ {&user_model.User{Name: "GiteaBot", Email: "GiteaBot@gitea.io", Passwd: passwd, MustChangePassword: false}, false},
+ {&user_model.User{Name: "GiteaBot2", Email: "GiteaBot2@gitea.io", Passwd: passwd, MustChangePassword: false}, true},
}
setting.Service.DefaultAllowCreateOrganization = true
@@ -89,9 +90,9 @@ func TestCreateUser_Issue5882(t *testing.T) {
for _, v := range tt {
setting.Admin.DisableRegularOrgCreation = v.disableOrgCreation
- assert.NoError(t, models.CreateUser(v.user))
+ assert.NoError(t, user_model.CreateUser(v.user))
- u, err := models.GetUserByEmail(v.user.Email)
+ u, err := user_model.GetUserByEmail(v.user.Email)
assert.NoError(t, err)
assert.Equal(t, !u.AllowCreateOrganization, v.disableOrgCreation)