mirror of
				https://github.com/h44z/wg-portal.git
				synced 2025-11-03 23:56:18 +00:00 
			
		
		
		
	code cleanup
This commit is contained in:
		@@ -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,
 | 
			
		||||
		},
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
			},
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
		},
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user