Compare commits

..

5 Commits

Author SHA1 Message Date
Alex
8137f121ed backend: moved setpassword to user model 2025-02-27 12:13:50 +01:00
Alex
82558edd5a removed comment 2025-02-27 12:13:27 +01:00
Alex
421b4753e5 backend fix mail recipients 2025-02-27 12:13:12 +01:00
Alex
e0717ec09a backend: status adjustments 2025-02-27 12:12:42 +01:00
Alex
d355c6906e tests 2025-02-27 12:12:12 +01:00
7 changed files with 28 additions and 30 deletions

View File

@@ -14,10 +14,10 @@ import (
"log"
"github.com/alexedwards/argon2id"
"github.com/gin-gonic/gin"
"GoMembership/internal/config"
"GoMembership/internal/constants"
"GoMembership/internal/database"
"GoMembership/internal/models"
"GoMembership/internal/repositories"
@@ -117,21 +117,20 @@ func TestSuite(t *testing.T) {
if err := initLicenceCategories(); err != nil {
log.Fatalf("Failed to init Categories: %v", err)
}
hash, err := argon2id.CreateHash("securepassword", argon2id.DefaultParams)
admin := models.User{
FirstName: "Ad",
LastName: "min",
Email: "admin@example.com",
Password: hash,
DateOfBirth: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
Company: "SampleCorp",
Phone: "+123456789",
Address: "123 Main Street",
ZipCode: "12345",
City: "SampleCity",
Status: 1,
Status: constants.ActiveStatus,
RoleID: 8,
}
admin.SetPassword("securepassword")
database.DB.Create(&admin)
validation.SetupValidators()
t.Run("userController", func(t *testing.T) {

View File

@@ -231,7 +231,7 @@ func (uc *UserController) LoginHandler(c *gin.Context) {
return
}
if !ok {
utils.RespondWithError(c, fmt.Errorf("%v %v", user.FirstName, user.LastName),
utils.RespondWithError(c, fmt.Errorf("%v %v(%v)", user.FirstName, user.LastName, user.Email),
"Login Error; wrong password",
http.StatusNotAcceptable,
errors.Responses.Fields.Login,
@@ -353,9 +353,10 @@ func (uc *UserController) VerifyMailHandler(c *gin.Context) {
return
}
user.Status = constants.ActiveStatus
user.Status = constants.VerifiedStatus
user.Verification = *verification
user.ID = verification.UserID
user.Password = ""
uc.Service.UpdateUser(user)
logger.Info.Printf("Verified User: %#v", user.Email)

View File

@@ -20,6 +20,7 @@ import (
"GoMembership/internal/config"
"GoMembership/internal/constants"
"GoMembership/internal/database"
"GoMembership/internal/middlewares"
"GoMembership/internal/models"
"GoMembership/internal/repositories"
@@ -72,7 +73,8 @@ func testUserController(t *testing.T) {
}
})
}
// activate user for login
database.DB.Model(&models.User{}).Where("email = ?", "john.doe@example.com").Update("status", constants.ActiveStatus)
loginEmail, loginCookie := testLoginHandler(t)
logoutCookie := testCurrentUserHandler(t, loginEmail, loginCookie)

View File

@@ -1,6 +1,7 @@
package models
import (
"GoMembership/pkg/logger"
"fmt"
"time"
@@ -52,13 +53,22 @@ func (u *User) GenerateMandateReference() string {
return fmt.Sprintf("%s%d%s", time.Now().Format("20060102"), u.ID, u.BankAccount.IBAN)
}
func (u *User) PasswordMatches(plaintextPassword string) (bool, error) {
match, err := argon2id.ComparePasswordAndHash(plaintextPassword, u.Password)
if err != nil {
return false, err
func (u *User) SetPassword(plaintextPassword string) error {
if plaintextPassword == "" {
return nil
}
return match, nil
hash, err := argon2id.CreateHash(plaintextPassword, argon2id.DefaultParams)
if err != nil {
return err
}
u.Password = hash
return nil
}
func (u *User) PasswordMatches(plaintextPassword string) (bool, error) {
logger.Error.Printf("plaintext: %v user password: %v", plaintextPassword, u.Password)
return argon2id.ComparePasswordAndHash(plaintextPassword, u.Password)
}
func (u *User) Safe() map[string]interface{} {

View File

@@ -3,7 +3,6 @@ package services
import (
"bytes"
"html/template"
"os"
"gopkg.in/gomail.v2"
@@ -35,7 +34,7 @@ func (s *EmailService) SendEmail(to string, subject string, body string, bodyTXT
}
msg.AddAlternative("text/html", body)
msg.WriteTo(os.Stdout)
// msg.WriteTo(os.Stdout)
if err := s.dialer.DialAndSend(msg); err != nil {
logger.Error.Printf("Could not send email to %s: %v", to, err)
@@ -209,7 +208,7 @@ func (s *EmailService) SendRegistrationNotification(user *models.User) error {
logger.Error.Print("Couldn't parse password mail")
return err
}
return s.SendEmail(user.Email, subject, htmlBody, plainBody, "")
return s.SendEmail(config.Recipients.UserRegistration, subject, htmlBody, plainBody, "")
}
func (s *EmailService) RelayContactFormMessage(sender string, name string, message string) error {

View File

@@ -8,7 +8,6 @@ import (
"GoMembership/internal/repositories"
"GoMembership/pkg/errors"
"github.com/alexedwards/argon2id"
"gorm.io/gorm"
"time"
@@ -55,9 +54,7 @@ func (service *UserService) UpdateUser(user *models.User) (*models.User, error)
return nil, errors.ErrUserNotFound
}
if user.Password != "" {
setPassword(user.Password, user)
}
user.SetPassword(user.Password)
// Validate subscription model
selectedModel, err := repositories.GetSubscriptionByName(&user.Membership.SubscriptionModel.Name)
@@ -84,7 +81,7 @@ func (service *UserService) UpdateUser(user *models.User) (*models.User, error)
func (service *UserService) RegisterUser(user *models.User) (id uint, token string, err error) {
setPassword(user.Password, user)
user.SetPassword(user.Password)
user.Status = constants.UnverifiedStatus
user.CreatedAt = time.Now()
@@ -117,12 +114,3 @@ func (service *UserService) GetUsers(where map[string]interface{}) (*[]models.Us
}
return service.Repo.GetUsers(where)
}
func setPassword(plaintextPassword string, u *models.User) error {
hash, err := argon2id.CreateHash(plaintextPassword, argon2id.DefaultParams)
if err != nil {
return err
}
u.Password = hash
return nil
}

View File

@@ -55,6 +55,5 @@ func (service *UserService) VerifyUser(token *string, verificationType *string)
t := time.Now()
verification.VerifiedAt = &t
// Update user status to active
return verification, nil
}