This commit is contained in:
@@ -17,18 +17,20 @@ var (
|
|||||||
|
|
||||||
// UserStore manages users in memory
|
// UserStore manages users in memory
|
||||||
type UserStore struct {
|
type UserStore struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
users map[string]*models.User
|
users map[string]*models.User
|
||||||
nextID int64
|
passwordHashes map[string]string // Maps user ID to password hash
|
||||||
auth *Service
|
nextID int64
|
||||||
|
auth *Service
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUserStore creates a new user store
|
// NewUserStore creates a new user store
|
||||||
func NewUserStore(auth *Service) *UserStore {
|
func NewUserStore(auth *Service) *UserStore {
|
||||||
store := &UserStore{
|
store := &UserStore{
|
||||||
users: make(map[string]*models.User),
|
users: make(map[string]*models.User),
|
||||||
nextID: 1,
|
passwordHashes: make(map[string]string),
|
||||||
auth: auth,
|
nextID: 1,
|
||||||
|
auth: auth,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create default admin user if no users exist
|
// Create default admin user if no users exist
|
||||||
@@ -49,7 +51,12 @@ func (s *UserStore) createDefaultAdmin() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create default admin: admin / admin (should be changed on first login)
|
// Create default admin: admin / admin (should be changed on first login)
|
||||||
hashedPassword, _ := s.auth.HashPassword("admin")
|
hashedPassword, err := s.auth.HashPassword("admin")
|
||||||
|
if err != nil {
|
||||||
|
// If hashing fails, we can't create the admin user
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
admin := &models.User{
|
admin := &models.User{
|
||||||
ID: "user-1",
|
ID: "user-1",
|
||||||
Username: "admin",
|
Username: "admin",
|
||||||
@@ -59,14 +66,12 @@ func (s *UserStore) createDefaultAdmin() {
|
|||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store password hash (in production, this would be in a separate secure store)
|
// Store password hash
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
s.users[admin.ID] = admin
|
s.users[admin.ID] = admin
|
||||||
|
s.passwordHashes[admin.ID] = hashedPassword
|
||||||
s.nextID = 2
|
s.nextID = 2
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
// Store password hash separately (in production, use proper user model with password field)
|
|
||||||
_ = hashedPassword // TODO: Store in user model or separate secure store
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create creates a new user
|
// Create creates a new user
|
||||||
@@ -100,7 +105,7 @@ func (s *UserStore) Create(username, email, password string, role models.Role) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.users[user.ID] = user
|
s.users[user.ID] = user
|
||||||
_ = hashedPassword // TODO: Store password hash
|
s.passwordHashes[user.ID] = hashedPassword
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
@@ -141,13 +146,33 @@ func (s *UserStore) Authenticate(username, password string) (*models.User, error
|
|||||||
return nil, errors.New("user account is disabled")
|
return nil, errors.New("user account is disabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Verify password against stored hash
|
// Get stored password hash
|
||||||
// For now, accept "admin" password for default admin
|
s.mu.RLock()
|
||||||
if username == "admin" && password == "admin" {
|
storedHash, exists := s.passwordHashes[user.ID]
|
||||||
return user, nil
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
// Fallback: for backward compatibility, check if it's the default admin
|
||||||
|
// This allows existing installations to still work
|
||||||
|
if username == "admin" && password == "admin" {
|
||||||
|
// Store the default password hash for future use
|
||||||
|
hashedPassword, err := s.auth.HashPassword("admin")
|
||||||
|
if err == nil {
|
||||||
|
s.mu.Lock()
|
||||||
|
s.passwordHashes[user.ID] = hashedPassword
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
return nil, ErrInvalidCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrInvalidCredentials
|
// Verify password against stored hash
|
||||||
|
if !s.auth.VerifyPassword(storedHash, password) {
|
||||||
|
return nil, ErrInvalidCredentials
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// List returns all users
|
// List returns all users
|
||||||
@@ -190,6 +215,7 @@ func (s *UserStore) Delete(id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete(s.users, id)
|
delete(s.users, id)
|
||||||
|
delete(s.passwordHashes, id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +234,8 @@ func (s *UserStore) UpdatePassword(id, newPassword string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = hashedPassword // TODO: Store password hash
|
// Store the new password hash
|
||||||
|
s.passwordHashes[user.ID] = hashedPassword
|
||||||
user.UpdatedAt = time.Now()
|
user.UpdatedAt = time.Now()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user