@@ -2,19 +2,21 @@ package auth
22
33import (
44 "fmt"
5- "strings "
5+ "slices "
66
77 "github.com/cpp-cyber/proclone/internal/ldap"
8+ "github.com/cpp-cyber/proclone/internal/proxmox"
89)
910
10- func NewAuthService () (* AuthService , error ) {
11+ func NewAuthService (proxmoxService * proxmox. ProxmoxService ) (* AuthService , error ) {
1112 ldapService , err := ldap .NewLDAPService ()
1213 if err != nil {
1314 return nil , fmt .Errorf ("failed to create LDAP service: %w" , err )
1415 }
1516
1617 return & AuthService {
17- ldapService : ldapService ,
18+ ldapService : ldapService ,
19+ proxmoxService : proxmoxService ,
1820 }, nil
1921}
2022
@@ -51,38 +53,18 @@ func (s *AuthService) Authenticate(username string, password string) (bool, erro
5153}
5254
5355func (s * AuthService ) IsAdmin (username string ) (bool , error ) {
54- // Input validation
55- if username == "" {
56- return false , fmt .Errorf ("username cannot be empty" )
57- }
58-
59- // Get user DN
60- userDN , err := s .ldapService .GetUserDN (username )
61- if err != nil {
62- return false , fmt .Errorf ("failed to get user DN: %w" , err )
63- }
64-
65- // Get user's groups
66- userGroups , err := s .ldapService .GetUserGroups (userDN )
56+ // Get user's groups from Proxmox
57+ userGroups , err := s .proxmoxService .GetUserGroups (username )
6758 if err != nil {
6859 return false , fmt .Errorf ("failed to get user groups: %w" , err )
6960 }
7061
71- // Load LDAP config to get admin group DN
72- config , err := ldap .LoadConfig ()
73- if err != nil {
74- return false , fmt .Errorf ("failed to load LDAP config: %w" , err )
75- }
76-
77- if config .AdminGroupDN == "" {
78- return false , fmt .Errorf ("admin group DN not configured" )
79- }
62+ // Get the admin group name from config
63+ adminGroupName := s .proxmoxService .Config .AdminGroupName
8064
8165 // Check if user is in the admin group
82- for _ , groupDN := range userGroups {
83- if strings .EqualFold (groupDN , "Proxmox-Admins" ) {
84- return true , nil
85- }
66+ if slices .Contains (userGroups , adminGroupName ) {
67+ return true , nil
8668 }
8769
8870 return false , nil
0 commit comments