7: Modeling and Viewing Users, Part II
Data Matrix ECC200 Encoder In JavaUsing Barcode creation for Java Control to generate, create Data Matrix image in Java applications.
length validation; in Listing 615 we constrained the name attribute to be 50 characters or less using the :maximum option:
Bar Code Drawer In JavaUsing Barcode generator for Java Control to generate, create bar code image in Java applications.
validates :name,
Barcode Reader In JavaUsing Barcode decoder for Java Control to read, scan read, scan image in Java applications.
:presence => true, :length => { :maximum => 50 }
Creating DataMatrix In Visual C#Using Barcode drawer for VS .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
For the password length validation, instead we ve used the :within option, passing it the range1 640 to enforce the desired length constraints
DataMatrix Generation In VS .NETUsing Barcode creation for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
712 A Password Migration
Creating DataMatrix In .NET FrameworkUsing Barcode printer for VS .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
At this point, you may be concerned that we re not storing user passwords anywhere; since we ve elected to use a virtual password, rather than storing it in the database, it exists only in memory How can we use this password for authentication The solution is to create a separate attribute dedicated to password storage, and our strategy will be to use the virtual password as raw material for an encrypted password, which we will store in the database upon user signup ( 8) and retrieve later for use in user authentication ( 9) Let s plan to store the encrypted password using an encrypted_password attribute in our User model We ll discuss the implementation details in Section 72, but we can get started with our encrypted password tests by noting that the encrypted password should at the least exist We can test this using the Ruby method respond_to , which accepts a symbol and returns true if the object responds to the given method or attribute and false otherwise:
Printing ECC200 In Visual Basic .NETUsing Barcode printer for Visual Studio .NET Control to generate, create ECC200 image in .NET framework applications.
$ rails console --sandbox >> user = Usernew >> userrespond_to (:password) => true >> userrespond_to (:encrypted_password) => false
Encoding GTIN - 13 In JavaUsing Barcode printer for Java Control to generate, create GS1 - 13 image in Java applications.
1 We saw ranges before in Section 431
Data Matrix ECC200 Drawer In JavaUsing Barcode generator for Java Control to generate, create DataMatrix image in Java applications.
Insecure Passwords
Create UCC - 12 In JavaUsing Barcode drawer for Java Control to generate, create GS1 - 12 image in Java applications.
We can test the existence of an encrypted_password attribute with the code in Listing 73, which uses RSpec s respond_to helper method
Code 39 Extended Generation In JavaUsing Barcode maker for Java Control to generate, create Code 3 of 9 image in Java applications.
Listing 73 Testing for the existence of an encrypted_password attribute
Code 128A Creator In JavaUsing Barcode drawer for Java Control to generate, create ANSI/AIM Code 128 image in Java applications.
spec/models/user_specrb
Generating EAN / UCC - 8 In JavaUsing Barcode printer for Java Control to generate, create EAN-8 image in Java applications.
describe User describe "password encryption" do before(:each) do @user = Usercreate!(@attr) end it "should have an encrypted password attribute" do @usershould respond_to(:encrypted_password) end end end
Code 128 Code Set A Generator In .NET FrameworkUsing Barcode creator for VS .NET Control to generate, create Code 128 Code Set A image in .NET framework applications.
Note that in the before(:each) block we create a user, rather than just calling Usernew We could actually get this test to pass using Usernew, but (as we ll see momentarily) setting the encrypted password will require that the user be saved to the database Using create! in this first case does no harm, and putting it in before(:each) will allow us to keep all the encrypted password tests in one describe block To get this test to pass, we ll need a migration to add the encrypted_password attribute to the users table:
Making Code-39 In Visual C#Using Barcode printer for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
$ rails generate migration add_password_to_users encrypted_password:string
Code 128 Scanner In .NET FrameworkUsing Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
Here the first argument is the migration name, and we ve also supplied a second argument with the name and type of attribute we want to create (Compare this to the original generation of the users table in Listing 61) We can choose any migration name we want, but it s convenient to end the name with _to_users, since in this case Rails can automatically construct a migration to add columns to the users table Moreover, by including the second argument, we ve given Rails enough information to construct the entire migration for us, as seen in Listing 74
Bar Code Generation In .NETUsing Barcode creator for Visual Studio .NET Control to generate, create barcode image in Visual Studio .NET applications.
7: Modeling and Viewing Users, Part II
Data Matrix Scanner In .NET FrameworkUsing Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
Listing 74 The migration to add an encrypted_password column to the users table
Bar Code Generation In Visual C#Using Barcode creator for .NET Control to generate, create bar code image in VS .NET applications.
db/migrate/<timestamp>_add_password_to_usersrb
Recognize GS1 - 13 In VS .NETUsing Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
class AddPasswordToUsers < ActiveRecord::Migration def selfup add_column :users, :encrypted_password, :string end def selfdown remove_column :users, :encrypted_password end end
This code uses the add_column method to add an encrypted_password column to the users table (and the complementary remove_column method to remove it when migrating down) The result is the data model shown in Figure 72 Now if we run the migration and prepare the test database, the test should pass, since the User model will respond to the encrypted_password attribute (Be sure to close any Rails consoles started in a sandbox; the sandbox locks the database and prevents the migration from going through)
$ rake db:migrate $ rake db:test:prepare
Of course, we can run the full test suite with rspec spec/, but sometimes it s convenient to run just one RSpec example, which we can do with the -e ( example ) flag:
$ rspec spec/models/user_specrb \ > -e "should have an encrypted password attribute" 1 example, 0 failures