forked from asm-products/firesize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.go
More file actions
114 lines (97 loc) · 2.7 KB
/
account.go
File metadata and controls
114 lines (97 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package models
import (
"fmt"
"math/rand"
"os"
"regexp"
"time"
"code.google.com/p/go.crypto/bcrypt"
"github.com/dgrijalva/jwt-go"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
type Account struct {
Id int64 `db:"id" json:"id"`
CreatedAt time.Time `db:"created_at" json:"created"`
UpdatedAt time.Time `db:"updated_at" json:"updated"`
Email string `db:"email" json:"email"`
EncryptedPassword []byte `db:"encrypted_password" json:"-"`
Plan string `db:"plan" json:"plan"`
Subdomain string `db:"subdomain" json:"subdomain"`
}
func FindAccountById(id int64) *Account {
accounts, err := Dbm.Select(Account{}, `select * from accounts where id = $1 limit 1`, id)
if err != nil {
panic(err)
}
if len(accounts) == 0 {
return nil
}
return accounts[0].(*Account)
}
func FindAccountByEmail(email string) *Account {
accounts, err := Dbm.Select(Account{}, `select * from accounts where lower(email) = lower($1) limit 1`, email)
if err != nil {
panic(err)
}
if len(accounts) == 0 {
return nil
}
return accounts[0].(*Account)
}
func FindAccountBySubdomain(subdomain string) *Account {
accounts, err := Dbm.Select(Account{}, `select * from accounts where subdomain = $1 limit 1`, subdomain)
if err != nil {
panic(err)
}
if len(accounts) == 0 {
return nil
}
return accounts[0].(*Account)
}
func (a *Account) String() string {
return fmt.Sprintf("Account(%a)", a.Email)
}
func (a *Account) GenEncryptedPassword(password string) error {
var err error
a.EncryptedPassword, err = bcrypt.GenerateFromPassword(
[]byte(password), bcrypt.DefaultCost)
return err
}
func (a *Account) GenJwt() (string, error) {
token := jwt.New(jwt.GetSigningMethod("HS256"))
token.Claims["account_id"] = a.Id
token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
return token.SignedString([]byte(os.Getenv("SECRET")))
}
func (a *Account) GenSubdomain() {
if a.Subdomain != "" {
return
}
randomLetters := make([]rune, 12)
for i := range randomLetters {
randomLetters[i] = letters[rand.Intn(len(letters))]
}
a.Subdomain = string(randomLetters)
}
func (a *Account) FiresizeUrl() string {
return fmt.Sprintf("https://%s.firesize.com", a.Subdomain)
}
var emailRegex = regexp.MustCompile("^.+@.+$")
// func (a *Account) Validate(v *revel.Validation) {
// v.Check(a.Email,
// revel.Required{},
// revel.MinSize{4},
// revel.Match{emailRegex},
// )
//
// ValidatePassword(v, a.Password).
// Key("account.Password")
// }
//
// func ValidatePassword(v *revel.Validation, password string) *revel.ValidationResult {
// return v.Check(password,
// revel.Required{},
// revel.MaxSize{15},
// revel.MinSize{5},
// )
// }