How do I register a Drupal user (or get currently a user) from Flex / Flash?

I have a Flash application hosted on a Drupal page. Some parts of the Flash application should be available to all users, but some of them should only be available to the registered user. (The special role does not matter, they are just an authorized user of the site).

Inside Flash, I can tell if a user is logged on to the screen by scraping the "? Q = user" page, but this is very fragile. What is the “correct” way to do this? I can install additional modules if needed, but they must be compatible with Drupal 6, not 5.

Likewise, if there is currently no user, how can I take the username and password they provide me and register it (or determine if the password is bad)?

+1


source to share


3 answers


I was able to do this using Drupal's "system" and "user" services:

  • install Service module
  • enable services "system" and "user"
  • for the code below, disable API Keys. (Or leave them on and provide additional arguments)
  • allow both authenticated and anonymous users to access services

At the time of this writing, AMFPHP has not yet been ported to Drupal 6, so I used XMLRPC. To use XMLRPC from ActionScript, I used the helper library in as3-rpclib . This library was written for ActionScript2, so in order to use it with Flex3 I had to schedule it as described in this page (scroll down the page and search for the May 10, 2008 comment "jameshyu").

After all of these prerequisites are met, you can verify the registered user (if any) and / or register the user with a code like the one below.



Here's the module that actually implements the login:

package
{
    import com.ak33m.rpc.xmlrpc.XMLRPCObject;

    import flash.events.*;
    import flash.net.*;

    import mx.collections.ItemResponder;
    import mx.rpc.AsyncToken;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;


    public class DrupalLogin
    {
        private var _api:XMLRPCObject;

        public function DrupalLogin( url:String )
        {
            _api = new XMLRPCObject();
            _api.endpoint = url;
            _api.destination = "";
        }

        public function set LoginResult( fn:Function ):void {_handleLoginResult = fn; }
        public function set CheckResult( fn:Function ):void {_handleCheckResult = fn; }
        public function set LogoutResult( fn:Function ):void {_handleLogoutResult = fn; }
        public function get User():String { return _user; }
        public function set TraceResult( fn:Function ):void {_handleTrace = fn; }

        private var _handleLoginResult:Function;
        private var _handleCheckResult:Function;
        private var _handleLogoutResult:Function;
        private var _handleTrace:Function;

        private function onTrace( st:String ):void
        {
            _handleTrace(st);
        }

        private var _firstCheckDone:Boolean = false;
        private var _loggedIn:Boolean = false;


        // The logged-in user ID (if any)
        private var _user:String = "";



        // *****************************************
        // doLogin
        // *****************************************

        // Function doLogin kicks off the process.
        public function doLogin( user:String, pwd:String ):void
        {
            onTrace( "******************* doLogin ********************" );

            if( !_firstCheckDone )
            {
                _handleLoginResult( false, "ALWAYS CALL doCheck() FIRST TO SEE IF YOU NEED TO LOG IN OR NOT" );
                return;
            }
            if( _loggedIn )
            {
                _handleLoginResult( true, "YOU ARE ALREADY LOGGED IN" );
                return;
            }

            var token:AsyncToken = _api.call( "user.login", _sid, user, pwd );
            var tresponder:ItemResponder = new ItemResponder(this.onLoginInfo,this.onLoginFault);
            token.addResponder(tresponder);
        }


        private function onLoginInfo (event:ResultEvent,token:Object = null):void
        {
            onTrace( "... got onLoginInfo" );

            _user = event.result.user.name;
            _loggedIn = true;
            _handleLoginResult( true, "logged in ok" );         
        }

        private function onLoginFault (event:FaultEvent, token:Object=null):void
        {
            onTrace( "   got onLoginFault" );

            _loggedIn = false;
            _handleLoginResult( false, "Fault: " + event.fault.faultString + " -- " + event.fault.faultCode);
        }


        // *****************************************
        // doLogout
        // *****************************************

        public function doLogout():void
        {
            onTrace( "******************* doLogout ********************" );

            if( !_firstCheckDone )
            {
                _handleLogoutResult( false, "ALWAYS CALL doCheck() FIRST TO SEE IF YOU ARE ABLE TO LOG OUT OR NOT" );
                return;
            }
            if( !_loggedIn )
            {
                _handleLogoutResult( true, "YOU ARE ALREADY LOGGED OUT" );
                return;
            }

            var token:AsyncToken = _api.call( "user.logout", _sid );
            var tresponder:ItemResponder = new ItemResponder(this.onLogoutInfo,this.onLogoutFault);
            token.addResponder(tresponder);
        }

        private function onLogoutInfo (event:ResultEvent,token:Object = null):void
        {
            onTrace( "got onLogoutInfo" );

            _loggedIn = false;
            _handleLogoutResult( true, "logged out ok" );           
        }

        private function onLogoutFault (event:FaultEvent, token:Object=null):void
        {
            onTrace( "got onLogoutFault" );

            _loggedIn = false;
            _handleLogoutResult( false, "Fault: " + event.fault.faultString + " -- " + event.fault.faultCode);
        }


        // *****************************************
        // doCheckLogin
        // *****************************************

        private var _sid:String;

        public function doCheckLogin():void
        {
            onTrace( "******************* doCheckLogin ********************" );

            var token:AsyncToken = _api.call( "system.connect" );
            var tresponder:ItemResponder = new ItemResponder(this.onCheckInfo,this.onCheckFault);
            token.addResponder(tresponder);

        }

        private function onCheckInfo (event:ResultEvent,token:Object = null):void
        {
            onTrace( "got onCheckInfo" );

            _user = event.result.user.name;
            _sid = event.result.user.sid;
            var roles:Object = event.result.user.roles;
            _loggedIn = false;
            for( var i:int=0; i<10; i++ )
            {
                var tmp:String = roles[i.toString()];
                if( tmp == "authenticated user" )
                    _loggedIn = true;
            }

            trace( "user = " + _user + ", sid=" + _sid + ", loggedIn=" + _loggedIn );
            _firstCheckDone = true;

            _handleCheckResult( _loggedIn, _loggedIn?("Currently logged in as " + _user):"Not logged in yet" );
        }

        private function onCheckFault (event:FaultEvent, token:Object=null):void
        {
            onTrace( "got onCheckFault" );

            _handleCheckResult( false, "Fault: " + event.fault.faultString + " -- " + event.fault.faultCode);
        }

    }
}

      

