At the Forge - Authenticating to a Rails Application
To install a new plugin—say, acts_as_authenticated—we must provide its URL to script/plugin. This is as easy as the following:
script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
Now, what happens when you install a plugin? Rails installs it into the vendor/plugins directory, under a new directory named after the plugin. Thus, my installation of acts_as_authenticated installed a number of files into vendor/plugins/acts_as_authenticated.
In and of itself, installing the plugin doesn't change my Rails installation or add any new functionality. Rather, a plugin typically creates one or more generators, which are used to create or modify files used by the application.
In the case of acts_as_authenticated, it comes with two different generators, which we can see by going into the generators subdirectory. Here, there are two generators, named authenticated and authenticated_mailer. If we go into the authenticated directory, we see authenticated_generator, which is what defines the generator. This allows us to go to the root directory of our Rails application and type:
script/generate authenticated user account
The above tells Rails that we want to use the authenticated plugin, which it finds in the plugin directory. The other arguments to this command indicate the model (and table name) we will use (user in this case), and the controller that should be generated to handle accounts.
The generator creates a migration file, defining the columns of the Users table using Ruby for greater database independence. In order to create the columns of the database, we must run the migration:
Using my PostgreSQL database client, I now can see that the migration did its job:
atf_development=# \d users Table "public.users" Column | Type | ---------------------------+-----------------------------+ id | integer | login | character varying(255) | email | character varying(255) | crypted_password | character varying(40) | salt | character varying(40) | created_at | timestamp without time zone | updated_at | timestamp without time zone | remember_token | character varying(255) | remember_token_expires_at | timestamp without time zone |
Now that I have incorporated acts_as_authenticated into my application, I should be able to do several simple things:
Mark pages as open to the public.
Mark pages as private—that is, open only to registered users.
Allow people to register.
Allow users to log in.
Allow users to log out.
All of this is not only possible with acts_as_authenticated, but it's also quite easy. To make pages require authentication by default, we can say:
Of course, if we require that people log in before they use the login page, users will find themselves in an infinite loop. So, we can add an exception for that at the top of account_controller.rb:
before_filter :login_required, :except => [:login, :signup]
Once this filter is in place, trying to visit any page other than login or :signup bounces us back to the login page.
I'm going to register, by entering my login name, my e-mail address and my password (twice) into the registration form. Once I click on the submit button, the application inserts my data into the database. I'm in there, with ID #1, and my plain-text data as well as my encrypted data.
Moreover, after registering with the site, I am now signed in as well. I can view any page I want, without having to log in again. My login will last until I go to the /account/logout URL. Unfortunately, the default index.rhtml page that comes with acts_as_authenticated does not make it clear when you have logged out. We can check that easily by adding a line to the top, showing the contents of non-blank notices:
<p><%= flash[:notice] if not flash[:notice].blank? %></p>
We now have a basically working version of an authenticated Web server. People can register (and log in if they are already registered), and we can add both restricted and unrestricted pages via the controller and the before_filter :login_required command.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Sony Settles in Linux Battle
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Profiles and RC Files
- Maru OS Brings Debian to Your Phone
- Understanding Ceph and Its Place in the Market
- Snappy Moves to New Platforms
- What's Our Next Fight?
- Libarchive Security Flaw Discovered
- Git 2.9 Released
- The Giant Zero, Part 0.x
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide