Archive

Posts Tagged ‘login logout flex’

Login Logout in Flex

login & logout for flex web application sample code

//main Application file

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” xmlns:lw=”components.LoginWindow”
layout=”absolute” width=”100%” height=”100%”
creationComplete=”initApplication()”
visible=”false” >

<mx:RemoteObject id=”userLoginRO”
destination=”UserSessionService”
fault=”faultHandler(event)”>
<mx:method name=”removeAttribute” result=”logoutHandler( event )”/>
</mx:RemoteObject>

<mx:Script>

<![CDATA[
import components.LoginWindow;
import mx.managers.PopUpManager;
import mx.events.MenuEvent;
import mx.controls.Menu;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.ModuleManager;
import mx.modules.Module;
import mx.modules.IModuleInfo;
import mx.events.MenuEvent;
import mx.controls.menuClasses.MenuBarItem;

private var moduleToBeLoaded:Module;

private var moduleInfo:IModuleInfo;
private var moduleName:String;
private var module:Module;

import vo.LoginMaster;
import vo.user.LoggedInUserDetails;

import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public var loggedInUser:LoginMaster;
public var loggedInUserID:int;
public var loggedInUserDetails:LoggedInUserDetails;

public var loginUserStatus:SharedObject;
public var isUserLogged:Boolean;

/*  Method used to get which menu item selected */
private function menuItemHandler(event:MenuEvent):void
{
moduleName=event.item.@modulename;
loadModule(event.item.@url);
}

/*  Method determines modules to be loaded from given URL */
private function loadModule(moduleURL:String):void
{
moduleInfo=ModuleManager.getModule(moduleURL);
moduleInfo.addEventListener(ModuleEvent.READY, renderModule);
moduleInfo.addEventListener(ModuleEvent.ERROR, renderModuleError);
moduleInfo.load();
}

/* Loads module into Parent Container */
private function renderModule(event:ModuleEvent):void
{
if (applicationModuleCanvas.getChildren().length > 0)
{
applicationModuleCanvas.removeChild(moduleToBeLoaded);
}

if (moduleName == "FirstModule"){
moduleToBeLoaded=moduleInfo.factory.create() as FirstModule;
}else if (moduleName == "SecondModule"){
moduleToBeLoaded=moduleInfo.factory.create() as SecondModule;
}
applicationModuleCanvas.addChild(moduleToBeLoaded);
}

/* Error Display Wile loading Modules  */
private function renderModuleError(event:ModuleEvent):void
{
if (event.errorText == "Error #2035")
{
Alert.show("Please Provide Module URL in MenuItem URl Property", "Module Not Found");
}
else
{
Alert.show(event.errorText, "Error");
}
}

/* Init Application with login window */
private function initApplication():void
{
loggedInUserID = 0;
loggedInUserDetails = new LoggedInUserDetails();
loggedInUser =  new LoginMaster();
var loginWindow:components.LoginWindow ;

loginUserStatus = SharedObject.getLocal("loginUserStatus");

if (loginUserStatus.data.isUserLogged==null) {
loginWindow = new components.LoginWindow();
PopUpManager.addPopUp(loginWindow, this, true);
PopUpManager.centerPopUp(loginWindow);
} else {
Application.application.visible = true;
welcomeNoteTxt.text = "Welcome ....."+ loginUserStatus.data.userName ;
}

}

/*  Method used to get which menu is selected */
private function menuHandler(event:MouseEvent):void
{
var selectedMenu:MenuBarItem=MenuBarItem(event.target)
if (XMLList(selectedMenu.data).*.length() == 0)
{
var moduleURL:String=MenuBarItem(event.target).data.@url;
if (moduleURL.length != 0)
{
moduleName=MenuBarItem(event.target).data.@modulename;
loadModule(moduleURL);
}
else
{
logOutUserFromApplication();
}
}
}

/*  Method used to logout from the application */
private function logOutUserFromApplication():void{
loginUserStatus.clear();
//userLoginRO.removeAttribute(loggedInUserID);
Application.application.visible = false;
initApplication();
}

/*  Method used to set LoggedIn UserDetails such as Welcome Note etc ..*/
public function setLoggedInUserDetails():void{
loggedInUser = loggedInUserDetails.login as LoginMaster;
loggedInUserID = loggedInUser.id;

loginUserStatus.data.isUserLogged = true;
loginUserStatus.data.userName = loggedInUser.firstName + "   "+ loggedInUser.lastName;
loginUserStatus.flush();

welcomeNoteTxt.text = "Welcome ....."+ loginUserStatus.data.userName ;
}

/* Fault Handler */
private function faultHandler(event:FaultEvent):void{
Alert.show("There was a problem "+event, "Error..");
}

/* logout Handler */
private function logoutHandler(event:ResultEvent):void{
Alert.show("You Have Successfully Logged Out ","User LogOut ");
}

]]>
</mx:Script>

