From 63d85d8123103794c40a617bbf00e13dce5668af Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Sat, 11 Jan 2025 22:56:25 +0100 Subject: [PATCH] code cleanup --- internal/app/app.go | 4 ++-- internal/app/migrate_v1.go | 16 ++++++++-------- internal/app/users/ldap_helper.go | 16 +++++++++++----- internal/app/users/user_manager.go | 2 +- internal/app/wireguard/wireguard_interfaces.go | 8 ++++---- internal/domain/context.go | 7 +++++-- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index b7318ba..84e6ff2 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -129,8 +129,8 @@ func (a *App) createDefaultUser(ctx context.Context) error { now := time.Now() admin, err := a.CreateUser(ctx, &domain.User{ BaseModel: domain.BaseModel{ - CreatedBy: "system", - UpdatedBy: "system", + CreatedBy: domain.CtxSystemAdminId, + UpdatedBy: domain.CtxSystemAdminId, CreatedAt: now, UpdatedAt: now, }, diff --git a/internal/app/migrate_v1.go b/internal/app/migrate_v1.go index e5ca6b4..27902c0 100644 --- a/internal/app/migrate_v1.go +++ b/internal/app/migrate_v1.go @@ -98,8 +98,8 @@ func migrateV1Users(oldDb, newDb *gorm.DB) error { } newUser := domain.User{ BaseModel: domain.BaseModel{ - CreatedBy: "v1migrator", - UpdatedBy: "v1migrator", + CreatedBy: domain.CtxSystemV1Migrator, + UpdatedBy: domain.CtxSystemV1Migrator, CreatedAt: oldUser.CreatedAt, UpdatedAt: oldUser.UpdatedAt, }, @@ -173,8 +173,8 @@ func migrateV1Interfaces(oldDb, newDb *gorm.DB) error { } newInterface := domain.Interface{ BaseModel: domain.BaseModel{ - CreatedBy: "v1migrator", - UpdatedBy: "v1migrator", + CreatedBy: domain.CtxSystemV1Migrator, + UpdatedBy: domain.CtxSystemV1Migrator, CreatedAt: oldDevice.CreatedAt, UpdatedAt: oldDevice.UpdatedAt, }, @@ -299,8 +299,8 @@ func migrateV1Peers(oldDb, newDb *gorm.DB) error { now := time.Now() user = domain.User{ BaseModel: domain.BaseModel{ - CreatedBy: "v1migrator", - UpdatedBy: "v1migrator", + CreatedBy: domain.CtxSystemV1Migrator, + UpdatedBy: domain.CtxSystemV1Migrator, CreatedAt: now, UpdatedAt: now, }, @@ -322,8 +322,8 @@ func migrateV1Peers(oldDb, newDb *gorm.DB) error { } newPeer := domain.Peer{ BaseModel: domain.BaseModel{ - CreatedBy: "v1migrator", - UpdatedBy: "v1migrator", + CreatedBy: domain.CtxSystemV1Migrator, + UpdatedBy: domain.CtxSystemV1Migrator, CreatedAt: oldPeer.CreatedAt, UpdatedAt: oldPeer.UpdatedAt, }, diff --git a/internal/app/users/ldap_helper.go b/internal/app/users/ldap_helper.go index 13837a8..32db6e1 100644 --- a/internal/app/users/ldap_helper.go +++ b/internal/app/users/ldap_helper.go @@ -2,15 +2,21 @@ package users import ( "fmt" + "strings" + "time" + "github.com/go-ldap/ldap/v3" "github.com/h44z/wg-portal/internal" "github.com/h44z/wg-portal/internal/config" "github.com/h44z/wg-portal/internal/domain" - "strings" - "time" ) -func convertRawLdapUser(providerName string, rawUser map[string]any, fields *config.LdapFields, adminGroupDN *ldap.DN) (*domain.User, error) { +func convertRawLdapUser( + providerName string, + rawUser map[string]any, + fields *config.LdapFields, + adminGroupDN *ldap.DN, +) (*domain.User, error) { now := time.Now() isAdmin, err := internal.LdapIsMemberOf(rawUser[fields.GroupMembership].([][]byte), adminGroupDN) @@ -20,8 +26,8 @@ func convertRawLdapUser(providerName string, rawUser map[string]any, fields *con return &domain.User{ BaseModel: domain.BaseModel{ - CreatedBy: "ldap_sync", - UpdatedBy: "ldap_sync", + CreatedBy: domain.CtxSystemLdapSyncer, + UpdatedBy: domain.CtxSystemLdapSyncer, CreatedAt: now, UpdatedAt: now, }, diff --git a/internal/app/users/user_manager.go b/internal/app/users/user_manager.go index 5c3d5f1..777402b 100644 --- a/internal/app/users/user_manager.go +++ b/internal/app/users/user_manager.go @@ -513,7 +513,7 @@ func (m Manager) updateLdapUsers( err := m.users.SaveUser(tctx, user.Identifier, func(u *domain.User) (*domain.User, error) { u.UpdatedAt = time.Now() - u.UpdatedBy = "ldap_sync" + u.UpdatedBy = domain.CtxSystemLdapSyncer u.Email = user.Email u.Firstname = user.Firstname u.Lastname = user.Lastname diff --git a/internal/app/wireguard/wireguard_interfaces.go b/internal/app/wireguard/wireguard_interfaces.go index 4718ae0..0d1da82 100644 --- a/internal/app/wireguard/wireguard_interfaces.go +++ b/internal/app/wireguard/wireguard_interfaces.go @@ -705,8 +705,8 @@ func (m Manager) importInterface(ctx context.Context, in *domain.PhysicalInterfa now := time.Now() iface := domain.ConvertPhysicalInterface(in) iface.BaseModel = domain.BaseModel{ - CreatedBy: "importer", - UpdatedBy: "importer", + CreatedBy: domain.CtxSystemWgImporter, + UpdatedBy: domain.CtxSystemWgImporter, CreatedAt: now, UpdatedAt: now, } @@ -742,8 +742,8 @@ func (m Manager) importPeer(ctx context.Context, in *domain.Interface, p *domain now := time.Now() peer := domain.ConvertPhysicalPeer(p) peer.BaseModel = domain.BaseModel{ - CreatedBy: "importer", - UpdatedBy: "importer", + CreatedBy: domain.CtxSystemWgImporter, + UpdatedBy: domain.CtxSystemWgImporter, CreatedAt: now, UpdatedAt: now, } diff --git a/internal/domain/context.go b/internal/domain/context.go index ebe11aa..80b1fb1 100644 --- a/internal/domain/context.go +++ b/internal/domain/context.go @@ -12,8 +12,11 @@ import ( const CtxUserInfo = "userInfo" const ( - CtxSystemAdminId = "_WG_SYS_ADMIN_" - CtxUnknownUserId = "_WG_SYS_UNKNOWN_" + CtxSystemAdminId = "_WG_SYS_ADMIN_" + CtxUnknownUserId = "_WG_SYS_UNKNOWN_" + CtxSystemLdapSyncer = "_WG_SYS_LDAP_SYNCER_" + CtxSystemWgImporter = "_WG_SYS_WG_IMPORTER_" + CtxSystemV1Migrator = "_WG_SYS_V1_MIGRATOR_" ) type ContextUserInfo struct {