Exemplo n.º 1
0
        public void Add(Type serviceType, Type requestType, Type responseType)
        {
            RequestTypes.Add(requestType);
            ServiceTypes.Add(serviceType);

            var restrictTo = requestType.FirstAttribute <RestrictAttribute>()
                             ?? serviceType.FirstAttribute <RestrictAttribute>();

            var reqFilterAttrs = new[] { requestType, serviceType }
            .SelectMany(x => x.AllAttributes().OfType <IRequestFilterBase>()).ToList();
            var resFilterAttrs = (responseType != null ? new[] { responseType, serviceType } : new[] { serviceType })
                                 .SelectMany(x => x.AllAttributes().OfType <IResponseFilterBase>()).ToList();

            var authAttrs = reqFilterAttrs.OfType <AuthenticateAttribute>().ToList();
            var actions   = GetImplementedActions(serviceType, requestType);

            authAttrs.AddRange(actions.SelectMany(x => x.AllAttributes <AuthenticateAttribute>()));

            var operation = new Operation
            {
                ServiceType              = serviceType,
                RequestType              = requestType,
                ResponseType             = responseType,
                RestrictTo               = restrictTo,
                Actions                  = actions.Map(x => x.Name.ToUpper()),
                Routes                   = new List <RestPath>(),
                RequestFilterAttributes  = reqFilterAttrs,
                ResponseFilterAttributes = resFilterAttrs,
                RequiresAuthentication   = authAttrs.Count > 0,
                RequiredRoles            = authAttrs.OfType <RequiredRoleAttribute>().SelectMany(x => x.RequiredRoles).ToList(),
                RequiresAnyRole          = authAttrs.OfType <RequiresAnyRoleAttribute>().SelectMany(x => x.RequiredRoles).ToList(),
                RequiredPermissions      = authAttrs.OfType <RequiredPermissionAttribute>().SelectMany(x => x.RequiredPermissions).ToList(),
                RequiresAnyPermission    = authAttrs.OfType <RequiresAnyPermissionAttribute>().SelectMany(x => x.RequiredPermissions).ToList(),
            };

            this.OperationsMap[requestType] = operation;
            this.OperationNamesMap[operation.Name.ToLowerInvariant()] = operation;
            if (responseType != null)
            {
                ResponseTypes.Add(responseType);
            }

            //Only count non-core ServiceStack Services, i.e. defined outside of ServiceStack.dll or Swagger
            var nonCoreServicesCount = OperationsMap.Values
                                       .Count(x => x.ServiceType.Assembly != typeof(Service).Assembly &&
                                              x.ServiceType.FullName != "ServiceStack.Api.Swagger.SwaggerApiService" &&
                                              x.ServiceType.FullName != "ServiceStack.Api.Swagger.SwaggerResourcesService" &&
                                              x.ServiceType.FullName != "ServiceStack.Api.OpenApi.OpenApiService" &&
                                              x.ServiceType.Name != "__AutoQueryServices");

            LicenseUtils.AssertValidUsage(LicenseFeature.ServiceStack, QuotaType.Operations, nonCoreServicesCount);
        }