This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
db.go
105 lines (91 loc) · 2.54 KB
/
db.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
package db
import (
"fmt"
"github.com/wtfd-tech/wtfd/internal/types"
"golang.org/x/crypto/bcrypt"
"time"
)
var (
challs *types.Challenges
)
type DB interface {
NewUser(u User) error
GetSolvesWithTime(u string) []_ORMChallengesByUser
Get(username string) (User, error)
Contains(username, displayname string) bool
Login(username, password string) error
GetUserCount() int64
GetSolveCount(chall types.Challenge) int64
UpdateUser(u User) error
DeleteUser(u User) error
SolvedChallenge(u User, c *types.Challenge) error
UserExists(u User) (bool, error)
DisplayNameExists(n string) (bool, error)
ChallengesSolved(u User) ([]*types.Challenge, error)
LoadUser(username string) (User, error)
UserByToken(token string) (User, error)
AllUsersSortedByPoints() ([]User, error)
}
// VerifyInfo saves if a users e-mail is verified
type VerifyInfo struct {
IsVerified bool
VerifyToken string
VerifyDeadline time.Time
}
// User was ist das wohl
type User struct {
Name string `json:"name"`
Hash []byte
DisplayName string `json:"displayname"`
Completed []*types.Challenge
Admin bool `json:"admin"`
Points int `json:"points"`
VerifiedInfo VerifyInfo
Created time.Time
}
// StartDB starts the db and returns a DB interface
func StartDB(c *types.Challenges) (DB, error) {
challs = c
x := xormdb{}
if err := x.Start(); err != nil {
return xormdb{}, err
}
return x, nil
}
// HasSolvedChallenge returns true if u has solved chall
func (u User) HasSolvedChallenge(chall *types.Challenge) bool {
for _, c := range u.Completed {
if c.Name == chall.Name {
return true
}
}
return false
}
// CalculatePoints calculates Points and updates user.Points
func (u *User) CalculatePoints() {
points := 0
for _, c := range u.Completed {
points += c.Points
}
u.Points = points
}
// ComparePassword checks if the password is valid
func (u *User) ComparePassword(password string) bool {
return bcrypt.CompareHashAndPassword(u.Hash, []byte(password)) == nil
}
// NewUserStruct creates a new user object
func NewUserStruct(d DB, name, password, displayname string) (User, error) {
if d.Contains(name, displayname) {
return User{}, errUserExisting
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return User{}, err
}
fmt.Printf("New User added: %s\n", name)
isAdmin := d.GetUserCount() == 0
if isAdmin {
fmt.Printf("New User %s is an Admin\n", name)
}
return User{Name: name, Hash: hash, DisplayName: displayname, Admin: isAdmin, VerifiedInfo: VerifyInfo{IsVerified: false}}, nil
}