Jan 13

Part 4 – Flex Cairngorm/WebORB Issue Tracker Tutorial – Invoking ActiveRecord Methods Directly From Flex

Tag: Flex and Rails, tutorialDerek Wischusen @ 11:52 pm

This is the fourth post in a series that covers certain features of Issue Tracker sample app (view, source) that I added to the Flex RoR SDK.

The first post explained how to set up the application.

The second post covered how you can get WebORB for Rails to return typed objects to Flex using the [RemoteClass] metadata tag.

The third post covered how to use ActiveRecord Associations with WebORB.

This post explains how you can invoke ActiveRecord methods directly inside of a Flex client.

WebORB gives you the ability call ActiveRecord methods from inside of Flex. Here is all you have to do to make this happen:

1. To do this you first set up a RemoteObject in Flex just like you would for any other service. For example, the following code is from the Services.mxml file in flex_issue_tracker2 project:

<mx:RemoteObject id="userService" destination="userServiceImpl"
		    showBusyCursor="true
                       result="event.token.resultHandler( event );"
		    fault="event.token.faultHandler( event );">
</mx:RemoteObject>

2. Open up the remoting-config.xml file (located in C:\rails\rails_issue_tracker2\config\WEB-INF\flex) and map the RemoteObject directly to an ActiveRecord class. This is the mapping for the userService RemoteObject:

    <destination id="userServiceImpl">
        <properties>
            <source>User</source>
        </properties>
    </destination>

Now when you make a method call on the userService RemoteObject it as though you are making the call directly on the User ActiveRecord class. This makes it possible to do some interesting things, such as calling ActiveRecord dynamic finder methods directly on the userService, which is what is currently happening in the UserDelegate class in Flex:

package com.rxr.issuetracker.business
{
import mx.rpc.IResponder;
import com.adobe.cairngorm.business.ServiceLocator;

public class UserDelegate
{
	private var responder : IResponder;
	private var service : Object;

	public function UserDelegate(responder : IResponder)
	{
	      this.service = ServiceLocator.getInstance().getService( "userService" );
	      this.responder = responder;
	}

	public function loginUser(username : String, password : String) : void
	{
	      var call : Object = service.find_by_username_and_password(username, password);
	      call.resultHandler = responder.result;
	      call.faultHandler = responder.fault;
	}

	public function registerUser(username : String, password : String) : void
	{
	      var call : Object = service.find_or_create_by_username_and_password(username, password);
	      call.resultHandler = responder.result;
	      call.faultHandler = responder.fault;
	}
}
}

ActiveRecord dynamic finders are finder methods that are automatically generated by Rails based on the column names in the table that the ActiveRecord wraps. For example, the users table has three columns: id, username, and password. So, the User ActiveRecord class automatically generates a method called find_by_username, and a method called find_by_username_and_password, and so on. Click here to read an article that provides an in-depth explanation of how dynamic finders work.

As you can see, there are two dynamic finders that are currently being used in the UserDelegate class: find_by_username_and_password(username, password); in the loginUser method which finds an existing user, and find_or_create_by_username_and_password(username, password); in the registerUser method which creates a new user if it does not find one that matches the provided username and password.

Ok, now that you know how invoke ActiveRecord methods inside of Flex I should note that I think that you should only do this where it really makes sense to do so. The obvious drawback to this approach is that you have to make a call to the server for each method that you invoke. In general, it is best to keep this type of logic in a server-side service class (like the ProjectService and the IssueService). I’ve only included this functionality in Issue Tracker Sample app to show that it is possible. I plan to remove this code in the next update of the application and move into a UserService class.

