aboutsummaryrefslogtreecommitdiffstats
path: root/models/user.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-05-17 00:31:38 +0100
committerGitHub <noreply@github.com>2020-05-16 19:31:38 -0400
commit9a2e47b23a6d460acfce9b1b77e6f9fb06ca1b75 (patch)
treeb1852472e1ecf6bdb1822b41655bdaf8afd87c1b /models/user.go
parentc18144086f9d4a06adbd4a7c08cfa6dab91224ec (diff)
downloadgitea-9a2e47b23a6d460acfce9b1b77e6f9fb06ca1b75.tar.gz
gitea-9a2e47b23a6d460acfce9b1b77e6f9fb06ca1b75.zip
Refactor Cron and merge dashboard tasks (#10745)
* Refactor Cron and merge dashboard tasks * Merge Cron and Dashboard tasks * Make every cron task report a system notice on completion * Refactor the creation of these tasks * Ensure that execution counts of tasks is correct * Allow cron tasks to be started from the cron page * golangci-lint fixes * Enforce that only one task with the same name can be registered Signed-off-by: Andrew Thornton <art27@cantab.net> * fix name check Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @guillep2k * as per @lafriks Signed-off-by: Andrew Thornton <art27@cantab.net> * Add git.CommandContext variants Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'models/user.go')
-rw-r--r--models/user.go51
1 files changed, 32 insertions, 19 deletions
diff --git a/models/user.go b/models/user.go
index 01cce2506d..8875840db7 100644
--- a/models/user.go
+++ b/models/user.go
@@ -1321,16 +1321,30 @@ func DeleteUser(u *User) (err error) {
return sess.Commit()
}
-// DeleteInactivateUsers deletes all inactivate users and email addresses.
-func DeleteInactivateUsers() (err error) {
+// DeleteInactiveUsers deletes all inactive users and email addresses.
+func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) (err error) {
users := make([]*User, 0, 10)
- if err = x.
- Where("is_active = ?", false).
- Find(&users); err != nil {
- return fmt.Errorf("get all inactive users: %v", err)
+ if olderThan > 0 {
+ if err = x.
+ Where("is_active = ? and created_unix < ?", false, time.Now().Add(-olderThan).Unix()).
+ Find(&users); err != nil {
+ return fmt.Errorf("get all inactive users: %v", err)
+ }
+ } else {
+ if err = x.
+ Where("is_active = ?", false).
+ Find(&users); err != nil {
+ return fmt.Errorf("get all inactive users: %v", err)
+ }
+
}
// FIXME: should only update authorized_keys file once after all deletions.
for _, u := range users {
+ select {
+ case <-ctx.Done():
+ return ErrCancelledf("Before delete inactive user %s", u.Name)
+ default:
+ }
if err = DeleteUser(u); err != nil {
// Ignore users that were set inactive by admin.
if IsErrUserOwnRepos(err) || IsErrUserHasOrgs(err) {
@@ -1814,25 +1828,23 @@ func synchronizeLdapSSHPublicKeys(usr *User, s *LoginSource, sshPublicKeys []str
}
// SyncExternalUsers is used to synchronize users with external authorization source
-func SyncExternalUsers(ctx context.Context) {
+func SyncExternalUsers(ctx context.Context, updateExisting bool) error {
log.Trace("Doing: SyncExternalUsers")
ls, err := LoginSources()
if err != nil {
log.Error("SyncExternalUsers: %v", err)
- return
+ return err
}
- updateExisting := setting.Cron.SyncExternalUsers.UpdateExisting
-
for _, s := range ls {
if !s.IsActived || !s.IsSyncEnabled {
continue
}
select {
case <-ctx.Done():
- log.Warn("SyncExternalUsers: Aborted due to shutdown before update of %s", s.Name)
- return
+ log.Warn("SyncExternalUsers: Cancelled before update of %s", s.Name)
+ return ErrCancelledf("Before update of %s", s.Name)
default:
}
@@ -1850,12 +1862,12 @@ func SyncExternalUsers(ctx context.Context) {
Find(&users)
if err != nil {
log.Error("SyncExternalUsers: %v", err)
- return
+ return err
}
select {
case <-ctx.Done():
- log.Warn("SyncExternalUsers: Aborted due to shutdown before update of %s", s.Name)
- return
+ log.Warn("SyncExternalUsers: Cancelled before update of %s", s.Name)
+ return ErrCancelledf("Before update of %s", s.Name)
default:
}
@@ -1877,7 +1889,7 @@ func SyncExternalUsers(ctx context.Context) {
for _, su := range sr {
select {
case <-ctx.Done():
- log.Warn("SyncExternalUsers: Aborted due to shutdown at update of %s before completed update of users", s.Name)
+ log.Warn("SyncExternalUsers: Cancelled at update of %s before completed update of users", s.Name)
// Rewrite authorized_keys file if LDAP Public SSH Key attribute is set and any key was added or removed
if sshKeysNeedUpdate {
err = RewriteAllPublicKeys()
@@ -1885,7 +1897,7 @@ func SyncExternalUsers(ctx context.Context) {
log.Error("RewriteAllPublicKeys: %v", err)
}
}
- return
+ return ErrCancelledf("During update of %s before completed update of users", s.Name)
default:
}
if len(su.Username) == 0 {
@@ -1980,8 +1992,8 @@ func SyncExternalUsers(ctx context.Context) {
select {
case <-ctx.Done():
- log.Warn("SyncExternalUsers: Aborted due to shutdown at update of %s before delete users", s.Name)
- return
+ log.Warn("SyncExternalUsers: Cancelled during update of %s before delete users", s.Name)
+ return ErrCancelledf("During update of %s before delete users", s.Name)
default:
}
@@ -2008,4 +2020,5 @@ func SyncExternalUsers(ctx context.Context) {
}
}
}
+ return nil
}