12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package orm
- import (
- "crypto/sha1"
- "encoding/base64"
- "fmt"
- "strings"
- "time"
- "github.com/sethvargo/go-password/password"
- )
- type Checkbox bool
- type Credential struct {
- Name string `csv:"firstname"`
- Surname string `csv:"lastname"`
- Password string
- PlainPassword string
- Email string `csv:"email"`
- AltEmail string
- DateFrom time.Time
- DateTo time.Time
- TelephoneNumber string
- Regenerate bool `schema:"Regenerate" sql:"default: false"`
- Exclude bool
- }
- func (c *Credential) sanitize(s string) string {
- lower := strings.ToLower(s)
- r := strings.NewReplacer("'", "", "-", "", " ", "", "ò", "o", "ì", "i")
- return r.Replace(lower)
- }
- func (c *Credential) Username() string {
- return strings.Join([]string{c.sanitize(c.Name), c.sanitize(c.Surname)}, ".")
- }
- func (c *Credential) SaltPassword(password string) string {
- bs := sha1.Sum([]byte(password + "salt"))
- str := base64.StdEncoding.EncodeToString(append(bs[:], []byte("salt")...))
- return fmt.Sprintf("%s", str)
- }
- func (c *Credential) GeneratePassword() (string, error) {
- password, err := password.Generate(8, 2, 0, false, true)
- if err != nil {
- return "", err
- }
- return password, nil
- }
- func (c *Credential) CompleteName() string {
- return fmt.Sprintf("%s, %s", c.Name, c.Surname)
- }
- func (c *Credential) MailAddress(domain string) string {
- return fmt.Sprintf("%s@%s", c.Username(), domain)
- }
|