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"
)
const AppVersion = "1.2.2"
const AppVersion = "1.3"
func main() {
var serverAddr string

View File

@ -171,6 +171,7 @@ func RegisterMiddleware(m *macaron.Macaron) {
if app.Installed {
ipAuth(ctx)
userAuth(ctx, sess)
urlAuth(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) {
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["LoginUid"] = user.Uid(sess)
ctx.Data["IsAdmin"] = user.IsAdmin(sess)
ctx.Data["AppName"] = app.Setting.AppName
}

View File

@ -62,9 +62,9 @@
<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 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>
{{{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}}}
</div>
</div>

View File

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

View File

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

View File

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