-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserDB.go
127 lines (105 loc) · 3.12 KB
/
userDB.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
package main
import (
"errors"
"log"
"golang.org/x/crypto/bcrypt"
)
//Create Table
func createTable() error {
// Roll Number Should Be unique.
createStatement, err := db.Prepare("CREATE TABLE IF NOT EXISTS students ( Rollno INTEGER PRIMARY KEY NOT NULL,Name TEXT NOT NULL,Coins INTEGER NOT NULL,Password TEXT NOT NULL, Role string TEXT NOT NULL, Activity INTEGER NOT NULL)")
if err != nil {
return err
}
awardStatement, err := db.Prepare("CREATE TABLE IF NOT EXISTS awards ( Time TIMESTAMP, AwardeeRollno INTEGER NOT NULL,Amount INTEGER NOT NULL)")
if err != nil {
return err
}
transferStatement, err := db.Prepare("CREATE TABLE IF NOT EXISTS transfers ( Time TIMESTAMP, SenderRollno INTEGER NOT NULL,RecieverRollno INTEGER NOT NULL,Amount INTEGER NOT NULL)")
if err != nil {
return err
}
// Create Tables
createStatement.Exec()
awardStatement.Exec()
transferStatement.Exec()
log.Println("tables ready.")
return nil
}
// Add New Users
func addUser(user *User) error {
// Add New User
addStatement, err := db.Prepare("INSERT INTO students ( Rollno , Name , Coins,Password, Role, Activity) VALUES(?,?,?,?,?,?)")
if err != nil {
log.Println("error preparing Statement")
return err
}
if user.Password == "" || user.Name == "" {
err = errors.New("empty Name/Password not allowed")
log.Println(err)
return err
}
// TODO: Storing a complete string in place of a bit
// TODO: is weird. You can do better than this.
user.Coin = 0 // starting point
user.Role = "STUDENT"
user.Activity = 0 // staring point
// Find role : assign the most powerful role
// student --> council core ---> admin
for _, rollno := range COUNCIL_CORE {
if rollno == user.Rollno {
user.Role = "COUNCIL_CORE"
break
}
}
for _, rollno := range ADMIN {
if rollno == user.Rollno {
user.Role = "ADMIN"
break
}
}
// Valid input
log.Println("add New User....")
// ? Why 14
bytes, err := bcrypt.GenerateFromPassword([]byte(user.Password), 14)
if err != nil {
log.Println("error while hashing the Password")
return err
}
_, err = addStatement.Exec(user.Rollno, user.Name, user.Coin, string(bytes),user.Role,user.Activity)
// Unique Constraint on Rollno
if err != nil {
log.Println("unable to Add user")
return err
} else {
log.Println("succesfully Added New User.")
}
return nil
}
func validateLogin(user *User, hasCookie bool) error {
var err error
var userPass string
getUserStatement, err := db.Prepare("SELECT * FROM students WHERE Rollno=?")
if err != nil {
log.Println("error preparing db Statement")
return err
}
defer getUserStatement.Close()
err = getUserStatement.QueryRow(user.Rollno).Scan(&user.Rollno, &user.Name, &user.Coin, &userPass, &user.Role, &user.Activity)
// If no such row exists(Both Rollno and Password should match) Scan will throw an error.
if err != nil {
log.Println("error while getting user Information.")
return err
}
// Validate login if cookie is available
// ! Do not access password field though.
if hasCookie {
return nil
}
err = bcrypt.CompareHashAndPassword([]byte(userPass), []byte(user.Password))
if err != nil {
return err
}
// Valid User
return nil
}