作者 tangxvhui

机会列表数据进行权限过滤

@@ -6,6 +6,8 @@ import ( @@ -6,6 +6,8 @@ import (
6 "oppmg/common/log" 6 "oppmg/common/log"
7 "oppmg/models" 7 "oppmg/models"
8 "oppmg/protocol" 8 "oppmg/protocol"
  9 + serverabc "oppmg/services/rbac"
  10 + "oppmg/storage/redisdata"
9 "oppmg/utils" 11 "oppmg/utils"
10 "strings" 12 "strings"
11 "time" 13 "time"
@@ -26,6 +28,105 @@ type SqlData struct { @@ -26,6 +28,105 @@ type SqlData struct {
26 Code string `orm:"column(code)"` 28 Code string `orm:"column(code)"`
27 } 29 }
28 30
  31 +//根据权限获取机会列表
  32 +func buildSqlForAuditList(usercompanyid int64, companyid int64, userid int64) string {
  33 + /*
  34 + OpportunityCheckLv1 int = 1
  35 + OpportunityCheckLv2 int = 2
  36 + OpportunityCheckLv3 int = 3
  37 + OpportunityCheckLv4 int = 4
  38 + */
  39 + var (
  40 + //自己提交的
  41 + sql1 string = fmt.Sprintf(` SELECT id FROM chance WHERE user_id=%d `, usercompanyid)
  42 + //自己可审核的
  43 + sql2 string = fmt.Sprintf(` SELECT a.id FROM chance AS a
  44 + JOIN audit_flow_process AS b ON a.id=b.chance_id
  45 + WHERE b.uid=%d AND a.review_status = 3 `, usercompanyid)
  46 + //公开到自己部门的
  47 + sql3 string = ` SELECT a.id FROM chance AS a
  48 + JOIN chance_department AS b ON a.id=b.chance_id
  49 + WHERE b.department_id IN (%s) AND a.review_status = 3 `
  50 + //指定提交的部门
  51 + sql4 string = ` SELECT id FROM chance WHERE department_id IN (%s) and review_status =3 `
  52 + //全公司公开的
  53 + sql5 string = ` SELECT id FROM chance where publish_status = 1 AND review_status = 3 `
  54 +
  55 + allsql string = ` SELECT t.id FROM (%s) as t `
  56 + unionsql string = ``
  57 + permissionObject serverabc.PermissionOptionObject
  58 + err error
  59 + )
  60 + //获取权限
  61 + if ok := redisdata.ExistUserPermission(userid); !ok {
  62 + //尝试重数据库获取
  63 + permissionMap, err := serverabc.GetUserPermission(usercompanyid, serverabc.M_SYSTEM_OPPORTUNITY)
  64 + if err != nil {
  65 + log.Debug("从数据库未获得对应权限 :%s", err)
  66 + unionsql = sql1 + " UNION " + sql2
  67 + return fmt.Sprintf(allsql, unionsql)
  68 + }
  69 + if v, ok := permissionMap[serverabc.M_SYSTEM_OPPORTUNITY]; !ok {
  70 + unionsql = sql1 + " UNION " + sql2
  71 + return fmt.Sprintf(allsql, unionsql)
  72 + } else {
  73 + permissionObject = v
  74 + }
  75 + } else {
  76 + //使用缓存
  77 + permissionObject, err = redisdata.GetUserPermission(userid, serverabc.M_SYSTEM_OPPORTUNITY)
  78 + if err != nil {
  79 + log.Debug("从缓存未获得对应权限 :%s", err)
  80 + unionsql = sql1 + " UNION " + sql2
  81 + return fmt.Sprintf(allsql, unionsql)
  82 + }
  83 + }
  84 + var (
  85 + usrPermission *serverabc.OptionOpportunity
  86 + ok bool
  87 + //预设仅可以查看自己
  88 + sqlslice = []string{sql1, sql2}
  89 + )
  90 + if usrPermission, ok = permissionObject.(*serverabc.OptionOpportunity); !ok {
  91 + log.Error("*serverabc.OptionOpportunity断言失败")
  92 + unionsql = sql1 + " UNION " + sql2
  93 + return fmt.Sprintf(allsql, unionsql)
  94 + }
  95 + log.Debug("获取到的权限规则:%v", usrPermission)
  96 + //进行权限判定
  97 + if _, ok := usrPermission.CheckMap[serverabc.OpportunityCheckLv4]; ok {
  98 + //查看所有
  99 + log.Debug("命中规则:查看所有")
  100 + return ""
  101 + }
  102 + if _, ok := usrPermission.CheckMap[serverabc.OpportunityCheckLv3]; ok {
  103 + //指定部门
  104 + ids := []string{}
  105 + for _, v := range usrPermission.CheckOption.Departments {
  106 + ids = append(ids, fmt.Sprint(v.Id))
  107 + }
  108 + if len(ids) > 0 {
  109 + log.Debug("命中规则:指定部门")
  110 + sqlslice = append(sqlslice, fmt.Sprintf(sql4, strings.Join(ids, ",")))
  111 + }
  112 + }
  113 + if _, ok := usrPermission.CheckMap[serverabc.OpportunityCheckLv2]; ok {
  114 + //公开到我的部门
  115 + ids := []string{}
  116 + sql6 := `SELECT a.id FROM department AS a WHERE a.id IN (
  117 + SELECT department_id FROM user_department WHERE user_company_id = ? AND enable_status = 1
  118 + ) `
  119 + utils.ExecuteQueryAll(&ids, sql6, usercompanyid)
  120 + if len(ids) > 0 {
  121 + log.Error("命中规则:公开到我的部门")
  122 + sqlslice = append(sqlslice, fmt.Sprintf(sql3, strings.Join(ids, ",")))
  123 + }
  124 + }
  125 + //添加规则全公司公开
  126 + sqlslice = append(sqlslice, sql5)
  127 + return fmt.Sprintf(allsql, strings.Join(sqlslice, " UNION "))
  128 +}
  129 +
29 func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64) (protocol.ResponseAuditList, error) { 130 func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64) (protocol.ResponseAuditList, error) {
30 131
31 var ( 132 var (
@@ -35,7 +136,9 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64 @@ -35,7 +136,9 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64
35 cnt int 136 cnt int
36 err error 137 err error
37 cond []interface{} 138 cond []interface{}
  139 + sqlFromPermission string
38 ) 140 )
  141 +
39 returnData := protocol.ResponseAuditList{ 142 returnData := protocol.ResponseAuditList{
40 ResponsePageInfo: protocol.ResponsePageInfo{ 143 ResponsePageInfo: protocol.ResponsePageInfo{
41 TotalPage: 0, 144 TotalPage: 0,
@@ -43,16 +146,32 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64 @@ -43,16 +146,32 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64
43 }, 146 },
44 List: make([]protocol.RspAuditList, 0), 147 List: make([]protocol.RspAuditList, 0),
45 } 148 }
46 - datasql.WriteString(`SELECT a.id,a.user_id,a.department_id,a.audit_template_id,a.chance_type_id 149 + usercompany, err := models.GetUserCompanyBy(userid, companyid)
  150 + if err != nil {
  151 + log.Error("GetUserCompanyBy(userid, companyid) err:%s", err)
  152 + return returnData, protocol.NewErrWithMessage("1")
  153 + }
  154 + sqlFromPermission = buildSqlForAuditList(usercompany.Id, usercompany.CompanyId, usercompany.UserId)
  155 + s1 := `SELECT a.id,a.user_id,a.department_id,a.audit_template_id,a.chance_type_id
47 ,a.publish_status,a.create_at,a.review_status,a.enable_status 156 ,a.publish_status,a.create_at,a.review_status,a.enable_status
48 ,a.discovery_score,a.comment_total ,a.code,d.nick_name 157 ,a.discovery_score,a.comment_total ,a.code,d.nick_name
49 FROM chance AS a 158 FROM chance AS a
50 JOIN user_company AS c ON c.id = a.user_id 159 JOIN user_company AS c ON c.id = a.user_id
51 JOIN user AS d ON c.user_id = d.id 160 JOIN user AS d ON c.user_id = d.id
52 - where a.company_id=? `)  
53 -  
54 - countsql.WriteString(`SELECT count(*) FROM chance as a  
55 - where a.company_id=? `) 161 + %s
  162 + where a.company_id=? `
  163 + s2 := ` SELECT count(*) FROM chance as a
  164 + JOIN user_company AS c ON c.id = a.user_id
  165 + JOIN user AS d ON c.user_id = d.id
  166 + %s
  167 + where a.company_id=?`
  168 + if len(sqlFromPermission) > 0 {
  169 + temp := fmt.Sprintf(`JOIN (%s) AS tt ON tt.id=a.id`, sqlFromPermission)
  170 + s1 = fmt.Sprintf(s1, temp)
  171 + s2 = fmt.Sprintf(s2, temp)
  172 + }
  173 + datasql.WriteString(s1)
  174 + countsql.WriteString(s2)
56 cond = append(cond, companyid) 175 cond = append(cond, companyid)
57 if param.ChanceTypeId > 0 { 176 if param.ChanceTypeId > 0 {
58 //一级分类过滤 177 //一级分类过滤
@@ -105,16 +105,19 @@ var CodePermissionObject = map[string]CodeToObject{ @@ -105,16 +105,19 @@ var CodePermissionObject = map[string]CodeToObject{
105 M_SYSTEM_ANNOUNCEMENT: NewPermissionOptionBase, //公告管理 105 M_SYSTEM_ANNOUNCEMENT: NewPermissionOptionBase, //公告管理
106 } 106 }
107 107
108 -func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject, error) { 108 +func GetUserPermission(userCompanyid int64, code ...string) (map[string]PermissionOptionObject, error) {
109 type CodeOpptionData struct { 109 type CodeOpptionData struct {
110 Code string `orm:"column(code)"` 110 Code string `orm:"column(code)"`
111 Opption string `orm:"column(opption)"` 111 Opption string `orm:"column(opption)"`
112 } 112 }
113 - const datasql string = `SELECT a.code,a.opption 113 + var datasql string = `SELECT a.code,a.opption
114 FROM role_menu AS a 114 FROM role_menu AS a
115 JOIN user_role AS b ON a.role_id = b.role_id 115 JOIN user_role AS b ON a.role_id = b.role_id
116 JOIN role AS c ON a.role_id = c.id 116 JOIN role AS c ON a.role_id = c.id
117 - WHERE b.user_company_id=? AND c.delete_at =0` 117 + WHERE b.user_company_id=? AND c.delete_at =0 `
  118 + if len(code) > 0 {
  119 + datasql = datasql + fmt.Sprintf(` and a.code = "%s" `, code[0])
  120 + }
118 var ( 121 var (
119 data []CodeOpptionData 122 data []CodeOpptionData
120 err error 123 err error
@@ -146,26 +149,3 @@ func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject, @@ -146,26 +149,3 @@ func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject,
146 } 149 }
147 return objMap, nil 150 return objMap, nil
148 } 151 }
149 -  
150 -// func ValidUserPermission(urlPath string, userid int64, companyid int64) bool {  
151 -// var (  
152 -// err error  
153 -// permissionbase PermissionBase  
154 -// ok bool = false  
155 -// permissionObj PermissionOptionObject  
156 -// )  
157 -// permissionbase, ok = RouterPermission[urlPath]  
158 -// if !ok {  
159 -// return true  
160 -// }  
161 -// permissionObj, err = redisdata.GetUserPermission(userid, permissionbase.CodeName)  
162 -// if err != nil {  
163 -// log.Error("未取到权限数据")  
164 -// return false  
165 -// }  
166 -// ok = permissionObj.GetValidFunc(permissionbase.ActionName)  
167 -// if ok {  
168 -// return true  
169 -// }  
170 -// return false  
171 -// }  
@@ -106,9 +106,9 @@ type OptionOpportunity struct { @@ -106,9 +106,9 @@ type OptionOpportunity struct {
106 /* 106 /*
107 机会管理高级设置中的 check 107 机会管理高级设置中的 check
108 1:禁止查看所有机会:禁止查看所有机会(除自己提交过的机会及可执行审核操作的机会) 108 1:禁止查看所有机会:禁止查看所有机会(除自己提交过的机会及可执行审核操作的机会)
109 -2:仅查看自己部门和公开机会:查看对自己部门公开的机会+公司公开的机会  
110 -3:特定部门的机会:自由配置选定部门的待审核、公司公开、部门公开的机会+查看对自己部门公开的机会  
111 -4:查看所有机会:查看所有部门的待审核机会、公开机会及部门公开机会 109 +2:仅查看自己部门和公开机会:查看对自己所在部门公开的机会+公司公开的机会
  110 +3:特定部门的机会:选定部门提交的公司公开、部门公开的机会;>只选择部门
  111 +4:查看所有机会:查看所有部门的公开机会及部门公开机会;
112 */ 112 */
113 const ( 113 const (
114 OpportunityCheckLv1 int = 1 114 OpportunityCheckLv1 int = 1
@@ -134,3 +134,13 @@ func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject, @@ -134,3 +134,13 @@ func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject,
134 err = json.Unmarshal([]byte(str), permissionObj) 134 err = json.Unmarshal([]byte(str), permissionObj)
135 return permissionObj, err 135 return permissionObj, err
136 } 136 }
  137 +
  138 +func ExistUserPermission(userid int64) bool {
  139 + key := GetKeyUserPermission(userid)
  140 + client := redis.GetRedis()
  141 + value := client.Exists(key).Val()
  142 + if value > 0 {
  143 + return true
  144 + }
  145 + return false
  146 +}