Пример #1
0
 public ResponseBase ExportMineWork([FromBody] ExportMineWorkRequest request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput("参数错误"));
     }
     try {
         IUserMineWork mineWork = WebApiRoot.MineWorkSet.GetById(request.MineWorkId);
         if (mineWork == null)
         {
             return(ResponseBase.NotExist());
         }
         if (mineWork.LoginName != User.LoginName)
         {
             return(ResponseBase.Forbidden("无权操作"));
         }
         string localJsonFileFullName  = SpecialPath.GetMineWorkLocalJsonFileFullName(request.MineWorkId);
         string serverJsonFileFullName = SpecialPath.GetMineWorkServerJsonFileFullName(request.MineWorkId);
         File.WriteAllText(localJsonFileFullName, request.LocalJson);
         File.WriteAllText(serverJsonFileFullName, request.ServerJson);
         return(ResponseBase.Ok());
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError <ResponseBase>(e.Message));
     }
 }
Пример #2
0
 public DataResponse <string> GetLocalJson([FromBody] DataRequest <Guid> request)
 {
     if (request == null)
     {
         return(ResponseBase.InvalidInput <DataResponse <string> >("参数错误"));
     }
     try {
         IUserMineWork mineWork = WebApiRoot.MineWorkSet.GetById(request.Data);
         if (mineWork == null)
         {
             return(ResponseBase.NotExist <DataResponse <string> >());
         }
         if (!User.IsAdmin() && mineWork.LoginName != User.LoginName)
         {
             return(ResponseBase.Forbidden <DataResponse <string> >("无权操作"));
         }
         string localJsonFileFullName = SpecialPath.GetMineWorkLocalJsonFileFullName(request.Data);
         string data = string.Empty;
         if (File.Exists(localJsonFileFullName))
         {
             data = File.ReadAllText(localJsonFileFullName);
         }
         return(DataResponse <string> .Ok(data));
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(ResponseBase.ServerError <DataResponse <string> >(e.Message));
     }
 }
Пример #3
0
        public GetWorkJsonResponse GetWorkJson([FromBody] GetWorkJsonRequest request)
        {
            if (request == null)
            {
                return(ResponseBase.InvalidInput <GetWorkJsonResponse>("参数错误"));
            }
            try {
                string workerName = string.Empty;
                // 如果是单机作业
                if (request.WorkId.IsSelfMineWorkId())
                {
                    var clientData = WebApiRoot.ClientDataSet.GetByClientId(request.ClientId);
                    if (clientData != null)
                    {
                        workerName = clientData.WorkerName;
                    }
                    return(GetWorkJsonResponse.Ok(string.Empty, string.Empty, workerName));
                }

                IUserMineWork mineWork = WebApiRoot.MineWorkSet.GetById(request.WorkId);
                if (mineWork == null)
                {
                    return(ResponseBase.NotExist <GetWorkJsonResponse>());
                }
                string localJsonFileFullName = SpecialPath.GetMineWorkLocalJsonFileFullName(request.WorkId);
                string localJson             = string.Empty;
                if (File.Exists(localJsonFileFullName))
                {
                    localJson = File.ReadAllText(localJsonFileFullName);
                    if (!string.IsNullOrEmpty(localJson))
                    {
                        var clientData = WebApiRoot.ClientDataSet.GetByClientId(request.ClientId);
                        if (clientData != null)
                        {
                            workerName = clientData.WorkerName;
                        }
                        localJson = localJson.Replace(NTKeyword.MinerNameParameterName, workerName);
                    }
                }
                string serverJsonFileFullName = SpecialPath.GetMineWorkServerJsonFileFullName(request.WorkId);
                string serverJson             = string.Empty;
                if (File.Exists(serverJsonFileFullName))
                {
                    serverJson = File.ReadAllText(serverJsonFileFullName);
                }
                return(GetWorkJsonResponse.Ok(localJson, serverJson, workerName));
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
                return(ResponseBase.ServerError <GetWorkJsonResponse>(e.Message));
            }
        }
