public DatabaseBackend(IDbConnectionFactory conn_factory, DbStorageFactory storage_factory, IAuthenticator auth, OAuthHandler handler) : base(conn_factory) { oauthHandler = handler; storageFactory = storage_factory; // TODO move this into (Encrypted)DbStorageFactory implementation CreateSchema (conn_factory); }
public DatabaseNoteRepository(IDbConnectionFactory factory, DbStorageFactory storageFactory, IUser user) : base(factory) { this.storage = storageFactory.GetDbStorage (user); engine = new Engine (storage); using (var db = connFactory.OpenDbConnection ()) { this.dbUser = db.Select<DBUser> (u => u.Username == user.Username)[0]; } if (dbUser.Manifest == null || string.IsNullOrEmpty (dbUser.Manifest.ServerId)) { // the user may not yet have synced dbUser.Manifest.ServerId = Guid.NewGuid ().ToString (); } }
// 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); } }
private void WireupGenericTestClasses(Funq.Container container) { container.Register<IAuthenticator> (c => { var factory = c.Resolve<IDbConnectionFactory> (); var dbauth = new DbAuthenticator (factory); //var dbauth = new DbTestAuthenticator (); var test_user = new DBUser { Username = RainyTestServer.TEST_USER, IsActivated = true, IsVerified = true }; test_user.CreateCryptoFields (RainyTestServer.TEST_PASS); // insert a dummy testuser using (var db = factory.OpenDbConnection ()) { db.InsertParam<DBUser> (test_user); } return dbauth; }); container.Register<IAdminAuthenticator> (c => { var admin_auth = new DummyAdminAuthenticator (ADMIN_TEST_PASS); return (IAdminAuthenticator)admin_auth; }); container.Register<IDbStorageFactory> (c => { var conn_factory = c.Resolve<IDbConnectionFactory> (); IDbStorageFactory storage_factory; bool use_encryption = true; if (use_encryption) storage_factory = new DbEncryptedStorageFactory (conn_factory, use_history: true); else storage_factory = new DbStorageFactory (conn_factory, use_history: true); return (IDbStorageFactory) storage_factory; }); container.Register<IDataBackend> (c => { var conn_factory = c.Resolve<IDbConnectionFactory> (); var storage_factory = c.Resolve<IDbStorageFactory> (); var auth = c.Resolve<IAuthenticator> (); var handler = c.Resolve<OAuthHandler> (); return new DatabaseBackend (conn_factory, storage_factory, auth, handler); }); 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; }); var connFactory = container.Resolve<IDbConnectionFactory> (); DatabaseBackend.CreateSchema (connFactory, true); // HACK so the user is inserted when a fixture SetUp is run container.Resolve<IAuthenticator> (); }