Showing posts with label restful_authentication. Show all posts
Showing posts with label restful_authentication. Show all posts

Monday, August 31, 2009

Rails Plugins : How to Install restful_authentication ?

To use role_requirement, the prerequisite is to install the restful_authentication plugin. These are the 3 steps that I took to achieve a succesful installation.

Step 1 - Grab the plugin from github.com


chee@ibm4linux:~/workspace/crm4test$ script/plugin install git://github.com/technoweenie/restful-authentication.git

Result

Initialized empty Git repository in /home/chee/workspace/crm4test/vendor/plugins/restful-authentication/.git/
remote: Counting objects: 89, done.
remote: Compressing objects: 100% (77/77), done.
remote: Total 89 (delta 5), reused 31 (delta 2)
Unpacking objects: 100% (89/89), done.
From git://github.com/technoweenie/restful-authentication
 * branch            HEAD       -> FETCH_HEAD

Step 2 - Generate user and sessions controllers

chee@ibm4linux:~/workspace/crm4test$ script/generate authenticated user sessions

This is what you will see in the command line editor (in Ubuntu 9.04, a Gnome Terminal Window)

Ready to generate.
----------------------------------------------------------------------
Once finished, don't forget to:

- Add routes to these resources. In config/routes.rb, insert routes like:
    map.signup '/signup', :controller => 'users', :action => 'new'
    map.login  '/login',  :controller => 'sessions', :action => 'new'
    map.logout '/logout', :controller => 'sessions', :action => 'destroy'

CCH : No need to do so as the latest version automatically includes these routes to routes.rb
 ----------------------------------------------------------------------

We've create a new site key in config/initializers/site_keys.rb.  If you have existing
user accounts their passwords will no longer work (see README). As always,
keep this file safe but don't post it in public.

----------------------------------------------------------------------
      exists  app/models/
      exists  app/controllers/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/sessions
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/users
      exists  config/initializers
      exists  test/functional/
      exists  test/functional/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/user.rb
      create  app/controllers/sessions_controller.rb
      create  app/controllers/users_controller.rb
      create  lib/authenticated_system.rb
      create  lib/authenticated_test_helper.rb
      create  config/initializers/site_keys.rb
      create  test/functional/sessions_controller_test.rb
      create  test/functional/users_controller_test.rb
      create  test/unit/user_test.rb
      create  test/fixtures/users.yml
      create  app/helpers/sessions_helper.rb
      create  app/helpers/users_helper.rb
      create  app/views/sessions/new.html.erb
      create  app/views/users/new.html.erb
      create  app/views/users/_user_bar.html.erb
      exists  db/migrate
      create  db/migrate/20090831032706_create_users.rb
       route  map.resource :session
       route  map.resources :users
       route  map.signup '/signup', :controller => 'users', :action => 'new'
       route  map.register '/register', :controller => 'users', :action => 'create'
       route  map.login '/login', :controller => 'sessions', :action => 'new'
       route  map.logout '/logout', :controller => 'sessions', :action => 'destroy'

Step 3 - Modify the Project Database 

chee@ibm4linux:~/workspace/crm4test$ rake db:migrate
(in /home/chee/workspace/crm4test)
==  CreateUsers: migrating ====================================================
-- create_table("users", {:force=>true})
   -> 0.0074s
-- add_index(:users, :login, {:unique=>true})
   -> 0.0374s
==  CreateUsers: migrated (0.0464s) ===========================================

You may be interested to look at the contents of the migration file as follows :-

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table "users", :force => true do |t|
      t.column :login, :string, :limit => 40
      t.column :name, :string, :limit => 100, :default => '', :null => true
      t.column :email, :string, :limit => 100
      t.column :crypted_password,          :string, :limit => 40
      t.column :salt,     :string, :limit => 40
      t.column :created_at,  :datetime
      t.column :updated_at, :datetime
      t.column :remember_token, :string, :limit => 40
      t.column :remember_token_expires_at, :datetime


    end
    add_index :users, :login, :unique => true
  end

  def self.down
    drop_table "users"
  end
end

The Generated User & Sessions Controllers

User.rb

