package <%= base_package %>.controllers.manager { import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; import org.restfulx.Rx; import org.restfulx.collections.ModelsCollection; import org.restfulx.models.RxModel; import org.restfulx.utils.RxUtils; import <%= base_package %>.models.domain.Session; import <%= base_package %>.models.domain.User; import <%= base_package %>.models.constant.ResourceResult; import <%= base_package %>.models.constant.ResourceAction; import <%= base_package %>.controllers.event.ResourceEvent; public class AuthenticationManager extends ResourceManager { /** Injector targets */ [Bindable] public var loginEvent:ResourceEvent; [Bindable] public var logoutEvent:ResourceEvent; private var _currentUser:User; [Bindable] public function get currentUser():User { return _currentUser; } public function set currentUser(value:User):void { _currentUser = value; } private var _sessionToken:*; [Bindable] public function get sessionToken():* { return _sessionToken; } public function set sessionToken(value:*):void { _sessionToken = value; } public function AuthenticationManager() { } override protected function create(resource:*):void { var r:* = discover(resource); login(r as Session); } override protected function destroy(resource:*):void { var r:* = discover(resource); logout(resource as Session); } public function login(session:Session):void { Rx.http(onLoginSuccess, onLoginFailure).invoke("session.fxml", { login: session.login, password: session.password }, "POST", true); } public function logout(session:Session):void { Rx.http(onLogout).invoke({URL: "session.fxml", method: "DELETE"}); } private function onLoginSuccess(result:User):void { currentUser = result; Rx.sessionToken = result.account.sessionToken; sessionToken = Rx.sessionToken; success(result, loginEvent); } private function onLoginFailure(result:Object):void { failure(result, loginEvent); } private function onLogout(result:Object):void { inject(result, logoutEvent); } } }