GB28181/RTSP/Ehome协议视频智能分析平台EasyCVR给用户分配通道后仍显示所有通道问题排查

EasyNVR、EasyGBS等视频智能分析平台在新增了用户管理之后,很多项目团队都反应该功能很实用,因此我们在开发EasyCVR的时候,也决定将该功能添加进去,优化用户体验。

但在我们完成EasyCVR添加用户功能,进入测试阶段的时候,我们发现给新添加的用户分配了一个通道,但是实际登录进去后却显示了所有通道。该问题就是我们目前要调整的内容。

管理员新建用户分配的通道只有第三个:

而该用户登录后进入设备管理列表查看,显示了全部通道:

我们在这个页面查看前端调用的接口:

查看接口代码发现是代码逻辑有误,用户查询设备时没有进行角色区分,所以导致显示不正确。

func (h *APIHandler) GetDevices_V2(c *gin.Context) {
       params := make(map[string]string)
       for k, v := range c.Request.URL.Query() {
              params[strings.ToLower(k)] = strings.Join(v, ",")
       }
       //分页
       if params["start"] != "" && params["limit"] != "" {
              //start, _ := strconv.Atoi(params["start"])
              //limit, _ := strconv.Atoi(params["limit"])
       }
       db.SQLite.LogMode(true)
       ack := NewMsgAck()
       ack.EasyDarwin.Header.MessageType = MSG_SC_SERVER_GET_DEVICES_ACK

       rows := make([]map[string]interface{}, 0)

       deviceService := cvrservice.GetDeviceService()
       dbDevices := make([]cvrdo.Device, 0)
       if id, ok := params["device"]; ok {
              if devID, err := strconv.Atoi(id); err == nil {
                     device := deviceService.GetDevice(uint(devID))
                     dbDevices = append(dbDevices, *device)
              } else {
                     dbDevices = *(deviceService.GetAllDevice())
              }
       } else {
              dbDevices = *(deviceService.GetAllDevice())
       }

所以我们要对代码逻辑稍作修改,首先查询用户所属角色,然后再查询角色拥有的通道,最后检查所有通道,将非拥有的通道过滤掉,这样就能够返回符合条件的数据。以下是我们实现的过程:

sess := sessions.Default(c)
//uid := sess.Get("uid")
uname := sess.Get("uname")
secdefault := utils.Conf().Section("default")
defAdminUser := secdefault.Key("default_admin_user").MustString("easycvr")
defGuestUser := secdefault.Key("default_guest_user").MustString("guest2020")
auth := utils.Conf().Section("base_config").Key("api_auth").MustBool(false)
if uname != defAdminUser && uname != defGuestUser && auth {
       devicearr, err := deviceService.GetDBDeviceByRoleID(uname)
       if err != nil {
              log.Printf("deviceService.GetDBDeviceByRoleID err :%v", err.Error())
              return
       }
       for _, v := range dbDevices {
              var is_cunzai = false
              for _, j := range devicearr {
                     if uint(j) == v.DeviceID {
                            is_cunzai = true
                            break
                     }

              }

              if is_cunzai {
                     tempdbDevices = append(tempdbDevices, v)
              }

       }
       dbDevices = tempdbDevices
}

再次查看通道列表,满足要求,能够正确的显示用户拥有的通道。

EasyCVR视频智能分析平台目前已经在银行自助服务智能监控小区园区视频监控营业厅人脸识别监控等项目发挥作用,如果大家想测试,欢迎联系我们获取测试账号。

原文地址:https://www.cnblogs.com/TSINGSEE/p/14189654.html