Skip to content

Commit

Permalink
Merge pull request #183 from hfiguiere/user-db-filename
Browse files Browse the repository at this point in the history
Fix UserDb API usage following changes for issue fxbox/users#53. r=ferjm
  • Loading branch information
ferjm committed Mar 14, 2016
2 parents 8b97928 + 80e88c1 commit 3bac806
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ docopt_macros = "0.6.80"
env_logger = "0.3.2"
foxbox_adapters = { git = "https://github.com/fxbox/adapters.git", rev = "ba54c80" }
foxbox_taxonomy = { git = "https://github.com/fxbox/taxonomy.git", rev = "4ff36cd" }
foxbox_users = { git = "https://github.com/fxbox/users.git", rev = "5b5beaa" }
foxbox_users = { git = "https://github.com/fxbox/users.git", rev = "8b82110" }
get_if_addrs = "0.3.1"
hyper = "0.7.2"
multicast_dns = { git = "https://github.com/fxbox/multicast-dns.git", rev = "a6e4bcc" }
Expand Down
10 changes: 9 additions & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::sync::atomic::{ AtomicBool, Ordering };
use upnp::UpnpManager;
use ws_server::WsServer;
use ws;
use foxbox_users::UsersManager;

#[derive(Clone)]
pub struct FoxBox {
Expand All @@ -36,7 +37,8 @@ pub struct FoxBox {
websockets: Arc<Mutex<HashMap<ws::util::Token, ws::Sender>>>,
pub config: Arc<ConfigService>,
upnp: Arc<UpnpManager>,
profile_service: Arc<ProfileService>
users_manager: Arc<UsersManager>,
profile_service: Arc<ProfileService>,
}

const DEFAULT_HOSTNAME: &'static str = "::"; // ipv6 default.
Expand All @@ -61,6 +63,7 @@ pub trait Controller : Send + Sync + Clone + Reflect + 'static {

fn get_config(&self) -> &ConfigService;
fn get_upnp_manager(&self) -> Arc<UpnpManager>;
fn get_users_manager(&self) -> Arc<UsersManager>;
fn get_profile(&self) -> &ProfileService;
}

Expand All @@ -83,6 +86,7 @@ impl FoxBox {
ws_port: ws_port,
config: Arc::new(ConfigService::new(&profile_service.path_for("foxbox.conf"))),
upnp: Arc::new(UpnpManager::new()),
users_manager: Arc::new(UsersManager::new(&profile_service.path_for("users_db.sqlite"))),
profile_service: Arc::new(profile_service)
}
}
Expand Down Expand Up @@ -215,6 +219,10 @@ impl Controller for FoxBox {
fn get_upnp_manager(&self) -> Arc<UpnpManager> {
self.upnp.clone()
}

fn get_users_manager(&self) -> Arc<UsersManager> {
self.users_manager.clone()
}
}

