feat($permission): 增加账户权限控制

Close #12
pull/21/merge
ouqiang 2017-10-01 22:53:22 +08:00
parent 3474636e23
commit 4daf0a15ba
6 changed files with 70 additions and 23 deletions

View File

@ -14,7 +14,7 @@ import (
"strings" "strings"
) )
const AppVersion = "1.2.2" const AppVersion = "1.3"
func main() { func main() {
var serverAddr string var serverAddr string

View File

@ -171,6 +171,7 @@ func RegisterMiddleware(m *macaron.Macaron) {
if app.Installed { if app.Installed {
ipAuth(ctx) ipAuth(ctx)
userAuth(ctx, sess) userAuth(ctx, sess)
urlAuth(ctx, sess)
setShareData(ctx, sess) setShareData(ctx, sess)
} }
}) })
@ -224,6 +225,33 @@ func userAuth(ctx *macaron.Context, sess session.Store) {
} }
} }
// URL权限验证
func urlAuth(ctx *macaron.Context, sess session.Store) {
if user.IsAdmin(sess) {
return
}
// 普通用户允许访问的URL地址
allowPaths := []string{
"",
"/task",
"/task/log",
"/host",
"/user/login",
"/user/logout",
"/user/editMyPassword",
}
uri := strings.TrimSpace(ctx.Req.URL.Path)
uri = strings.TrimRight(uri, "/")
for _, path := range allowPaths {
if path == uri {
return
}
}
ctx.Status(403)
}
/** 设置共享数据 **/ /** 设置共享数据 **/
func setShareData(ctx *macaron.Context, sess session.Store) { func setShareData(ctx *macaron.Context, sess session.Store) {
ctx.Data["URI"] = ctx.Req.URL.Path ctx.Data["URI"] = ctx.Req.URL.Path
@ -239,6 +267,7 @@ func setShareData(ctx *macaron.Context, sess session.Store) {
} }
ctx.Data["LoginUsername"] = user.Username(sess) ctx.Data["LoginUsername"] = user.Username(sess)
ctx.Data["LoginUid"] = user.Uid(sess) ctx.Data["LoginUid"] = user.Uid(sess)
ctx.Data["IsAdmin"] = user.IsAdmin(sess)
ctx.Data["AppName"] = app.Setting.AppName ctx.Data["AppName"] = app.Setting.AppName
} }

View File

@ -62,9 +62,9 @@
<div class="right menu"> <div class="right menu">
<a class="item {{{if or (eq .Controller "task") (eq .Controller "delaytask")}}}active{{{end}}}" href="/task"><i class="tasks icon"></i></a> <a class="item {{{if or (eq .Controller "task") (eq .Controller "delaytask")}}}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>
{{{if .IsAdmin}}}
<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 "manage"}}}active{{{end}}}" href="/manage/slack/edit"><i class="settings icon"></i></a>
<a class="item {{{if eq .Controller "manage"}}}active{{{end}}}" href="/manage/slack/edit"><i class="settings icon"></i></a>
{{{end}}} {{{end}}}
</div> </div>
</div> </div>

View File

