mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-04-05 20:17:53 +08:00
实现用户组功能
This commit is contained in:
parent
bbe06db66f
commit
6fb801d7ba
@ -47,9 +47,14 @@ func RegisterDataBase() {
|
||||
}
|
||||
|
||||
port := beego.AppConfig.String("db_port")
|
||||
parameters := beego.AppConfig.String("parameters")
|
||||
|
||||
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=%s", username, password, host, port, database, url.QueryEscape(timezone))
|
||||
|
||||
if parameters != "" {
|
||||
dataSource += "&" + parameters
|
||||
}
|
||||
|
||||
if err := orm.RegisterDataBase("default", "mysql", dataSource); err != nil {
|
||||
beego.Error("注册默认数据库失败:", err)
|
||||
os.Exit(1)
|
||||
|
@ -120,4 +120,14 @@ func initialization() {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
group := models.NewMemberGroup()
|
||||
group.GroupId = 1
|
||||
group.GroupName = "管理员组"
|
||||
group.GroupNumber = 1
|
||||
group.CreateTime = time.Now()
|
||||
group.CreateAt = 1
|
||||
group.IsEnableDelete = false
|
||||
|
||||
|
||||
}
|
||||
|
@ -34,6 +34,9 @@ db_port=3306
|
||||
db_database=mindoc_db
|
||||
db_username=root
|
||||
db_password=123456
|
||||
#其他连接参数请参考https://github.com/go-sql-driver/mysql
|
||||
parameters=allowAllFiles=false
|
||||
|
||||
|
||||
####################sqlite3 数据库配置###########################
|
||||
#db_adapter=sqlite3
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
type BaseController struct {
|
||||
beego.Controller
|
||||
Member *models.Member
|
||||
MemberResourceList []*models.Resource
|
||||
Option map[string]string
|
||||
EnableAnonymous bool
|
||||
EnableDocumentHistory bool
|
||||
@ -77,6 +78,25 @@ func (c *BaseController) Prepare() {
|
||||
}
|
||||
}
|
||||
}
|
||||
roleId := 4
|
||||
if c.Member != nil && c.Member.MemberId > 0 {
|
||||
roleId = c.Member.Role
|
||||
}
|
||||
|
||||
resourceList,err := models.NewMemberGroup().FindMemberGroupResourceList(roleId)
|
||||
if err != nil {
|
||||
beego.Error("获取用户许可资源时出错 =>", err)
|
||||
c.ShowErrorPage(500,"获取用户许可资源时出错")
|
||||
}
|
||||
c.MemberResourceList = resourceList
|
||||
c.Data["MemberResource"] = resourceList
|
||||
|
||||
for _,resource := range resourceList {
|
||||
if resource.ControllerName == controller && resource.ActionName == action && resource.HttpMethod == c.Ctx.Input.Method() {
|
||||
return
|
||||
}
|
||||
}
|
||||
c.ShowErrorPage(403,"权限不足")
|
||||
}
|
||||
|
||||
// SetMember 获取或设置当前登录用户信息,如果 MemberId 小于 0 则标识删除 Session
|
||||
@ -154,7 +174,7 @@ func (c *BaseController) ShowErrorPage(errCode int, errMsg string) {
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := beego.ExecuteViewPathTemplate(&buf, "document/export.tpl", beego.BConfig.WebConfig.ViewsPath, map[string]interface{}{"ErrorMessage": errMsg, "errCode": errCode, "BaseUrl": conf.BaseUrl}); err != nil {
|
||||
if err := beego.ExecuteViewPathTemplate(&buf, "errors/error.tpl", beego.BConfig.WebConfig.ViewsPath, map[string]interface{}{"ErrorMessage": errMsg, "errCode": errCode, "BaseUrl": conf.BaseUrl}); err != nil {
|
||||
c.Abort("500")
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,14 @@ type ResourcesController struct {
|
||||
ManagerController
|
||||
}
|
||||
|
||||
func (c *ResourcesController) ResourceList() {
|
||||
c.Prepare()
|
||||
c.TplName = "resources/ResourceList.tpl"
|
||||
}
|
||||
|
||||
func (c *ResourcesController) AddResource() {
|
||||
c.Prepare()
|
||||
c.TplName = "resources/AddResource.tpl"
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ type MemberGroup struct {
|
||||
CreateRealName string `orm:"-" json:"create_real_name"`
|
||||
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
|
||||
Resources string `orm:"column(resources);type(text);null" json:"-"`
|
||||
IsDelete bool `orm:"column(is_delete);type(bool);default(false)" json:"is_delete"`
|
||||
IsEnableDelete bool `orm:"column(is_enable_delete);type(bool);default(true)" json:"is_enable_delete"`
|
||||
ResourceList []*Resource `orm:"-" json:"resource_list"`
|
||||
ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
|
||||
ModifyName string `orm:"-" json:"modify_name"`
|
||||
@ -209,10 +209,10 @@ func (m *MemberGroup) FindMemberGroupList(keyword string) ([]*MemberGroup,error)
|
||||
}
|
||||
|
||||
//查询指定用户组的资源列表
|
||||
func (m *MemberGroupMembers) FindMemberGroupResourceList(groupId int) ([]*Resource,error) {
|
||||
func (m *MemberGroup) FindMemberGroupResourceList(groupId int) ([]*Resource,error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
var memberGroup *MemberGroup
|
||||
memberGroup := NewMemberGroup()
|
||||
|
||||
err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).One(memberGroup)
|
||||
|
||||
|
@ -90,8 +90,4 @@ func (m *Resource) Delete(resourceId int) (err error) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -30,12 +30,14 @@ type Member struct {
|
||||
Phone string `orm:"size(255);column(phone);null;default(null)" json:"phone"`
|
||||
Avatar string `orm:"size(1000);column(avatar)" json:"avatar"`
|
||||
//用户角色:0 超级管理员 /1 管理员/ 2 普通用户 .
|
||||
Role int `orm:"column(role);type(int);default(1);index" json:"role"`
|
||||
RoleName string `orm:"-" json:"role_name"`
|
||||
Status int `orm:"column(status);type(int);default(0)" json:"status"` //用户状态:0 正常/1 禁用
|
||||
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
|
||||
CreateAt int `orm:"type(int);column(create_at)" json:"create_at"`
|
||||
LastLoginTime time.Time `orm:"type(datetime);column(last_login_time);null" json:"last_login_time"`
|
||||
Role int `orm:"column(role);type(int);default(1);index" json:"role"`
|
||||
RoleName string `orm:"-" json:"role_name"`
|
||||
Status int `orm:"column(status);type(int);default(0)" json:"status"` //用户状态:0 正常/1 禁用
|
||||
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
|
||||
CreateAt int `orm:"type(int);column(create_at)" json:"create_at"`
|
||||
LastLoginTime time.Time `orm:"type(datetime);column(last_login_time);null" json:"last_login_time"`
|
||||
//用户权限列表
|
||||
ResourceList []*Resource `orm:"-" json:"resource_list"`
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
@ -287,6 +289,7 @@ func (m *Member) Exist(field string,value interface{}) bool {
|
||||
|
||||
return o.QueryTable(m.TableNameWithPrefix()).Filter(field,value).Exist()
|
||||
}
|
||||
|
||||
//是否是管理员
|
||||
func (c *Member) IsAdministrator() bool {
|
||||
if c == nil || c.MemberId <= 0 {
|
||||
@ -294,6 +297,7 @@ func (c *Member) IsAdministrator() bool {
|
||||
}
|
||||
return c.Role == 0 || c.Role == 1
|
||||
}
|
||||
|
||||
//是否是超级管理员
|
||||
func (c *Member) IsSuperAdministrator() bool {
|
||||
if c == nil || c.MemberId <= 0 {
|
||||
@ -368,7 +372,6 @@ func (m *Member) Valid(isHashPassword bool) error {
|
||||
}
|
||||
|
||||
//删除一个用户.
|
||||
|
||||
func (m *Member) Delete(oldId int, newId int) error {
|
||||
o := orm.NewOrm()
|
||||
|
||||
|
@ -45,6 +45,7 @@ func init() {
|
||||
beego.Router("/manager/user_group_member/list/:id:int", &controllers.ManagerController{},"*:MemberGroupMemberList")
|
||||
|
||||
//权限资源管理
|
||||
beego.Router("/resource/list", &controllers.ResourcesController{},"*:ResourceList")
|
||||
beego.Router("/resource/add", &controllers.ResourcesController{},"*:AddResource")
|
||||
|
||||
beego.Router("/setting", &controllers.SettingController{}, "*:Index")
|
||||
|
131
views/resources/ResourceList.tpl
Normal file
131
views/resources/ResourceList.tpl
Normal file
@ -0,0 +1,131 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<title>权限资源管理 - Powered by MinDoc</title>
|
||||
|
||||
<!-- Bootstrap -->
|
||||
<link href="{{cdncss "/static/bootstrap/css/bootstrap.min.css"}}" rel="stylesheet" type="text/css">
|
||||
<link href="{{cdncss "/static/font-awesome/css/font-awesome.min.css"}}" rel="stylesheet" type="text/css">
|
||||
<link href="/static/bootstrap/plugins/bootstrap-treegrid/css/jquery.treegrid.css" rel="stylesheet" type="text/css">
|
||||
<link href="{{cdncss "/static/css/main.css"}}" rel="stylesheet">
|
||||
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
||||
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
|
||||
<!--[if lt IE 9]>
|
||||
<script src="{{cdnjs "/static/html5shiv/3.7.3/html5shiv.min.js"}}"></script>
|
||||
<script src="{{cdnjs "/static/respond.js/1.4.2/respond.min.js" }}"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
<body>
|
||||
<div class="manual-reader">
|
||||
{{template "widgets/header.tpl" .}}
|
||||
<div class="container manual-body">
|
||||
<div class="row">
|
||||
<div class="page-left">
|
||||
<ul class="menu">
|
||||
{{template "manager/manager_widgets.tpl.tpl" .}}
|
||||
</ul>
|
||||
</div>
|
||||
<div class="page-right">
|
||||
<div class="m-box">
|
||||
<div class="box-head">
|
||||
<strong class="box-title">权限资源管理</strong>
|
||||
<button type="button" class="btn btn-success btn-sm pull-right" data-toggle="modal" data-target="#addMemberDialogModal">添加资源</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="box-body" id="resourceList">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-bordered tree" id="resourceTreeGrid">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>资源名称</th>
|
||||
<th>控制器名称</th>
|
||||
<th>动作名称</th>
|
||||
<th>请求类型</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $index,$item := .Lists}}
|
||||
<tr>
|
||||
<td></td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{template "widgets/footer.tpl" .}}
|
||||
</div>
|
||||
<!-- Delete Book Modal -->
|
||||
<div class="modal fade" id="deleteBookModal" tabindex="-1" role="dialog" aria-labelledby="deleteBookModalLabel">
|
||||
<div class="modal-dialog" role="document">
|
||||
<form method="post" id="deleteBookForm" action="{{urlfor "ManagerController.DeleteBook"}}">
|
||||
<input type="hidden" name="book_id" value="">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">删除项目</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<span style="font-size: 14px;font-weight: 400;">确定删除项目吗?</span>
|
||||
<p></p>
|
||||
<p class="text error-message">删除项目后将无法找回。</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<span id="form-error-message2" class="error-message"></span>
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
|
||||
<button type="submit" id="btnDeleteBook" class="btn btn-primary" data-loading-text="删除中...">确定删除</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<script src="{{cdnjs "/static/jquery/1.12.4/jquery.min.js"}}"></script>
|
||||
<script src="{{cdnjs "/static/bootstrap/js/bootstrap.min.js"}}"></script>
|
||||
<script src="{{cdnjs "/static/vuejs/vue.min.js"}}" type="text/javascript"></script>
|
||||
<script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
|
||||
<script src="/static/bootstrap/plugins/bootstrap-treegrid/js/jquery.treegrid.js"></script>
|
||||
<script src="/static/bootstrap/plugins/bootstrap-treegrid/js/jquery.treegrid.bootstrap3.js"></script>
|
||||
<script src="{{cdnjs "/static/js/main.js"}}" type="text/javascript"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
/**
|
||||
* 删除项目
|
||||
*/
|
||||
function deleteBook($id) {
|
||||
$("#deleteBookModal").find("input[name='book_id']").val($id);
|
||||
$("#deleteBookModal").modal("show");
|
||||
}
|
||||
$(function () {
|
||||
$("#resourceTreeGrid").treegrid();
|
||||
/**
|
||||
* 删除项目
|
||||
*/
|
||||
$("#deleteBookForm").ajaxForm({
|
||||
beforeSubmit : function () {
|
||||
$("#btnDeleteBook").button("loading");
|
||||
},
|
||||
success : function (res) {
|
||||
if(res.errcode === 0){
|
||||
window.location = window.location.href;
|
||||
}else{
|
||||
showError(res.message,"#form-error-message2");
|
||||
}
|
||||
$("#btnDeleteBook").button("reset");
|
||||
},
|
||||
error : function () {
|
||||
showError("服务器异常","#form-error-message2");
|
||||
$("#btnDeleteBook").button("reset");
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user