Rails, OpenID, and Acts as Authenticated

5 Mar 2007

This weekend I added OpenID to a Rails application for the first time, and this blog post describes the steps I took to integrate OpenID with Acts as Authenticated for account creation and access.

First I installed David’s OpenID Rails plugin (as discussed at David’s blog) into my application which was already using AAA to handle account creations and logins. I then created the following migration to add the OpenID identity URL to my user model:

class AddOpenId < ActiveRecord::Migration
  def self.up
    add_column :users, :identity_url, :string
  end

  def self.down
    remove_column :users, :identity_url
  end
end

And I changed the User model to allow accounts to be created either with login/email/password or with only an identity url (only changed lines are listed):

class User < ActiveRecord::Base
  validates_presence_of :login,
    :email, :if => :not_openid?
  validates_length_of :login,
    :within => 3..40, :if => :not_openid?
  validates_length_of :email,
    :within => 3..100, :if => :not_openid?
  validates_uniqueness_of :login, :email, :salt, :allow_nil => true

  def password_required?
    not_openid? && (crypted_password.blank? or not password.blank?)
  end
 
  def not_openid?
    identity_url.blank?
  end
end

This allows me to create User records without the usual required fields as long as the user created the account via an OpenID login.

And finally, the controller changes:

class AccountController < ApplicationController
  def login
    if using_open_id?
      open_id_authentication
    elsif params[:login]
      password_authentication(params[:login], params[:password])
    end
  end

  protected
 
    def password_authentication(login, password)
      if self.current_user = User.authenticate(params[:login], params[:password])
        successful_login
      else
        failed_login("Invalid login or password")
      end
    end
 
    def open_id_authentication
      authenticate_with_open_id do |result, identity_url|
        if result.successful?
          if self.current_user = User.find_or_create_by_identity_url(identity_url)
            successful_login
          else
            failed_login "Sorry, no user by that identity URL exists (#{identity_url})"
          end
        else
          failed_login result.message
        end
      end
    end

  private
 
    def successful_login
      redirect_back_or_default(index_url)
      flash[:notice] = "Logged in successfully"
    end

    def failed_login(message)
      redirect_to(:action => ‘login’)
      flash[:warning] = message
    end
end

That’s it! You can see it in action at the Rails plugin directory.

Update
I updated this code to match the plugin changes that were made between the time I installed the plugin and the time I posted this entry. :)

Update 2
I made another change to the code based on Geoff’s comment. Thanks, Geoff!



Pioneers vs. Planners

20 Feb 2007

Ryan Carson has a post about why you should ditch your freelancer. Having recently joined the ranks of full-time freelancers, I read his thoughts with some interest. Courtenay had something to say about it as well. There’s a little something missing from the discussion, though, and that’s the concept of Pioneers vs. Planners.

When you are creating a new product, or a significant revision of a product, you need developers who are Pioneers. These are developers who enjoy tackling new problems, are good at translating ideas (often more nebulous than detailed requirements) and business processes into code, deal with ambiguity fairly well, and basically can be given a rough direction and be expected to find a way to the correct destination. Pioneers are the ones you want to have on board to help you build your next big thing.

When your development efforts are spent more on bug fixes, small feature additions, and maintaining your product, you need City Planners. These developers are good at following and extending what’s been done before, enjoy the puzzle of figuring out what an application actually does vs. what it’s supposed to do, and thrive on having a specific set of instructions to delineate exactly what is to be done. City Planners are the developers you need to help keep things going smoothly.

Some developers are good at (and/or enjoy being) both Pioneers and Planners. Most aren’t. Most prefer one of the two roles, and have to move out of their preferred work environment to take on the other role. As an aside, this is often what you see at startups when the original developers start leaving the company—from their point of view, all the fun, new projects get replaced with boring maintenance work.

It’s entirely possible that the freelancers with whom Ryan worked had other clients competing for their time or they simply weren’t reliable for long-term work. It’s also possible (and I’d say, likely) that he hired Pioneers to build his apps and then expected those Pioneers to become Planners when it suited his needs. When that didn’t work out, he went and found Planners (maintenance developers) in the form of an offshore team. I’d wager that the next time he has an idea for a brand new application, he’ll find he again wants some of those expensive and talented developers to build it for him.

As a Pioneer myself, this suits me just fine. Feel free to keep sending me fun, new projects that need that talent to be successful. When your project gets to maintenance mode, I’ll be happy to help advise you when you are ready to start looking for some Planners.



10,000 Rails developers

2 Feb 2007

I was just reviewing the Google Analytics stats for the month of January for AgileWebDevelopment.com, the home of the Rails plugin directory, and saw some interesting numbers.

The number of “absolute unique visitors” for the month was 12,295. Those visitors logged 22,814 visits and 75,890 page views.

I would guess that AWD isn’t necessarily one of the first sites you would see when you are just casually gathering info about Rails, but instead probably gets most of its visits from people who are actually using Rails. If that guess is true, then I’ll also guess there’s about 10,000 Rails developers out there (discounting the 12,295 number a bit for cases like one person at multiple computers, casual browsers who found the site but aren’t using Rails, etc.). That’s cool.



Rails developer in Seattle available

31 Jan 2007

If you are looking for a Rails developer to do some work for you, someone who has written the book on doing e-commerce with Rails :), and who has experience in every aspect of designing, developing, and deploying Rails applications, do get in touch. I’m available for projects large and small, near and far.



Meet up at RailsConf 2007

12 Jan 2007

I just launched a ConferenceMeetup site for RailsConf 2007. As I wrote in my post announcing ConferenceMeetup, this was an app inspired by what I wish I would have had when I attended RailsConf 2006 and RubyConf 2006, and I hope others attending RailsConf this year will find it useful.

If you’re planning on attending RailsConf this year, you’re welcome to create a page for yourself at the RailsConf 2007 ConferenceMeetup site. You’ll need to enter the conference password to create your account. The password is “chunky bacon” (without the quotes). With the huge turnout that’s expected to be at the conference, this may be the best way to “bump into” the people you’d like to meet while you’re there.

If you have comments or suggestions about the site, I’d love to hear them. Feel free to let me know what you think.