用户登陆后保存uid到session

pull/21/merge
ouqiang 2017-04-23 19:17:29 +08:00
parent ba5b0cd6ad
commit d12976f68e
6 changed files with 23 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -59,8 +59,10 @@
<div class="right menu"> <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 "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 "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> <a class="item {{{if eq .Controller "admin"}}}active{{{end}}}" href="/admin"><i class="settings icon"></i>管理</a>
{{{end}}}
</div> </div>
</div> </div>
</div> </div>

View File

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