<mx:VBox width=”100%”
height=”100%”>

<mx:Canvas width=”100%”
height=”8%”>

<!–<mx:Image id=”companylogo”
source=”@Embed(source=’images/companylogo.jpg’)”
x=”10″
y=”4″/>–>
<mx:Text x=”809″ y=”33″ width=”349″ id=”welcomeNoteTxt” enabled=”false” fontWeight=”bold” textAlign=”left”/>

</mx:Canvas>

<mx:MenuBar id=”appMenuBar”
labelField=”@label”
width=”100%”
height=”25″
fontWeight=”bold”
fontSize=”12″
themeColor=”#ffcc00″
itemClick=”menuItemHandler(event)”
click=”menuHandler(event)”>
<mx:XMLList>

<menuitem label=”FirstModule”
url=”FirstModule.swf”
modulename=”FirstModule”/>
<menuitem label=”SecondModule”
url=”SecondModule.swf”
modulename=”SecondModule”/>
<menuitem label=”Logout”/>
</mx:XMLList>
</mx:MenuBar>

<!– modules//FirstModule.swf –>
<!– Selected Modules from menu will be displayed in this Canvas –>
<mx:Canvas id=”applicationModuleCanvas”
width=”100%”
height=”85%”
cornerRadius=”0″
backgroundColor=”#FDFDFD”
borderStyle=”inset”>
</mx:Canvas>
</mx:VBox>
</mx:Application>

// loginwindow file under src/components

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:TitleWindow xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” width=”461″ height=”202″
creationComplete=”initLoginWindow()” title=”Enter UserName and Password to continue …” fontSize=”12″>

<!– RemoteObject Used to Authenticate User–>
<mx:RemoteObject id=”userManagementRO”
destination=”userManagementService”
fault=”loginWindowfaultHandler(event)”>

<mx:method name=”validateUserNamePassword” result=”loginUserHandler(event)”/>

</mx:RemoteObject>

<mx:Script>
<![CDATA[
import vo.user.RoleMenuMapping;

import mx.core.Application;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.managers.PopUpManager;
import mx.utils.StringUtil;
import mx.messaging.FlexClient;

import vo.LoginMaster;
import vo.user.LoggedInUserDetails;

import flash.net.SharedObject;

public var loggedInUser:LoginMaster;
public var loggedInUserDetails:LoggedInUserDetails;

/*  Check User Name, Password were EMPTY */
private function loginUser():void{

var isUserNamePasswordNotNull:Boolean=true;
var userName:String=StringUtil.trim(userName.text);
var password:String=StringUtil.trim(password.text);

if(userName.length==0 && password.length==0){
loginStatusLbl.text="Please Enter UserName and Password ","User Login";
isUserNamePasswordNotNull=false;
}else if(userName.length==0){
loginStatusLbl.text="Please Enter User Name","User Login";
isUserNamePasswordNotNull=false;
}else if(password.length==0){
loginStatusLbl.text="Please Enter Password","User Login";
isUserNamePasswordNotNull=false;
}

if(isUserNamePasswordNotNull==true){
userManagementRO.validateUserNamePassword(userName,password);
}
}

/*  RemoteObject Fault Handler */
private function loginWindowfaultHandler(event:FaultEvent):void{
Alert.show("There is a problem."+event, "Error..");
}

/*  RemoteObject Login User Handler */
private function loginUserHandler(event:ResultEvent):void{

loggedInUserDetails= event.result as vo.user.LoggedInUserDetails;

if(loggedInUserDetails==null){
loginStatusLbl.text="Invalid UserName or Password";
password.text="";
loggedInUserDetails = new LoggedInUserDetails();
}else{

loggedInUser = loggedInUserDetails.login as LoginMaster;
Application.application.loggedInUserDetails = loggedInUserDetails ;

PopUpManager.removePopUp(this);
Application.application.visible=true;
Application.application.setLoggedInUserDetails();
}

}

/*  LoginWindow.mxml InitApplication Function */
private function initLoginWindow():void{
loggedInUserDetails = new LoggedInUserDetails();
loggedInUser =  new LoginMaster();
}

private function goToApp():void{
PopUpManager.removePopUp(this);
Application.application.visible = true;
Application.application.welcomeNoteTxt.text = "Using Goo App ";
}

]]>
</mx:Script>
<mx:HBox height=”100%” width=”100%”>
<mx:Canvas height=”100%” width=”100%”>

