示例#1
0
文件: Main.cs 项目: gmstrbytes/Rainy
        // This is the "Composition Root" in the IoC pattern that wires all
        // our objects/implementations up, based on a given configuration.
        // config sanity checks SHOULD NOT go here

        private static void ComposeObjectGraph(Funq.Container container)
        {
            var config = Config.Global;

            container.Register <SqliteConfig> (c => new SqliteConfig {
                File = Path.Combine(config.DataPath, "rainy.db")
            });

            container.Register <PostgreConfig> (c => {
                dynamic txt_conf = config.Postgre;
                var psql_conf    = new PostgreConfig();
                if (!string.IsNullOrEmpty(txt_conf.Username))
                {
                    psql_conf.Username = txt_conf.Username;
                }
                if (!string.IsNullOrEmpty(txt_conf.Password))
                {
                    psql_conf.Password = txt_conf.Password;
                }
                if (!string.IsNullOrEmpty(txt_conf.Database))
                {
                    psql_conf.Database = txt_conf.Database;
                }
                if (!string.IsNullOrEmpty(txt_conf.Host))
                {
                    psql_conf.Host = txt_conf.Host;
                }
                if (txt_conf.Port > 0)
                {
                    psql_conf.Port = (uint)txt_conf.Port;
                }

                return(psql_conf);
            });

            if (config.Backend == "xml")
            {
                // use username/password pairs from the config file
                container.Register <IAuthenticator> (c => {
                    return(new ConfigFileAuthenticator(config.User));
                });

                // we store notes in XML files in the DataPath
                container.Register <IDataBackend> (c => {
                    var auth          = c.Resolve <IAuthenticator> ();
                    var factory       = c.Resolve <IDbConnectionFactory> ();
                    var oauth_handler = c.Resolve <OAuthHandler> ();
                    return(new FileSystemBackend(config.DataPath, factory, auth, oauth_handler, false));
                });
            }
            else
            {
                // database based backends
                switch ((string)config.Backend)
                {
                case "sqlite":
                    container.Register <IDbConnectionFactory> (c => {
                        var conf = container.Resolve <SqliteConfig> ();
                        var connection_string = conf.ConnectionString;
                        var factory           = new OrmLiteConnectionFactory(connection_string, SqliteDialect.Provider);

                        if (!File.Exists(conf.File))
                        {
                            DatabaseBackend.CreateSchema(factory);
                        }

                        return((IDbConnectionFactory)factory);
                    });
                    break;

                case "postgre":
                    container.Register <IDbConnectionFactory> (c => {
                        var connection_string = container.Resolve <PostgreConfig> ().ConnectionString;
                        var factory           = new OrmLiteConnectionFactory(connection_string, PostgreSqlDialect.Provider);
                        DatabaseBackend.CreateSchema(factory);
                        return(factory);
                    });
                    break;
                }

                container.Register <IAuthenticator> (c => {
                    var factory = c.Resolve <IDbConnectionFactory> ();
//					var sfactory = new OrmLiteConnectionFactory ();
                    var dbauth = new DbAuthenticator(factory);
                    //var dbauth = new ConfigFileAuthenticator (Config.Global.Users);

                    // we have to make sure users from the config file exist with the configured password
                    // in the db
                    // TODO delete old users? or restrict to webinterface?
                    if (dbauth is ConfigFileAuthenticator)
                    {
                        foreach (dynamic user in Config.Global.Users)
                        {
                            string username = user.Username;
                            string password = user.Password;
                            using (var db = factory.OpenDbConnection()) {
                                var db_user = db.FirstOrDefault <DBUser> (u => u.Username == username);
                                if (db_user != null)
                                {
                                    var need_update = db_user.UpdatePassword(password);
                                    if (need_update)
                                    {
                                        db.UpdateOnly(new DBUser {
                                            PasswordHash = db_user.PasswordHash
                                        }, u => new { u.PasswordHash }, (DBUser p) => p.Username == username);
                                    }
                                }
                                else
                                {
                                    // create the user in the db
                                    var new_user      = new DBUser();
                                    new_user.Username = username;
                                    new_user.CreateCryptoFields(password);
                                    new_user.UpdatePassword(password);
                                    db.Insert <DBUser> (new_user);
                                }
                            }
                        }
                    }
                    return(dbauth);
                });
//
                container.Register <IAdminAuthenticator> (c => {
                    var auth = new ConfigFileAdminAuthenticator();
                    return(auth);
                });

                container.Register <OAuthHandler> (c => {
                    var auth    = c.Resolve <IAuthenticator> ();
                    var factory = c.Resolve <IDbConnectionFactory> ();
                    //				ITokenRepository<AccessToken> access_tokens = new SimpleTokenRepository<AccessToken> ();
                    //				ITokenRepository<RequestToken> request_tokens = new SimpleTokenRepository<RequestToken> ();
                    ITokenRepository <AccessToken> access_tokens   = new DbAccessTokenRepository <AccessToken> (factory);
                    ITokenRepository <RequestToken> request_tokens = new DbRequestTokenRepository <RequestToken> (factory);
                    ITokenStore token_store = new RainyTokenStore(access_tokens, request_tokens);
                    OAuthHandler handler    = new OAuthHandler(auth, access_tokens, request_tokens, token_store);
                    return(handler);
                });

                container.Register <DbStorageFactory> (c => {
                    var conn_factory    = c.Resolve <IDbConnectionFactory> ();
                    bool use_encryption = (bool)Config.Global.UseNoteEncryption;

                    var storage_factory = new DbStorageFactory(conn_factory, use_encryption, use_history: true);
                    return(storage_factory);
                });

                container.Register <IDataBackend> (c => {
                    var conn_factory    = c.Resolve <IDbConnectionFactory> ();
                    var storage_factory = c.Resolve <DbStorageFactory> ();
                    var handler         = c.Resolve <OAuthHandler> ();
                    var auth            = c.Resolve <IAuthenticator> ();
                    return(new DatabaseBackend(conn_factory, storage_factory, auth, handler));
                });

/*				container.Register<OAuthHandler> (c => {
 *                                      var factory = c.Resolve<IDbConnectionFactory> ();
 *                                      var access_token_repo = new DbAccessTokenRepository<AccessToken> (factory);
 *                                      var request_token_repo = new SimpleTokenRepository<RequestToken> ();
 *                                      var auth = c.Resolve<IAuthenticator> ();
 *                                      var token_store = new Rainy.OAuth.SimpleStore.SimpleTokenStore (access_token_repo, request_token_repo);
 *
 *                                      var handler = new OAuthHandler (auth, token_store);
 *                                      return handler;
 *                              });
 */
                if (config.Development == true)
                {
                    AddDummyUserIfRequired(container);
                }
            }
        }
