74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package authelia
|
|
|
|
import (
|
|
"os"
|
|
|
|
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
|
"go.yaml.in/yaml/v4"
|
|
)
|
|
|
|
type AutheliaUser struct {
|
|
Password string `yaml:"password"`
|
|
DisplayName string `yaml:"displayname"`
|
|
Email string `yaml:"email"`
|
|
Groups []string `yaml:"groups"`
|
|
GivenName string `yaml:"given_name"`
|
|
MiddleName string `yaml:"middle_name"`
|
|
FamilyName string `yaml:"family_name"`
|
|
Nickname string `yaml:"nickname"`
|
|
Gender string `yaml:"gender"`
|
|
Birthdate string `yaml:"birthdate"`
|
|
Website string `yaml:"website"`
|
|
Profile string `yaml:"profile"`
|
|
Picture string `yaml:"picture"`
|
|
ZoneInfo string `yaml:"zoneinfo"`
|
|
Locale string `yaml:"locale"`
|
|
PhoneNumber string `yaml:"phone_number"`
|
|
PhoneExtension string `yaml:"phone_extension"`
|
|
Disabled bool `yaml:"disabled"`
|
|
Address any `yaml:"address"`
|
|
Extra map[string]any `yaml:"extra"`
|
|
}
|
|
|
|
type AutheliaUsers struct {
|
|
Users map[string]AutheliaUser `yaml:"users"`
|
|
}
|
|
|
|
func ReadDatabase() (AutheliaUsers, error) {
|
|
var users AutheliaUsers
|
|
|
|
// Get config
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return users, err
|
|
}
|
|
|
|
// Read YAML file
|
|
filePath := config.Authelia.UsersLocation
|
|
fileContent, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return users, err
|
|
}
|
|
|
|
// Unmarshal YAML
|
|
err = yaml.Unmarshal(fileContent, &users)
|
|
|
|
return users, err
|
|
}
|
|
|
|
func WriteDatabase(users AutheliaUsers) error {
|
|
// Get config
|
|
config, err := helpers.GetConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Marshal YAML
|
|
yamlData, err := yaml.Marshal(users)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
filePath := config.Authelia.UsersLocation
|
|
return os.WriteFile(filePath, yamlData, 0644)
|
|
}
|