|
@@ -0,0 +1,357 @@
|
|
|
+package data_manage_permission
|
|
|
+
|
|
|
+import (
|
|
|
+ "eta/eta_api/utils"
|
|
|
+ "fmt"
|
|
|
+ "github.com/beego/beego/v2/client/orm"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+type ExcelInfoPermission struct {
|
|
|
+ ExcelInfoPermissionId int64 `json:"excel_info_permission_id" orm:"column(excel_info_permission_id);pk"`
|
|
|
+ ExcelInfoId int32 `json:"excel_info_id"` // ETA表格id
|
|
|
+ Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
|
|
|
+ SysUserId int32 `json:"sys_user_id"` // 系统用户id
|
|
|
+ ModifyTime time.Time `json:"modify_time"` // 变更时间
|
|
|
+ CreateTime time.Time `json:"create_time"` // 关系建立时间
|
|
|
+}
|
|
|
+
|
|
|
+type ExcelClassifyPermission struct {
|
|
|
+ ExcelClassifyPermissionId int64 `json:"excel_classify_permission_id" orm:"column(excel_classify_permission_id);pk"`
|
|
|
+ ExcelClassifyId int32 `json:"excel_classify_id"` // 分类id
|
|
|
+ Source int32 `json:"source"` // 表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1
|
|
|
+ SysUserId int32 `json:"sys_user_id"` // 系统用户id
|
|
|
+ ModifyTime time.Time `json:"modify_time"` // 变更时间
|
|
|
+ CreateTime time.Time `json:"create_time"` // 关系建立时间
|
|
|
+}
|
|
|
+
|
|
|
+// SetIsPermissionByExcelClassifyIdList
|
|
|
+// @Description: 设置表格分类是否涉密
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-04-01 10:53:23
|
|
|
+// @param classifyIdList []int
|
|
|
+// @param excelClassifySource int
|
|
|
+// @return err error
|
|
|
+func SetIsPermissionByExcelClassifyIdList(classifyIdList []int, excelClassifySource int) (err error) {
|
|
|
+ num := len(classifyIdList)
|
|
|
+ if num <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ o, err := orm.NewOrmUsingDB("data").Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = o.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ //// 获取已经配置涉密的分类权限
|
|
|
+ //excelClassifyList := make([]*ExcelClassify, 0)
|
|
|
+ //sql := `SELECT * FROM excel_classify WHERE is_join_permission = ? `
|
|
|
+ //_, err = o.Raw(sql, 1).QueryRows(&excelClassifyList)
|
|
|
+ //if err != nil {
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ //excelClassifyMap := make(map[int]*ExcelClassify)
|
|
|
+ //for _, v := range excelClassifyList {
|
|
|
+ // excelClassifyMap[v.ClassifyId] = v
|
|
|
+ //}
|
|
|
+
|
|
|
+ // 先将所有已经设置了涉密的分类设置为不涉密
|
|
|
+ sql := `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE is_join_permission = 1 AND source = ?`
|
|
|
+ _, err = o.Raw(sql, 0, excelClassifySource).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(classifyIdList) > 0 {
|
|
|
+ // 将对应的分类设置为涉密
|
|
|
+ sql = `UPDATE excel_classify SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_classify_id in (` + utils.GetOrmInReplace(num) + `) `
|
|
|
+ _, err = o.Raw(sql, 1, excelClassifySource, classifyIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO 判断是否要记录移除的分类,用于发送通知给客户
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// SetPermissionByExcelIdList
|
|
|
+// @Description: 根据表格ID列表设置表格的用户权限
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-04-01 10:26:17
|
|
|
+// @param excelIdList []string
|
|
|
+// @param userIdList []int
|
|
|
+// @param source int
|
|
|
+// @return err error
|
|
|
+func SetPermissionByExcelIdList(excelIdList []string, userIdList []int, source int) (err error) {
|
|
|
+ excelNum := len(excelIdList)
|
|
|
+ if excelNum <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ o, err := orm.NewOrmUsingDB("data").Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = o.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 获取已经配置的表格权限用户
|
|
|
+ excelInfoPermissionList := make([]*ExcelInfoPermission, 0)
|
|
|
+ sql := `SELECT * FROM excel_info_permission WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
|
|
|
+ _, err = o.Raw(sql, source, excelIdList).QueryRows(&excelInfoPermissionList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ excelInfoPermissionMap := make(map[string]*ExcelInfoPermission)
|
|
|
+ for _, v := range excelInfoPermissionList {
|
|
|
+ excelInfoPermissionMap[fmt.Sprint(v.ExcelInfoId, "_", v.SysUserId)] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ // 标记表格是否纳入权限管控
|
|
|
+ {
|
|
|
+ // 默认 标记表格为纳入权限管控
|
|
|
+ isJoinPermission := 1
|
|
|
+ // 用户不选的情况下,说明是要给这些表格移除权限管控
|
|
|
+ if len(userIdList) <= 0 {
|
|
|
+ // 标记表格为不纳入权限管控
|
|
|
+ isJoinPermission = 0
|
|
|
+ }
|
|
|
+ sql = `UPDATE excel_info SET is_join_permission=?,modify_time=now() WHERE source = ? AND excel_info_id in (` + utils.GetOrmInReplace(excelNum) + `) `
|
|
|
+ _, err = o.Raw(sql, isJoinPermission, source, excelIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 待添加的配置项
|
|
|
+ addList := make([]*ExcelInfoPermission, 0)
|
|
|
+
|
|
|
+ // 遍历待配置的表格和用户,筛选出需要添加的配置项
|
|
|
+ for _, excelInfoIdStr := range excelIdList {
|
|
|
+ excelInfoId, tmpErr := strconv.ParseInt(excelInfoIdStr, 10, 64)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, userId := range userIdList {
|
|
|
+ key := fmt.Sprint(excelInfoId, "_", userId)
|
|
|
+ if _, ok := excelInfoPermissionMap[key]; ok {
|
|
|
+ // 如果存在那么就移除,说明不需要处理了
|
|
|
+ delete(excelInfoPermissionMap, key)
|
|
|
+ } else {
|
|
|
+ // 如果不存在,那么就提那家
|
|
|
+ addList = append(addList, &ExcelInfoPermission{
|
|
|
+ //PermissionId: 0,
|
|
|
+ ExcelInfoId: int32(excelInfoId),
|
|
|
+ SysUserId: int32(userId),
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加待配置项
|
|
|
+ if len(addList) > 0 {
|
|
|
+ _, err = o.InsertMulti(500, addList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除废弃的配置项
|
|
|
+ {
|
|
|
+ // 待移除的配置项
|
|
|
+ deletePermissionIdList := make([]int64, 0)
|
|
|
+ for _, v := range excelInfoPermissionMap {
|
|
|
+ deletePermissionIdList = append(deletePermissionIdList, v.ExcelInfoPermissionId)
|
|
|
+ }
|
|
|
+
|
|
|
+ deletePermissionIdNum := len(deletePermissionIdList)
|
|
|
+ if deletePermissionIdNum > 0 {
|
|
|
+ sql = "DELETE FROM excel_info_permission WHERE excel_info_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
|
|
|
+ _, err = o.Raw(sql, deletePermissionIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// SetPermissionByExcelClassifyIdList
|
|
|
+// @Description: 根据表格分类ID列表设置分类的用户权限
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-03-28 14:53:04
|
|
|
+// @param classifyIdList []int
|
|
|
+// @param userIdList []int
|
|
|
+// @return err error
|
|
|
+func SetPermissionByExcelClassifyIdList(classifyIdList []int, userIdList []int, classifyType int) (err error) {
|
|
|
+ userNum := len(userIdList)
|
|
|
+ if userNum <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ o, err := orm.NewOrmUsingDB("data").Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ _ = o.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = o.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 获取当前选择用户已经配置的表格分类权限
|
|
|
+ classifyPermissionList := make([]*ExcelClassifyPermission, 0)
|
|
|
+ sql := `SELECT * FROM excel_classify_permission WHERE source = ? AND sys_user_id in (` + utils.GetOrmInReplace(userNum) + `) `
|
|
|
+ _, err = o.Raw(sql, classifyType, userIdList).QueryRows(&classifyPermissionList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ classifyPermissionMap := make(map[string]*ExcelClassifyPermission)
|
|
|
+ for _, v := range classifyPermissionList {
|
|
|
+ classifyPermissionMap[fmt.Sprint(v.ExcelClassifyId, "_", v.SysUserId)] = v
|
|
|
+ }
|
|
|
+
|
|
|
+ // 待添加的配置项
|
|
|
+ addList := make([]*ExcelClassifyPermission, 0)
|
|
|
+
|
|
|
+ // 遍历待配置的表格和用户,筛选出需要添加的配置项
|
|
|
+ for _, userId := range userIdList {
|
|
|
+ for _, classifyId := range classifyIdList {
|
|
|
+ key := fmt.Sprint(classifyId, "_", userId)
|
|
|
+ if _, ok := classifyPermissionMap[key]; ok {
|
|
|
+ // 如果存在那么就移除,说明不需要处理了
|
|
|
+ delete(classifyPermissionMap, key)
|
|
|
+ } else {
|
|
|
+ // 如果不存在,那么就提那家
|
|
|
+ addList = append(addList, &ExcelClassifyPermission{
|
|
|
+ //PermissionId: 0,
|
|
|
+ ExcelClassifyId: int32(classifyId),
|
|
|
+ Source: int32(classifyType),
|
|
|
+ SysUserId: int32(userId),
|
|
|
+ ModifyTime: time.Now(),
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加待配置项
|
|
|
+ if len(addList) > 0 {
|
|
|
+ _, err = o.InsertMulti(500, addList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除废弃的配置项
|
|
|
+ {
|
|
|
+ // 获取移除的配置项
|
|
|
+ deletePermissionIdList := make([]int64, 0)
|
|
|
+ for _, v := range classifyPermissionMap {
|
|
|
+ deletePermissionIdList = append(deletePermissionIdList, v.ExcelClassifyPermissionId)
|
|
|
+ }
|
|
|
+
|
|
|
+ deletePermissionIdNum := len(deletePermissionIdList)
|
|
|
+ if deletePermissionIdNum > 0 {
|
|
|
+ sql = "DELETE FROM excel_classify_permission WHERE excel_classify_permission_id in (" + utils.GetOrmInReplace(deletePermissionIdNum) + ")"
|
|
|
+ _, err = o.Raw(sql, deletePermissionIdList).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetPermissionExcelClassifyIdListByUserId
|
|
|
+// @Description: 根据用户ID获取已经配置的分类id列表
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-03-29 16:24:46
|
|
|
+// @param userId int
|
|
|
+// @param classifyType int
|
|
|
+// @return excelClassifyIdList []int
|
|
|
+// @return err error
|
|
|
+func GetPermissionExcelClassifyIdListByUserId(userId int, classifyType int) (excelClassifyIdList []int, err error) {
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE source = ? AND sys_user_id = ? `
|
|
|
+ _, err = o.Raw(sql, classifyType, userId).QueryRows(&excelClassifyIdList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetPermissionExcelIdListByDataId
|
|
|
+// @Description: 根据资产(表格、表格、表格)ID获取已经配置的用户id列表
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-03-29 16:24:46
|
|
|
+// @param dataId int
|
|
|
+// @param source int
|
|
|
+// @return excelIdList []int
|
|
|
+// @return err error
|
|
|
+func GetPermissionExcelIdListByDataId(dataId int, source int) (excelIdList []int, err error) {
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT sys_user_id FROM excel_info_permission WHERE source = ? AND excel_info_id= ? `
|
|
|
+ _, err = o.Raw(sql, source, dataId).QueryRows(&excelIdList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetPermissionExcelIdList
|
|
|
+// @Description: 获取用户权限的表格列表
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-03-28 16:50:47
|
|
|
+// @param userId int
|
|
|
+// @param excelInfoId int
|
|
|
+// @return idList []int
|
|
|
+// @return err error
|
|
|
+func GetPermissionExcelIdList(userId, excelInfoId int) (idList []int, err error) {
|
|
|
+ pars := []interface{}{userId}
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT excel_info_id FROM excel_info_permission WHERE sys_user_id = ? `
|
|
|
+ if excelInfoId > 0 {
|
|
|
+ sql += ` AND excel_info_id = ? `
|
|
|
+ pars = append(pars, excelInfoId)
|
|
|
+ }
|
|
|
+ _, err = o.Raw(sql, pars).QueryRows(&idList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetPermissionExcelClassifyIdList
|
|
|
+// @Description: 获取用户权限的表格分类列表
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-03-28 16:50:47
|
|
|
+// @param userId int
|
|
|
+// @param classifyId int
|
|
|
+// @return idList []int
|
|
|
+// @return err error
|
|
|
+func GetPermissionExcelClassifyIdList(userId, classifyId int) (idList []int, err error) {
|
|
|
+ pars := []interface{}{userId}
|
|
|
+ o := orm.NewOrmUsingDB("data")
|
|
|
+ sql := `SELECT excel_classify_id FROM excel_classify_permission WHERE sys_user_id = ? `
|
|
|
+ if classifyId > 0 {
|
|
|
+ sql += ` AND excel_classify_id = ? `
|
|
|
+ pars = append(pars, classifyId)
|
|
|
+ }
|
|
|
+ _, err = o.Raw(sql, pars).QueryRows(&idList)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|