/
default.go
149 lines (115 loc) · 3.16 KB
/
default.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
package controllers
import (
"github.com/astaxie/beego"
. "github.com/gtechx/base/common"
"github.com/gtechx/chatserver/db"
)
type MainController struct {
beego.Controller
}
// func (c *MainController) Prepare() {
// account := String(c.GetSession("account"))
// println("MainController Index account ", account)
// if account != "" {
// c.Redirect("/user/index", 303)
// return
// }
// }
func (c *MainController) Index() {
// c.Data["Website"] = "beego.me"
// c.Data["Email"] = "astaxie@gmail.com"
account := String(c.GetSession("account"))
if account != "" {
c.Redirect("/user/index", 302)
return
}
c.TplName = "index.tpl"
}
func (c *MainController) Register() {
if c.Ctx.Request.Method == "POST" {
account := c.GetString("account")
password := c.GetString("password")
c.Data["post"] = true
flag, err := gtdb.Manager().IsAccountExists(account)
if err != nil {
c.Data["error"] = "数据库错误:" + err.Error()
goto end
}
if flag || account == "admin" || account == "root" {
c.Data["error"] = "账号已经存在"
goto end
}
if password == "" {
c.Data["error"] = "密码不能为空"
goto end
}
salt := GetSalt(beego.AppConfig.DefaultInt("saltcount", 6))
md5password := GetSaltedPassword(password, salt)
println("salt:", salt, "password:", password, "md5password:", md5password)
tbl_account := >db.Account{Account: account, Password: md5password, Salt: salt, Regip: c.Ctx.Input.IP()}
err = gtdb.Manager().CreateAccount(tbl_account)
if err != nil {
c.Data["error"] = "数据库错误:" + err.Error()
}
}
end:
c.TplName = "register.tpl"
}
func (c *MainController) Login() {
if c.Ctx.Request.Method == "POST" {
account := c.GetString("account")
password := c.GetString("password")
c.Data["post"] = true
flag, err := gtdb.Manager().IsAccountExists(account)
if err != nil {
c.Data["error"] = "数据库错误"
goto end
}
if !flag {
c.Data["error"] = "账号不存在!"
goto end
}
// uid, err := gtdb.Manager().GetUIDByAccount(account)
// if err != nil {
// c.Data["error"] = "数据库错误"
// goto end
// }
tbl_account, err := gtdb.Manager().GetAccount(account)
if err != nil {
c.Data["error"] = "数据库错误"
goto end
}
md5password := GetSaltedPassword(password, tbl_account.Salt)
if md5password != tbl_account.Password {
c.Data["error"] = "密码错误"
goto end
}
println("account ", account, " logined success")
c.Data["account"] = account
c.SetSession("account", account)
flag, _ = gtdb.Manager().IsAdmin(account)
if flag {
tbl_admin, err := gtdb.Manager().GetAdmin(account)
if err == nil {
c.SetSession("admin", tbl_admin)
}
}
c.Redirect("/user/index", 302)
return
}
end:
c.TplName = "login.tpl"
}
func (c *MainController) Install() {
key := c.GetString("key")
if key == "testkey" {
err := gtdb.Manager().Install()
if err == nil {
c.Ctx.Output.Body([]byte("<html><body>install success!<br/><a href=\"/\">点击进入主页</a></body></html>"))
} else {
c.Ctx.Output.Body([]byte("{error:" + err.Error() + "}"))
}
} else {
c.Ctx.Output.Body([]byte("{error:only admin can install the db}"))
}
}