From 9021294c4b6b64a06bcb67222af5bfa498726cec Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sat, 25 May 2013 11:08:58 -0400 Subject: [PATCH] Auth by username instead of email Added username to User table/model --- app/components/app_tab_panel.rb | 2 +- app/components/user_stats.rb | 1 + app/components/users.rb | 1 + app/views/devise/registrations/new.html.haml | 4 ++++ app/views/devise/sessions/new.html.erb | 8 ++++---- config/initializers/devise.rb | 2 +- db/migrate/20130525143240_add_username_to_user.rb | 6 ++++++ db/schema.rb | 4 +++- spec/factories/users.rb | 4 ++++ 9 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20130525143240_add_username_to_user.rb diff --git a/app/components/app_tab_panel.rb b/app/components/app_tab_panel.rb index faf4bc8..58013a3 100644 --- a/app/components/app_tab_panel.rb +++ b/app/components/app_tab_panel.rb @@ -3,7 +3,7 @@ class AppTabPanel < Netzke::Basepack::TabPanel action :sign_out do |c| c.icon = :door_out - c.text = "Sign out #{controller.current_user.email}" if controller.current_user + c.text = "Exit" if controller.current_user end def configure(c) diff --git a/app/components/user_stats.rb b/app/components/user_stats.rb index cc52fa2..beec72c 100644 --- a/app/components/user_stats.rb +++ b/app/components/user_stats.rb @@ -4,6 +4,7 @@ class UserStats < Netzke::Base bike = user.bike %Q(
+

Username: #{user.username}

Total Hours Worked: #{user.total_hours}

Hours worked in #{Time.now.strftime('%B')}: #{user.current_month_hours}

Current bike Shop ID: #{bike.shop_id if bike}

diff --git a/app/components/users.rb b/app/components/users.rb index b844e4a..45ef596 100644 --- a/app/components/users.rb +++ b/app/components/users.rb @@ -5,6 +5,7 @@ class Users < Netzke::Basepack::Grid c.model = "User" c.columns = [ + :username, :first_name, :last_name, :nickname, diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index 49aeb22..fb3f7f3 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -2,6 +2,10 @@ %h2 Sign up = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| = devise_error_messages! + %div + = f.label :username + %br/ + = f.text_field :username %div = f.label :first_name %br/ diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 6acf013..34db7fc 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -3,8 +3,8 @@

Sign in

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %> -
<%= f.label :email %>
- <%= f.email_field :email %>
+
<%= f.label :username%>
+ <%= f.text_field :username%>
<%= f.label :password %>
<%= f.password_field :password %>
@@ -21,9 +21,9 @@ <% if Rails.env.development? %> <% User.all.each do |user| %> <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %> - <%= f.hidden_field :email, :value => user.email %>
+ <%= f.hidden_field :username, :value => user.username%> <%= f.hidden_field :password, :value => 'password' %> -
<%= f.submit "Sign in as #{user.email}" %>
+
<%= f.submit "Sign in as #{user.username}" %>
<% end %> <% end %> <% end %> diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index e0d6120..e923b33 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -26,7 +26,7 @@ Devise.setup do |config| # session. If you need permissions, you should implement that in a before filter. # You can also supply a hash where the value is a boolean determining whether # or not authentication should be aborted when the value is not present. - # config.authentication_keys = [ :email ] + config.authentication_keys = [ :username ] # Configure parameters from the request object used for authentication. Each entry # given should be a request method and it will automatically be passed to the diff --git a/db/migrate/20130525143240_add_username_to_user.rb b/db/migrate/20130525143240_add_username_to_user.rb new file mode 100644 index 0000000..9911140 --- /dev/null +++ b/db/migrate/20130525143240_add_username_to_user.rb @@ -0,0 +1,6 @@ +class AddUsernameToUser < ActiveRecord::Migration + def change + add_column :users, :username, :string + add_index :users, :username, :unique => true + end +end diff --git a/db/schema.rb b/db/schema.rb index fcb1f7c..c68d03e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130419010051) do +ActiveRecord::Schema.define(:version => 20130525143240) do create_table "bike_actions", :force => true do |t| t.string "action", :limit => 128, :null => false @@ -177,10 +177,12 @@ ActiveRecord::Schema.define(:version => 20130419010051) do t.string "first_name", :default => "", :null => false t.string "last_name", :default => "", :null => false t.string "nickname" + t.string "username" end add_index "users", ["bike_id"], :name => "index_users_on_bike_id", :unique => true add_index "users", ["email"], :name => "index_users_on_email", :unique => true add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true + add_index "users", ["username"], :name => "index_users_on_username", :unique => true end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 53c85a8..6d01799 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -1,5 +1,6 @@ FactoryGirl.define do factory :user do + sequence(:username) { |n| "user_#{n}" } sequence(:email) { |n| "user_#{n}@example.com" } password 'password' password_confirmation { password } @@ -9,16 +10,19 @@ FactoryGirl.define do association :user_role, factory: :role_user factory :staff do + username "staff" first_name 'Staff' association :user_role, factory: :role_staff end factory :admin do + username "admin" first_name 'Admin' association :user_role, factory: :role_admin end factory :bike_admin do + username "bike_admin" first_name 'BikeAdmin' association :user_role, factory: :role_bike_admin end