Skip to content
This repository was archived by the owner on Jul 11, 2019. It is now read-only.

Commit d487c20

Browse files
committed
bug fixed & permission management in progress
1 parent dec2723 commit d487c20

File tree

9 files changed

+84
-30
lines changed

9 files changed

+84
-30
lines changed

templates/base/foot.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div class="container">Copyright @ 2014 GoFtp Authors</div>
1+
<div class="container">Copyright @ 2014 - 2015 GoFtp Authors</div>

templates/base/nav.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</a>
1111
<a class="hidden-xs logo" href="/">
1212
<a class="navbar-brand" href="/">
13-
<img style="height:36px;" src="/img/favicon.png">
13+
<img style="height:36px;" src="/public/img/favicon.png">
1414
Go Ftp
1515
</a>
1616
</a>

templates/login.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
<form class="form-signin" method="post">
4242
{{.XsrfFormHtml}}
4343
<h2 class="form-signin-heading">GoFtp管理系统</h2>
44+
{{if .Flash.error}}
45+
<font style="color:red">{{.Flash.error}}</font>
46+
{{end}}
4447
<input name="name" type="text" class="input-block-level" placeholder="账号">
4548
<input name="pass" type="password" class="input-block-level" placeholder="密码">
46-
<input type="submit" value="登录" class="btn btn-large btn-primary">&nbsp; <font color=red>{{.msg}}</font>
49+
<input type="submit" value="登录" class="btn btn-large btn-primary">
4750
</form>
4851
</div>
4952
</body>

templates/perm/list.html

+4-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<thead>
7272
<th>文件夹</th><th>创建时间</th><th>拥有者</th><th>拥有组</th>
7373
<th>权限(Owner)</th><th>权限(Group)</th>
74-
<th>权限(Others)</th><th>操作</th>
74+
<th>权限(Others)</th>
7575
</thead>
7676
<tbody>
7777
{{$parent := .parent}}
@@ -87,9 +87,9 @@
8787
{{$owner := .Owner}}
8888
{{$group := .Group}}
8989
{{if .IsDir}}
90-
<span class="glyphicon glyphicon-folder-open"></span>&nbsp; <a href="/perm/?path={{$parent}}/{{$name}}">{{$name}}</a>
90+
<span class="glyphicon glyphicon-folder-open"></span>&nbsp; <a href="/perm/?path={{call $.subPath $name}}">{{$name}}</a>
9191
{{else}}
92-
<a href="/down?path={{$parent}}/{{$name}}">{{$name}}</a>
92+
<a href="/down?path={{call $.subPath $name}}">{{$name}}</a>
9393
{{end}}</td><td>{{.ModTime}}</td>
9494
<td><select class="owner" id="owner_{{$name}}">
9595
{{range $users}}
@@ -107,8 +107,7 @@
107107
<td><input id="group_r_{{$name}}" class="check" type="checkbox"{{if call $.hasPerm .Mode 4 "r"}} checked{{end}}> r
108108
<input id="group_w_{{$name}}" class="check" type="checkbox"{{if call $.hasPerm .Mode 5 "w"}} checked{{end}}> w</td>
109109
<td><input id="other_r_{{$name}}" class="check" type="checkbox"{{if call $.hasPerm .Mode 7 "r"}} checked{{end}}> r
110-
<input id="other_w_{{$name}}" class="check" type="checkbox"{{if call $.hasPerm .Mode 8 "w"}} checked{{end}}> w</td>
111-
<td> </td></tr>
110+
<input id="other_w_{{$name}}" class="check" type="checkbox"{{if call $.hasPerm .Mode 8 "w"}} checked{{end}}> w</td></tr>
112111
{{end}}
113112
</tbody>
114113
</table>

templates/user/chgpass.html

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
{{define "body"}}
55
<div style="padding:10px;padding-top:60px">
6+
<div>
7+
{{if .Flash.info}}
8+
<span class="bg-info">{{.Flash.info}}</span>
9+
{{end}}
10+
</div>
611
<form class="form-horizontal" method="post">
712
{{.XsrfFormHtml}}
813
<legend>修改密码</legend>

web/actions.go

+40-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package web
22

