Procházet zdrojové kódy

添加权限控制

weicky před 4 roky
rodič
revize
1ce1a1ede2

+ 28 - 0
src/cnphper.com/redisdog/http_index.go

@@ -127,3 +127,31 @@ func index_favicon(resp http.ResponseWriter, req *http.Request) {
 		}
 	}
 }
+
+func index_forbidden(resp http.ResponseWriter, req *http.Request) {
+	sess, ok := checkLogin(resp, req)
+	if !ok {
+		return
+	}
+	//视图输出
+	files := []string{
+		filepath.Join(Cfg.TmplDir, "forbidden.tmpl"),
+		filepath.Join(Cfg.TmplDir, "header.tmpl"),
+		filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
+	}
+	tmpl, err := template.New("forbidden.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
+	if err != nil {
+		resp.WriteHeader(500)
+		io.WriteString(resp, err.Error())
+		return
+	}
+	tmpl.Execute(resp, struct {
+		Sess  *Session
+		Req   *http.Request
+		Title string
+	}{
+		sess,
+		req,
+		"首页",
+	})
+}

+ 33 - 3
src/cnphper.com/redisdog/http_syscfg_account.go

@@ -30,6 +30,11 @@ func syscfg_account(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		resp.Header().Set("Location", "/index/forbidden")
+		resp.WriteHeader(302)
+		return
+	}
 	//视图输出
 	files := []string{
 		filepath.Join(Cfg.TmplDir, "syscfg", "account.tmpl"),
@@ -53,10 +58,15 @@ func syscfg_account(resp http.ResponseWriter, req *http.Request) {
 }
 
 func syscfg_account_list(resp http.ResponseWriter, req *http.Request) {
-	_, ok := checkLogin(resp, req)
+	sess, ok := checkLogin(resp, req)
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	mdlAccounts := model.NewAccounts(Db)
 	list, err := mdlAccounts.GetAll()
@@ -69,10 +79,15 @@ func syscfg_account_list(resp http.ResponseWriter, req *http.Request) {
 }
 
 func syscfg_account_get(resp http.ResponseWriter, req *http.Request) {
-	_, ok := checkLogin(resp, req)
+	sess, ok := checkLogin(resp, req)
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	idStr := req.Form.Get("id")
 	if idStr == "" {
@@ -95,10 +110,15 @@ func syscfg_account_get(resp http.ResponseWriter, req *http.Request) {
 }
 
 func syscfg_account_set(resp http.ResponseWriter, req *http.Request) {
-	_, ok := checkLogin(resp, req)
+	sess, ok := checkLogin(resp, req)
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	Id := req.PostForm.Get("Id")
 	Account := req.PostForm.Get("Account")
@@ -187,6 +207,11 @@ func syscfg_account_del(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	idStr := req.Form.Get("id")
 	if idStr == "" {
@@ -215,6 +240,11 @@ func syscfg_account_reset_pwd(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	idStr := req.PostForm.Get("id")
 	if idStr == "" {

+ 16 - 1
src/cnphper.com/redisdog/http_syscfg_misc.go

@@ -23,6 +23,11 @@ func syscfg_misc(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		resp.Header().Set("Location", "/index/forbidden")
+		resp.WriteHeader(302)
+		return
+	}
 	//视图输出
 	files := []string{
 		filepath.Join(Cfg.TmplDir, "syscfg", "misc.tmpl"),
@@ -46,10 +51,15 @@ func syscfg_misc(resp http.ResponseWriter, req *http.Request) {
 }
 
 func syscfg_misc_get(resp http.ResponseWriter, req *http.Request) {
-	_, ok := checkLogin(resp, req)
+	sess, ok := checkLogin(resp, req)
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	mdl := model.NewSysCfg(Db)
 	values, err := mdl.GetByKeys([]string{"monitor_loop_interval", "monitor_mail_interval", "log_kept_days"})
 	if err != nil {
@@ -65,6 +75,11 @@ func syscfg_misc_set(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	moniotrLoopIntervalStr := req.PostForm.Get("monitor_loop_interval")
 	if moniotrLoopIntervalStr == "" {

+ 16 - 1
src/cnphper.com/redisdog/http_syscfg_warn.go

@@ -22,6 +22,11 @@ func syscfg_warn(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		resp.Header().Set("Location", "/index/forbidden")
+		resp.WriteHeader(302)
+		return
+	}
 	//视图输出
 	files := []string{
 		filepath.Join(Cfg.TmplDir, "syscfg", "warn.tmpl"),
@@ -45,10 +50,15 @@ func syscfg_warn(resp http.ResponseWriter, req *http.Request) {
 }
 
 func syscfg_warn_get(resp http.ResponseWriter, req *http.Request) {
-	_, ok := checkLogin(resp, req)
+	sess, ok := checkLogin(resp, req)
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	mdl := model.NewSysCfg(Db)
 	values, err := mdl.GetByKeys([]string{"smtp_host", "smtp_port", "smtp_user", "smtp_pwd", "smtp_sender"})
 	if err != nil {
@@ -65,6 +75,11 @@ func syscfg_warn_set(resp http.ResponseWriter, req *http.Request) {
 	if !ok {
 		return
 	}
+	if !sess.Account.IsSuper {
+		ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
+		resp.Write(ret)
+		return
+	}
 	req.ParseForm()
 	smtpHost := req.PostForm.Get("smtp_host")
 	if smtpHost == "" {

+ 1 - 0
src/cnphper.com/redisdog/main.go

@@ -190,6 +190,7 @@ func startHttpServer() *http.Server {
 	handle.HandleFunc("/login", login_index)
 	//首页
 	handle.HandleFunc("/", index_default)
+	handle.HandleFunc("/index/forbidden", index_forbidden)
 	handle.HandleFunc("/index/stats", index_stats)
 	handle.HandleFunc("/index/info", index_info)
 	//用户操作

+ 0 - 3
tmpl/debug/sendmail.tmpl

@@ -9,9 +9,6 @@
 	<div class="row">
 		<div class="col-md-4 col-md-offset-4">
 			<div class="pannel">
-				<div class="pannel_title">
-					<h3>邮件发送测试</h3>
-				</div>
 				<div class="pannel_body">
 					<form id="mainform">
 						<div class="form-group">

+ 16 - 0
tmpl/forbidden.tmpl

@@ -0,0 +1,16 @@
+{{template "header.tmpl" .}}
+</head>
+<body>
+{{template "navbar.tmpl" .}}
+
+<div class="container-fluid">
+	<h4 class="text-center">403 Forbidden</h4>
+	<hr />
+	<p class="text-danger">您不是系统管理员,无权限访问该页面!</p>
+</div>
+
+<script type="text/javascript">
+var $SESS = {{.Sess}};
+</script>
+</body>
+</html>