class UsersController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem
 

  # render new.rhtml
  def new
    @user = User.new
  end

  def create
    logout_keeping_session!
    @user = User.new(params[:user])
    success = @user && @user.save
    if success && @user.errors.empty?
            # Protects against session fixation attacks, causes request forgery
      # protection if visitor resubmits an earlier form using back
      # button. Uncomment if you understand the tradeoffs.
      # reset session
      self.current_user = @user # !! now logged in
      redirect_back_or_default('/')
      flash[:notice] = "Thanks for signing up!  We're sending you an email with your activation code."
    else
      flash[:error]  = "We couldn't set up that account, sorry.  Please try again, or contact an admin (link is above)."
      render :action => 'new'
    end
  end
end

sessions.rb

# This controller handles the login/logout function of the site. 
class SessionsController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem

  # render new.rhtml
  def new
  end

  def create
    logout_keeping_session!
    user = User.authenticate(params[:login], params[:password])
    if user
      # Protects against session fixation attacks, causes request forgery
      # protection if user resubmits an earlier form using back
      # button. Uncomment if you understand the tradeoffs.
      # reset_session
      self.current_user = user
      new_cookie_flag = (params[:remember_me] == "1")
      handle_remember_cookie! new_cookie_flag
      redirect_back_or_default('/')
      flash[:notice] = "Logged in successfully"
    else
      note_failed_signin
      @login       = params[:login]
      @remember_me = params[:remember_me]
      render :action => 'new'
    end
  end

  def destroy
    logout_killing_session!
    flash[:notice] = "You have been logged out."
    redirect_back_or_default('/')
  end

protected
  # Track failed login attempts
  def note_failed_signin
    flash[:error] = "Couldn't log you in as '#{params[:login]}'"
    logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
  end
end

Sunday, August 23, 2009

Migration : Migrating an Application from Rails 1.2.5 to Rails 2.3.3

Since Nov 2007, I had a Rails 1.2 e-CRM application running on Windows Server 2003 and powered by Apache 2 and a pack of Mongrels web servers.

A couple of weeks ago, I discovered Ubuntu 9.04 and decided to migrate the e-CRM to Rails 2.3.3 running on Ubuntu 9.04.

What have I achieved thus far ?

1. Installing Rails 2.3.3 on Ubuntu 9.04
2. Installing Mongrel, Capistrano, Passenger and Apache on Ubuntu 9.04.
3. Installing Aptana Radrails on Ubuntu 9.04
4. Since Aptana Radrails was slow, I then installed Eclipse Ganymade (3.4.2) and managed to install the latest Aptana Radrails .1.30 as a plugin to Eclipse

When I attempted to run e-CRM on Rails 1.2, I ran into a number of incompatibilities including the need to update the following Rails Plugins

1. active_scaffold
2. active_scaffold_export
3. active_scaffold_tools (still unable to get a version compatible with Rails 2.3.3)
4. role_requirement
5. use restful_authentication instead of acts_as_authenticated
6. record_select
7. acts_as_audited

Then after wasting a couple of days, I discovered that Ubunti 9.04 being a Linux distro is case-sensitive and I had to pay attention to renaming the images files in the public\images folder.

Also, I used this script (command line) to auto-convert my rhtml files to html.erb as recommended for Rails 2.x

for f in $(find . -name '*.rhtml') ; do c=$(dirname $f)/$(basename $f .rhtml).html.erb ; mv $f $c ; done

Why the need to change rhtml to html.erb ?

Apparently in Rails 2.x, it’s been revealed that the .rhtml and .rxml file extensions will be deprecated in favor of .erb and .builder, respectively.
Why such a change – well, .rhtml files are not strictly html files. They are generic embedded ruby files that can be used for email templates, csv files, vcards etc… .erb is just a more generic, accurate extension for the file type. The same goes for .rxml – it can be used for more than just xml files. Think of all the rss feeds you’ve written with rxml files. Now you can stop being tied down by the limited .rxml file extension and can start using .builder.
FYI, the ‘old’ extensions won’t be fully deprecated until Rails 3.0.


BTW, this site was also useful reference in my quest to upgrade from Rails 1.2 to 2.3.3

Some additional plugins needed by active_scaffold include

a) in_place_editing - git://github.com/rails/in_place_editing.git
b) render_component


More to come...

Welcome to Rails.. Rails... Rails !

In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.

As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam

CCH
10th June 2010, 19:42