and here is an example of using it:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script source="LoginExample.as" />
    <mx:Button id="btnGoodLogin" click="btnGoodLogin_onClick()"  label="Good Login" enabled="true" y="28"/>
    <mx:Button id="btnBadLogin" click="btnBadLogin_onClick()"  label="Bad Login" enabled="true" y="28" x="112"/>
    <mx:Button id="btnLogout" click="btnLogout_onClick()"  label="Logout" enabled="true" y="28" x="219"/>
    <mx:Button id="btnCheck" click="btnCheck_onClick()"  label="Check" enabled="true" y="28" x="325"/>
    <mx:Text id="txtResult"  y="58" width="263"/>
</mx:Application>





import flash.events.*;
import flash.net.*;

private var _login:DrupalLogin;

private function setup():void
{
    if( _login==null )
    {
        var url:String = "http://myserver/mydrupal?q=services/xmlrpc";

        _login = new DrupalLogin(url);
        _login.CheckResult = handleCheckResult;
        _login.LoginResult = handleLoginResult;
        _login.LogoutResult = handleLogoutResult;
        _login.TraceResult = handleTraceResult;
    }
}

private function btnGoodLogin_onClick():void
{
    setup();
    _login.doLogin( "goodname", "goodpwd" );
}

private function btnBadLogin_onClick():void
{
    setup();
    _login.doLogin( "badname", "badpwd" );
}

private function btnLogout_onClick():void
{
    setup();
    _login.doLogout();
}

private function btnCheck_onClick():void
{
    setup();
    _login.doCheckLogin();
}



private function showResult( result:String):void
{
    trace( "showResult: " + result );
    txtResult.text = result;    
}

private function handleTraceResult( text:String ):void
{
    trace( text );
}



private function handleCheckResult( loggedIn:Boolean, txt:String="" ):void
{
    if( txt != "" )
        txt = " (" + txt + ")";

    if( loggedIn )
        showResult( "ALREADY LOGGED IN AS " + _login.User + txt);
    else
        showResult( "NOT LOGGED IN YET" + txt );
}

private function handleLoginResult( loggedIn:Boolean, txt:String="" ):void
{
    if( txt != "" )
        txt = " (" + txt + ")";

    if( loggedIn )
        showResult( "LOGIN ATTEMPT SUCCEEDED" + txt);
    else
        showResult( "LOGIN ATTEMPT FAILED" + txt );
}

private function handleLogoutResult( loggedOut:Boolean, txt:String="" ):void
{
    if( txt != "" )
        txt = " (" + txt + ")";

    if( loggedOut )
        showResult( "LOGOUT ATTEMPT SUCCEEDED" + txt );
    else
        showResult( "LOGOUT ATTEMPT FAILED" + txt);
}

      

+2


source


"Correct solution

you probably want to look at something like a service module . basically, you want to expose custom services to your flash application, so you can send requests (via XML-RPC or any other implementation) to get information like "what access does the current logged in user have" or "login this user" with username x and password y "and so on.

Do it yourself quickly

if you are familiar with module development, you can quickly create your own module just to get the functionality you need, without having to go through the "real" web services layer. here's a quick example creating a module called "myservices":



<?php
// myservices.info
   name = My Services
   description = Expose basic services
   core = 6.x

<?php
// myservices.module

   function myservices_menu() {
       $items['myservices/user'] = array(
         'title' => 'Get auth',
         'page callback' => 'myservices_get_user',
         'access arguments' => array('access content'),
       );
       return $items;
   }

   function myservices_get_user() {
       global $user;
       if (in_array('authenticated user', $user->roles) ) {
           print 'yes';
       } else {
           print 'no';
       }
   }

      

put these two files in the "myservices" directory in your modules directory, activate it and then go to http://yourdomain.com/myservices/user

if you are authenticated it will just return yes

, if not no

, etc ... which you can pick up in your flash app.

+4


source


I know this is a far cry from when this post was originally made, but if you are still interested in this and haven't taken the time to research it, I figured out how to successfully handle login via flash document. (The one that's built in or is external for that matter.) It doesn't even require AMFPHP to be installed on the system. (This is good because the last time I checked the maintenance module that AMFPHP depends on does not yet have the recommended status.)

Here I have set up a demo page: AS3 Login Page

You can login and log out and it will find that you are already logged in. This is still ongoing, but you need most of the material.

If you'd like to see the documentation and want a copy of the source, it's all available here: www.andrewnprice.com

Hope this helps. I have to update this demo sometime next week, so check back if you want to use some of the other features that I mention in the post.

0


source







All Articles