Beispiel #1
0
        public override void Configure(Container container)
        {
            //Register Typed Config some services might need to access
            var appSettings = new AppSettings();
            AppConfig = new AppConfig(appSettings);
            container.Register(AppConfig);

            //NLog
            container.Register(x => new NLogFactory());
            //Elmah
            container.Register(x => new ElmahLogFactory(container.Resolve<NLogFactory>()));
            LogManager.LogFactory = container.Resolve<ElmahLogFactory>();
            container.Register<ILog>(x => LogManager.GetLogger(GetType()));

            //Cache
            container.Register<ICacheClientExtended>(new MemoryCacheClientExtended());

            //Plugins.Add(new RazorFormat());

            container.Register<IDbConnectionFactory>(new OrmLiteConnectionFactory(AppConfig.ConnectionString, false, SqlServerDialect.Provider));
            ConfigureAuth(container);

            //Register all repositories
            container.Register<IDBSetupRepository>(c => new DBSetupRepository { _db = c.Resolve<IDbConnectionFactory>() });
            container.Register<IGalleryRepository>(c => new GalleryRepository { _db = c.Resolve<IDbConnectionFactory>() });
            container.Register<IUserRepository>(c => new UserRepository { _db = c.Resolve<IDbConnectionFactory>() });
            container.Register<IPhotoRepository>(c => new PhotoRepository { _db = c.Resolve<IDbConnectionFactory>() });

            //Setup database
            SetupDatabase(container);

            //Register services
            container.Register<IBaseService>(c => new BaseService
                {
                    UserRepo = c.Resolve<IUserRepository>(),
                    Cacher = c.Resolve<ICacheClientExtended>(),
                    Logger = c.Resolve<ILog>(),
                    AppConfig = c.Resolve<AppConfig>()
                });

            container.Register<IImageProcessingService>(c => new ImageProcessingService());

            container.Register<IUserService>(c => new UserService() {_baseService = c.Resolve<IBaseService>()});

            container.Register<IPhotoService>(c => new PhotoService
                {
                    _baseService = c.Resolve<IBaseService>(),
                    _photoRepo = c.Resolve<IPhotoRepository>(),
                    ImageProcessingService = c.Resolve<IImageProcessingService>(),
                    AppConfig = c.Resolve<AppConfig>(),
                    UserService = c.Resolve<IUserService>()
                });

            container.Register<IGalleryService>(c => new GalleryService
            {
                _baseService = c.Resolve<IBaseService>(),
                _galleryRepo = c.Resolve<IGalleryRepository>(),
                PhotoService = c.Resolve<IPhotoService>(),
                UserService = c.Resolve<IUserService>()
            });

            ConfigureServiceRoutes();

            Plugins.Add(new SwaggerFeature());
            Plugins.Add(new CorsFeature());

            /*
            SetConfig(new EndpointHostConfig {
                CustomHttpHandlers = {
                    { HttpStatusCode.NotFound, new RazorHandler("/notfound") },
                    { HttpStatusCode.Unauthorized, new RazorHandler("/login") },
                }
            });
            */

            PhotoExtension.PhotoService = container.Resolve<IPhotoService>();

            //Set MVC to use the same Funq IOC as ServiceStack
            ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
            ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
        }
        public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
        {
            base.OnAuthenticated(authService, session, tokens, authInfo);

            var appSettings = new AppSettings();
            var config = new AppConfig(appSettings);

            //Populate all matching fields from this session to your own custom User table
            var user = session.TranslateTo<User>();
            user.Id = int.Parse(session.UserAuthId);
            user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
                ? CreateGravatarUrl(session.Email, 64)
                : null;

            if (UserService.IsUserOwner(user.Id))
            {
                OwnerId = UserService.GetOwnerIdByUserId(user.Id);
                Owner = UserService.GetOwnerById(OwnerId);
            }

            foreach (var authToken in session.ProviderOAuthAccess)
            {
                if (authToken.Provider == FacebookAuthProvider.Name)
                {
                    user.FacebookName = authToken.DisplayName;
                    user.FacebookFirstName = authToken.FirstName;
                    user.FacebookLastName = authToken.LastName;
                    user.FacebookEmail = authToken.Email;
                    user.Email = authToken.Email;
                    user.UserName = authToken.DisplayName;
                }
                else if (authToken.Provider == TwitterAuthProvider.Name)
                {
                    user.TwitterName = authToken.DisplayName;
                    user.Email = authToken.Email;
                    user.UserName = authToken.DisplayName;
                }
                else if (authToken.Provider == GoogleOpenIdOAuthProvider.Name)
                {
                    user.GoogleUserId = authToken.UserId;
                    user.GoogleFullName = authToken.FullName;
                    user.GoogleEmail = authToken.Email;
                    user.Email = authToken.Email;
                    user.UserName = authToken.FullName;

                }
                else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
                {
                    user.YahooUserId = authToken.UserId;
                    user.YahooFullName = authToken.FullName;
                    user.YahooEmail = authToken.Email;
                    user.Email = authToken.Email;
                    user.UserName = authToken.FullName;
                }
            }

            if (config.AdminUserNames.Contains(session.Email) && !session.HasRole(RoleNames.Admin))
            {
                using (var assignRoles = authService.ResolveService<AssignRolesService>())
                {
                    assignRoles.Post(new AssignRoles {
                        UserName = session.UserAuthName,
                        Roles = { RoleNames.Admin }
                    });
                }
            }

            //Resolve the DbFactory from the IOC and persist the user info
            authService.TryResolve<IDbConnectionFactory>().Run(db => db.Save(user));
        }