diff --git a/.env.example b/.env.example index b8e1c1f..4c29ad7 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,4 @@ APP_LISTEN_PORT=3000 APP_BEHIND_PROXY=false DATABASE_DSN="host=localhost user=camarades password=camarades dbname=camarades port=5432 sslmode=disable TimeZone=Europe/Zurich" SESSIONS_LOCATION=./sessions.db +AUTHELIA_USERS_LOCATION=./users.yml \ No newline at end of file diff --git a/.gitignore b/.gitignore index b6c620e..971cd36 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ static/assets pop-camarades *.db __debug_bin* +users.yml \ No newline at end of file diff --git a/controllers/contacts.go b/controllers/contacts.go index 40fbfad..d85afc6 100644 --- a/controllers/contacts.go +++ b/controllers/contacts.go @@ -495,6 +495,18 @@ func ContactAdd(c *fiber.Ctx) error { person.PostalCode = data.PostalCode person.City = data.City + if len(data.Email) > 0 { + var personEmail []models.Person + result := db.Find(&personEmail, "LOWER(email) = LOWER(?)", data.Email) + if result.Error != nil { + return result.Error + } + + if result.RowsAffected > 0 { + errors = append(errors, "L'adresse email est déjà utilisée par un membre ou un sympathisant") + } + } + sectionID, err := strconv.ParseUint(data.Section, 10, 0) if err == nil { for _, section := range sections { @@ -744,6 +756,18 @@ func ContactEdit(c *fiber.Ctx) error { person.PostalCode = data.PostalCode person.City = data.City + if len(data.Email) > 0 { + var personEmail []models.Person + result := db.Find(&personEmail, "LOWER(email) = LOWER(?)", data.Email) + if result.Error != nil { + return result.Error + } + + if result.RowsAffected > 0 { + errors = append(errors, "L'adresse email est déjà utilisée par un membre ou un sympathisant") + } + } + sectionID, err := strconv.ParseUint(data.Section, 10, 0) if err == nil { for _, section := range sections { diff --git a/controllers/members.go b/controllers/members.go index e1c0405..8e0f0c1 100644 --- a/controllers/members.go +++ b/controllers/members.go @@ -18,7 +18,7 @@ import ( type PersonValidation struct { LastName string `validate:"max=100"` FirstName string `validate:"required,min=1,max=100"` - Email string `validate:"max=100"` + Email string `validate:"max=100,email"` Phone string `validate:"max=100"` Mobile string `validate:"max=100"` Address1 string `validate:"max=100"` @@ -498,6 +498,18 @@ func MemberAdd(c *fiber.Ctx) error { } } + if len(data.Email) > 0 { + var personEmail []models.Person + result := db.Find(&personEmail, "LOWER(email) = LOWER(?)", data.Email) + if result.Error != nil { + return result.Error + } + + if result.RowsAffected > 0 { + errors = append(errors, "L'adresse email est déjà utilisée par un membre ou un sympathisant") + } + } + person.IsContact = false person.IsMember = true person.LastName = data.LastName @@ -747,6 +759,18 @@ func MemberEdit(c *fiber.Ctx) error { } } + if len(data.Email) > 0 { + var personEmail []models.Person + result := db.Find(&personEmail, "LOWER(email) = LOWER(?)", data.Email) + if result.Error != nil { + return result.Error + } + + if result.RowsAffected > 0 { + errors = append(errors, "L'adresse email est déjà utilisée par un membre ou un sympathisant") + } + } + person.IsContact = false person.IsMember = true person.LastName = data.LastName diff --git a/helpers/authelia/users.go b/helpers/authelia/users.go new file mode 100644 index 0000000..62bbf24 --- /dev/null +++ b/helpers/authelia/users.go @@ -0,0 +1 @@ +package authelia diff --git a/helpers/config.go b/helpers/config.go index e87443d..e6c0791 100644 --- a/helpers/config.go +++ b/helpers/config.go @@ -20,6 +20,9 @@ type Config struct { Sessions struct { Location string `env:"SESSIONS_LOCATION"` } + Authelia struct { + UsersLocation string `env:"AUTHELIA_USERS_LOCATION"` + } } var configParsed bool diff --git a/helpers/database.go b/helpers/database.go index 00ed2f8..73aacdf 100644 --- a/helpers/database.go +++ b/helpers/database.go @@ -52,6 +52,7 @@ func GetDatabase() (*gorm.DB, error) { &models.Role{}, &models.UserRole{}, &models.Person{}, + &models.PersonAccount{}, &models.List{}, &models.ListItem{}, &models.Field{}, diff --git a/models/people.go b/models/people.go index 8cab6a0..6086734 100644 --- a/models/people.go +++ b/models/people.go @@ -1,6 +1,11 @@ package models -import "gorm.io/gorm" +import ( + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) type Person struct { gorm.Model @@ -8,7 +13,7 @@ type Person struct { IsContact bool FirstName string LastName string - Email string + Email string `gorm:"unique"` Phone string Mobile string Address1 string @@ -18,3 +23,21 @@ type Person struct { SectionID uint Section Section } + +type PersonAccount struct { + gorm.Model + PersonID uint + Person Person + UUID uuid.UUID `gorm:"unique"` + Enabled bool + Email string `gorm:"unique"` + GivenName string + FamilyName string + DisplayName string + Groups string + InitialPassword string + InvitationSent time.Time + AccountReady bool + AccountCreated bool + UpdateNeeded bool +} diff --git a/models/users.go b/models/users.go index 367c020..8eb330d 100644 --- a/models/users.go +++ b/models/users.go @@ -10,7 +10,7 @@ import ( type User struct { gorm.Model Name string - Email string + Email string `gorm:"unique"` Password string TotpSecret sql.NullString IsAdmin bool