5 Responses to “Part 4 – Flex Cairngorm/WebORB Issue Tracker Tutorial – Invoking ActiveRecord Methods Directly From Flex”

  1. maxkar says:

    2. Open up the remote-services.xml file (located in C:\rails\rails_issue_tracker2\config\WEB-INF\flex) and map the RemoteObject directly to an ActiveRecord class. This is the mapping for the userService RemoteObject:

    Correction to file name:
    remote-services.xml = remoting-config.xml

  2. Derek Wischusen says:

    Right you are. Thank you for spotting the error.

  3. Tyler Larson says:

    This seems like a bad idea to open access to classes on the back end. What happens when the person decompiles the file and then writes something that calls service.find_by_username(username).delete() or some other function they shouldn’t. Maybe we can set up something to prevent these things on the server but it seems like this might open up big holes in security. If there is an answer I might convert tomorrow but this could be very bad.

  4. Nerdmaster says:

    I think this is a great concept, but I have to agree with Tyler. You’d have to be very careful to only open some of the methods, and be amazingly restrictive about what you returned. Once you have access to an object, you can access almost anything related to that object. Imagine calling service.find_by_username_and_password(’valid_user’, ‘valid_password’).class.find(:all) to get a full list of users and passwords.

    Additionally, even if you only expose certain methods (find_by_username_and_password), if the user is able to see your internal data structure, they might now know that your DB has a field “is_superuser” and knows better how to attack you via other methods.

    I imagine WebORB has a lot of security options, but my problem would be forgetting to secure that one, seemingly-harmless method that turns out to give a hacker exactly what he needs to totally trash my app’s data.

  5. Prem says:

    I really like this project and I downloaded the code and looking thorough it . I cant seem to find the UserService in any of the code. Also it would be useful to have a SQL Script to create the db if thats available.

    Thanks much again
    Prem

Leave a Reply

Valium online uk
Pfizer viagra price
Propecia information
Buy brand name viagra
Viagra ordering
Viagra price canada
Phentermine hcl without prescription
Prescription viagra canada
Viagra sale uk
Buy pfizer viagra without prescription
Ordering cialis online
Valium 10 mg
10mg prednisone
Tramadol no prescription overnight delivery
Authentic phentermine 37.5
Valium cheapest
Propecia best prices
Buy tramadol hcl
Buy xanax cheap online
Prednisone 40 mg
Generic tramadol
Purchase cialis without a prescription
Order cheap viagra online
Best price tramadol
Discount viagra usa
Propecia generic online
Buy propecia online without a prescription
Overnight delivery viagra
Propecia cost
Cialis price
Cheapest cialis professional
Xanax no rx
Generic viagra 100mg
Buy valium cheap online
Buy tramadol cod
Australia viagra online
Best way to take tramadol
Order cheap phentermine
40 mg prednisone side effects
Cheap phentermine without prescription
Cialis online canadian pharmacy
Cheap tramadol cod
Viagra canada prices
Viagra cheap no prescription
Cialis 20 mg dosage
Valium online fast delivery
Buy viagra online cheap
Propecia online uk
Tramadol cod delivery
Prescriptions for phentermine
Valium drug side effects
Levitra 20mg
Tramadol pharmacy
Viagra cheapest
Order tramadol overnight
Cialis order online
Buy valium no rx
Cialis dosage 20mg
Buying viagra in new zealand
Genuine viagra online
Viagra professional online
Purchase levitra online
Brand name cialis
Cheapest place to buy viagra online
Prescription viagra uk
Tramadol dosage
Phentermine 37.5mg side effects
Ordering propecia from canada
Cialis prescription cost
Dose of xanax
Purchase tramadol without prescription
Viagra india price
Buy viagra from canada
Free cialis samples
Buy phentermine 37.5mg online
Tramadol online no prescription overnight
Discount viagra pills
Cheap 100mg viagra
Propecia generic canada
Canada viagra
Xanax buy uk
Phentermine 37.5 buy online
Where to buy cialis without prescription

