Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
h44z committed Jan 11, 2025
1 parent 26d3257 commit 63d85d8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
16 changes: 8 additions & 8 deletions internal/app/migrate_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
16 changes: 11 additions & 5 deletions internal/app/users/ldap_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/app/users/user_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/app/wireguard/wireguard_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
7 changes: 5 additions & 2 deletions internal/domain/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 63d85d8

Please sign in to comment.