作者 唐旭辉

权限进行中。。

... ... @@ -215,11 +215,11 @@ func (c *AuthController) Me() {
}
menus, err := serveauth.GetUserHasMenu(userid, companyid)
if err != nil {
log.Error("获取用户菜单")
log.Error("获取用户菜单失败")
}
companys, err := serveauth.UserHasCompanys(userid)
if err != nil {
log.Error("获取用户的公司")
log.Error("获取用户的公司失败")
}
data := map[string]interface{}{
"user": userinfo,
... ...
... ... @@ -261,18 +261,6 @@ func (c *RbacController) RoleHasMenu() {
return
}
//TODO
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
}
//RoleMenuEdit 设置角色的菜单
//@router /role/menu/edit
func (c *RbacController) RoleMenuEdit() {
... ...
... ... @@ -64,20 +64,6 @@ func AddUserRole(m *UserRole, om ...orm.Ormer) (id int64, err error) {
return
}
func GetUserRole(userid, companyid int64) ([]*UserRole, error) {
o := orm.NewOrm()
var (
err error
result []*UserRole
)
_, err = o.QueryTable(&UserRole{}).
Filter("user_id", userid).
Filter("company_id", companyid).
Filter("enable_status", 1).
All(&result)
return result, err
}
func CountUserRoleByRole(roleid int64) (int64, error) {
var (
cnt int64
... ...
... ... @@ -329,10 +329,50 @@ func UserHasCompanys(userid int64) ([]protocol.MeCompany, error) {
}
func GetUserHasMenu(userid, companyid int64) ([]protocol.PermissionItem, error) {
var (
list = make([]protocol.PermissionItem, 0)
err error
companyinfo *models.Company
ucompany *models.UserCompany
)
companyinfo, err = models.GetCompanyById(companyid)
if companyinfo.AdminId == userid {
return getAdminHasMenu()
}
//非主管人员
ucompany, err = models.GetUserCompanyBy(userid, companyid)
if err != nil {
log.Error("user_company数据获取失败:%s", err)
return list, protocol.NewErrWithMessage("1")
}
const datasql string = `SELECT DISTINCT(a.menu_id)
FROM role_menu AS a
JOIN user_role AS b ON a.role_id = b.role_id
JOIN role AS c ON a.role_id = c.id
WHERE b.user_company_id=? AND c.delete_at =0 `
var menuids []string
err = utils.ExecuteQueryAll(&menuids, datasql, ucompany.Id)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
datasql2 := `SELECT id,name,icon,parent_id,senior_status,sort,code
FROM menu WHERE enabled=1 AND id in(%s) ORDER BY sort`
datasql2 = fmt.Sprintf(datasql2, strings.Join(menuids, ","))
err = utils.ExecuteQueryAll(&list, datasql2)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
return list, nil
}
//主管拥有全部的菜单
func getAdminHasMenu() ([]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
list = make([]protocol.PermissionItem, 0)
err error
)
err = utils.ExecuteQueryAll(&list, datasql)
... ...
package rbac
import (
"encoding/json"
"fmt"
"oppmg/common/log"
"oppmg/models"
... ... @@ -65,46 +66,46 @@ func GetRoleHasMenu(roleid int64, companyid int64) (*protocol.ResponseRoleMenus,
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 GetRoleHasMenuAll(roleid int64, companyid int64) (map[string]PermissionOptionObject, 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]PermissionOptionObject)
// )
// 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 := json.Unmarshal([]byte(v.Opption), obj)
// if err != nil {
// log.Error("解析权限规则失败", err)
// continue
// }
// returnList[v.Code] = obj
// }
// }
// return returnList, nil
// }
func RoleMenuEdit(companyid int64, roleId int64, menuids []int64) error {
roleMenus, err := GetRoleHasMenu(roleId, companyid)
... ... @@ -140,12 +141,13 @@ func RoleMenuEdit(companyid int64, roleId int64, menuids []int64) error {
return protocol.NewErrWithMessage("1")
}
}
opptionbt, _ := json.Marshal(&PermissionOptionBase{Check: 1})
for _, v := range addMenu {
m := models.RoleMenu{
RoleId: roleId,
MenuId: v.Id,
Code: v.Code,
Opption: "{}",
Opption: string(opptionbt),
}
if _, err = o.Insert(&m); err != nil {
log.Error("添加角色和菜单关系失败:%s", err)
... ...
package rbac
import (
"encoding/json"
)
//PermissionBase 路由对应的权限
type PermissionBase struct {
CodeName string //模块标识
ActionName string
MethodMap map[string]func()
}
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 (
M_ENTERPRISE string = "ENTERPRISE" //企业基础设置
... ... @@ -37,56 +15,31 @@ const (
M_SYSTEM_ANNOUNCEMENT string = "SYSTEM_ANNOUNCEMENT" //公告管理
)
type PermissionContentObject interface {
StringUnmarshal(string) error
ObjectMarshal() (string, error)
type PermissionOptionObject interface {
// StringUnmarshal(string) error
// ObjectMarshal() string
GetValidFunc(string) func(UserObject) bool
}
type UserObject struct {
UserId int64 `json:"user_id"`
CompanyId int64 `json:"company_id"`
UserCompanyId int64 `json:"user_company_id"`
}
type PermissionContentBase struct {
Check int8 `json:"check"`
}
func (p *PermissionContentBase) ValidDefault(obj UserObject) bool {
if p.Check == 1 {
return true
}
return false
}
func NewPermissionContentBase() PermissionContentObject {
return &PermissionContentBase{}
}
func (p *PermissionContentBase) StringUnmarshal(s string) error {
err := json.Unmarshal([]byte(s), p)
return err
//PermissionBase 路由对应的权限
type PermissionBase struct {
CodeName string //模块标识
ActionName string
}
func (p *PermissionContentBase) ObjectMarshal() (string, error) {
bt, err := json.Marshal(p)
if err != nil {
return "", err
}
return string(bt), err
}
var routerPermission = map[string]PermissionBase{}
type CodeToObject func() PermissionContentObject
type CodeToObject func() PermissionOptionObject
var CodePermissionObject = map[string]CodeToObject{
M_ENTERPRISE_ORGANIZATION: NewPermissionContentBase, //公司组织架构设置
M_ENTERPRISE_EMPLOYEE_POST: NewPermissionContentBase, //公司职务管理
M_ENTERPRISE_EMPLOYEE_ROLE: NewPermissionContentBase, //员工角色/权限设置
M_ENTERPRISE_EMPLOYEE: NewPermissionContentBase, //公司员工管理
M_ENTERPRISE: NewPermissionContentBase, //企业基础设置(大节点)
M_SYSTEM: NewPermissionContentBase, //系统设置(大节点)
M_ENTERPRISE_PROFILE: NewPermissionContentBase, //企业信息维护
M_SYSTEM_OPPORTUNITY_TEMPLATE: NewPermissionContentBase, //机会模板管理
M_SYSTEM_RATING: NewPermissionContentBase, //评分模式
M_SYSTEM_OPPORTUNITY: NewPermissionContentBase, //机会管理
M_ENTERPRISE_ORGANIZATION: NewPermissionOptionBase, //公司组织架构设置
M_ENTERPRISE_EMPLOYEE_POST: NewPermissionOptionBase, //公司职务管理
M_ENTERPRISE_EMPLOYEE_ROLE: NewPermissionOptionBase, //员工角色/权限设置
M_ENTERPRISE_EMPLOYEE: NewPermissionOptionBase, //公司员工管理
M_ENTERPRISE: NewPermissionOptionBase, //企业基础设置(大节点)
M_SYSTEM: NewPermissionOptionBase, //系统设置(大节点)
M_ENTERPRISE_PROFILE: NewPermissionOptionBase, //企业信息维护
M_SYSTEM_OPPORTUNITY_TEMPLATE: NewPermissionOptionBase, //机会模板管理
M_SYSTEM_RATING: NewPermissionOptionBase, //评分模式
M_SYSTEM_OPPORTUNITY: NewPermissionOptionBase, //机会管理
}
... ...
package rbac
import "encoding/json"
type UserObject struct {
UserId int64 `json:"user_id"`
CompanyId int64 `json:"company_id"`
UserCompanyId int64 `json:"user_company_id"`
}
//PermissionOptionBase 基本权限
type PermissionOptionBase struct {
Check int8 `json:"check"`
}
var (
_ PermissionOptionObject = &PermissionOptionBase{}
)
func NewPermissionOptionBase() PermissionOptionObject {
return &PermissionOptionBase{}
}
func (p *PermissionOptionBase) ValidDefault(obj UserObject) bool {
if p.Check == 1 {
return true
}
return false
}
//GetValidFunc PermissionOptionBase 接口实现
func (p *PermissionOptionBase) GetValidFunc(k string) func(UserObject) bool {
m := map[string]func(UserObject) bool{
"default": p.ValidDefault,
}
if _, ok := m[k]; ok {
return m[k]
}
return nil
}
//StringUnmarshal PermissionOptionBase 接口实现
func (p *PermissionOptionBase) StringUnmarshal(s string) error {
err := json.Unmarshal([]byte(s), p)
return err
}
//ObjectMarshal PermissionOptionBase 接口实现
func (p *PermissionOptionBase) ObjectMarshal() string {
bt, err := json.Marshal(p)
if err != nil {
return ""
}
return string(bt)
}
/*
机会管理模块
CheckOpp
CheckDeparment
OptionOpportunity
*/
//CheckOpp 特殊的查看条件设定
type CheckOpp struct {
Departments []CheckDeparment `json:"departments"`
}
//CheckDeparment 特殊的查看条件设定中关于部门的设定
type CheckDeparment struct {
Id int64 `json:"id"`
Wait int `json:"wail"`
OpenAll int `json:"open_all"`
OpenDepart int `json:"open_depart"`
}
//OptionOpportunity 机会管理 高级权限设置
type OptionOpportunity struct {
Check int `json:"check"`
CheckOption CheckOpp
EditSorce int `json:"edit_sorce"`
EditPublicStatus int `json:"edit_public_status"`
CloseChance int `json:"close_chance"`
}
var (
_ PermissionOptionObject = &OptionOpportunity{}
)
func NewOptionOpportunity() PermissionOptionObject {
return &OptionOpportunity{}
}
func (p *OptionOpportunity) ValidDefault(obj UserObject) bool {
if p.Check == 1 {
return true
}
return false
}
//GetValidFunc PermissionOptionBase 接口实现
func (p *OptionOpportunity) GetValidFunc(k string) func(UserObject) bool {
m := map[string]func(UserObject) bool{
"default": p.ValidDefault,
}
if _, ok := m[k]; ok {
return m[k]
}
return nil
}
//StringUnmarshal PermissionOptionBase 接口实现
func (p *OptionOpportunity) StringUnmarshal(s string) error {
err := json.Unmarshal([]byte(s), p)
return err
}
//ObjectMarshal PermissionOptionBase 接口实现
func (p *OptionOpportunity) ObjectMarshal() string {
bt, err := json.Marshal(p)
if err != nil {
return ""
}
return string(bt)
}
... ...