Exemple #1
0
        public override SmtActionResult ActionInvoker(string actionName, Dictionary <string, object> parameter)
        {
            SmtActionResult result = null;

            switch (actionName)
            {
            case ControllerAction.SmtFileBase.GetByID:
                result = GetByID(int.Parse(parameter["id"].ToString()), GetQuery());
                break;

            case ControllerAction.SmtFileBase.Download:
                result = Download(int.Parse(parameter["id"].ToString()), GetQuery());
                break;

            case ControllerAction.SmtFileBase.Add:
                result = Add(parameter["file"] as IFormFile);
                break;

            case ControllerAction.SmtFileBase.Update:
                result = Update(int.Parse(parameter["id"].ToString()), parameter["file"] as IFormFile, GetQuery());
                break;

            case ControllerAction.SmtFileBase.Delete:
                result = Delete(int.Parse(parameter["id"].ToString()), GetQuery());
                break;

            default:
                break;
            }

            return(result);
        }
        private TokenManager.LoginToken CheckLogin(
            string tokenText, HttpContext context,
            string controllerName, string actionName, out SmtActionResult result)
        {
            result = null;
            TokenManager.LoginToken token = null;

            token = TokenManager.LoginToken.VerifyTokenString(tokenText);
            if (token == null)
            {
                result = new SmtActionResult
                {
                    StatusCode  = System.Net.HttpStatusCode.Unauthorized,
                    ResultValue = "VerifyTokenString"
                };
            }
            else if (CheckTokenValid(token, context) == false)
            {
                result = new SmtActionResult
                {
                    StatusCode  = System.Net.HttpStatusCode.Unauthorized,
                    ResultValue = "CheckTokenValid"
                };
            }
            else if (CheckActionPermission(controllerName, actionName, token) == false)
            {
                result = new SmtActionResult
                {
                    StatusCode  = System.Net.HttpStatusCode.Unauthorized,
                    ResultValue = "CheckActionPermission"
                };
            }

            return(token);
        }
        /// <summary>
        /// response type is chosen by client request Headers["response"]:
        ///     "json" -> json text
        ///     "protobuf" -> protobuf binary
        /// </summary>
        /// <param name="resultValue"></param>
        /// <returns></returns>
        protected SmtActionResult CreateObjectResult(
            object resultValue, System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK)
        {
            var result = new SmtActionResult();

            result.ResultType  = SmtActionResult.ActionResultType.Object;
            result.ResultValue = resultValue;
            result.StatusCode  = status;
            return(result);
        }
        /// <summary>
        /// response type text/plain
        /// </summary>
        /// <param name="resultValue"></param>
        /// <returns></returns>
        protected SmtActionResult CreatePlainTextResult(
            object resultValue, System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK)
        {
            var result = new SmtActionResult();

            result.ResultType  = SmtActionResult.ActionResultType.PlainText;
            result.ContentType = "text/plain";
            result.ResultValue = resultValue;
            result.StatusCode  = status;
            return(result);
        }
        /// <summary>
        /// response body is binary stream
        /// </summary>
        /// <param name="resultValue"></param>
        /// <param name="mimeType"></param>
        /// <returns></returns>
        protected SmtActionResult CreateStreamResult(
            Stream resultValue,
            string mimeType)
        {
            var result = new SmtActionResult();

            result.ResultType  = SmtActionResult.ActionResultType.Stream;
            result.ResultValue = resultValue;
            result.ContentType = mimeType;
            result.StatusCode  = System.Net.HttpStatusCode.OK;
            return(result);
        }
        private SmtActionResult RequestExecutor(
            string controller, string action, Dictionary <string, object> parameter,
            HttpContext context, string requestType)
        {
            SmtActionResult result  = null;
            var             request = context.Request;

            try
            {
                if (ControllerAction.Smt.ControllerName == controller &&
                    SmtControllerAnonymousActions.Contains(action) == true)
                {
                    result = ControllerInvoker(controller, action, parameter, requestType, context, null);
                }
                else if (SmtSettings.Instance.AllowAnonymousActions.Contains(string.Format("{0}.{1}", controller, action)))
                {
                    result = ControllerInvoker(controller, action, parameter, requestType, context, null);
                }
                else
                {
                    var tokenText = request.Headers["token"][0];
                    TokenManager.LoginToken token = CheckLogin(tokenText, context, controller, action, out SmtActionResult checkLoginResult);
                    if (checkLoginResult != null)// check login failed.
                    {
                        result = checkLoginResult;
                    }
                    else
                    {
                        result = ControllerInvoker(controller, action, parameter, requestType, context, token);
                    }
                }
            }
            catch (System.Security.Cryptography.CryptographicException ex)
            {
                _logger.LogError(ex.ToString());
                result = new SmtActionResult
                {
                    StatusCode  = System.Net.HttpStatusCode.Unauthorized,
                    ResultValue = ex.Message
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                result = new SmtActionResult
                {
                    StatusCode  = System.Net.HttpStatusCode.BadRequest,
                    ResultType  = SmtActionResult.ActionResultType.Object,
                    ResultValue = ex.Message
                };
            }
            return(result);
        }
        /// <summary>
        /// response body is binary stream
        /// </summary>
        /// <param name="resultValue"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        protected SmtActionResult CreateFileResult(
            Stream resultValue,
            string fileName)
        {
            var result = new SmtActionResult();

            result.ResultType  = SmtActionResult.ActionResultType.File;
            result.ResultValue = resultValue;
            result.ContentType = MimeMapping.GetMimeType(fileName);
            result.FileName    = fileName;
            result.StatusCode  = System.Net.HttpStatusCode.OK;
            return(result);
        }
        public override SmtActionResult ActionInvoker(string actionName, Dictionary <string, object> parameter)
        {
            SmtActionResult result = null;

            switch (actionName)
            {
            case ControllerAction.SmtImageFileBase.GetThumbnailByID:
                result = GetThumbnailByID(int.Parse(parameter["id"].ToString()), GetQuery());
                break;

            default:
                result = base.ActionInvoker(actionName, parameter);
                break;
            }

            return(result);
        }
        public override SmtActionResult ActionInvoker(string actionName, Dictionary <string, object> parameter)
        {
            SmtActionResult result = null;

            switch (actionName)
            {
            case ControllerAction.SmtEntityBase.Get:
                result = Get(ConvertRequestBody <QueryExpression>(parameter["body"] as System.IO.Stream), GetQuery());
                break;

            case ControllerAction.SmtEntityBase.GetByID:
                result = GetByID(int.Parse(parameter["id"].ToString()), GetQuery());
                break;

            case ControllerAction.SmtEntityBase.GetAll:
                result = GetAll(ConvertRequestBody <QueryExpression>(parameter["body"] as System.IO.Stream), GetQuery());
                break;

            case ControllerAction.SmtEntityBase.GetUpdate:
                result = GetUpdate(ConvertRequestBody <QueryExpression>(parameter["body"] as System.IO.Stream), GetQuery());
                break;

            case ControllerAction.SmtEntityBase.Save:
                result = Save(ConvertRequestBody <List <DtoType> >(parameter["body"] as System.IO.Stream));
                break;

            case ControllerAction.SmtEntityBase.Add:
                result = Add(ConvertRequestBody <DtoType>(parameter["body"] as System.IO.Stream));
                break;

            case ControllerAction.SmtEntityBase.Update:
                result = Update(ConvertRequestBody <DtoType>(parameter["body"] as System.IO.Stream));
                break;

            case ControllerAction.SmtEntityBase.Delete:
                result = Delete(ConvertRequestBody <DtoType>(parameter["body"] as System.IO.Stream));
                break;

            default:
                break;
            }

            return(result);
        }
        public override SmtActionResult ActionInvoker(string actionName, Dictionary <string, object> parameter)
        {
            SmtActionResult result = null;

            switch (actionName)
            {
            case ControllerAction.Smt.Register:
                result = Register(parameter["user"].ToString(), parameter["tenantname"].ToString());
                break;

            case ControllerAction.Smt.TenantLogin:
                result = TenantLogin(parameter["user"].ToString(), parameter["pass"].ToString());
                break;

            case ControllerAction.Smt.TenantLoginWithIdToken:
                result = TenantLoginWithIdTokenAsync(parameter).Result;
                break;

            case ControllerAction.Smt.UserLogin:
                result = UserLogin(parameter["tenant"].ToString(), parameter["user"].ToString(), parameter["pass"].ToString());
                break;

            case ControllerAction.Smt.LockUser:
                result = LockUser(parameter["user"].ToString(), bool.Parse(parameter["islocked"].ToString()));
                break;

            case ControllerAction.Smt.ChangePassword:
                result = ChangePassword(parameter["currentpass"].ToString(), parameter["newpass"].ToString());
                break;

            case ControllerAction.Smt.TenantRequestToken:
                result = TenantRequestToken(parameter["email"].ToString(), parameter["purpose"].ToString());
                break;

            case ControllerAction.Smt.UserRequestToken:
                result = UserRequestToken(parameter["email"].ToString(), parameter["tenantname"].ToString(), parameter["purpose"].ToString());
                break;

            case ControllerAction.Smt.ResetPassword:
                result = ResetPassword(parameter["token"].ToString(), parameter["pass"].ToString());
                break;

            case ControllerAction.Smt.Logout:
                result = Logout();
                break;

            case ControllerAction.Smt.IP:
                result = CreateObjectResult(GetIPAddress());
                break;

            case ControllerAction.Smt.Ping:
                result = CreateObjectResult(null);
                break;

            case ControllerAction.Smt.TenantLogout:
                result = TenantLogout(parameter["user"].ToString(), parameter["pass"].ToString());
                break;

            case ControllerAction.Smt.UserLogout:
                result = UserLogout(parameter["tenant"].ToString(), parameter["user"].ToString(), parameter["pass"].ToString());
                break;

            default:
                break;
            }

            return(result);
        }
        private async Task WriteResponse(HttpResponse response, string responseType, SmtActionResult result)
        {
            response.StatusCode = (int)result.StatusCode;

            if (result.ResultValue == null)
            {
                return;
            }

            //status code not allow write content to response body
            if (result.StatusCode == System.Net.HttpStatusCode.NotModified)
            {
                return;
            }

            //Unauthorized is only for check login token
            if (result.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                await response.WriteAsync(result.ResultValue.ToString());

                return;
            }

            response.ContentType = result.ContentType;
            switch (result.ResultType)
            {
            case SmtActionResult.ActionResultType.PlainText:
                await response.WriteAsync(result.ResultValue.ToString());

                break;

            case SmtActionResult.ActionResultType.Object:
                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    switch (responseType)
                    {
                    case SerializeType.Protobuf:
                        response.Headers["Content-Encoding"] = "gzip";
                        response.ContentType = "application/octet-stream";
                        SmtSettings.Instance.BinarySerializer.Serialize(response.Body, result.ResultValue);
                        break;

                    case SerializeType.Json:
                        response.Headers["Content-Encoding"] = "gzip";
                        response.ContentType = "application/json;charset=utf-8";
                        SmtSettings.Instance.JsonSerializer.Serialize(response.Body, result.ResultValue);
                        break;
                    }
                }
                else
                {
                    var r = System.Text.Encoding.ASCII.GetBytes(result.ResultValue as string);
                    await response.Body.WriteAsync(r, 0, r.Length);
                }
                break;

            case SmtActionResult.ActionResultType.Stream:
                using (var stream = result.ResultValue as Stream)
                {
                    response.ContentLength = stream.Length;
                    await stream.CopyToAsync(response.Body);
                }
                break;

            case SmtActionResult.ActionResultType.File:
                var contentDisposition = new ContentDispositionHeaderValue("attachment");
                contentDisposition.SetHttpFileName(result.FileName);
                response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
                using (var stream = result.ResultValue as Stream)
                {
                    response.ContentLength = stream.Length;
                    await stream.CopyToAsync(response.Body);
                }
                break;
            }
        }