1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package orm
- import (
- "crypto/sha1"
- "encoding/base64"
- "fmt"
- "strings"
- "github.com/sethvargo/go-password/password"
- )
- type Checkbox bool
- type Credential struct {
- Name string
- Surname string
- Password string
- PlainPassword string
- Email string
- AltEmail string
- TelephoneNumber string
- Regenerate bool `schema:"Regenerate" sql:"default: false"`
- }
- 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)
- }
|