পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Saturday, March 11, 2017

Ruby on Rails 2.1 - Basic HTTP Auth

Rails provide various ways of implementing authentication and authorization. But the simplest one is a new module which has been added in Rails 2.0. This module turned out to be a great way to do API authentication over SSL.
To use this authentication you will need to use SSL for traffic transportation. In out tutorial we are going to test it without a SSL.

Let us start with our library example we have discussed throughout of the tutorial. We do not have much to do to implement authentication. I'm going to add few lines in blue in our ~library/app/controllers/book_controller.rb:
Finally your book_controller.rb file will look like as follows −
class BookController < ApplicationController

USER_ID, PASSWORD = "zara", "pass123"
 
# Require authentication only for edit and delete operation
   before_filter :authenticate, :only => [ :edit, :delete ]
  
def list
   @books = Book.find(:all)
end
   
def show
   @book = Book.find(params[:id])
end
   
def new
   @book = Book.new
   @subjects = Subject.find(:all)
end
   
def create
   @book = Book.new(params[:book])
   if @book.save
      redirect_to :action => 'list'
   else
      @subjects = Subject.find(:all)
      render :action => 'new'
   end
end
   
def edit
   @book = Book.find(params[:id])
   @subjects = Subject.find(:all)
end
   
def update
   @book = Book.find(params[:id])
   if @book.update_attributes(params[:book])
      redirect_to :action => 'show', :id => @book
   else
      @subjects = Subject.find(:all)
      render :action => 'edit'
   end
end
   
def delete
   Book.find(params[:id]).destroy
   redirect_to :action => 'list'
end
   
def show_subjects
   @subject = Subject.find(params[:id])
end
   
private
   def authenticate
      authenticate_or_request_with_http_basic do |id, password| 
         id == USER_ID && password == PASSWORD
      end
   end
end
Let me explain these new lines −
  • First line is just to define user ID and password to access various pages.
  • Second line, I have put before_filter which is used to run the configured method authenticate before any action in the controller. A filter may be limited to specific actions by declaring the actions to include or exclude. Both options accept single actions (:only => :index) or arrays of actions (:except => [:foo, :bar]). So here we have put authentication for edit and delete operations only.
  • Because of second line, whenever you would try to edit or delete a book record, it will execute private authenticate method.
  • A private authenticate method is calling authenticate_or_request_with_http_basic method which comprises of a block and displays a dialogue box to ask for User ID and Password to proceed. If you enter a correct user ID and password then it will proceed otherwise it would display access denied.
Now try to edit or delete any available record, to do so you would have to go through authentication process using following dialogue box.
Http Basic Authentication

No comments:

Post a Comment