void checkWrite(OnlineMgr.OnlineInfo info)
 {
     if (!isCanWrite(info))
     {
         throw new InvalidCastException("无操作权限。");
     }
 }
Exemple #2
0
 public string ToJsonFromUser(OnlineMgr.OnlineInfo onlineInfo)
 {
     if (onlineInfo == null || onlineInfo.Level > 2)
     {
         return("{}");
     }
     return(ToJson(Fence));
     //if (onlineInfo.UserName == "admin" || onlineInfo.Level == 1)
     //    return ToJson();
     //return JsonConvert.SerializeObject(filterByUser(onlineInfo), Formatting.Indented);
 }
 bool isCanWrite(OnlineMgr.OnlineInfo info)
 {
     return(info != null && info.UserName == "system");
 }
 bool isCanRead(OnlineMgr.OnlineInfo info)
 {
     return(info != null && (info.UserName == "system" || info.UserName == "admin"));
 }
        private void updateMessage(HttpListenerRequest hRequest, StreamWriter writer, string key, ref OnlineMgr.OnlineInfo onlineInfo, HttpRequestInfo reqInfo, System.Collections.Specialized.NameValueCollection headers)
        {
            //if (DateTime.Now > new DateTime(2021, 08, 01))
            //{
            //    writer.WriteLine("error,试用已到期.");
            //    return;
            //}
            if (reqInfo.Sort != "ship")
            {
                Console.WriteLine(reqInfo.Sort);
            }
            switch (reqInfo.Sort)
            {
                #region 用户登录、退出及用户操作
            case "login":
                if (headers.AllKeys.Any(_ => _ == "user") && headers.AllKeys.Any(_ => _ == "pass"))
                {
                    key = OnlineMgr.Instance.Login(headers["user"], headers["pass"]);
                }
                else
                {
                    throw new InvalidCastException("未找到登录的用户名或密码。");
                }
                if (string.IsNullOrEmpty(key))
                {
                    throw new InvalidCastException("用户名或密码错误。");
                }
                onlineInfo = OnlineMgr.Instance.GetOnlineInfo(key);
                UserInfo userInfo = UserMgr.Instance.Get(onlineInfo.UserName);
                writer.WriteLine($"{key},{userInfo.Longitude},{userInfo.Latitude},{userInfo.Scale},{onlineInfo.Level}");
                break;

            case "logout":
                OnlineMgr.Instance.Logout(key);
                onlineInfo = null;
                writer.WriteLine("seccess");
                break;

            case "user":
                writer.WriteLine(JsonConvert.SerializeObject(onlineInfo, Formatting.Indented));
                break;

            case "users":
                checkRead(onlineInfo);
                writer.WriteLine(UserMgr.Instance.ToJsonSafe());
                break;

            case "changepass":
                if (onlineInfo == null)
                {
                    throw new InvalidCastException("当前未登录或登录已过期,请退出重新登录.");
                }
                if (!headers.AllKeys.Any(_ => _ == "pass"))
                {
                    throw new InvalidCastException("未找到新密码,修改失败.");
                }
                UserMgr.Instance.ChangePass(onlineInfo.UserName, headers["pass"]);
                writer.WriteLine("seccess");
                break;

            case "adduser":
                checkWrite(onlineInfo);
                addUser(headers);
                writer.WriteLine("seccess");
                break;

            case "updateuser":
                checkWrite(onlineInfo);
                updateUser(headers);
                writer.WriteLine("seccess");
                break;

            case "deleteuser":
                checkWrite(onlineInfo);
                int count = deleteUser(reqInfo.Source);
                writer.WriteLine($"seccess,成功删除 {count} 个用户。");
                break;

                #endregion 用户登录、退出及用户操作
                #region 围栏信息查询及增删改操作
            case "searchall":
                writer.WriteLine(FenceMgr.Instance.ToJsonFromUser(onlineInfo));
                break;

            case "gate":
                checkRead(onlineInfo);
                writer.WriteLine(FenceMgr.Instance.Read(getIds(reqInfo), FenceNum.Gate));
                break;

            case "region":
                checkRead(onlineInfo);
                writer.WriteLine(FenceMgr.Instance.Read(getIds(reqInfo), FenceNum.Region));
                break;

            case "gatebridge":
                checkRead(onlineInfo);
                writer.WriteLine(FenceMgr.Instance.Read(getIds(reqInfo), FenceNum.BridgeGate));
                break;

            case "regionbridge":
                checkRead(onlineInfo);
                writer.WriteLine(FenceMgr.Instance.Read(getIds(reqInfo), FenceNum.BridgeRegion));
                break;

            case "setregion":
                checkWrite(onlineInfo);
                FenceMgr.Instance.Set <FenceRegionsInfo>(hRequest.InputStream);
                break;

            case "setgate":
                checkWrite(onlineInfo);
                FenceMgr.Instance.Set <GateInfo>(hRequest.InputStream);
                break;

            case "deletegate":
            case "deleteregion":
                checkWrite(onlineInfo);
                settingFence(reqInfo);
                writer.WriteLine("seccess");
                break;

            case "setbridge":
            case "deletebridge":
                checkRead(onlineInfo);
                settingFence(reqInfo);
                writer.WriteLine("seccess");
                break;

                #endregion 围栏信息查询及增删改操作
            case "ship":
                writeShipInfo(writer, reqInfo.Source);
                break;

            case "shield":
                checkWrite(onlineInfo);
                writeShields(writer, reqInfo.Source);
                break;

            default:
                writer.WriteLine("error,不支持的消息字段.");
                break;
            }
        }