Skip to content

Commit

Permalink
feat($permission): 增加账户权限控制
Browse files Browse the repository at this point in the history
  • Loading branch information
ouqiang committed Oct 1, 2017
1 parent 3474636 commit 4daf0a1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gocron-node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"
)

const AppVersion = "1.2.2"
const AppVersion = "1.3"

func main() {
var serverAddr string
Expand Down
29 changes: 29 additions & 0 deletions routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func RegisterMiddleware(m *macaron.Macaron) {
if app.Installed {
ipAuth(ctx)
userAuth(ctx, sess)
urlAuth(ctx, sess)
setShareData(ctx, sess)
}
})
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions templates/common/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 7 additions & 1 deletion templates/host/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -16,6 +17,7 @@ <h3 class="ui dividing header">
</h3>
</div>
</div>
{{{end}}}
<form class="ui form">
<div class="three fields">
<div class="field">
Expand Down Expand Up @@ -49,11 +51,15 @@ <h3 class="ui dividing header">
<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>
Expand Down
48 changes: 29 additions & 19 deletions templates/task/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -14,6 +15,7 @@ <h3 class="ui dividing header">
</h3>
</div>
</div>
{{{end}}}
<form class="ui form">
<div class="six fields search">
<div class="one wide field">
Expand Down Expand Up @@ -54,21 +56,25 @@ <h3 class="ui dividing header">
</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>
Expand All @@ -86,12 +92,14 @@ <h3 class="ui dividing header">
<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>
Expand All @@ -113,7 +121,7 @@ <h3 class="ui dividing header">
</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}}}
Expand All @@ -124,6 +132,8 @@ <h3 class="ui dividing header">
{{{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>
Expand Down
2 changes: 2 additions & 0 deletions templates/task/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -22,6 +23,7 @@ <h3 class="ui dividing header">
</h3>
</div>
</div>
{{{end}}}
<form class="ui form">
<div class="six fields search">
<div class="field">
Expand Down

0 comments on commit 4daf0a1

Please sign in to comment.