Пример #4
0
        protected bool IsValidAdmin <TResponse>(ISignableData data, out TResponse response, out UserData user) where TResponse : ResponseBase, new()
        {
            user = null;
            if (!WebApiRoot.UserSet.IsReadied)
            {
                string message = "服务器用户集启动中,请稍后";
                response = ResponseBase.NotExist <TResponse>(message);
                return(false);
            }
            ClientSignData query = ClientSign;

            if (!Timestamp.IsInTime(query.Timestamp))
            {
                response = ResponseBase.Expired <TResponse>();
                return(false);
            }
            if (!string.IsNullOrEmpty(query.LoginName))
            {
                user = WebApiRoot.UserSet.GetUser(query.UserId);
            }
            if (user == null && !string.IsNullOrEmpty(query.LoginName))
            {
                user = WebApiRoot.UserSet.GetUser(query.UserId);
            }
            if (user == null)
            {
                string message = "用户不存在";
                response = ResponseBase.NotExist <TResponse>(message);
                return(false);
            }
            else if (!user.IsAdmin())
            {
                string message = "对不起,您不是超管";
                response = ResponseBase.NotExist <TResponse>(message);
                return(false);
            }
            string mySign = RpcUser.CalcSign(user.LoginName, user.Password, query.Timestamp, data);

            if (query.Sign != mySign)
            {
                string message = "登录名或密码错误";
                response = ResponseBase.Forbidden <TResponse>(message);
                Write.DevDebug(() => $"{message} sign:{query.Sign} mySign:{mySign}");
                return(false);
            }
            response = null;
            return(true);
        }
Пример #5
0
        private static bool IsValidUser(
            ClientSignData clientSign, ISignableData data, bool isLoginAction,
            out ResponseBase response, out UserData user)
        {
            user = null;
            if (!AppRoot.UserSet.IsReadied)
            {
                string message = "服务器用户集启动中,请稍后";
                response = ResponseBase.NotExist(message);
                return(false);
            }
            if (!Timestamp.IsInTime(clientSign.Timestamp))
            {
                response = ResponseBase.Expired();
                return(false);
            }
            if (!string.IsNullOrEmpty(clientSign.LoginName))
            {
                user = AppRoot.UserSet.GetUser(clientSign.UserId);
            }
            if (user == null)
            {
                string message = "用户不存在";
                response = ResponseBase.NotExist(message);
                return(false);
            }
            if (isLoginAction)
            {
                if (!AppRoot.UserSet.CheckLoginTimes(clientSign.LoginName))
                {
                    response = ResponseBase.Forbidden("对不起,您的尝试太过频繁");
                    return(false);
                }
            }
            string mySign = HashUtil.CalcSign(user.LoginName, user.Password, clientSign.Timestamp, data);

            if (clientSign.Sign != mySign)
            {
                string message = "签名错误:1. 可能因为登录名或密码错误;2. 可能因为软件版本过期需要升级软件。";
                response = ResponseBase.Forbidden(message);
                return(false);
            }
            response = null;
            return(true);
        }
Пример #6
0
        protected bool IsValidUser <TResponse>(ISignableData data, out TResponse response, out UserData user) where TResponse : ResponseBase, new()
        {
            user = null;
            if (!WebApiRoot.UserSet.IsReadied)
            {
                string message = "服务器用户集启动中,请稍后";
                response = ResponseBase.NotExist <TResponse>(message);
                return(false);
            }
            ClientSignData query = ClientSign;

            if (!Timestamp.IsInTime(query.Timestamp))
            {
                response = ResponseBase.Expired <TResponse>();
                return(false);
            }
            // 对于User来说LoginName可以是LoginName、Email、Mobile
            if (!string.IsNullOrEmpty(query.LoginName))
            {
                user = WebApiRoot.UserSet.GetUser(UserId.Create(query.LoginName));
            }
            if (user == null)
            {
                string message = "用户不存在";
                response = ResponseBase.NotExist <TResponse>(message);
                return(false);
            }
            if (user.IsAdmin())
            {
                response = null;
                return(true);
            }
            string mySign = RpcUser.CalcSign(user.LoginName, user.Password, query.Timestamp, data);

            if (query.Sign != mySign)
            {
                string message = "签名错误:1. 可能因为登录名或密码错误;2. 可能因为软件版本过期需要升级软件,请将软件升级到最新版本再试。";
                response = ResponseBase.Forbidden <TResponse>(message);
                return(false);
            }
            response = null;
            return(true);
        }
Пример #7
0
        public ResponseBase DisableUser([FromBody] DataRequest <string> request)
        {
            if (request == null || string.IsNullOrEmpty(request.Data))
            {
                return(ResponseBase.InvalidInput <DataResponse <string> >("参数错误"));
            }
            var user = AppRoot.UserSet.GetUser(UserId.CreateLoginNameUserId(request.Data));

            if (user == null)
            {
                return(ResponseBase.NotExist($"登录名 {request.Data} 不存在"));
            }
            if (user.IsAdmin())
            {
                return(ResponseBase.InvalidInput <DataResponse <string> >("不能操作admin"));
            }
            try {
                AppRoot.UserSet.Disable(request.Data);
                return(ResponseBase.Ok("禁用成功"));
            }
            catch (Exception e) {
                return(ResponseBase.ServerError(e.Message));
            }
        }