<mx:FormItem label=”User Name ” height=”24″ x=”10″ y=”48″ fontSize=”12″ fontWeight=”bold”>
</mx:FormItem>

<mx:FormItem label=”Password ” height=”24″ x=”10″ y=”80″ fontSize=”12″ fontWeight=”bold”>
</mx:FormItem>

<mx:Button id=”loginUserLoginBtn” label=”Login” click=”loginUser()”
cornerRadius=”0″ y=”126″ x=”363″ width=”68″ height=”28″ fontSize=”12″ tabIndex=”3″/>

<mx:TextInput id=”password” width=”321″ height=”28″ x=”110″ y=”80″ fontSize=”12″ displayAsPassword=”true” styleName=”CTextInput” tabIndex=”2″  text=”admin”/>
<mx:TextInput id=”userName” width=”321″ height=”24″ x=”110″ y=”48″ fontSize=”12″ styleName=”CTextInput” tabIndex=”1″ text=”admin”/>

<mx:Label id=”loginStatusLbl” height=”30″ x=”10″ y=”10″ width=”418″ fontWeight=”bold” color=”#F71E0E”
fontSize=”12″ fontStyle=”normal” enabled=”true” textDecoration=”normal” textAlign=”center” />
<mx:Button x=”266″ y=”126″ label=”GoTo App” click=”goToApp()”/>
</mx:Canvas>
</mx:HBox>
</mx:TitleWindow>

//First Module
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Module xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” width=”400″ height=”300″>
<mx:Label x=”126″ y=”123″ text=”First Module” width=”194″/>
</mx:Module>

// Second Module
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Module xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” width=”400″ height=”300″>
<mx:Label x=”129″ y=”127″ text=”Second Module”/>
</mx:Module>

//remoting-config.xml entries

<destination id=”userManagementService” channels=”my-amf”>
<properties>
<source>com.loginlogout.UserMgmService</source>
</properties>
<adapter ref=”java-object” />
</destination>

<destination id=”UserSessionService” channels = “my-amf”>
<properties>
<source>com.loginlogout.FlexSessionUtil</source>
</properties>
<adapter ref=”java-object” />
</destination>

//FlexSessionUtil  to manage Flex Server Session for logged in user – will be removed when user log out.
import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

public class FlexSessionUtil {

/**
* Set the given object in the session with an attribute name
*
* @param attributeName
*            the string name for the attribute
* @param sessionObject
*            Object value for the attribute name.
* @return void
*/

public static void setAttribute(String attributeName, Object sessionObject) {
FlexSession flexSession = FlexContext.getFlexSession();
flexSession.setAttribute(attributeName, sessionObject);

}

/**
* Get the given object in the session with an attribute name
*
* @param attributeName
*            the string name for the attribute
* @return Object
*/
public static Object getAttribute(String attributeName) {
FlexSession flexSession = FlexContext.getFlexSession();
return flexSession.getAttribute(attributeName);
}

/**
* Remove the given object in the session with an attribute name
*
* @param attributeName
*            the string name for the attribute
* @return void
*/
public static void removeAttribute(String attributeName) {
FlexSession flexSession = FlexContext.getFlexSession();
if (flexSession.getAttribute(attributeName) != null)
flexSession.removeAttribute(attributeName);
}

}

// write your server side code to check user authentication
inside your serverside java file import this class FlexSessionUtil to maintain loggedin user session

import com.spm.utils.FlexSessionUtil;

FlexSessionUtil.setAttribute(login.getId().toString(), login);

Follow

Get every new post delivered to your Inbox.