Exemple #1
0
 /// <summary>
 /// Failure.
 /// </summary>
 /// <param name="code">错误编码.</param>
 /// <param name="title">Title.</param>
 /// <param name="messages">消息.</param>
 /// <returns>Return T.</returns>
 protected static FriendlyException Failure(int code, string title, params string[] messages)
 {
     return(FriendlyThrowException.ThrowException(code, title, messages));
 }
Exemple #2
0
 /// <summary>
 /// 失败.
 /// </summary>
 /// <param name="messages">错误编码.</param>
 /// <returns>Friendly Exception.</returns>
 protected static FriendlyException Failure(params string[] messages)
 {
     return(FriendlyThrowException.ThrowException(messages));
 }
Exemple #3
0
 /// <summary>
 /// Failure.
 /// </summary>
 /// <param name="code">错误编码.</param>
 /// <param name="messages">消息.</param>
 /// <returns>Return T.</returns>
 protected static FriendlyException Failure(ResponseCode code, params string[] messages)
 {
     return(FriendlyThrowException.ThrowException(code, messages));
 }
Exemple #4
0
        /// <inheritdoc/>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var filters = context.ActionDescriptor.FilterDescriptors.OrderBy(x => x.Order);

            if (filters.All(x => x.Filter is not IgnoreActionLogAttribute))
            {
                var logWriter = ServiceLocator.ServiceProvider.GetService <ILogWriter>();
                logWriter.Information("Request Executing", context.ActionArguments);
            }

            if (filters.Any(x => x.Filter is IgnoreAuthenticationAttribute))
            {
                base.OnActionExecuting(context);
                return;
            }

            // 没有鉴权标识
            if (!(filters.FirstOrDefault(x => x.Filter is AuthenticationAttribute)?.Filter is AuthenticationAttribute authorize))
            {
                base.OnActionExecuting(context);
                return;
            }

            // 兼容 Allow Anonymous
            if (HasAllowAnonymous(context.ActionDescriptor))
            {
                base.OnActionExecuting(context);
                return;
            }

            // 类型错误(未登录)
            if (!(context.HttpContext.Items[AppConst.CurrentUserHttpItemKey] is ICurrentUser user))
            {
                var tmp = FriendlyThrowException.ThrowException(ResponseCode.Unauthorized, "当前操作需要登入");
                context.Result = new JsonResult(new GlobalResponse(tmp))
                {
                    StatusCode = 200,
                };
                return;
            }

            // 用户是admin
            if (user.Roles.Contains(Roles.Admin))
            {
                base.OnActionExecuting(context);
                return;
            }

            // 配置了指定角色
            if (authorize.CurrentRoles.Any())
            {
                // 角色不包含在指定角色中
                if (user.Roles.Any() ||
                    user.Roles.All(x => !authorize.CurrentRoles.Contains(x)))
                {
                    var tmp = FriendlyThrowException.ThrowException(ResponseCode.UnauthorizedRoles, "当前用户权限不足");
                    context.Result = new JsonResult(new GlobalResponse(tmp))
                    {
                        StatusCode = 200,
                    };
                }
            }
        }