#[allow(dead_code)]
Expand Down
6 changes: 3 additions & 3 deletions src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use controller::Controller;
use foxbox_users::users_router::UsersRouter;
use iron::{ AfterMiddleware, Chain, Handler, Iron, IronResult,
Request, Response };
use iron::error::{ IronError };
Expand Down Expand Up @@ -58,11 +57,12 @@ impl<T: Controller> HttpServer<T> {
pub fn start(&mut self) {
let router = service_router::create(self.controller.clone());

let users_manager = self.controller.get_users_manager();
let mut mount = Mount::new();
mount.mount("/", static_router::create())
mount.mount("/", static_router::create(users_manager.clone()))
.mount("/ping", Ping)
.mount("/services", router)
.mount("/users", UsersRouter::init());
.mount("/users", users_manager.get_router_chain());

let mut chain = Chain::new(mount);
chain.link_after(Custom404);
Expand Down
25 changes: 15 additions & 10 deletions src/service_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use controller::Controller;
use foxbox_users::auth_middleware::{ AuthEndpoint, AuthMiddleware };
use foxbox_users::AuthEndpoint;
use iron::{ AfterMiddleware, headers, IronError, IronResult, Request,
Response };
use iron::headers::ContentType;
Expand Down Expand Up @@ -140,9 +140,7 @@ pub fn create<T: Controller>(controller: T) -> Chain {
};

let mut chain = Chain::new(router);
chain.around(AuthMiddleware {
auth_endpoints: auth_endpoints
});
chain.around(controller.get_users_manager().get_middleware(auth_endpoints));

chain.link_after(CORS);

Expand All @@ -153,7 +151,6 @@ pub fn create<T: Controller>(controller: T) -> Chain {
describe! service_router {
before_each {
use controller::FoxBox;
use foxbox_users::users_router::UsersRouter;
use iron::Headers;
use iron_test::request;
use mount::Mount;
Expand All @@ -162,19 +159,27 @@ describe! service_router {
let service_router = create(controller.clone());

let mut mount = Mount::new();
mount.mount("", service_router)
.mount("/users", UsersRouter::init());
// This is ugly here, but 1/ I can do 'use controller::Controller'
// in this block and 2/ if I don't, I get told I need to do it
// for the trait.
let manager = {
use controller::Controller;
let manager = controller.get_users_manager();
mount.mount("", service_router)
.mount("/users", manager.get_router_chain());
manager
};
}

describe! services {
before_each {
extern crate serde_json;

use foxbox_users::users_db::{ UserBuilder, UsersDb };
use foxbox_users::UserBuilder;
use iron::headers::{ Authorization, Basic, Bearer };
use iron_test::response;

let db = UsersDb::new();
let db = manager.get_db();
db.clear().ok();
let user = UserBuilder::new()
.id(1).name(String::from("username"))
Expand Down Expand Up @@ -252,7 +257,7 @@ describe! service_router {
assert!(headers.has::<headers::AccessControlAllowOrigin>());
assert!(headers.has::<headers::AccessControlAllowHeaders>());
assert!(headers.has::<headers::AccessControlAllowMethods>());
}
};
}
}
}
18 changes: 12 additions & 6 deletions src/static_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use foxbox_users::users_db::{ UsersDb, ReadFilter };
use foxbox_users::{ UsersManager, UsersDb, ReadFilter };
use iron::middleware::Handler;
use iron::prelude::*;
use iron::status;
use router::Router;
use staticfile::Static;
use std::path::Path;
use std::sync::Arc;

fn handler(req: &mut Request) -> IronResult<Response> {
let db = UsersDb::new();
fn handler(req: &mut Request, db: &UsersDb) -> IronResult<Response> {
let handler = match db.read(ReadFilter::IsAdmin(true)) {
Ok(users) => {
if users.is_empty() {
Expand All @@ -27,9 +27,15 @@ fn handler(req: &mut Request) -> IronResult<Response> {
Handler::handle(&handler, req)
}

pub fn create() -> Router {
pub fn create(manager: Arc<UsersManager>) -> Router {
let mut router = Router::new();
router.any("", handler);
router.any("*", handler);
let usersmanager = manager.clone();
router.any("", move |req: &mut Request| -> IronResult<Response> {
handler(req, &usersmanager.get_db())
});
let usersmanager = manager.clone();
router.any("*", move |req: &mut Request| -> IronResult<Response> {
handler(req, &usersmanager.get_db())
});
router
}
3 changes: 1 addition & 2 deletions src/ws_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
extern crate url;

use controller::Controller;
use foxbox_users::auth_middleware::AuthMiddleware;
use self::url::Url;
use std::thread;
use ws;
Expand Down Expand Up @@ -66,7 +65,7 @@ impl<T: Controller> Handler for WsHandler<T> {
_ => return self.close_with_error("Missing authorization"),
};

if let Err(_) = AuthMiddleware::verify(&token) {
if let Err(_) = self.controller.get_users_manager().verify_token(&token) {
return self.close_with_error("Authorization failed");
}

Expand Down

0 comments on commit 3bac806

Please sign in to comment.