0 Replies Latest reply on Jul 25, 2011 8:58 AM by jeffcg2

    Flex login and CFLogin roles examples

    jeffcg2 Level 1

      I'm trying to understand integrating a Flex Login to CFLogin - CFLoginUser plumbing.

       

      I have been looking for some examples, but haven't found any yet.

       

      So I'm trying to roll my own. I see 3 things necessary mxml file, Application.cfc and a login.cfc.

       

      This is where I am at so far. Is this going to get me there??

       

      Application.cfc

      <cfcomponent>

       

       

       

      <cfset

       

       

      This.name = "myapp">

      <cfset

       

       

      This.Sessionmanagement="True">

      <cfset

       

       

      This.loginstorage="session">

       

      <cffunction

       

       

      name="getRoles" access="remote">

      <

       

      cfreturn #GetUserRoles()#>

      </cffunction>

      <cffunction

       

      name="getAuthUser" access="remote">

      <

       

      cfreturn #GetAuthUser()#>

      </cffunction>

      <cffunction

       

      name="logout" access="remote">

      <

       

      cflogout>

      </cffunction>

      </cfcomponent>

       

      cflogin.cfc

      <cfcomponent>

       

      <cffunction name="login" access="remote" returntype="boolean" >

       

      <cfargument name="email" type="string" default="" required="yes">

       

      <cfargument name="password" type="string" default="" required="yes">

       

      <cfquery name="qSecurity" datasource="mydb">

       

      SELECT username, password, role

       

      FROM dbo.security

       

      WHERE username=<cfqueryparam value="#arguments.email#" CFSQLTYPE="CF_SQL_VARCHAR">

       

      AND password=<cfqueryparam value="#arguments.password#" CFSQLTYPE='CF_SQL_VARCHAR'>

       

      AND role = 'user'

       

      or username=<cfqueryparam value="#arguments.email#" CFSQLTYPE="CF_SQL_VARCHAR">

       

      AND password=<cfqueryparam value="#arguments.password#" CFSQLTYPE='CF_SQL_VARCHAR'>

       

      AND role = 'admin'

       

      </cfquery>

       

      <cfif qSecurity.recordCount is 1>

       

      <cflogin>

       

      <cfloginuser name="#qSecurity.username#" password="#qSecurity.password#" roles="#qSecurity.role#">

       

      </cflogin>

       

      <cfreturn true>

       

      <cfelse>

       

      <cflogout>

       

      <cfreturn false>

       

      </cfif>

       

      </cffunction>

       

      <cffunction name="getAdminData" access="remote" roles="admin" returntype="string">

      <

      cfreturn "Some Secure Admin Data!">

       

      </cffunction>

       

       

      <cffunction name="getUserData" access="remote" roles="cecmo" returntype="string">

      <

      cfreturn "Some Secure User Data">

       

      </cffunction>

      </cfcomponent>

       

       

      MXML

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application

       

       

      xmlns:fx="http://ns.adobe.com/mxml/2009"

      xmlns:s="

      library://ns.adobe.com/flex/spark"

      xmlns:mx="

      library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:cflogin="services.cflogin.*" xmlns:valueObjects="valueObjects.*" xmlns:application="services.application.*">

       

       

      <s:layout>

       

       

      <s:VerticalLayout/>

       

       

      </s:layout>

       

       

      <fx:Declarations>

       

       

      <cflogin:Cflogin id="cflogin" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>

       

       

      <s:CallResponder id="loginResult" result="loginResult_resultHandler(event)"/>

       

       

      <s:CallResponder id="logoutResult" result="logoutResult_resultHandler(event)"/>

       

       

      <s:CallResponder id="getUserDataResult"/>

       

       

      <s:CallResponder id="getAdminDataResult"/>

       

       

      <application:Application id="application" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>

       

       

      <s:CallResponder id="getAuthUserResult" result="getAuthUserResult_resultHandler(event)"/>

       

       

      <s:CallResponder id="getRolesResult" result="getRolesResult_resultHandler(event)"/>

       

       

       

      </fx:Declarations>

       

       

      <fx:Script>

      <![CDATA[

       

       

      import mx.controls.Alert;

       

      import mx.events.FlexEvent;

       

      import mx.rpc.events.ResultEvent;

       

       

      protected function button_clickHandler(event:MouseEvent):void

      {

      loginResult.token = cflogin.login(emailTextInput.text,passwordTextInput.text);

      }

       

       

       

      protected function txtUsername_creationCompleteHandler(event:FlexEvent):void

      {

      getAuthUserResult.token = application.getAuthUser();

      }

       

       

       

      protected function txtRoles_creationCompleteHandler(event:FlexEvent):void

      {

      getRolesResult.token = application.getRoles();

      }

       

       

       

      protected function btnLogout_clickHandler(event:MouseEvent):void

      {

      logoutResult.token = application.logout();

      }

       

       

       

      protected function logoutResult_resultHandler(event:ResultEvent):void

      {

       

       

      this.txtUsername.text = this.getAuthUserResult.lastResult;

       

      this.txtRoles.text = this.getRolesResult.lastResult;

      }

       

       

      protected function loginResult_resultHandler(event:ResultEvent):void

      {

       

       

      this.txtUsername.text = this.getAuthUserResult.lastResult;

       

      this.txtRoles.text = this.getRolesResult.lastResult;

      }

       

       

      protected function getAuthUserResult_resultHandler(event:ResultEvent):void

      {

       

       

      this.txtUsername.text = this.getAuthUserResult.lastResult;

      }

       

       

      protected function getRolesResult_resultHandler(event:ResultEvent):void

      {

       

       

      this.txtRoles.text = this.getRolesResult.lastResult;

      }

       

       

      protected function btnUserData_clickHandler(event:MouseEvent):void

      {

      getUserDataResult.token = cflogin.getUserData();

      }

       

       

       

      protected function btnAdminData_clickHandler(event:MouseEvent):void

      {

      getAdminDataResult.token = cflogin.getAdminData();

      }

       

      ]]>

       

       

      </fx:Script>

       

       

      <s:Label text="UserName" id="lblUserName"/>

       

       

      <s:Label id="txtUsername" creationComplete="txtUsername_creationCompleteHandler(event)" text="{getAuthUserResult.lastResult}"/>

       

       

      <s:Label text="Roles" id="lblRoles"/>

       

       

      <s:Label id="txtRoles" creationComplete="txtRoles_creationCompleteHandler(event)" text="{getRolesResult.lastResult}"/>

       

       

      <mx:Form defaultButton="{button}">

       

       

      <mx:FormItem label="Email">

       

       

      <s:TextInput id="emailTextInput"/>

       

      </mx:FormItem>

       

      <mx:FormItem label="Password">

       

      <s:TextInput id="passwordTextInput"/>

       

      </mx:FormItem>

       

      <s:Button label="Login" id="button" click="button_clickHandler(event)"/>

       

      <s:Button label="Logout" id="btnLogout" click="btnLogout_clickHandler(event)"/>

       

      </mx:Form>

       

      <s:Button label="User Data" id="btnUserData" click="btnUserData_clickHandler(event)"/>

       

      <s:TextInput id="txtUserData" text="{getUserDataResult.lastResult}" width="399"/>

       

      <s:Button label="Admin Data" id="btnAdminData" click="btnAdminData_clickHandler(event)"/>

       

      <s:TextInput id="txtAdminData" text="{getAdminDataResult.lastResult}" width="402"/>

      </s:Application>