2021-11-18 18:42:27 +01:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-22 16:21:55 +01:00
|
|
|
"crypto/md5"
|
2021-11-18 18:42:27 +01:00
|
|
|
"fmt"
|
2021-11-22 16:21:55 +01:00
|
|
|
"image/png"
|
|
|
|
"io"
|
2021-11-18 18:42:27 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
admin_model "code.gitea.io/gitea/models/admin"
|
2021-12-10 09:14:24 +01:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2021-11-18 18:42:27 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 08:29:02 +02:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-03-30 10:42:47 +02:00
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-18 18:42:27 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-22 16:21:55 +01:00
|
|
|
"code.gitea.io/gitea/modules/avatar"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-18 18:42:27 +01:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2021-11-24 10:49:20 +01:00
|
|
|
func DeleteUser(u *user_model.User) error {
|
2021-11-18 18:42:27 +01:00
|
|
|
if u.IsOrganization() {
|
|
|
|
return fmt.Errorf("%s is an organization not a user", u.Name)
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:14:24 +01:00
|
|
|
ctx, committer, err := db.TxContext()
|
2021-11-18 18:42:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-10 09:14:24 +01:00
|
|
|
defer committer.Close()
|
2021-11-18 18:42:27 +01:00
|
|
|
|
|
|
|
// Note: A user owns any repository or belongs to any organization
|
|
|
|
// cannot perform delete operation.
|
|
|
|
|
|
|
|
// Check ownership of repository.
|
2022-05-20 16:08:52 +02:00
|
|
|
count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: u.ID})
|
2021-11-18 18:42:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetRepositoryCount: %v", err)
|
|
|
|
} else if count > 0 {
|
|
|
|
return models.ErrUserOwnRepos{UID: u.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check membership of organization.
|
2022-03-29 08:29:02 +02:00
|
|
|
count, err = organization.GetOrganizationCount(ctx, u)
|
2021-11-18 18:42:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetOrganizationCount: %v", err)
|
|
|
|
} else if count > 0 {
|
|
|
|
return models.ErrUserHasOrgs{UID: u.ID}
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:42:47 +02:00
|
|
|
// Check ownership of packages.
|
|
|
|
if ownsPackages, err := packages_model.HasOwnerPackages(ctx, u.ID); err != nil {
|
|
|
|
return fmt.Errorf("HasOwnerPackages: %v", err)
|
|
|
|
} else if ownsPackages {
|
|
|
|
return models.ErrUserOwnPackages{UID: u.ID}
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:42:27 +01:00
|
|
|
if err := models.DeleteUser(ctx, u); err != nil {
|
|
|
|
return fmt.Errorf("DeleteUser: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:14:24 +01:00
|
|
|
if err := committer.Commit(); err != nil {
|
2021-11-18 18:42:27 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-12-10 09:14:24 +01:00
|
|
|
committer.Close()
|
2021-11-18 18:42:27 +01:00
|
|
|
|
2021-12-10 09:14:24 +01:00
|
|
|
if err = asymkey_model.RewriteAllPublicKeys(); err != nil {
|
2021-11-22 16:21:55 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-05-20 16:08:52 +02:00
|
|
|
if err = asymkey_model.RewriteAllPrincipalKeys(db.DefaultContext); err != nil {
|
2021-11-22 16:21:55 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-18 18:42:27 +01:00
|
|
|
// Note: There are something just cannot be roll back,
|
|
|
|
// so just keep error logs of those operations.
|
2021-11-24 10:49:20 +01:00
|
|
|
path := user_model.UserPath(u.Name)
|
2021-11-18 18:42:27 +01:00
|
|
|
if err := util.RemoveAll(path); err != nil {
|
|
|
|
err = fmt.Errorf("Failed to RemoveAll %s: %v", path, err)
|
2022-03-22 16:22:54 +01:00
|
|
|
_ = admin_model.CreateNotice(ctx, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
|
2021-11-18 18:42:27 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Avatar != "" {
|
|
|
|
avatarPath := u.CustomAvatarRelativePath()
|
|
|
|
if err := storage.Avatars.Delete(avatarPath); err != nil {
|
|
|
|
err = fmt.Errorf("Failed to remove %s: %v", avatarPath, err)
|
2022-03-22 16:22:54 +01:00
|
|
|
_ = admin_model.CreateNotice(ctx, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err))
|
2021-11-18 18:42:27 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteInactiveUsers deletes all inactive users and email addresses.
|
|
|
|
func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) error {
|
2021-11-24 10:49:20 +01:00
|
|
|
users, err := user_model.GetInactiveUsers(ctx, olderThan)
|
2021-11-18 18:42:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: should only update authorized_keys file once after all deletions.
|
|
|
|
for _, u := range users {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return db.ErrCancelledf("Before delete inactive user %s", u.Name)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err := DeleteUser(u); err != nil {
|
|
|
|
// Ignore users that were set inactive by admin.
|
2022-03-30 10:42:47 +02:00
|
|
|
if models.IsErrUserOwnRepos(err) || models.IsErrUserHasOrgs(err) || models.IsErrUserOwnPackages(err) {
|
2021-11-18 18:42:27 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_model.DeleteInactiveEmailAddresses(ctx)
|
|
|
|
}
|
2021-11-22 16:21:55 +01:00
|
|
|
|
|
|
|
// UploadAvatar saves custom avatar for user.
|
2021-11-24 10:49:20 +01:00
|
|
|
func UploadAvatar(u *user_model.User, data []byte) error {
|
2021-11-22 16:21:55 +01:00
|
|
|
m, err := avatar.Prepare(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
u.UseCustomAvatar = true
|
|
|
|
// Different users can upload same image as avatar
|
|
|
|
// If we prefix it with u.ID, it will be separated
|
|
|
|
// 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)))))
|
2021-11-24 10:49:20 +01:00
|
|
|
if err = user_model.UpdateUserCols(ctx, u, "use_custom_avatar", "avatar"); err != nil {
|
2021-11-22 16:21:55 +01:00
|
|
|
return fmt.Errorf("updateUser: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storage.SaveFrom(storage.Avatars, u.CustomAvatarRelativePath(), func(w io.Writer) error {
|
|
|
|
if err := png.Encode(w, *m); err != nil {
|
|
|
|
log.Error("Encode: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("Failed to create dir %s: %v", u.CustomAvatarRelativePath(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return committer.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAvatar deletes the user's custom avatar.
|
2021-11-24 10:49:20 +01:00
|
|
|
func DeleteAvatar(u *user_model.User) error {
|
2021-11-22 16:21:55 +01:00
|
|
|
aPath := u.CustomAvatarRelativePath()
|
|
|
|
log.Trace("DeleteAvatar[%d]: %s", u.ID, aPath)
|
|
|
|
if len(u.Avatar) > 0 {
|
|
|
|
if err := storage.Avatars.Delete(aPath); err != nil {
|
|
|
|
return fmt.Errorf("Failed to remove %s: %v", aPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u.UseCustomAvatar = false
|
|
|
|
u.Avatar = ""
|
|
|
|
if _, err := db.GetEngine(db.DefaultContext).ID(u.ID).Cols("avatar, use_custom_avatar").Update(u); err != nil {
|
|
|
|
return fmt.Errorf("UpdateUser: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|