This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodels.go
259 lines (219 loc) · 5.88 KB
/
models.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package photoshare
import (
"bytes"
"code.google.com/p/go.crypto/bcrypt"
"crypto/rand"
"database/sql"
"github.com/coopernurse/gorp"
"math"
"net/http"
"time"
)
const (
pageSize = 20
recoveryCodeLength = 30
recoveryCodeCharacters = "abcdefghijklmnopqrstuvwxyz0123456789"
)
type photoList struct {
Items []photo `json:"photos"`
Total int64 `json:"total"`
CurrentPage int64 `json:"currentPage"`
NumPages int64 `json:"numPages"`
}
func newPhotoList(photos []photo, total int64, page int64) *photoList {
numPages := int64(math.Ceil(float64(total) / float64(pageSize)))
return &photoList{
Items: photos,
Total: total,
CurrentPage: page,
NumPages: numPages,
}
}
type tag struct {
ID int64 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
}
type tagCount struct {
Name string `db:"name" json:"name"`
Photo string `db:"photo" json:"photo"`
NumPhotos int64 `db:"num_photos" json:"numPhotos"`
}
type photo struct {
ID int64 `db:"id" json:"id"`
OwnerID int64 `db:"owner_id" json:"ownerId"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
Title string `db:"title" json:"title"`
Filename string `db:"photo" json:"photo"`
Tags []string `db:"-" json:"tags,omitempty"`
UpVotes int64 `db:"up_votes" json:"upVotes"`
DownVotes int64 `db:"down_votes" json:"downVotes"`
}
func (photo *photo) PreInsert(s gorp.SqlExecutor) error {
photo.CreatedAt = time.Now()
return nil
}
func (photo *photo) validate(ctx *context, r *http.Request, errors map[string]string) error {
if photo.OwnerID == 0 {
errors["ownerID"] = "Owner ID is missing"
}
if photo.Title == "" {
errors["title"] = "Title is missing"
}
if len(photo.Title) > 200 {
errors["title"] = "Title is too long"
}
if photo.Filename == "" {
errors["photo"] = "Photo filename not set"
}
return nil
}
func (photo *photo) canEdit(user *user) bool {
if user == nil || !user.IsAuthenticated {
return false
}
return user.IsAdmin || photo.OwnerID == user.ID
}
func (photo *photo) canDelete(user *user) bool {
return photo.canEdit(user)
}
func (photo *photo) canVote(user *user) bool {
if user == nil || !user.IsAuthenticated {
return false
}
if photo.OwnerID == user.ID {
return false
}
return !user.hasVoted(photo.ID)
}
type permissions struct {
Edit bool `json:"edit"`
Delete bool `json:"delete"`
Vote bool `json:"vote"`
}
type photoDetail struct {
photo `db:"-"`
OwnerName string `db:"owner_name" json:"ownerName"`
Permissions *permissions `db:"-" json:"perms"`
}
// User represents users in database
type user struct {
ID int64 `db:"id" json:"id"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
Name string `db:"name" json:"name"`
Password string `db:"password" json:""`
Email string `db:"email" json:"email"`
Votes string `db:"votes" json:""`
IsAdmin bool `db:"admin" json:"isAdmin"`
IsActive bool `db:"active" json:"isActive"`
RecoveryCode sql.NullString `db:"recovery_code" json:""`
IsAuthenticated bool `db:"-" json:"isAuthenticated"`
}
// PreInsert hook
func (user *user) PreInsert(s gorp.SqlExecutor) error {
user.IsActive = true
user.CreatedAt = time.Now()
user.Votes = "{}"
user.encryptPassword()
return nil
}
func (user *user) validate(ctx *context, r *http.Request, errors map[string]string) error {
if user.Name == "" {
errors["name"] = "Name is missing"
} else {
ok, err := ctx.datamapper.isUserNameAvailable(user)
if err != nil {
return err
}
if !ok {
errors["name"] = "Name already taken"
}
}
if user.Email == "" {
errors["email"] = "Email is missing"
} else if !validateEmail(user.Email) {
errors["email"] = "Invalid email address"
} else {
ok, err := ctx.datamapper.isUserEmailAvailable(user)
if err != nil {
return err
}
if !ok {
errors["email"] = "Email already taken"
}
}
// tbd: we need flag user is third-party
if user.Password == "" {
errors["password"] = "Password is missing"
}
return nil
}
func (user *user) generateRecoveryCode() (string, error) {
buf := bytes.Buffer{}
randbytes := make([]byte, recoveryCodeLength)
if _, err := rand.Read(randbytes); err != nil {
return "", err
}
numChars := len(recoveryCodeCharacters)
for i := 0; i < recoveryCodeLength; i++ {
index := int(randbytes[i]) % numChars
char := recoveryCodeCharacters[index]
buf.WriteString(string(char))
}
code := buf.String()
user.RecoveryCode = sql.NullString{String: code, Valid: true}
return code, nil
}
func (user *user) resetRecoveryCode() {
user.RecoveryCode = sql.NullString{String: "", Valid: false}
}
func (user *user) changePassword(password string) error {
user.Password = password
return user.encryptPassword()
}
func (user *user) encryptPassword() error {
if user.Password == "" {
return nil
}
hashed, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
user.Password = string(hashed)
return nil
}
func (user *user) checkPassword(password string) bool {
if user.Password == "" {
return false
}
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
return err == nil
}
func (user *user) registerVote(photoID int64) {
user.setVotes(append(user.getVotes(), photoID))
}
func (user *user) hasVoted(photoID int64) bool {
for _, value := range user.getVotes() {
if value == photoID {
return true
}
}
return false
}
func (user *user) getVotes() []int64 {
return pgArrToIntSlice(user.Votes)
}
func (user *user) setVotes(votes []int64) {
user.Votes = intSliceToPgArr(votes)
}
type page struct {
index int64
offset int64
size int64
}
func newPage(index int64) *page {
offset := (index - 1) * pageSize
if offset < 0 {
offset = 0
}
return &page{index, offset, pageSize}
}