mirror of
https://github.com/h44z/wg-portal.git
synced 2026-01-11 14:36:16 +00:00
bulk actions for peers and users (#492)
This commit is contained in:
@@ -2,6 +2,7 @@ package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
@@ -118,3 +119,30 @@ func (p PeerService) SendPeerEmail(
|
||||
func (p PeerService) GetPeerStats(ctx context.Context, id domain.InterfaceIdentifier) ([]domain.PeerStatus, error) {
|
||||
return p.peers.GetPeerStats(ctx, id)
|
||||
}
|
||||
|
||||
func (p PeerService) BulkDelete(ctx context.Context, ids []domain.PeerIdentifier) error {
|
||||
for _, id := range ids {
|
||||
if err := p.peers.DeletePeer(ctx, id); err != nil {
|
||||
return fmt.Errorf("failed to delete peer %s: %w", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p PeerService) BulkUpdate(ctx context.Context, ids []domain.PeerIdentifier, updateFn func(*domain.Peer)) error {
|
||||
for _, id := range ids {
|
||||
peer, err := p.peers.GetPeer(ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get peer %s: %w", id, err)
|
||||
}
|
||||
|
||||
updateFn(peer)
|
||||
|
||||
if _, err := p.peers.UpdatePeer(ctx, peer); err != nil {
|
||||
return fmt.Errorf("failed to update peer %s: %w", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -72,7 +72,11 @@ func (u UserService) DeactivateApi(ctx context.Context, id domain.UserIdentifier
|
||||
return u.users.DeactivateApi(ctx, id)
|
||||
}
|
||||
|
||||
func (u UserService) ChangePassword(ctx context.Context, id domain.UserIdentifier, oldPassword, newPassword string) (*domain.User, error) {
|
||||
func (u UserService) ChangePassword(
|
||||
ctx context.Context,
|
||||
id domain.UserIdentifier,
|
||||
oldPassword, newPassword string,
|
||||
) (*domain.User, error) {
|
||||
oldPassword = strings.TrimSpace(oldPassword)
|
||||
newPassword = strings.TrimSpace(newPassword)
|
||||
|
||||
@@ -121,3 +125,30 @@ func (u UserService) GetUserPeerStats(ctx context.Context, id domain.UserIdentif
|
||||
func (u UserService) GetUserInterfaces(ctx context.Context, id domain.UserIdentifier) ([]domain.Interface, error) {
|
||||
return u.wg.GetUserInterfaces(ctx, id)
|
||||
}
|
||||
|
||||
func (u UserService) BulkDelete(ctx context.Context, ids []domain.UserIdentifier) error {
|
||||
for _, id := range ids {
|
||||
if err := u.users.DeleteUser(ctx, id); err != nil {
|
||||
return fmt.Errorf("failed to delete user %s: %w", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u UserService) BulkUpdate(ctx context.Context, ids []domain.UserIdentifier, updateFn func(*domain.User)) error {
|
||||
for _, id := range ids {
|
||||
user, err := u.users.GetUser(ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get user %s: %w", id, err)
|
||||
}
|
||||
|
||||
updateFn(user)
|
||||
|
||||
if _, err := u.users.UpdateUser(ctx, user); err != nil {
|
||||
return fmt.Errorf("failed to update user %s: %w", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user