protected void OnRegister(IAppHost appHost)
 {
     if (AccessRole != null && appHost.GetContainer().Exists <ICrudEvents>())
     {
         appHost.RegisterServices(ServiceRoutes);
     }
 }
コード例 #2
0
        public void Register(IAppHost appHost, AuthFeature feature)
        {
            var authRepo = HostContext.AppHost.GetAuthRepository();

            if (authRepo == null)
            {
                throw new NotSupportedException("ApiKeyAuthProvider requires a registered IAuthRepository");
            }

            if (!(authRepo is IManageApiKeys apiRepo))
            {
                throw new NotSupportedException(authRepo.GetType().Name + " does not implement IManageApiKeys");
            }

            appHost.RegisterServices(ServiceRoutes);

            feature.AuthEvents.Add(new ApiKeyAuthEvents(this));

            if (InitSchema)
            {
                using (apiRepo as IDisposable)
                {
                    apiRepo.InitApiKeySchema();
                }
            }
        }
        public void Register(IAppHost appHost)
        {
            if (AppName == null)
            {
                ServiceRoutes.Remove(typeof(DesktopFileService));
            }
            else
            {
                DesktopConfig.Instance.AppName = AppName;
            }

            appHost.RegisterServices(ServiceRoutes);
            DesktopConfig.Instance.ImportParams.AddRange(ImportParams);
            DesktopConfig.Instance.ProxyConfigs.AddRange(ProxyConfigs);
        }
コード例 #4
0
        public override void Register(IAppHost appHost, AuthFeature feature)
        {
            base.Register(appHost, feature);
            var manageApiKeys = HostContext.AppHost.AssertManageApiKeysAsync();

            using (manageApiKeys as IDisposable)
            {
                if (InitSchema)
                {
                    manageApiKeys.InitApiKeySchema();
                }
            }

            appHost.RegisterServices(ServiceRoutes);

            feature.AuthEvents.Add(new ApiKeyAuthEvents(this));
        }
コード例 #5
0
        /// <summary>
        /// Activate the validation mechanism, so every request DTO with an existing validator
        /// will be validated.
        /// </summary>
        /// <param name="appHost">The app host</param>
        public void Register(IAppHost appHost)
        {
            if (TreatInfoAndWarningsAsErrors)
            {
                if (!appHost.GlobalRequestFiltersAsync.Contains(ValidationFilters.RequestFilterAsync))
                {
                    appHost.GlobalRequestFiltersAsync.Add(ValidationFilters.RequestFilterAsync);
                }

                if (!appHost.GlobalMessageRequestFiltersAsync.Contains(ValidationFilters.RequestFilterAsync))
                {
                    appHost.GlobalMessageRequestFiltersAsync.Add(ValidationFilters.RequestFilterAsync);
                }
            }
            else
            {
                if (!appHost.GlobalRequestFiltersAsync.Contains(ValidationFilters.RequestFilterAsyncIgnoreWarningsInfo))
                {
                    appHost.GlobalRequestFiltersAsync.Add(ValidationFilters.RequestFilterAsyncIgnoreWarningsInfo);
                }

                if (!appHost.GlobalMessageRequestFiltersAsync.Contains(ValidationFilters.RequestFilterAsyncIgnoreWarningsInfo))
                {
                    appHost.GlobalMessageRequestFiltersAsync.Add(ValidationFilters.RequestFilterAsyncIgnoreWarningsInfo);
                }

                if (!appHost.GlobalResponseFiltersAsync.Contains(ValidationFilters.ResponseFilterAsync))
                {
                    appHost.GlobalResponseFiltersAsync.Add(ValidationFilters.ResponseFilterAsync);
                }

                if (!appHost.GlobalMessageResponseFiltersAsync.Contains(ValidationFilters.ResponseFilterAsync))
                {
                    appHost.GlobalMessageResponseFiltersAsync.Add(ValidationFilters.ResponseFilterAsync);
                }
            }

            if (ValidationSource != null)
            {
                appHost.Register(ValidationSource);
                ValidationSource.InitSchema();
            }

            var container           = appHost.GetContainer();
            var hasValidationSource = ValidationSource != null || container.Exists <IValidationSource>();

            if (hasValidationSource && AccessRole != null)
            {
                appHost.RegisterServices(ServiceRoutes);
            }

            if (ScanAppHostAssemblies)
            {
                container.RegisterValidators(((ServiceStackHost)appHost).ServiceAssemblies.ToArray());
            }

            appHost.AddToAppMetadata(metadata => {
                metadata.Plugins.Validation = new ValidationInfo {
                    HasValidationSource      = hasValidationSource.NullIfFalse(),
                    HasValidationSourceAdmin = (container.TryResolve <IValidationSource>() is IValidationSourceAdmin).NullIfFalse(),
                    ServiceRoutes            = ServiceRoutes.ToMetadataServiceRoutes(),
                    TypeValidators           = !EnableDeclarativeValidation ? null
                        : appHost.ScriptContext.ScriptMethods.SelectMany(x =>
                                                                         ScriptMethodInfo.GetScriptMethods(x.GetType(), where : mi =>
                                                                                                           typeof(ITypeValidator).IsAssignableFrom(mi.ReturnType)))
                                               .Map(x => x.ToScriptMethodType()),
                    PropertyValidators = !EnableDeclarativeValidation ? null
                        : appHost.ScriptContext.ScriptMethods.SelectMany(x =>
                                                                         ScriptMethodInfo.GetScriptMethods(x.GetType(), where : mi =>
                                                                                                           typeof(IPropertyValidator).IsAssignableFrom(mi.ReturnType)))
                                         .Map(x => x.ToScriptMethodType()),
                    AccessRole = AccessRole,
                };
            });

            appHost.GetPlugin <MetadataFeature>()?.ExportTypes.Add(typeof(ValidationRule));
        }