33
import (
4-
"fmt"
4+
"io"
5+
"net/url"
56

67
"github.com/lunny/tango"
78
"github.com/tango-contrib/binding"
9+
"github.com/tango-contrib/flash"
810
"github.com/tango-contrib/renders"
911
"github.com/tango-contrib/session"
1012
"github.com/tango-contrib/xsrf"
@@ -34,7 +36,6 @@ func (a *BaseAuthAction) AskLogin() bool {
3436

3537
func (a *BaseAction) IsLogin() bool {
3638
id := a.Session.Get("userId")
37-
fmt.Println(id)
3839
return id != nil
3940
}
4041

@@ -84,11 +85,13 @@ func (a *MainAction) Get() {
8485
type LoginAction struct {
8586
BaseAction
8687
xsrf.Checker
88+
flash.Flash
8789
}
8890

8991
func (a *LoginAction) Get() error {
9092
return a.Render("login.html", renders.T{
9193
"XsrfFormHtml": a.Checker.XsrfFormHtml(),
94+
"Flash": a.Flash.Data(),
9295
})
9396
}
9497

@@ -100,19 +103,21 @@ func (a *LoginAction) Post() error {
100103
}
101104

102105
if user.Name == "" || user.Pass == "" {
103-
return a.Render("login.html", renders.T{
104-
"msg": "用户名或者密码错误",
105-
})
106+
a.Flash.Set("error", "用户名或者密码错误")
107+
a.Redirect("/login")
108+
return nil
106109
}
107110

108111
p, err := DB.GetUser(user.Name)
109112
if err != nil {
110-
return err
113+
a.Flash.Set("error", err.Error())
114+
a.Redirect("/login")
115+
return nil
111116
}
112117
if p != user.Pass {
113-
return a.Render("login.html", renders.T{
114-
"msg": "用户名或者密码错误",
115-
})
118+
a.Flash.Set("error", "用户名或者密码错误")
119+
a.Redirect("/login")
120+
return nil
116121
}
117122

118123
a.SetLogin(user.Name)
@@ -132,3 +137,29 @@ func (a *LogoutAction) Get() {
132137
a.Logout()
133138
a.Redirect("/")
134139
}
140+
141+
type DownAction struct {
142+
BaseAuthAction
143+
}
144+
145+
func (d *DownAction) Get() error {
146+
var err error
147+
p := d.Form("path")
148+
p, err = url.QueryUnescape(p)
149+
if err != nil {
150+
return err
151+
}
152+
153+
driver, err := Factory.NewDriver()
154+
if err != nil {
155+
return err
156+
}
157+
158+
_, rd, err := driver.GetFile(p, 0)
159+
if err != nil {
160+
return err
161+
}
162+
163+
_, err = io.Copy(d.ResponseWriter, rd)
164+
return err
165+
}

web/perm.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ func (c *PermAction) Get() error {
4545
}
4646
var pathinfos []server.FileInfo
4747
err = driver.ListDir(p, func(f server.FileInfo) error {
48-
pathinfos = append(pathinfos, f)
48+
if f.Name() != "." {
49+
pathinfos = append(pathinfos, f)
50+
}
4951
return nil
5052
})
5153
if err != nil {
@@ -69,6 +71,9 @@ func (c *PermAction) Get() error {
6971
"hasPerm": hasPerm,
7072
"users": users,
7173
"groups": groups,
74+
"subPath": func(subPath string) string {
75+
return url.QueryEscape(path.Join(parent, subPath))
76+
},
7277
})
7378
}
7479

web/user.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package web
22

33
import (
4+
"github.com/tango-contrib/flash"
45
"github.com/tango-contrib/renders"
56
"github.com/tango-contrib/xsrf"
67
)
@@ -33,6 +34,7 @@ func (a *UserAction) Get() error {
3334
type ChgPassAction struct {
3435
UserBaseAction
3536
xsrf.Checker
37+
flash.Flash
3638
}
3739

3840
func (c *ChgPassAction) Before() {
@@ -50,6 +52,7 @@ func (c *ChgPassAction) Get() error {
5052
"user": user,
5153
"userId": c.LoginUserId(),
5254
"XsrfFormHtml": c.Checker.XsrfFormHtml(),
55+
"Flash": c.Flash.Data(),
5356
})
5457
}
5558

@@ -63,6 +66,7 @@ func (c *ChgPassAction) Post() error {
6366
if err != nil {
6467
return err
6568
}
69+
c.Flash.Set("info", "修改密码成功")
6670
c.Redirect("/user/chgpass")
6771
return nil
6872
}

web/web.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/lunny/tango"
88
"github.com/syndtr/goleveldb/leveldb"
99
"github.com/tango-contrib/binding"
10+
"github.com/tango-contrib/flash"
1011
"github.com/tango-contrib/renders"
1112
"github.com/tango-contrib/session"
1213
"github.com/tango-contrib/xsrf"
@@ -63,25 +64,31 @@ func Web(listen, static, templates, admin, pass string, tls bool, certFile, keyF
6364
adminUser = admin
6465

6566
t := tango.Classic()
66-
t.Use(tango.Static(tango.StaticOptions{
67-
RootPath: static,
68-
}))
69-
t.Use(renders.New(renders.Options{
70-
Reload: true, // if reload when template is changed
71-
Directory: templates,
72-
}))
73-
t.Use(session.New(session.Options{
67+
sess := session.New(session.Options{
7468
MaxAge: timeout,
75-
}))
76-
t.Use(auth())
77-
t.Use(binding.Bind())
78-
t.Use(xsrf.New(timeout))
69+
})
70+
t.Use(
71+
tango.Static(tango.StaticOptions{
72+
RootPath: static,
73+
}),
74+
renders.New(renders.Options{
75+
Reload: true, // if reload when template is changed
76+
Directory: templates,
77+
}),
78+
sess,
79+
auth(),
80+
binding.Bind(),
81+
xsrf.New(timeout),
82+
flash.Flashes(sess),
83+
)
7984

8085
t.Get("/", new(MainAction))
8186
t.Any("/login", new(LoginAction))
8287
t.Get("/logout", new(LogoutAction))
88+
t.Get("/down", new(DownAction))
8389
t.Group("/user", func(g *tango.Group) {
8490
g.Get("/", new(UserAction))
91+
g.Any("/chgpass", new(ChgPassAction))
8592
g.Any("/add", new(UserAddAction))
8693
g.Any("/edit", new(UserEditAction))
8794
g.Any("/del", new(UserDelAction))

0 commit comments

Comments
 (0)