示例#2
0
文件: Main.cs 项目: Dynalon/Rainy
        // This is the "Composition Root" in the IoC pattern that wires all
        // our objects/implementations up, based on a given configuration.
        // config sanity checks SHOULD NOT go here
        private static void ComposeObjectGraph(Funq.Container container)
        {
            var config = Config.Global;

            container.Register<SqliteConfig> (c => new SqliteConfig {
                File = Path.Combine (config.DataPath, "rainy.db")
            });

            container.Register<PostgreConfig> (c => {
                dynamic txt_conf = config.Postgre;
                var psql_conf = new PostgreConfig ();
                if (!string.IsNullOrEmpty (txt_conf.Username)) psql_conf.Username = txt_conf.Username;
                if (!string.IsNullOrEmpty (txt_conf.Password)) psql_conf.Password = txt_conf.Password;
                if (!string.IsNullOrEmpty (txt_conf.Database)) psql_conf.Database = txt_conf.Database;
                if (!string.IsNullOrEmpty (txt_conf.Host)) psql_conf.Host = txt_conf.Host;
                if (txt_conf.Port > 0) psql_conf.Port = (uint) txt_conf.Port;

                return psql_conf;
            });

            if (config.Backend == "xml") {

                // use username/password pairs from the config file
                container.Register<IAuthenticator> (c => {
                    return new ConfigFileAuthenticator(config.User);
                });

                // we store notes in XML files in the DataPath
                container.Register<IDataBackend> (c => {
                    var auth = c.Resolve<IAuthenticator> ();
                    var factory = c.Resolve<IDbConnectionFactory> ();
                    var oauth_handler = c.Resolve<OAuthHandler> ();
                    return new FileSystemBackend (config.DataPath, factory, auth, oauth_handler, false);
                });

            } else {
                // database based backends
                switch ((string) config.Backend) {
                    case "sqlite":
                    container.Register<IDbConnectionFactory> (c => {
                        var conf = container.Resolve<SqliteConfig> ();
                        var connection_string = conf.ConnectionString;
                        var factory = new OrmLiteConnectionFactory (connection_string, SqliteDialect.Provider);

                        if (!File.Exists (conf.File)) {
                            DatabaseBackend.CreateSchema (factory);
                        }

                        return (IDbConnectionFactory) factory;
                    });
                    break;
                    case "postgre":
                    container.Register<IDbConnectionFactory> (c => {
                        var connection_string = container.Resolve<PostgreConfig> ().ConnectionString;
                        var factory = new OrmLiteConnectionFactory (connection_string, PostgreSqlDialect.Provider);
                        DatabaseBackend.CreateSchema (factory);
                        return factory;
                    });
                    break;
                }

                container.Register<IAuthenticator> (c => {
                    var factory = c.Resolve<IDbConnectionFactory> ();
            //					var sfactory = new OrmLiteConnectionFactory ();
                    var dbauth = new DbAuthenticator (factory);
                    //var dbauth = new ConfigFileAuthenticator (Config.Global.Users);

                    // we have to make sure users from the config file exist with the configured password
                    // in the db
                    // TODO delete old users? or restrict to webinterface?
                    if (dbauth is ConfigFileAuthenticator) {
                        foreach (dynamic user in Config.Global.Users) {
                            string username = user.Username;
                            string password = user.Password;
                            using (var db = factory.OpenDbConnection ()) {
                                var db_user = db.FirstOrDefault<DBUser> (u => u.Username == username);
                                if (db_user != null) {
                                    var need_update = db_user.UpdatePassword (password);
                                    if (need_update)
                                        db.UpdateOnly (new DBUser { PasswordHash = db_user.PasswordHash }, u => new { u.PasswordHash }, (DBUser p) => p.Username == username);
                                } else {
                                    // create the user in the db
                                    var new_user = new DBUser ();
                                    new_user.Username = username;
                                    new_user.CreateCryptoFields (password);
                                    new_user.UpdatePassword (password);
                                    db.Insert<DBUser> (new_user);
                                }
                            }
                        }
                    }
                    return dbauth;
                });
            //
                container.Register<IAdminAuthenticator> (c => {
                    var auth = new ConfigFileAdminAuthenticator ();
                    return auth;
                });

                container.Register<OAuthHandler> (c => {
                    var auth = c.Resolve<IAuthenticator> ();
                    var factory = c.Resolve<IDbConnectionFactory> ();
                    //				ITokenRepository<AccessToken> access_tokens = new SimpleTokenRepository<AccessToken> ();
                    //				ITokenRepository<RequestToken> request_tokens = new SimpleTokenRepository<RequestToken> ();
                    ITokenRepository<AccessToken> access_tokens = new DbAccessTokenRepository<AccessToken> (factory);
                    ITokenRepository<RequestToken> request_tokens = new DbRequestTokenRepository<RequestToken> (factory);
                    ITokenStore token_store = new RainyTokenStore (access_tokens, request_tokens);
                    OAuthHandler handler = new OAuthHandler (auth, access_tokens, request_tokens, token_store);
                    return handler;
                });

                container.Register<DbStorageFactory> (c => {
                    var conn_factory = c.Resolve<IDbConnectionFactory> ();
                    bool use_encryption = (bool) Config.Global.UseNoteEncryption;

                    var storage_factory = new DbStorageFactory (conn_factory, use_encryption, use_history: true);
                    return storage_factory;
                });

                container.Register<IDataBackend> (c => {
                    var conn_factory = c.Resolve<IDbConnectionFactory> ();
                    var storage_factory = c.Resolve<DbStorageFactory> ();
                    var handler = c.Resolve<OAuthHandler> ();
                    var auth = c.Resolve<IAuthenticator> ();
                    return new DatabaseBackend (conn_factory, storage_factory, auth, handler);
                });

            /*				container.Register<OAuthHandler> (c => {
                    var factory = c.Resolve<IDbConnectionFactory> ();
                    var access_token_repo = new DbAccessTokenRepository<AccessToken> (factory);
                    var request_token_repo = new SimpleTokenRepository<RequestToken> ();
                    var auth = c.Resolve<IAuthenticator> ();
                    var token_store = new Rainy.OAuth.SimpleStore.SimpleTokenStore (access_token_repo, request_token_repo);

                    var handler = new OAuthHandler (auth, token_store);
                    return handler;
                });
            */
                AddDummyUserIfRequired (container);
            }
        }