Viagra generic cheap
Buy cheap viagra online uk
Cheapest levitra
Prescription propecia
Phentermine buy uk
Where can i buy viagra in the uk
Prednisone tablets
Levitra price
Cheapest generic viagra online
Cialis 20mg side effects
Cheapest online cialis
Valium without prescription
Buy phentermine 37.5mg pills
Prednisone online
Phentermine without a prescription
Phentermine 37.5 pills
Phentermine purchase online
How to buy phentermine online
Purchase xanax
Buy propecia
Mail order phentermine
Buy cheap valium online
Levitra purchase
Prednisone dosages
Cheapest cialis price
Online valium without prescription
Valium no rx
Buy generic cialis uk
Levitra online
Phentermine 37.5mg
Buy levitra online canada
Cialis purchase online
Buy viagra online uk no prescription
Viagra online shop
Viagra buy online no prescription
Generic levitra uk
Discount viagra india
Buying valium in spain
Order prednisone no prescription
Viagra online uk
Viagra prescription cost
Propecia uk pharmacy
Phentermine online free shipping
Generic cialis overnight
Best prices for cialis
Buy generic xanax no prescription
Best levitra prices
Best viagra dose
Buy viagra online in ireland
No prescription cialis online
100mg tramadol effects
Order tramadol online cod
Buy xanax overnight
Blood pressure and prednisone
Viagra lowest prices
Pharmacy tramadol
Xanax 1 mg dose
2.5mg cialis
Xanax 1mg side effects
Canada pharmacy valium
Order xanax cod
100mg tramadol online
Australia viagra prescription
Drug phentermine
Buy viagra uk no prescription
Purchase phentermine online
Levitra online cheap
Levitra canada
Cialis canada no prescription
Buy viagra 100mg
Viagra to buy
Prednisone tablets 10 mg
Free cialis online
Viagra no prescription online
Buying cialis
Purchase phentermine
Xanax generic dosage
Prednisone 20mg side effects
Buy valium without prescription uk
Buy xanax 2mg no prescription
Generic xanax xr
Tramadol cheapest
Xanax online cheap
Tramadol prescription online
Fedex tramadol
Where to buy cialis online
Xanax no prescription required
Where to buy viagra online
Tramadol without prescription overnight delivery
Buy tramadol hydrochloride
Cialis samples canada
Phentermine with no prescription
Best price cialis
Prescription phentermine online
Xanax bars side effects
Viagra 50 mg online without prescription
Viagra generic
Viagra prescription
Valium online pharmacy
Viagra pharmacy uk
Propecia 1mg generic
Cialis over the counter
Generic xanax no prescription
Where to buy phentermine cheap
Buy xanax online without prescription
Tramadol free shipping
Buy propecia cheap
Viagra without prescription uk
Viagra express delivery
Propecia ireland
Xanax price per pill
Buying viagra in london
Xanax 0.5 mg
Cheap cialis soft tabs
Prescription valium
Generic cialis uk
Buy phentermine hcl 37.5 no prescription
Dosage of xanax
Buying viagra online
Buying xanax online without prescription
Generic viagra sales
Where to buy propecia in canada
Cialis prices uk
Buy levitra
Phentermine 37.5 wholesale
Valium online overnight
Buy cialis in the uk
Buy cialis viagra
Buy viagra australia
Phentermine buy australia
Viagra shop online
Real phentermine without prescription
Phentermine diet pills without prescription
Buy phentermine online without prescription
Order tramadol online overnight
Viagra pharmacy prices
Viagra in france
Phentermine canadian pharmacy
Brand viagra cheap
Cialis medication
Viagra fast delivery
Overnight tramadol no prescription
Viagra purchase uk
Buy viagra online in australia
Online prescription tramadol
Get viagra
Order xanax online
Viagra canada mastercard
Viagra in the uk
Buying prednisone online
Phentermine online uk
How to buy valium without a prescription
Cheap levitra no prescription
Cialis soft tabs online
Where to buy viagra in england
Valium generic
Viagra online cheap
Low cost levitra
Xanax with no prescription
Cheap cialis pills
Cialis discount price
Cheap generic viagra
Cheap viagra online without prescription
Xanax no prescription overnight
Viagra dosage information
Cheap levitra uk