@ -4,7 +4,8 @@
{{{ template "host/menu" . }}} {{{ template "host/menu" . }}}
<div class="twelve wide column"> <div class="twelve wide column">
<div class="pageHeader"> {{{if .IsAdmin}}}
<div class="pageHeader">
<div class="segment"> <div class="segment">
<h3 class="ui dividing header"> <h3 class="ui dividing header">
<a href="/host/create"> <a href="/host/create">
@ -16,6 +17,7 @@
</h3> </h3>
</div> </div>
</div> </div>
{{{end}}}
<form class="ui form"> <form class="ui form">
<div class="three fields"> <div class="three fields">
<div class="field"> <div class="field">
@ -49,11 +51,15 @@
<td>{{{.Port}}}</td> <td>{{{.Port}}}</td>
<td>{{{.Remark}}}</td> <td>{{{.Remark}}}</td>
<td class="operation"> <td class="operation">
{{{if $.IsAdmin}}}
<a class="ui purple button" href="/host/edit/{{{.Id}}}"></a> <a class="ui purple button" href="/host/edit/{{{.Id}}}"></a>
<button class="ui positive button" onclick="util.removeConfirm('/host/remove/{{{.Id}}}')"></button><br> <button class="ui positive button" onclick="util.removeConfirm('/host/remove/{{{.Id}}}')"></button><br>
{{{end}}}
<div style="margin-top: 5px;"> <div style="margin-top: 5px;">
<a class="ui twitter button" href="/task?host_id={{{.Id}}}"></a> <a class="ui twitter button" href="/task?host_id={{{.Id}}}"></a>
{{{if $.IsAdmin}}}
<button class="ui blue button" @click="ping({{{.Id}}})"></button> <button class="ui blue button" @click="ping({{{.Id}}})"></button>
{{{end}}}
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -2,7 +2,8 @@
<div class="ui grid"> <div class="ui grid">
{{{template "task/menu" .}}} {{{template "task/menu" .}}}
<div class="twelve wide column"> <div class="twelve wide column">
<div class="pageHeader"> {{{if .IsAdmin}}}
<div class="pageHeader">
<div class="segment"> <div class="segment">
<h3 class="ui dividing header"> <h3 class="ui dividing header">
<a href="/task/create"> <a href="/task/create">
@ -14,6 +15,7 @@
</h3> </h3>
</div> </div>
</div> </div>
{{{end}}}
<form class="ui form"> <form class="ui form">
<div class="six fields search"> <div class="six fields search">
<div class="one wide field"> <div class="one wide field">
@ -54,21 +56,25 @@
</div> </div>
</div> </div>
</form> </form>
<div class="field"> {{{if .IsAdmin}}}
<select id="batch-operation"> <div class="field">
<option value="0"></option> <select id="batch-operation">
<option value="1"></option> <option value="0"></option>
<option value="2"></option> <option value="1"></option>
<option value="3"></option> <option value="2"></option>
</select> <option value="3"></option>
</div> </select>
</div>
{{{end}}}
<br> <br>
<table class="ui celled table task-list"> <table class="ui celled table task-list">
<thead> <thead>
<tr> <tr>
<th> {{{if .IsAdmin}}}
<input type="checkbox" onclick="checkAll(this)" style="width:25px;height: 25px;"> <th>
</th> <input type="checkbox" onclick="checkAll(this)" style="width:25px;height: 25px;">
</th>
{{{end}}}
<th>ID</th> <th>ID</th>
<th></th> <th></th>
<th></th> <th></th>
@ -86,12 +92,14 @@
<tbody> <tbody>
{{{range $i, $v := .Tasks}}} {{{range $i, $v := .Tasks}}}
<tr> <tr>
<td> {{{if $.IsAdmin}}}
<input type="checkbox" <td>
class="sub-check" <input type="checkbox"
data-id="{{{.Id}}}" class="sub-check"
style="width:25px;height: 25px;"> data-id="{{{.Id}}}"
</td> style="width:25px;height: 25px;">
</td>
{{{end}}}
<td>{{{.Id}}}</td> <td>{{{.Id}}}</td>
<td>{{{.Name}}}</td> <td>{{{.Name}}}</td>
<td>{{{if eq .Level 1}}}{{{else}}}{{{end}}}</td> <td>{{{if eq .Level 1}}}{{{else}}}{{{end}}}</td>
@ -113,7 +121,7 @@
</td> </td>
<td> <td>
<div class="ui buttons operation"> <div class="ui buttons operation">
{{{if $.IsAdmin}}}
<a href="/task/edit/{{{.Id}}}" ><i class="edit big icon" title="编辑"></i></a>&nbsp;&nbsp;&nbsp;&nbsp; <a href="/task/edit/{{{.Id}}}" ><i class="edit big icon" title="编辑"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;
{{{if eq .Level 1}}} {{{if eq .Level 1}}}
{{{if eq .Status 1}}} {{{if eq .Status 1}}}
@ -124,6 +132,8 @@
{{{end}}} {{{end}}}
<a href="javascript:void(0);" @click="remove({{{.Id}}})"><i class="remove big icon" title="删除"></i></a> <a href="javascript:void(0);" @click="remove({{{.Id}}})"><i class="remove big icon" title="删除"></i></a>
<a href="javascript:void(0);" @click="run({{{.Id}}})"><i class="rocket big icon" title="手动执行"></i></a>&nbsp;&nbsp; <a href="javascript:void(0);" @click="run({{{.Id}}})"><i class="rocket big icon" title="手动执行"></i></a>&nbsp;&nbsp;
{{{end}}}
<a href="/task/log?task_id={{{.Id}}}"><i class="bar chart icon big" title="查看日志"></i></a> <a href="/task/log?task_id={{{.Id}}}"><i class="bar chart icon big" title="查看日志"></i></a>
</div> </div>
</td> </td>

View File

@ -13,6 +13,7 @@
{{{ template "task/menu" . }}} {{{ template "task/menu" . }}}
<div class="twelve wide column"> <div class="twelve wide column">
{{{if .IsAdmin}}}
<div class="pageHeader"> <div class="pageHeader">
<div class="segment"> <div class="segment">
<h3 class="ui dividing header"> <h3 class="ui dividing header">
@ -22,6 +23,7 @@
</h3> </h3>
</div> </div>
</div> </div>
{{{end}}}
<form class="ui form"> <form class="ui form">
<div class="six fields search"> <div class="six fields search">
<div class="field"> <div class="field">