mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-14 13:08:11 +02:00
Cleanup
- Move len sessions to template - Move time conversion inside CleanupExpiredUserSessions
This commit is contained in:
parent
0b354c8048
commit
1ea114fa67
@ -6,6 +6,7 @@ package auth
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
@ -117,12 +118,12 @@ func UpdateSessionActivity(ctx context.Context, sessionID, currentIP string) err
|
||||
|
||||
// CleanupExpiredUserSessions removes old session records based on retention policy.
|
||||
// It deletes:
|
||||
// - Sessions that were logged out more than retentionSeconds ago
|
||||
// - Abandoned sessions (never logged out) whose last activity is older than maxLifetime + retentionSeconds
|
||||
func CleanupExpiredUserSessions(ctx context.Context, retentionSeconds, maxLifetime int64) error {
|
||||
// - Sessions that were logged out more than retention ago
|
||||
// - Abandoned sessions (never logged out) whose last activity is older than maxLifetime + retention
|
||||
func CleanupExpiredUserSessions(ctx context.Context, retention, maxLifetime time.Duration) error {
|
||||
now := int64(timeutil.TimeStampNow())
|
||||
logoutCutoff := now - retentionSeconds
|
||||
abandonedCutoff := now - maxLifetime - retentionSeconds
|
||||
logoutCutoff := now - int64(retention.Seconds())
|
||||
abandonedCutoff := now - int64(maxLifetime.Seconds()) - int64(retention.Seconds())
|
||||
|
||||
_, err := db.GetEngine(ctx).Where(
|
||||
builder.Or(
|
||||
|
||||
@ -5,6 +5,7 @@ package auth_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
@ -190,9 +191,9 @@ func TestCleanupExpiredUserSessions(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
retentionSeconds := int64(86400 * 30) // 30 days
|
||||
maxLifetime := int64(86400) // 1 day
|
||||
require.NoError(t, auth_model.CleanupExpiredUserSessions(t.Context(), retentionSeconds, maxLifetime))
|
||||
retention := 30 * 24 * time.Hour // 30 days
|
||||
maxLifetime := 24 * time.Hour // 1 day
|
||||
require.NoError(t, auth_model.CleanupExpiredUserSessions(t.Context(), retention, maxLifetime))
|
||||
|
||||
// Active session should still exist
|
||||
_, err = auth_model.GetUserSessionByID(t.Context(), "sess-cleanup-active")
|
||||
@ -207,10 +208,10 @@ func TestCleanupExpiredUserSessionsAbandoned(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
now := timeutil.TimeStampNow()
|
||||
retentionSeconds := int64(86400 * 30) // 30 days
|
||||
maxLifetime := int64(86400) // 1 day
|
||||
retention := 30 * 24 * time.Hour // 30 days
|
||||
maxLifetime := 24 * time.Hour // 1 day
|
||||
|
||||
cutoff := int64(now) - maxLifetime - retentionSeconds
|
||||
cutoff := int64(now) - int64(maxLifetime.Seconds()) - int64(retention.Seconds())
|
||||
|
||||
// Abandoned session clearly older than cutoff — should be cleaned up.
|
||||
_, err := db.GetEngine(t.Context()).Insert(&auth_model.UserSession{
|
||||
@ -239,7 +240,7 @@ func TestCleanupExpiredUserSessionsAbandoned(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, auth_model.CleanupExpiredUserSessions(t.Context(), retentionSeconds, maxLifetime))
|
||||
require.NoError(t, auth_model.CleanupExpiredUserSessions(t.Context(), retention, maxLifetime))
|
||||
|
||||
// Clearly old abandoned session should be gone.
|
||||
_, err = auth_model.GetUserSessionByID(t.Context(), "sess-cleanup-abandoned-old")
|
||||
|
||||
@ -43,7 +43,6 @@ func UserSessions(ctx *context.Context) {
|
||||
}
|
||||
|
||||
ctx.Data["Sessions"] = sessions
|
||||
ctx.Data["SessionsTotal"] = len(sessions)
|
||||
|
||||
activeCount := 0
|
||||
for _, s := range sessions {
|
||||
|
||||
@ -182,9 +182,8 @@ func registerCleanupUserSessions() {
|
||||
OlderThan: time.Hour * 24 * 30, // 30 day retention
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
olderThanConfig := config.(*OlderThanConfig)
|
||||
retentionSeconds := int64(olderThanConfig.OlderThan.Seconds())
|
||||
maxLifetime := setting.SessionConfig.Maxlifetime
|
||||
return auth_model.CleanupExpiredUserSessions(ctx, retentionSeconds, maxLifetime)
|
||||
maxLifetime := time.Duration(setting.SessionConfig.Maxlifetime) * time.Second
|
||||
return auth_model.CleanupExpiredUserSessions(ctx, olderThanConfig.OlderThan, maxLifetime)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
<div class="admin-setting-content">
|
||||
<h4 class="ui top attached header">
|
||||
{{ctx.Locale.Tr "settings.sessions"}} — {{.User.Name}} ({{ctx.Locale.Tr "admin.total" .SessionsTotal}})
|
||||
{{ctx.Locale.Tr "settings.sessions"}} — {{.User.Name}} ({{ctx.Locale.Tr "admin.total" (len .Sessions)}})
|
||||
<div class="ui right">
|
||||
<a class="ui primary tiny button" href="{{AppSubUrl}}/-/admin/users/{{.User.ID}}">{{ctx.Locale.Tr "admin.users.details"}}</a>
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user