The previous section explained [golang gin framework] 43. Gin mall project - after the actual combat of microservices, the addition, deletion, modification and inquiry of the administrator of the background Rbac microservice and the association between the administrator and the role . microservice
1. Realize background permission management Rbac permission addition, deletion, modification and query microservice server function
1. Create an Access model
To add, delete, modify and check permissions, you need to create a corresponding model, so create an Access.go model file under server/rbac/models, refer to [golang gin framework] 14. Gin mall project-RBAC management code is as follows:
package models
//权限模型
type Access struct {
Id int64
ModuleName string //模块名称
ActionName string //操作名称
Type int64 //节点类型 : 1、表示模块 2、表示菜单 3、操作
Url string //路由跳转地址
ModuleId int64 //此module_id和当前模型的id关联 module_id= 0 表示模块
Sort int64
Description string
Status int64
AddTime int64
AccessItem []Access `gorm:"foreignKey:ModuleId;references:Id"` // 表的自关联,获取该数据的子分类
Checked bool `gorm:"-"` // 用户是否有该权, 忽略本字段,给struct加一个自定义属性,和数据库没有关系
}
func (Access) TableName() string {
return "access"
}
2. Create rbacAccess.proto under the proto folder
Refer to [golang gin framework] 14. Gin mall project - RBAC management , create rbacAccess.proto. Generate permission-related methods, the specific code is as follows:
syntax = "proto3";
package access;
option go_package = "./proto/rbacAccess";
//权限管理
service RbacAccess {
//获取权限rpc方法: 请求参数AccessGetRequest, 响应参数AccessGetResponse
rpc AccessGet(AccessGetRequest) returns (AccessGetResponse) {}
//增加权限rpc方法: 请求参数AccessAddRequest, 响应参数AccessAddResponse
rpc AccessAdd(AccessAddRequest) returns (AccessAddResponse) {}
//编辑权限rpc方法: 请求参数AccessEditRequest, 响应参数AccessEditResponse
rpc AccessEdit(AccessEditRequest) returns (AccessEditResponse) {}
//删除权限rpc方法: 请求参数AccessDeleteRequest, 响应参数AccessDeleteResponse
rpc AccessDelete(AccessDeleteRequest) returns (AccessDeleteResponse) {}
}
//权限相关model: 参考models/access.go
message AccessModel{
int64 id=1;
string moduleName =2;
string actionName=3;
int64 type=4;
string url=5;
int64 moduleId=6;
int64 sort =7;
string description=8;
int64 status=9;
int64 addTime=10;
repeated AccessModel accessItem=11; //权限的自关联(切片类型)
}
//获取权限请求参数
message AccessGetRequest{
//权限id, 也可以不传,不传的话就是获取所有权限列表
int64 id =1;
}
//获取权限响应参数
message AccessGetResponse{
//权限model切片
repeated AccessModel AccessList=1;
}
//增加权限请求参数
message AccessAddRequest{
string moduleName =1;
string actionName=2;
int64 type=3;
string url=4;
int64 moduleId=5;
int64 sort =6;
string description=7;
int64 status=8;
int64 addTime=9;
}
//增加权限响应参数
message AccessAddResponse{
//是否增加成功
bool success=1;
//返回状态说明
string message=2;
}
//编辑权限请求参数
message AccessEditRequest{
int64 id=1;
string moduleName =2;
string actionName=3;
int64 type=4;
string url=5;
int64 moduleId=6;
int64 sort =7;
string description=8;
int64 status=9;
int64 addTime=10;
}
//编辑权限响应参数
message AccessEditResponse{
//是否编辑成功
bool success=1;
//返回状态说明
string message=2;
}
//删除权限请求参数
message AccessDeleteRequest{
//权限id
int64 id=1;
}
//删除权限响应参数
message AccessDeleteResponse{
//是否删除成功
bool success=1;
//返回状态说明
string message=2;
}
3. Generate access-related pb.go, pb.micro.go files
Run the command protoc --proto_path=. --micro_out=. --go_out=:.proto/rbacAccess.proto under server/rbac
4. Create the rbacAccess.go file under the handler folder to implement the service method in proto
Refer to [golang gin framework] 14. Gin mall project - RBAC management_ permission addition, deletion, modification and query method code, copy the corresponding method implementation code to rbacAccess.go, and modify it to become a microservice code, specifically rbacAccess The .go code is as follows:
package handler
import (
"context"
"strconv"
"rbac/models"
pb "rbac/proto/rbacAccess"
)
type RbacAccess struct{}
//获取权限
func (e *RbacAccess) AccessGet(ctx context.Context, req *pb.AccessGetRequest, res *pb.AccessGetResponse) error {
accessList := []models.Access{}
where := "1=1"
if req.Id > 0 { // 当传入权限id时,获取对应的权限数据, 当没有传入权限id时,获取权限列表数据
where += " AND id=" + strconv.Itoa(int(req.Id))
} else {
where += " AND module_id = 0"
}
//获取权限数据以及对应的关联数据
models.DB.Where(where).Preload("AccessItem").Find(&accessList)
//处理数据
var tempList []*pb.AccessModel
for _, v := range accessList {
//处理自关联的子数据
var tempItemList []*pb.AccessModel
for _, k := range v.AccessItem {
tempItemList = append(tempItemList, &pb.AccessModel{
Id: int64(k.Id),
ModuleName: k.ModuleName,
ActionName: k.ActionName,
Type: int64(k.Type),
Url: k.Url,
ModuleId: int64(k.ModuleId),
Sort: int64(k.Sort),
Description: k.Description,
Status: int64(k.Status),
AddTime: int64(k.AddTime),
})
}
//处理数据
tempList = append(tempList, &pb.AccessModel{
Id: int64(v.Id),
ModuleName: v.ModuleName,
ActionName: v.ActionName,
Type: int64(v.Type),
Url: v.Url,
ModuleId: int64(v.ModuleId),
Sort: int64(v.Sort),
Description: v.Description,
Status: int64(v.Status),
AddTime: int64(v.AddTime),
AccessItem: tempItemList, //关联的子数据
})
}
res.AccessList = tempList
return nil
}
//增加权限
func (e *RbacAccess) AccessAdd(ctx context.Context, req *pb.AccessAddRequest, res *pb.AccessAddResponse) error {
access := models.Access{
ModuleName: req.ModuleName,
Type: req.Type,
ActionName: req.ActionName,
Url: req.Url,
ModuleId: req.ModuleId,
Sort: req.Sort,
Description: req.Description,
Status: req.Status,
}
err := models.DB.Create(&access).Error
if err != nil {
res.Success = false
res.Message = "增加数据失败"
} else {
res.Success = true
res.Message = "增加数据成功"
}
return err
}
//修改权限
func (e *RbacAccess) AccessEdit(ctx context.Context, req *pb.AccessEditRequest, res *pb.AccessEditResponse) error {
access := models.Access{Id: req.Id}
models.DB.Find(&access)
access.ModuleName = req.ModuleName
access.Type = req.Type
access.ActionName = req.ActionName
access.Url = req.Url
access.ModuleId = req.ModuleId
access.Sort = req.Sort
access.Description = req.Description
access.Status = req.Status
err := models.DB.Save(&access).Error
if err != nil {
res.Success = false
res.Message = "修改数据失败"
} else {
res.Success = true
res.Message = "修改数据成功"
}
return err
}
//删除权限
func (e *RbacAccess) AccessDelete(ctx context.Context, req *pb.AccessDeleteRequest, res *pb.AccessDeleteResponse) error {
access := models.Access{Id: req.Id}
models.DB.Find(&access)
if access.ModuleId == 0 { //顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ?", access.Id).Find(&accessList)
if len(accessList) > 0 {
res.Success = false
res.Message = "当前模块下面有菜单或者操作,请删除菜单或者操作以后再来删除这个数据"
} else {
models.DB.Delete(&access)
res.Success = true
res.Message = "删除数据成功"
}
} else { //操作 或者菜单
models.DB.Delete(&access)
res.Success = true
res.Message = "删除数据成功"
}
return nil
}
5. Register the permission microservice in the main.go file
Just import pbManager "rbac/proto/rbacAccess" and add the following code in main():
// Register handler:注册权限微服务
if err := pbAccess.RegisterRbacAccessHandler(srv.Server(), new(handler.RbacAccess)); err != nil {
logger.Fatal(err)
}
The specific code is as follows:
package main
import (
"rbac/handler"
"rbac/models"
pb "rbac/proto/rbacLogin"
pbRole "rbac/proto/rbacRole"
pbManager "rbac/proto/rbacManager"
pbAccess "rbac/proto/rbacAccess"
"go-micro.dev/v4"
"go-micro.dev/v4/logger"
"github.com/go-micro/plugins/v4/registry/consul"
)
var (
service = "rbac"
version = "latest"
)
func main() {
//集成consul
consulReg := consul.NewRegistry()
// Create service
//读取.ini里面的配置
addr := models.Config.Section("consul").Key("addr").String()
srv := micro.NewService(
micro.Address(addr), //指定微服务的ip: 选择注册服务器地址,也可以不配置,默认为本机,也可以选择consul集群中的client
micro.Name(service),
micro.Version(version),
//注册consul
micro.Registry(consulReg),
)
srv.Init(
micro.Name(service),
micro.Version(version),
)
// Register handler:注册登录微服务
if err := pb.RegisterRbacLoginHandler(srv.Server(), new(handler.RbacLogin)); err != nil {
logger.Fatal(err)
}
// Register handler:注册角色微服务
if err := pbRole.RegisterRbacRoleHandler(srv.Server(), new(handler.RbacRole)); err != nil {
logger.Fatal(err)
}
// Register handler:注册管理员微服务
if err := pbManager.RegisterRbacManagerHandler(srv.Server(), new(handler.RbacManager)); err != nil {
logger.Fatal(err)
}
// Register handler:注册权限微服务
if err := pbAccess.RegisterRbacAccessHandler(srv.Server(), new(handler.RbacAccess)); err != nil {
logger.Fatal(err)
}
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}
2. Realize the background authority management Rbac authority client microservice function
1. Copy the rbacAccess.go and rbacAccess folders under the server/rbac/proto folder to the proto folder in the client project
2. Call Rbac permission to add, delete, modify and check microservices
In the Index(), Add(), DoAdd(), Edit(), DoEdit(), Delete() methods of controllers/admin/access.go, call the Rbac permission addition, deletion, modification and query microservice function, which needs to be introduced in import The rbacAccess microservice related package, the code is as follows:
import (
pbAccess "goshop/proto/rbacAccess"
)
The original controllers/admin/access.go code is as follows: You can also refer to [golang gin framework] 14. Gin mall project-RBAC management
package admin
import (
"github.com/gin-gonic/gin"
"goshop/models"
"net/http"
"strings"
)
type AccessController struct {
BaseController
}
func (con AccessController) Index(c *gin.Context) {
//获取权限列表
accessList := []models.Access{}
models.DB.Where("module_id = ?", 0).Preload("AccessItem").Find(&accessList)
c.HTML(http.StatusOK, "admin/access/index.html", gin.H{
"accessList": accessList,
})
}
func (con AccessController) Add(c *gin.Context) {
//获取顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ?", 0).Find(&accessList)
c.HTML(http.StatusOK, "admin/access/add.html", gin.H{
"accessList": accessList,
})
}
func (con AccessController) DoAdd(c *gin.Context) {
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入参数错误", "/admin/access/add")
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/add")
return
}
//实例化access
access := models.Access{
ModuleName: moduleName,
ActionName: actionName,
Type: accessType,
Url: url,
ModuleId: moduleId,
Sort: sort,
Status: status,
Description: description,
}
err5 := models.DB.Create(&access).Error
if err5 != nil {
con.Error(c, "添加权限失败", "/admin/access/add")
return
}
con.Success(c, "添加权限成功", "/admin/access")
}
//编辑
func (con AccessController) Edit(c *gin.Context) {
//获取id
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
access := models.Access{Id: id}
models.DB.Find(&access)
//获取顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ?", 0).Find(&accessList)
c.HTML(http.StatusOK, "admin/access/edit.html", gin.H{
"access": access,
"accessList": accessList,
})
}
//编辑:提交
func (con AccessController) DoEdit(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.PostForm("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/edit?id"+models.String(id))
return
}
//获取要修改的数据
access := models.Access{Id: id}
models.DB.Find(&access)
access.ModuleName = moduleName
access.ActionName = actionName
access.Type = accessType
access.Url = url
access.ModuleId = moduleId
access.Sort = sort
access.Status = status
access.Description = description
//保存
err5 := models.DB.Save(&access).Error
if err5 != nil {
con.Error(c, "编辑权限失败", "/admin/access/edit?id"+models.String(id))
return
}
con.Success(c, "编辑权限成功", "/admin/access")
}
//删除
func (con AccessController) Delete(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
//获取要删除的数据
access := models.Access{Id: id}
models.DB.Find(&access)
if access.ModuleId == 0 { // 顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ? ", access.Id).Find(&accessList)
if len(accessList) > 0 {
con.Error(c, "当前模块下子菜单,请先删除子菜单后再来删除这个数据", "/admin/access")
return
}
}
// 操作 或者 菜单, 或者顶级模块下面没有子菜单, 可以直接删除
err = models.DB.Delete(&access).Error
if err != nil {
con.Error(c, "删除数据失败", "/admin/access")
return
}
con.Success(c, "删除数据成功", "/admin/access")
}
(1). The Index() method calls the microservice code
Original method:
func (con AccessController) Index(c *gin.Context) {
//获取权限列表
accessList := []models.Access{}
models.DB.Where("module_id = ?", 0).Preload("AccessItem").Find(&accessList)
c.HTML(http.StatusOK, "admin/access/index.html", gin.H{
"accessList": accessList,
})
}
The improved method:
func (con AccessController) Index(c *gin.Context) {
//获取权限列表
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
res, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{})
c.HTML(http.StatusOK, "admin/access/index.html", gin.H{
"accessList": res.AccessList,
})
}
(2). The Add() method calls the microservice code
Original method:
func (con AccessController) Add(c *gin.Context) {
//获取顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ?", 0).Find(&accessList)
c.HTML(http.StatusOK, "admin/access/add.html", gin.H{
"accessList": accessList,
})
}
The improved method:
func (con AccessController) Add(c *gin.Context) {
//获取顶级模块
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
res, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{})
c.HTML(http.StatusOK, "admin/access/add.html", gin.H{
"accessList": res.AccessList,
})
}
(3). The DoAdd() method calls the microservice code
Original method:
func (con AccessController) DoAdd(c *gin.Context) {
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入参数错误", "/admin/access/add")
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/add")
return
}
//实例化access
access := models.Access{
ModuleName: moduleName,
ActionName: actionName,
Type: accessType,
Url: url,
ModuleId: moduleId,
Sort: sort,
Status: status,
Description: description,
}
err5 := models.DB.Create(&access).Error
if err5 != nil {
con.Error(c, "添加权限失败", "/admin/access/add")
return
}
con.Success(c, "添加权限成功", "/admin/access")
}
The improved method:
func (con AccessController) DoAdd(c *gin.Context) {
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入参数错误", "/admin/access/add")
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/add")
return
}
//调用权限微服务功能:实现权限的添加
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
res, _ := rbacClient.AccessAdd(context.Background(), &pbAccess.AccessAddRequest{
ModuleName: moduleName,
Type: int64(accessType),
ActionName: actionName,
Url: url,
ModuleId: int64(moduleId),
Sort: int64(sort),
Description: description,
Status: int64(status),
})
if !res.Success {
con.Error(c, "添加权限失败", "/admin/access/add")
return
}
con.Success(c, "添加权限成功", "/admin/access")
}
(3). The Edit() method calls the microservice code
Original method:
//编辑
func (con AccessController) Edit(c *gin.Context) {
//获取id
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
access := models.Access{Id: id}
models.DB.Find(&access)
//获取顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ?", 0).Find(&accessList)
c.HTML(http.StatusOK, "admin/access/edit.html", gin.H{
"access": access,
"accessList": accessList,
})
}
The improved method:
//编辑
func (con AccessController) Edit(c *gin.Context) {
//获取id
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
//获取当前id对应的access
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
access, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{
Id: int64(id),
})
//获取顶级模块
resAccess, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{})
c.HTML(http.StatusOK, "admin/access/edit.html", gin.H{
"access": access.AccessList[0],
"accessList": resAccess.AccessList,
})
}
(4). The DoEdit() method calls the microservice code
Original method:
//编辑:提交
func (con AccessController) DoEdit(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.PostForm("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/edit?id"+models.String(id))
return
}
//获取要修改的数据
access := models.Access{Id: id}
models.DB.Find(&access)
access.ModuleName = moduleName
access.ActionName = actionName
access.Type = accessType
access.Url = url
access.ModuleId = moduleId
access.Sort = sort
access.Status = status
access.Description = description
//保存
err5 := models.DB.Save(&access).Error
if err5 != nil {
con.Error(c, "编辑权限失败", "/admin/access/edit?id"+models.String(id))
return
}
con.Success(c, "编辑权限成功", "/admin/access")
}
The improved method:
//编辑:提交
func (con AccessController) DoEdit(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.PostForm("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/edit?id"+models.String(id))
return
}
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
accessRes, _ := rbacClient.AccessEdit(context.Background(), &pbAccess.AccessEditRequest{
Id: int64(id),
ModuleName: moduleName,
Type: int64(accessType),
ActionName: actionName,
Url: url,
ModuleId: int64(moduleId),
Sort: int64(sort),
Description: description,
Status: int64(status),
})
if !accessRes.Success {
con.Error(c, "编辑权限失败", "/admin/access/edit?id="+models.String(id))
return
}
con.Success(c, "编辑权限成功", "/admin/access/edit?id="+models.String(id))
}
(5). The Delete() method calls the microservice code
Original method:
//删除
func (con AccessController) Delete(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
//获取要删除的数据
access := models.Access{Id: id}
models.DB.Find(&access)
if access.ModuleId == 0 { // 顶级模块
accessList := []models.Access{}
models.DB.Where("module_id = ? ", access.Id).Find(&accessList)
if len(accessList) > 0 {
con.Error(c, "当前模块下子菜单,请先删除子菜单后再来删除这个数据", "/admin/access")
return
}
}
// 操作 或者 菜单, 或者顶级模块下面没有子菜单, 可以直接删除
err = models.DB.Delete(&access).Error
if err != nil {
con.Error(c, "删除数据失败", "/admin/access")
return
}
con.Success(c, "删除数据成功", "/admin/access")
}
The improved method:
//删除
func (con AccessController) Delete(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
//获取我们要删除的数据
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
accessRes, _ := rbacClient.AccessDelete(context.Background(), &pbAccess.AccessDeleteRequest{
Id: int64(id),
})
if !accessRes.Success { //顶级模块
con.Error(c, accessRes.Message, "/admin/access")
return
}
//操作 或者菜单
con.Success(c, "删除数据成功", "/admin/access")
}
(6). The complete code is as follows
package admin
import (
"context"
"github.com/gin-gonic/gin"
"goshop/models"
pbAccess "goshop/proto/rbacAccess"
"net/http"
"strings"
)
type AccessController struct {
BaseController
}
func (con AccessController) Index(c *gin.Context) {
//获取权限列表
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
res, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{})
c.HTML(http.StatusOK, "admin/access/index.html", gin.H{
"accessList": res.AccessList,
})
}
func (con AccessController) Add(c *gin.Context) {
//获取顶级模块
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
res, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{})
c.HTML(http.StatusOK, "admin/access/add.html", gin.H{
"accessList": res.AccessList,
})
}
func (con AccessController) DoAdd(c *gin.Context) {
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入参数错误", "/admin/access/add")
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/add")
return
}
//调用权限微服务功能:实现权限的添加
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
res, _ := rbacClient.AccessAdd(context.Background(), &pbAccess.AccessAddRequest{
ModuleName: moduleName,
Type: int64(accessType),
ActionName: actionName,
Url: url,
ModuleId: int64(moduleId),
Sort: int64(sort),
Description: description,
Status: int64(status),
})
if !res.Success {
con.Error(c, "添加权限失败", "/admin/access/add")
return
}
con.Success(c, "添加权限成功", "/admin/access")
}
//编辑
func (con AccessController) Edit(c *gin.Context) {
//获取id
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
//获取当前id对应的access
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
access, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{
Id: int64(id),
})
//获取顶级模块
resAccess, _ := rbacClient.AccessGet(context.Background(), &pbAccess.AccessGetRequest{})
c.HTML(http.StatusOK, "admin/access/edit.html", gin.H{
"access": access.AccessList[0],
"accessList": resAccess.AccessList,
})
}
//编辑:提交
func (con AccessController) DoEdit(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.PostForm("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//获取表单数据
moduleName := strings.Trim(c.PostForm("module_name"), " ")
actionName := strings.Trim(c.PostForm("action_name"), " ")
accessType, err1 := models.Int(c.PostForm("type"))
url := c.PostForm("url")
moduleId, err2 := models.Int(c.PostForm("module_id"))
sort, err3 := models.Int(c.PostForm("sort"))
status, err4 := models.Int(c.PostForm("status"))
description := strings.Trim(c.PostForm("description"), " ")
//判断err
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
con.Error(c, "传入数据错误", "/admin/access/edit?id"+models.String(id))
return
}
//判断moduleName
if moduleName == "" {
con.Error(c, "模块名称不能为空", "/admin/access/edit?id"+models.String(id))
return
}
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
accessRes, _ := rbacClient.AccessEdit(context.Background(), &pbAccess.AccessEditRequest{
Id: int64(id),
ModuleName: moduleName,
Type: int64(accessType),
ActionName: actionName,
Url: url,
ModuleId: int64(moduleId),
Sort: int64(sort),
Description: description,
Status: int64(status),
})
if !accessRes.Success {
con.Error(c, "编辑权限失败", "/admin/access/edit?id="+models.String(id))
return
}
con.Success(c, "编辑权限成功", "/admin/access/edit?id="+models.String(id))
}
//删除
func (con AccessController) Delete(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/access")
return
}
//获取我们要删除的数据
rbacClient := pbAccess.NewRbacAccessService("rbac", models.RbacClient)
accessRes, _ := rbacClient.AccessDelete(context.Background(), &pbAccess.AccessDeleteRequest{
Id: int64(id),
})
if !accessRes.Success { //顶级模块
con.Error(c, accessRes.Message, "/admin/access")
return
}
//操作 或者菜单
con.Success(c, "删除数据成功", "/admin/access")
}
3. Verification permission management Rbac permission addition, deletion, modification and query microservice function
1. Start the server first
See [golang gin framework] 40. Gin mall project - Captcha verification code microservice code in microservice combat , here also start the verification code captcha microservice server code and authority management Rbac microservice (user login microservice server, role Manage the microservice server, the administrator manages the microservice server) server
2. Start the client
Run in the project root directory: go run main.go to start the project
3. Verify the authority management Rbac authority addition, deletion, modification and check whether the microservice operation is successful
Visit the background login page, enter the user name, password, verification code, log in to the background, enter the rights management page, and add, delete, modify and check the rights
After the delete operation, it is found that there is no such data, indicating that the delete operation is correct
Well, permission management Rbac permission addition, deletion, modification and query microservice function client operation is completed, here the server side of the microservice operation, the client function is roughly [golang gin framework] 43. Gin mall project - microservice actual combat background Rbac microservice The addition, deletion, modification and query of administrators and the association between administrators and roles are similar. You can refer to this article for operation. The following section continues to explain the role and authority association microservices of authority management Rbac microservices