-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathuser.go
66 lines (51 loc) · 1.39 KB
/
user.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
package elasticthought
import (
"errors"
"fmt"
"github.com/tleyden/go-couch"
)
// An ElasticThought user.
type User struct {
ElasticThoughtDoc
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
// Create a new User
func NewUser() *User {
return &User{
ElasticThoughtDoc: ElasticThoughtDoc{Type: DOC_TYPE_USER},
}
}
// Create a new User based on values in another user
func NewUserFromUser(other User) *User {
user := NewUser()
user.Username = other.Username
user.Email = other.Email
user.Password = other.Password
return user
}
// Does this username/password combo exist in the database? If so, return the
// user. If not, return an error.
func AuthenticateUser(db couch.Database, username, password string) (*User, error) {
userId := docIdFromUsername(username)
authenticatedUser := NewUser()
err := db.Retrieve(userId, authenticatedUser)
if err != nil {
// no valid user
return nil, err
}
// we found a valid user, but do passwords match?
if authenticatedUser.Password != password {
err := errors.New("Passwords do not match")
return nil, err
}
return authenticatedUser, err
}
// The doc id of this user. If the username is "foo", the doc id will be "user:foo"
func (u User) DocId() string {
return docIdFromUsername(u.Username)
}
func docIdFromUsername(username string) string {
return fmt.Sprintf("user:%v", username)
}