2021-11-18 10:42:27 -07:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 11:20:29 -07:00
// SPDX-License-Identifier: MIT
2021-11-18 10:42:27 -07:00
package user
import (
"context"
"fmt"
2023-05-21 09:13:47 -06:00
"os"
"strings"
2021-11-18 10:42:27 -07:00
"time"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
2022-03-29 00:29:02 -06:00
"code.gitea.io/gitea/models/organization"
2022-03-30 02:42:47 -06:00
packages_model "code.gitea.io/gitea/models/packages"
2021-12-09 18:27:50 -07:00
repo_model "code.gitea.io/gitea/models/repo"
2022-10-16 17:29:26 -06:00
system_model "code.gitea.io/gitea/models/system"
2021-11-18 10:42:27 -07:00
user_model "code.gitea.io/gitea/models/user"
2022-07-14 01:22:09 -06:00
"code.gitea.io/gitea/modules/eventsource"
2021-11-22 08:21:55 -07:00
"code.gitea.io/gitea/modules/log"
2022-07-14 01:22:09 -06:00
"code.gitea.io/gitea/modules/setting"
2021-11-18 10:42:27 -07:00
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
2023-05-21 09:13:47 -06:00
"code.gitea.io/gitea/services/agit"
2024-03-04 01:57:39 -07:00
asymkey_service "code.gitea.io/gitea/services/asymkey"
2023-10-19 07:16:11 -06:00
org_service "code.gitea.io/gitea/services/org"
2022-07-14 01:22:09 -06:00
"code.gitea.io/gitea/services/packages"
2023-05-21 09:13:47 -06:00
container_service "code.gitea.io/gitea/services/packages/container"
2023-09-07 22:51:15 -06:00
repo_service "code.gitea.io/gitea/services/repository"
2021-11-18 10:42:27 -07:00
)
2023-03-14 01:45:21 -06:00
// RenameUser renames a user
func RenameUser ( ctx context . Context , u * user_model . User , newUserName string ) error {
2024-10-12 21:13:55 -06:00
if newUserName == u . Name {
return nil
}
2023-05-21 09:13:47 -06:00
// Non-local users are not allowed to change their username.
if ! u . IsOrganization ( ) && ! u . IsLocal ( ) {
return user_model . ErrUserIsNotLocal {
UID : u . ID ,
Name : u . Name ,
}
}
if err := user_model . IsUsableUsername ( newUserName ) ; err != nil {
return err
}
onlyCapitalization := strings . EqualFold ( newUserName , u . Name )
oldUserName := u . Name
if onlyCapitalization {
u . Name = newUserName
if err := user_model . UpdateUserCols ( ctx , u , "name" ) ; err != nil {
u . Name = oldUserName
return err
}
2023-09-16 08:39:12 -06:00
return repo_model . UpdateRepositoryOwnerNames ( ctx , u . ID , newUserName )
2023-05-21 09:13:47 -06:00
}
2023-03-14 01:45:21 -06:00
ctx , committer , err := db . TxContext ( ctx )
if err != nil {
return err
}
defer committer . Close ( )
2023-05-21 09:13:47 -06:00
isExist , err := user_model . IsUserExist ( ctx , u . ID , newUserName )
if err != nil {
2023-03-14 01:45:21 -06:00
return err
}
2023-05-21 09:13:47 -06:00
if isExist {
return user_model . ErrUserAlreadyExist {
Name : newUserName ,
}
}
if err = repo_model . UpdateRepositoryOwnerName ( ctx , oldUserName , newUserName ) ; err != nil {
return err
}
if err = user_model . NewUserRedirect ( ctx , u . ID , oldUserName , newUserName ) ; err != nil {
return err
}
if err := agit . UserNameChanged ( ctx , u , newUserName ) ; err != nil {
return err
}
if err := container_service . UpdateRepositoryNames ( ctx , u , newUserName ) ; err != nil {
return err
}
u . Name = newUserName
u . LowerName = strings . ToLower ( newUserName )
if err := user_model . UpdateUserCols ( ctx , u , "name" , "lower_name" ) ; err != nil {
u . Name = oldUserName
u . LowerName = strings . ToLower ( oldUserName )
2023-03-14 01:45:21 -06:00
return err
}
2023-05-21 09:13:47 -06:00
// Do not fail if directory does not exist
if err = util . Rename ( user_model . UserPath ( oldUserName ) , user_model . UserPath ( newUserName ) ) ; err != nil && ! os . IsNotExist ( err ) {
u . Name = oldUserName
u . LowerName = strings . ToLower ( oldUserName )
return fmt . Errorf ( "rename user directory: %w" , err )
}
if err = committer . Commit ( ) ; err != nil {
u . Name = oldUserName
u . LowerName = strings . ToLower ( oldUserName )
if err2 := util . Rename ( user_model . UserPath ( newUserName ) , user_model . UserPath ( oldUserName ) ) ; err2 != nil && ! os . IsNotExist ( err2 ) {
log . Critical ( "Unable to rollback directory change during failed username change from: %s to: %s. DB Error: %v. Filesystem Error: %v" , oldUserName , newUserName , err , err2 )
return fmt . Errorf ( "failed to rollback directory change during failed username change from: %s to: %s. DB Error: %w. Filesystem Error: %v" , oldUserName , newUserName , err , err2 )
}
return err
}
return nil
2023-03-14 01:45:21 -06:00
}
2021-11-18 10:42:27 -07:00
// 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.
2022-07-14 01:22:09 -06:00
func DeleteUser ( ctx context . Context , u * user_model . User , purge bool ) error {
2021-11-18 10:42:27 -07:00
if u . IsOrganization ( ) {
return fmt . Errorf ( "%s is an organization not a user" , u . Name )
}
2024-03-31 20:06:35 -06:00
if u . IsActive && user_model . IsLastAdminUser ( ctx , u ) {
2024-01-14 23:51:43 -07:00
return models . ErrDeleteLastAdminUser { UID : u . ID }
}
2022-07-14 01:22:09 -06:00
if purge {
// Disable the user first
// NOTE: This is deliberately not within a transaction as it must disable the user immediately to prevent any further action by the user to be purged.
if err := user_model . UpdateUserCols ( ctx , & user_model . User {
ID : u . ID ,
IsActive : false ,
IsRestricted : true ,
IsAdmin : false ,
ProhibitLogin : true ,
Passwd : "" ,
Salt : "" ,
PasswdHashAlgo : "" ,
MaxRepoCreation : 0 ,
} , "is_active" , "is_restricted" , "is_admin" , "prohibit_login" , "max_repo_creation" , "passwd" , "salt" , "passwd_hash_algo" ) ; err != nil {
return fmt . Errorf ( "unable to disable user: %s[%d] prior to purge. UpdateUserCols: %w" , u . Name , u . ID , err )
}
// Force any logged in sessions to log out
// FIXME: We also need to tell the session manager to log them out too.
eventsource . GetManager ( ) . SendMessage ( u . ID , & eventsource . Event {
Name : "logout" ,
} )
// Delete all repos belonging to this user
// Now this is not within a transaction because there are internal transactions within the DeleteRepository
// BUT: the db will still be consistent even if a number of repos have already been deleted.
// And in fact we want to capture any repositories that are being created in other transactions in the meantime
//
// An alternative option here would be write a DeleteAllRepositoriesForUserID function which would delete all of the repos
// but such a function would likely get out of date
2023-10-19 07:16:11 -06:00
err := repo_service . DeleteOwnerRepositoriesDirectly ( ctx , u )
if err != nil {
return err
2022-07-14 01:22:09 -06:00
}
// Remove from Organizations and delete last owner organizations
// Now this is not within a transaction because there are internal transactions within the DeleteOrganization
// BUT: the db will still be consistent even if a number of organizations memberships and organizations have already been deleted
// And in fact we want to capture any organization additions that are being created in other transactions in the meantime
//
// An alternative option here would be write a function which would delete all organizations but it seems
// but such a function would likely get out of date
for {
2023-11-23 20:49:41 -07:00
orgs , err := db . Find [ organization . Organization ] ( ctx , organization . FindOrgOptions {
2022-07-14 01:22:09 -06:00
ListOptions : db . ListOptions {
PageSize : repo_model . RepositoryListDefaultPageSize ,
Page : 1 ,
} ,
UserID : u . ID ,
IncludePrivate : true ,
} )
if err != nil {
2022-10-24 13:29:17 -06:00
return fmt . Errorf ( "unable to find org list for %s[%d]. Error: %w" , u . Name , u . ID , err )
2022-07-14 01:22:09 -06:00
}
if len ( orgs ) == 0 {
break
}
for _ , org := range orgs {
2024-03-04 01:16:03 -07:00
if err := models . RemoveOrgUser ( ctx , org , u ) ; err != nil {
2022-07-14 01:22:09 -06:00
if organization . IsErrLastOrgOwner ( err ) {
2023-10-19 07:16:11 -06:00
err = org_service . DeleteOrganization ( ctx , org , true )
if err != nil {
return fmt . Errorf ( "unable to delete organization %d: %w" , org . ID , err )
}
2022-07-14 01:22:09 -06:00
}
if err != nil {
2022-10-24 13:29:17 -06:00
return fmt . Errorf ( "unable to remove user %s[%d] from org %s[%d]. Error: %w" , u . Name , u . ID , org . Name , org . ID , err )
2022-07-14 01:22:09 -06:00
}
}
}
}
// Delete Packages
if setting . Packages . Enabled {
if _ , err := packages . RemoveAllPackages ( ctx , u . ID ) ; err != nil {
return err
}
}
}
2023-10-10 22:24:07 -06:00
ctx , committer , err := db . TxContext ( ctx )
2021-11-18 10:42:27 -07:00
if err != nil {
return err
}
2021-12-10 01:14:24 -07:00
defer committer . Close ( )
2021-11-18 10:42:27 -07:00
// Note: A user owns any repository or belongs to any organization
2022-07-14 01:22:09 -06:00
// cannot perform delete operation. This causes a race with the purge above
// however consistency requires that we ensure that this is the case
2021-11-18 10:42:27 -07:00
// Check ownership of repository.
2022-05-20 08:08:52 -06:00
count , err := repo_model . CountRepositories ( ctx , repo_model . CountRepositoryOptions { OwnerID : u . ID } )
2021-11-18 10:42:27 -07:00
if err != nil {
2022-10-24 13:29:17 -06:00
return fmt . Errorf ( "GetRepositoryCount: %w" , err )
2021-11-18 10:42:27 -07:00
} else if count > 0 {
return models . ErrUserOwnRepos { UID : u . ID }
}
// Check membership of organization.
2022-03-29 00:29:02 -06:00
count , err = organization . GetOrganizationCount ( ctx , u )
2021-11-18 10:42:27 -07:00
if err != nil {
2022-10-24 13:29:17 -06:00
return fmt . Errorf ( "GetOrganizationCount: %w" , err )
2021-11-18 10:42:27 -07:00
} else if count > 0 {
return models . ErrUserHasOrgs { UID : u . ID }
}
2022-03-30 02:42:47 -06:00
// Check ownership of packages.
if ownsPackages , err := packages_model . HasOwnerPackages ( ctx , u . ID ) ; err != nil {
2022-10-24 13:29:17 -06:00
return fmt . Errorf ( "HasOwnerPackages: %w" , err )
2022-03-30 02:42:47 -06:00
} else if ownsPackages {
return models . ErrUserOwnPackages { UID : u . ID }
}
2023-02-12 22:11:41 -07:00
if err := deleteUser ( ctx , u , purge ) ; err != nil {
2022-10-24 13:29:17 -06:00
return fmt . Errorf ( "DeleteUser: %w" , err )
2021-11-18 10:42:27 -07:00
}
2021-12-10 01:14:24 -07:00
if err := committer . Commit ( ) ; err != nil {
2021-11-18 10:42:27 -07:00
return err
}
2024-03-31 20:06:35 -06:00
_ = committer . Close ( )
2021-11-18 10:42:27 -07:00
2024-03-04 01:57:39 -07:00
if err = asymkey_service . RewriteAllPublicKeys ( ctx ) ; err != nil {
2021-11-22 08:21:55 -07:00
return err
}
2024-03-04 01:57:39 -07:00
if err = asymkey_service . RewriteAllPrincipalKeys ( ctx ) ; err != nil {
2021-11-22 08:21:55 -07:00
return err
}
2024-03-31 20:06:35 -06:00
// Note: There are something just cannot be roll back, so just keep error logs of those operations.
2021-11-24 02:49:20 -07:00
path := user_model . UserPath ( u . Name )
2024-03-31 20:06:35 -06:00
if err = util . RemoveAll ( path ) ; err != nil {
err = fmt . Errorf ( "failed to RemoveAll %s: %w" , path , err )
2022-10-16 17:29:26 -06:00
_ = system_model . CreateNotice ( ctx , system_model . NoticeTask , fmt . Sprintf ( "delete user '%s': %v" , u . Name , err ) )
2021-11-18 10:42:27 -07:00
}
if u . Avatar != "" {
avatarPath := u . CustomAvatarRelativePath ( )
2024-03-31 20:06:35 -06:00
if err = storage . Avatars . Delete ( avatarPath ) ; err != nil {
err = fmt . Errorf ( "failed to remove %s: %w" , avatarPath , err )
2022-10-16 17:29:26 -06:00
_ = system_model . CreateNotice ( ctx , system_model . NoticeTask , fmt . Sprintf ( "delete user '%s': %v" , u . Name , err ) )
2021-11-18 10:42:27 -07:00
}
}
return nil
}
2024-03-31 20:06:35 -06:00
// DeleteInactiveUsers deletes all inactive users and their email addresses.
2021-11-18 10:42:27 -07:00
func DeleteInactiveUsers ( ctx context . Context , olderThan time . Duration ) error {
2024-03-31 20:06:35 -06:00
inactiveUsers , err := user_model . GetInactiveUsers ( ctx , olderThan )
2021-11-18 10:42:27 -07:00
if err != nil {
return err
}
// FIXME: should only update authorized_keys file once after all deletions.
2024-03-31 20:06:35 -06:00
for _ , u := range inactiveUsers {
if err = DeleteUser ( ctx , u , false ) ; err != nil {
// Ignore inactive users that were ever active but then were set inactive by admin
if models . IsErrUserOwnRepos ( err ) || models . IsErrUserHasOrgs ( err ) || models . IsErrUserOwnPackages ( err ) {
2024-10-22 19:28:28 -06:00
log . Warn ( "Inactive user %q has repositories, organizations or packages, skipping deletion: %v" , u . Name , err )
2021-11-18 10:42:27 -07:00
continue
}
2024-03-31 20:06:35 -06:00
select {
case <- ctx . Done ( ) :
return db . ErrCancelledf ( "when deleting inactive user %q" , u . Name )
default :
return err
}
2021-11-18 10:42:27 -07:00
}
}
2024-03-31 20:06:35 -06:00
return nil // TODO: there could be still inactive users left, and the number would increase gradually
2021-11-18 10:42:27 -07:00
}