コード例 #6
0
ファイル: AuthFeature.cs プロジェクト: ycdng/ServiceStack
        public void Register(IAppHost appHost)
        {
            hasRegistered = true;
            AuthenticateService.Init(sessionFactory, AuthProviders);

            var unitTest = appHost == null;

            if (unitTest)
            {
                return;
            }

            if (HostContext.StrictMode)
            {
                var sessionInstance = sessionFactory();
                if (TypeSerializer.HasCircularReferences(sessionInstance))
                {
                    throw new StrictModeException($"User Session {sessionInstance.GetType().Name} cannot have circular dependencies", "sessionFactory",
                                                  StrictModeCodes.CyclicalUserSession);
                }
            }

            appHost.RegisterServices(ServiceRoutes);

            var sessionFeature = RegisterPlugins.OfType <SessionFeature>().First();

            sessionFeature.SessionExpiry          = SessionExpiry;
            sessionFeature.PermanentSessionExpiry = PermanentSessionExpiry;

            appHost.LoadPlugin(RegisterPlugins.ToArray());

            if (IncludeAuthMetadataProvider && appHost.TryResolve <IAuthMetadataProvider>() == null)
            {
                appHost.Register <IAuthMetadataProvider>(new AuthMetadataProvider());
            }

            AuthProviders.OfType <IAuthPlugin>().Each(x => x.Register(appHost, this));

            AuthenticateService.HtmlRedirect               = HtmlRedirect;
            AuthenticateService.HtmlRedirectAccessDenied   = HtmlRedirectAccessDenied;
            AuthenticateService.HtmlRedirectReturnParam    = HtmlRedirectReturnParam;
            AuthenticateService.HtmlRedirectReturnPathOnly = HtmlRedirectReturnPathOnly;
            AuthenticateService.AuthResponseDecorator      = AuthResponseDecorator;
            if (ValidateFn != null)
            {
                AuthenticateService.ValidateFn = ValidateFn;
            }

            var authNavItems = AuthProviders.Select(x => (x as AuthProvider)?.NavItem).Where(x => x != null);

            if (!ViewUtils.NavItemsMap.TryGetValue("auth", out var navItems))
            {
                ViewUtils.NavItemsMap["auth"] = navItems = new List <NavItem>();
            }

            var isDefaultHtmlRedirect = HtmlRedirect == "~/" + LocalizedStrings.Login.Localize();

            if (IncludeDefaultLogin && isDefaultHtmlRedirect && !appHost.VirtualFileSources.FileExists("/login.html"))
            {
                appHost.VirtualFileSources.GetMemoryVirtualFiles().WriteFile("/login.html",
                                                                             Templates.HtmlTemplates.GetLoginTemplate());
            }

            navItems.AddRange(authNavItems);

            appHost.AddToAppMetadata(meta => {
                meta.Plugins.Auth = new AuthInfo {
                    HasAuthSecret       = (appHost.Config.AdminAuthSecret != null).NullIfFalse(),
                    HasAuthRepository   = appHost.GetContainer().Exists <IAuthRepository>().NullIfFalse(),
                    IncludesRoles       = IncludeRolesInAuthenticateResponse.NullIfFalse(),
                    IncludesOAuthTokens = IncludeOAuthTokensInAuthenticateResponse.NullIfFalse(),
                    HtmlRedirect        = HtmlRedirect?.TrimStart('~'),
                    AuthProviders       = AuthenticateService.GetAuthProviders().Map(x => new MetaAuthProvider {
                        Type    = x.Type,
                        Name    = x.Provider,
                        NavItem = (x as AuthProvider)?.NavItem,
                        Meta    = x.Meta,
                    })
                };
            });
        }
コード例 #7
0
 public void Register(IAppHost appHost)
 {
     appHost.RegisterServices(ServiceRoutes);
     DesktopConfig.Instance.ImportParams.AddRange(ImportParams);
     DesktopConfig.Instance.ProxyConfigs.AddRange(ProxyConfigs);
 }