用户登陆后保存uid到session

pull/21/merge
ouqiang 8 years ago
parent ba5b0cd6ad
commit d12976f68e

@ -35,10 +35,9 @@ var CmdWeb = cli.Command{
Value: "prod",
Usage: "runtime environment, dev|test|prod",
},
cli.StringFlag{
cli.BoolFlag{
Name: "d",
Value: "false",
Usage: "-d=true, run app as daemon, not support windows",
Usage: "-d, run app as daemon, not support windows",
},
},
}
@ -111,11 +110,7 @@ func becomeDaemon(ctx *cli.Context) {
if utils.IsWindows() {
return
}
var daemond string = "false"
if ctx.IsSet("d") {
daemond = ctx.String("d")
}
if (daemond != "true") {
if !ctx.IsSet("d") {
return
}

@ -19,8 +19,7 @@ type User struct {
Deleted time.Time `xorm:"datetime deleted"`
IsAdmin int8 `xorm:"tinyint notnull default 0"` // 是否是管理员 1:管理员 0:普通用户
Status Status `xorm:"tinyint notnull default 1"` // 1: 正常 0:禁用
Page int `xorm:"-"`
PageSize int `xorm:"-"`
BaseModel `xorm:"-"`
}
// 新增

@ -163,6 +163,7 @@ func setShareData(m *macaron.Macaron) {
ctx.Data["Action"] = paths[1]
}
ctx.Data["LoginUsername"] = user.Username(sess)
ctx.Data["LoginUid"] = user.Uid(sess)
})
}

@ -6,7 +6,6 @@ import (
"github.com/ouqiang/gocron/models"
"github.com/go-macaron/session"
"github.com/ouqiang/gocron/modules/logger"
"time"
)
// @author qiang.ou<qingqianludao@gmail.com>
@ -30,20 +29,20 @@ func ValidateLogin(ctx *macaron.Context, sess session.Store) string {
return json.CommonFailure("用户名或密码错误")
}
sess.Set("username", username)
sess.Set("username", userModel.Name)
sess.Set("uid", userModel.Id)
return json.Success("登录成功", nil)
}
func Logout(ctx *macaron.Context, sess session.Store) {
if IsLogin(sess) {
err := sess.Delete("username")
err := sess.Destory(ctx)
if err != nil {
logger.Error("用户退出登录失败", err)
}
}
ctx.SetSecureCookie("MacaronSession", "", 0, "/", "", nil, nil, time.Now().AddDate(-1, 0, 0))
Login(ctx)
}
@ -56,9 +55,18 @@ func Username(sess session.Store) string {
return ""
}
func Uid(sess session.Store) int {
uid,ok := sess.Get("uid").(int)
if ok {
return uid
}
return 0
}
func IsLogin(sess session.Store) bool {
username,ok := sess.Get("username").(string)
if ok && username != "" {
uid, ok := sess.Get("uid").(int)
if ok && uid > 0 {
return true
}

@ -59,8 +59,10 @@
<div class="right menu">
<a class="item {{{if eq .Controller "task"}}}active{{{end}}}" href="/task"><i class="tasks icon"></i>任务</a>
<a class="item {{{if eq .Controller "host"}}}active{{{end}}}" href="/host"><i class="linux icon"></i>主机</a>
<a class="item {{{if eq .Controller "user"}}}active{{{end}}}" href="/user"><i class="user icon"></i>账户</a>
<!-- <a class="item {{{if eq .Controller "user"}}}active{{{end}}}" href="/user"><i class="user icon"></i>账户</a> -->
{{{if gt .LoginUid 0}}}
<a class="item {{{if eq .Controller "admin"}}}active{{{end}}}" href="/admin"><i class="settings icon"></i>管理</a>
{{{end}}}
</div>
</div>
</div>

@ -55,7 +55,7 @@
<textarea rows="5" name="command">{{{.Task.Command}}}</textarea>
</div>
</div>
<div class="four fields">
<div class="three fields">
<div class="field">
<label>任务超时时间 (单位秒, 默认0不限制超时)</label>
<input type="text" name="timeout" value="{{{.Task.Timeout}}}">

Loading…
Cancel
Save