-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-creation.go
69 lines (64 loc) · 1.75 KB
/
user-creation.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
package controllers
import (
"fmt"
"time"
"regexp"
"golang.org/x/crypto/bcrypt"
"github.com/ECHibiki/Community-Banners-2.0/bannerdb"
)
func init(){
fmt.Print("")
}
func validateIPCreation(ip string) bool{
cooldown := time.Now().Unix() - controller_settings.AccountInterval * 60
rows , err := bannerdb.Query(`
SELECT * FROM antispam WHERE ip = ? AND type="create" AND unix < ?
` , []interface{}{ ip , cooldown} )
if err != nil{
panic (err)
}
return len(rows) == 0
}
func updateIPCreation(ip string){
time_now := time.Now().Unix()
cooldown := time_now - controller_settings.AccountInterval * 60
_ , del_err := bannerdb.Query(`
DELETE FROM antispam WHERE unix < ? AND type="create"
` , []interface{}{ cooldown } )
if del_err != nil{
panic (del_err)
}
_ , ins_err := bannerdb.Query(`
INSERT INTO antispam VALUES ( ? , ? , ? )
` , []interface{}{ ip , time_now , "create"} )
if ins_err != nil{
panic (ins_err)
}
}
func addNewUserToDB(name string, pass string) string{
invalid_reg := regexp.MustCompile("/[^a-zA-Z0-9_\\-]/")
if invalid_reg.MatchString(name) {
return "Name has invalid characters(alphanumeric names only)"
}
hashed_bytes , berr := bcrypt.GenerateFromPassword([]byte(pass), 10)
if berr != nil{
panic (berr)
}
rows, err := bannerdb.Query(`
SELECT name FROM users WHERE name = ?
`, []interface{}{ name })
if err != nil{
panic (err)
}
if len(rows) == 0{
_, ins_err := bannerdb.Query(`
INSERT INTO users VALUES (NULL , ? , ? )
`, []interface{}{ name , string(hashed_bytes) })
if ins_err != nil{
panic (ins_err)
}
return ""
} else{
return "Username Already Exists"
}
}