作者 唐旭辉

权限调试 修改

... ... @@ -18,19 +18,6 @@ type BaseController struct {
beego.Controller
}
func (this *BaseController) Options() {
this.AllowCross() //允许跨域
this.Data["json"] = map[string]interface{}{"status": 204, "message": "ok", "moreinfo": ""}
this.ServeJSON()
}
func (this *BaseController) AllowCross() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
//this.Ctx.WriteString("")
}
//Prepare 实现beego.ControllerInterface 的接口
func (this *BaseController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
... ...
... ... @@ -226,6 +226,17 @@ func (c *RbacController) RoleHasMenu() {
return
}
func (c *RbacController) GetRoleMenuAll() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
log.Info("====>")
list, err := serverbac.GetRoleHasMenuAll(1, 1)
msg = protocol.NewReturnResponse(list, err)
return
}
func (c *RbacController) RoleMenuEdit() {
return
}
... ...
... ... @@ -9,6 +9,7 @@ type RoleMenu struct {
RoleId int64 `orm:"column(role_id);null" description:"role.id"`
MenuId int64 `orm:"column(menu_id);null" description:"menu.id"`
Opption string `orm:"column(opption);size(500);null" description:"配置"`
Code string `orm:"column(code)"`
}
func (t *RoleMenu) TableName() string {
... ...
... ... @@ -40,7 +40,7 @@ type ResponseDepartmentInfo struct {
ID int64 `json:"id"`
CompanyID int64 `json:"company_id"` //公司
Name string `json:"name"` //部门名字
ParantID int64 `json:"parant_id"` //父级部门Id
ParantID int64 `json:"parantId"` //父级部门Id
Manages []DepartmentManager `json:"manages"` //部门管理员
Members []DepartmentMember `json:"members"` //部门成员
Member int64 `json:"member"` //成员数
... ...
package routers
import (
"encoding/json"
)
//PermissionBase 路由对应的权限
type PermissionBase struct {
CodeName string //模块标识
ActionName string
MethodMap map[string]func()
}
func (p PermissionBase) ValidMapping(actionName string, fn func()) {
p.MethodMap[actionName] = fn
// MethodMap map[string]func()
}
var routerPermission = map[string]PermissionBase{
... ... @@ -18,22 +18,61 @@ var routerPermission = map[string]PermissionBase{
"/v1/department/delete": PermissionBase{CodeName: "OPPMG_DEPARTMENT", ActionName: "delete"},
}
//PermissionWithPosition 职位管理相关
// type PermissionWithPosition struct {
// PermissionBase
// }
var permissionObject = map[string]interface{}{
"": 0,
}
//模块编号
const (
MENU_DEPARTMENT string = "OPPMG_DEPARTMENT" //公司部门管理模块
MENU_POSITION string = "OPPMG_POSITION" //公司职务管理
MENU_RBAC string = "OPPMG_RBAC" //员工角色/权限设置
MENU_USER string = "OPPMG_USER" //公司员工管理
MENU_ENTERPRISE_BASIC string = "OPPMG_ENTERPRISE_BASIC" //企业基础设置(大节点)
MENU_SYSTEM_FUNCTION string = "OPPMG_SYSTEM_FUNCTION" //系统功能(大节点)
MENU_CONMPANY string = "OPPMG_CONMPANY" //企业信息维护
MENU_CHANCE_TEMP string = "OPPMG_CHANCE_TEMP" //机会模板管理
MENU_SORCE string = "OPPMG_SORCE" //评分模式
MENU_CHANCE string = "OPPMG_CHANCE" //机会管理
)
//GetCode 模块标识
// func (c *PermissionWithPosition) GetCodeName() string {
// return "OPPMG_DEPARTMENT"
// }
type PermissionContentObject interface {
StringUnmarshal(string) error
ObjectMarshal() (string, error)
}
//Valid 权限校验
// func (c PermissionWithPosition) Valid(userCompanyID int64) bool {
// return true
// }
type PermissionContentBase struct {
Check int8 `json:"check"`
}
//PermissionWithDepart 部门管理相关
// type PermissionWithDepart struct {
// Check int8 `json:"check"`
// }
func NewPermissionContentBase() PermissionContentObject {
return &PermissionContentBase{}
}
func (p *PermissionContentBase) StringUnmarshal(s string) error {
err := json.Unmarshal([]byte(s), p)
return err
}
func (p *PermissionContentBase) ObjectMarshal() (string, error) {
bt, err := json.Marshal(p)
if err != nil {
return "", err
}
return string(bt), err
}
type CodeToObject func() PermissionContentObject
var CodePermissionObject = map[string]CodeToObject{
MENU_DEPARTMENT: NewPermissionContentBase, //公司部门管理模块
MENU_POSITION: NewPermissionContentBase, //公司职务管理
MENU_RBAC: NewPermissionContentBase, //员工角色/权限设置
MENU_USER: NewPermissionContentBase, //公司员工管理
MENU_ENTERPRISE_BASIC: NewPermissionContentBase, //企业基础设置(大节点)
MENU_SYSTEM_FUNCTION: NewPermissionContentBase, //系统功能(大节点)
MENU_CONMPANY: NewPermissionContentBase, //企业信息维护
MENU_CHANCE_TEMP: NewPermissionContentBase, //机会模板管理
MENU_SORCE: NewPermissionContentBase, //评分模式
MENU_CHANCE: NewPermissionContentBase, //机会管理
}
... ...
... ... @@ -3,6 +3,8 @@ package routers
import (
"oppmg/controllers"
"github.com/astaxie/beego/context"
"oppmg/middleware"
"github.com/astaxie/beego"
... ... @@ -84,9 +86,20 @@ func init() {
beego.NSRouter("/company", &controllers.CompanyController{}, "post:InitCompany"),
beego.NSRouter("/company/info", &controllers.CompanyController{}, "post:GetCompanyForUCenter"),
)
beego.AddNamespace(nsV1)
beego.AddNamespace(nsAuth)
beego.AddNamespace(nsUcenter)
nsTest := beego.NewNamespace("/test",
beego.NSCond(func(ctx *context.Context) bool {
if beego.BConfig.RunMode != "prod" {
return true
}
return false
}),
beego.NSRouter("/te", &controllers.RbacController{}, "get:GetRoleMenuAll"),
)
beego.AddNamespace(nsTest)
beego.SetStaticPath("/log", beego.AppConfig.String("log_filename"))
}
... ...
package rbac
import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
)
//获取全部的权限菜单
func GetMenuAll() ([]protocol.PermissionItem, error) {
const datasql string = `SELECT id,name,icon,parent_id,senior_status,sort,code
FROM menu WHERE enabled=1 ORDER BY sort `
var (
list []protocol.PermissionItem
err error
)
err = utils.ExecuteQueryAll(&list, datasql)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
return list, nil
}
//
func GetRoleHasMenu(roleid int64, companyid int64) (*protocol.ResponseRoleMenus, error) {
var (
roleData *models.Role
err error
)
roleData, err = models.GetRoleById(roleid)
if err != nil {
log.Error("获取角色数据失败:%s", err)
return nil, err
}
if roleData.Types != models.ROLETYPES_ROLE {
log.Error("角色类型错误")
return nil, protocol.NewErrWithMessage("1")
}
if roleData.CompanyId != companyid {
log.Error("角色的公司id不匹配")
return nil, protocol.NewErrWithMessage("1")
}
var (
rolemenu []models.RoleMenu
ids []int64
)
rolemenu, err = models.GetRoleMenuByRole(roleid)
if err != nil {
log.Error("获取角色下的菜单数据失败:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
for _, v := range rolemenu {
ids = append(ids, v.MenuId)
}
data := &protocol.ResponseRoleMenus{
RoleId: roleData.Id,
RoleName: roleData.Name,
MenuId: ids,
}
return data, nil
}
func GetRoleHasMenuAll(roleid int64, companyid int64) (map[string]PermissionContentObject, error) {
var (
roleData *models.Role
err error
)
roleData, err = models.GetRoleById(roleid)
if err != nil {
log.Error("获取角色数据失败:%s", err)
return nil, err
}
if roleData.Types != models.ROLETYPES_ROLE {
log.Error("角色类型错误")
return nil, protocol.NewErrWithMessage("1")
}
if roleData.CompanyId != companyid {
log.Error("角色的公司id不匹配")
return nil, protocol.NewErrWithMessage("1")
}
var (
rolemenu []models.RoleMenu
returnList = make(map[string]PermissionContentObject)
)
rolemenu, err = models.GetRoleMenuByRole(roleid)
if err != nil {
log.Error("获取角色下的菜单数据失败:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
for _, v := range rolemenu {
if fn, ok := CodePermissionObject[v.Code]; ok {
obj := fn()
err := obj.StringUnmarshal(v.Opption)
if err != nil {
log.Error("解析权限规则失败", err)
continue
}
returnList[v.Code] = obj
}
}
return returnList, nil
}
func RoleMenuEdit(companyid int64, roleid int64, menuids []int64) {
}
... ...
package rbac
import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"encoding/json"
)
//获取全部的权限菜单
func GetMenuAll() ([]protocol.PermissionItem, error) {
const datasql string = `SELECT id,name,icon,parent_id,senior_status,sort,code
FROM menu WHERE enabled=1 ORDER BY sort `
var (
list []protocol.PermissionItem
err error
)
err = utils.ExecuteQueryAll(&list, datasql)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
return list, nil
//PermissionBase 路由对应的权限
type PermissionBase struct {
CodeName string //模块标识
ActionName string
// MethodMap map[string]func()
}
//
func GetRoleHasMenu(roleid int64, companyid int64) (*protocol.ResponseRoleMenus, error) {
var (
roleData *models.Role
err error
)
roleData, err = models.GetRoleById(roleid)
if err != nil {
log.Error("获取角色数据失败:%s", err)
return nil, err
}
if roleData.Types != models.ROLETYPES_ROLE {
log.Error("角色类型错误")
return nil, protocol.NewErrWithMessage("1")
}
if roleData.CompanyId != companyid {
log.Error("角色的公司id不匹配")
return nil, protocol.NewErrWithMessage("1")
}
var (
rolemenu []models.RoleMenu
ids []int64
)
rolemenu, err = models.GetRoleMenuByRole(roleid)
var routerPermission = map[string]PermissionBase{
"/v1/department/list": PermissionBase{CodeName: "OPPMG_DEPARTMENT", ActionName: "check"},
"/v1/department/add": PermissionBase{CodeName: "OPPMG_DEPARTMENT", ActionName: "add"},
"/v1/department/edit": PermissionBase{CodeName: "OPPMG_DEPARTMENT", ActionName: "edit"},
"/v1/department/delete": PermissionBase{CodeName: "OPPMG_DEPARTMENT", ActionName: "delete"},
}
var permissionObject = map[string]interface{}{
"": 0,
}
//模块编号
const (
MENU_DEPARTMENT string = "OPPMG_DEPARTMENT" //公司部门管理模块
MENU_POSITION string = "OPPMG_POSITION" //公司职务管理
MENU_RBAC string = "OPPMG_RBAC" //员工角色/权限设置
MENU_USER string = "OPPMG_USER" //公司员工管理
MENU_ENTERPRISE_BASIC string = "OPPMG_ENTERPRISE_BASIC" //企业基础设置(大节点)
MENU_SYSTEM_FUNCTION string = "OPPMG_SYSTEM_FUNCTION" //系统功能(大节点)
MENU_CONMPANY string = "OPPMG_CONMPANY" //企业信息维护
MENU_CHANCE_TEMP string = "OPPMG_CHANCE_TEMP" //机会模板管理
MENU_SORCE string = "OPPMG_SORCE" //评分模式
MENU_CHANCE string = "OPPMG_CHANCE" //机会管理
)
type PermissionContentObject interface {
StringUnmarshal(string) error
ObjectMarshal() (string, error)
}
type PermissionContentBase struct {
Check int8 `json:"check"`
}
func NewPermissionContentBase() PermissionContentObject {
return &PermissionContentBase{}
}
func (p *PermissionContentBase) StringUnmarshal(s string) error {
err := json.Unmarshal([]byte(s), p)
return err
}
func (p *PermissionContentBase) ObjectMarshal() (string, error) {
bt, err := json.Marshal(p)
if err != nil {
log.Error("获取角色下的菜单数据失败:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
for _, v := range rolemenu {
ids = append(ids, v.MenuId)
return "", err
}
data := &protocol.ResponseRoleMenus{
RoleId: roleData.Id,
RoleName: roleData.Name,
MenuId: ids,
}
return data, nil
return string(bt), err
}
func RoleMenuEdit(companyid int64, roleid int64, menuids []int64) {
type CodeToObject func() PermissionContentObject
var CodePermissionObject = map[string]CodeToObject{
MENU_DEPARTMENT: NewPermissionContentBase, //公司部门管理模块
MENU_POSITION: NewPermissionContentBase, //公司职务管理
MENU_RBAC: NewPermissionContentBase, //员工角色/权限设置
MENU_USER: NewPermissionContentBase, //公司员工管理
MENU_ENTERPRISE_BASIC: NewPermissionContentBase, //企业基础设置(大节点)
MENU_SYSTEM_FUNCTION: NewPermissionContentBase, //系统功能(大节点)
MENU_CONMPANY: NewPermissionContentBase, //企业信息维护
MENU_CHANCE_TEMP: NewPermissionContentBase, //机会模板管理
MENU_SORCE: NewPermissionContentBase, //评分模式
MENU_CHANCE: NewPermissionContentBase, //机会管理
}
... ...
... ... @@ -70,6 +70,7 @@ token 的响应内容
```json
{
"token":""
"code": 0,
"msg": "成功",
"data": {
... ... @@ -147,7 +148,6 @@ token 的响应内容
## 公司部门管理
### 部门列表
- 请求路径:/v1/department/list
... ... @@ -155,43 +155,45 @@ token 的响应内容
- 请求 json:
```json
{
}
{}
```
- 正常响应 json
```json
{
"code": 0,
"msg": "成功",
"data": {
"list": [
{
"id": 1,
"company_id": 1,
"name": "部门1",
"parant_id": 0,
"manages": [{
"id":1,
"name":"主管"
}],
"member": 1
},
{
"id": 2,
"company_id": 1,
"name": "部门1-2",
"parant_id": 1,
"manages": [{
"id":1,
"name":"主管"
}],
"member": 1
}
]
}
"code": 0,
"msg": "成功",
"data": {
"lists": [
{
"id": 1,
"company_id": 1,
"name": "部门1",
"parant_id": 0,
"manages": [
{
"id": 1,
"name": "主管"
}
],
"member": 1
},
{
"id": 2,
"company_id": 1,
"name": "部门1-2",
"parant_id": 1,
"manages": [
{
"id": 1,
"name": "主管"
}
],
"member": 1
}
]
}
}
```
... ... @@ -199,9 +201,6 @@ token 的响应内容
---
### 添加部门
- 请求路径:/v1/department/add
... ... @@ -289,7 +288,6 @@ token 的响应内容
## 公司职位管理
### 职位列表
- 请求路径:/v1/position/list
... ... @@ -297,64 +295,62 @@ token 的响应内容
- 请求 json:
```json
{
}
{}
```
- 响应 json
```json
{
"code": 0,
"msg": "成功",
"data": [
{
"id": 1,
"name": "董事长",
"parent_id": 0
},
{
"id": 2,
"name": "总经理",
"parent_id": 1
},
{
"id": 3,
"name": "副总经理",
"parent_id": 2
},
{
"id": 4,
"name": "技术总监",
"parent_id": 2
},
{
"id": 5,
"name": "职位a",
"parent_id": 0
},
{
"id": 6,
"name": "职位b-1",
"parent_id": 9
},
{
"id": 7,
"name": "职位a-2",
"parent_id": 5
},
{
"id": 8,
"name": "职位a-1-1",
"parent_id": 6
},
{
"id": 9,
"name": "职位b-1",
"parent_id": 0
}
]
"code": 0,
"msg": "成功",
"data": [
{
"id": 1,
"name": "董事长",
"parent_id": 0
},
{
"id": 2,
"name": "总经理",
"parent_id": 1
},
{
"id": 3,
"name": "副总经理",
"parent_id": 2
},
{
"id": 4,
"name": "技术总监",
"parent_id": 2
},
{
"id": 5,
"name": "职位a",
"parent_id": 0
},
{
"id": 6,
"name": "职位b-1",
"parent_id": 9
},
{
"id": 7,
"name": "职位a-2",
"parent_id": 5
},
{
"id": 8,
"name": "职位a-1-1",
"parent_id": 6
},
{
"id": 9,
"name": "职位b-1",
"parent_id": 0
}
]
}
```
... ... @@ -448,6 +444,7 @@ token 的响应内容
---
## 公司员工角色
### 角色列表
- 请求路径:/v1/rbac/role/list
... ... @@ -455,113 +452,110 @@ token 的响应内容
- 请求 json:
```json
{
}
{}
```
- 响应 json
```json
{
"code": 0,
"msg": "成功",
"data": {
"list": [
{
"id": 1,
"company_id": 1,
"name": "部门1",
"parant_id": 0,
"manages": [],
"members": [
{
"id": 1,
"name": "Jennifer Clark"
}
],
"member": 1
},
{
"id": 2,
"company_id": 1,
"name": "部门1-2",
"parant_id": 1,
"manages": [],
"members": [],
"member": 1
},
{
"id": 3,
"company_id": 1,
"name": "部门1-2-3",
"parant_id": 2,
"manages": [],
"members": [],
"member": 0
},
{
"id": 4,
"company_id": 1,
"name": "部门1-2-4",
"parant_id": 2,
"manages": [],
"members": [],
"member": 0
},
{
"id": 5,
"company_id": 1,
"name": "部门1-2-5",
"parant_id": 3,
"manages": [],
"members": [],
"member": 0
},
{
"id": 7,
"company_id": 1,
"name": "部门1-2-5-7",
"parant_id": 5,
"manages": [],
"members": [],
"member": 0
},
{
"id": 10,
"company_id": 1,
"name": "部门2",
"parant_id": 0,
"manages": [],
"members": [],
"member": 0
},
{
"id": 26,
"company_id": 1,
"name": "部门名称1",
"parant_id": 1,
"manages": [
{
"id": 1,
"name": "Jennifer Clark"
}
],
"members": [],
"member": 0
},
{
"id": 27,
"company_id": 1,
"name": "部门名称1",
"parant_id": 1,
"manages": [],
"members": [],
"member": 0
}
]
}
"code": 0,
"msg": "成功",
"data": {
"list": [
{
"id": 1,
"company_id": 1,
"name": "部门1",
"parant_id": 0,
"manages": [],
"members": [
{
"id": 1,
"name": "Jennifer Clark"
}
],
"member": 1
},
{
"id": 2,
"company_id": 1,
"name": "部门1-2",
"parant_id": 1,
"manages": [],
"members": [],
"member": 1
},
{
"id": 3,
"company_id": 1,
"name": "部门1-2-3",
"parant_id": 2,
"manages": [],
"members": [],
"member": 0
},
{
"id": 4,
"company_id": 1,
"name": "部门1-2-4",
"parant_id": 2,
"manages": [],
"members": [],
"member": 0
},
{
"id": 5,
"company_id": 1,
"name": "部门1-2-5",
"parant_id": 3,
"manages": [],
"members": [],
"member": 0
},
{
"id": 7,
"company_id": 1,
"name": "部门1-2-5-7",
"parant_id": 5,
"manages": [],
"members": [],
"member": 0
},
{
"id": 10,
"company_id": 1,
"name": "部门2",
"parant_id": 0,
"manages": [],
"members": [],
"member": 0
},
{
"id": 26,
"company_id": 1,
"name": "部门名称1",
"parant_id": 1,
"manages": [
{
"id": 1,
"name": "Jennifer Clark"
}
],
"members": [],
"member": 0
},
{
"id": 27,
"company_id": 1,
"name": "部门名称1",
"parant_id": 1,
"manages": [],
"members": [],
"member": 0
}
]
}
}
```
... ... @@ -569,9 +563,6 @@ token 的响应内容
---
### 添加角色
- 请求路径:/v1/rbac/role/add
... ...