forked from adamwalach/openvpn-web-ui
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprofile.go
85 lines (73 loc) · 1.71 KB
/
profile.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
package controllers
import (
"html/template"
passlib "gopkg.in/hlandau/passlib.v1"
"github.com/adamwalach/openvpn-web-ui/lib"
"github.com/adamwalach/openvpn-web-ui/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/validation"
)
type ProfileController struct {
BaseController
}
func (c *ProfileController) NestPrepare() {
if !c.IsLogin {
c.Ctx.Redirect(302, c.LoginPath())
return
}
c.Data["breadcrumbs"] = &BreadCrumbs{
Title: "Profile",
}
}
func (c *ProfileController) Get() {
c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
c.Data["profile"] = c.Userinfo
c.TplName = "profile.html"
}
func (c *ProfileController) Post() {
c.TplName = "profile.html"
c.Data["profile"] = c.Userinfo
flash := beego.NewFlash()
user := models.User{}
if err := c.ParseForm(&user); err != nil {
beego.Error(err)
flash.Error(err.Error())
flash.Store(&c.Controller)
return
}
user.Login = c.Userinfo.Login
c.Data["profile"] = user
if vMap := validateUser(user); vMap != nil {
c.Data["validation"] = vMap
return
}
hash, err := passlib.Hash(user.Password)
if err != nil {
flash.Error("Unable to hash password")
flash.Store(&c.Controller)
return
}
c.Userinfo.Email = user.Email
c.Userinfo.Name = user.Name
c.Userinfo.Password = hash
o := orm.NewOrm()
if _, err := o.Update(c.Userinfo); err != nil {
flash.Error(err.Error())
} else {
flash.Success("Profile has been updated")
}
flash.Store(&c.Controller)
}
func validateUser(user models.User) map[string]map[string]string {
valid := validation.Validation{}
b, err := valid.Valid(&user)
if err != nil {
beego.Error(err)
return nil
}
if !b {
return lib.CreateValidationMap(valid)
}
return nil
}