From e142bd0f29982d25b0a9089d7f0ecf6a832e80ab Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 17 Mar 2015 01:05:49 -0500 Subject: [PATCH] Auth with emails, not names; don't store char names; bugfixes. --- app/controllers/users_controller.rb | 8 +++--- app/models/user.rb | 22 +++++++++++----- app/views/users/login.html.erb | 4 +-- .../20150317052416_replace_name_with_id_in_user.rb | 6 +++++ db/schema.rb | 4 +-- lib/calefaction/eve.rb | 2 +- lib/tasks/calefaction.rake | 30 ++++++++++++---------- 7 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 db/migrate/20150317052416_replace_name_with_id_in_user.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c29c30d..09f57b1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -9,14 +9,14 @@ class UsersController < ApplicationController def login if request.post? - if params[:username].blank? || params[:password].blank? - flash.now[:alert] = 'Both a character name and password are required.' + if params[:email].blank? || params[:password].blank? + flash.now[:alert] = 'Both an email and a password are required.' render 'login' and return end - user = User.find_by(name: params[:username]) + user = User.find_by(email: params[:email]) if user.nil? || !user.authenticate(params[:password]) - flash.now[:alert] = 'Incorrect character name or password.' + flash.now[:alert] = 'Incorrect email address or password.' render 'login' and return end diff --git a/app/models/user.rb b/app/models/user.rb index aa05e5e..f8b516b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,12 +4,13 @@ class User < ActiveRecord::Base has_secure_password alias_attribute :admin?, :is_admin - def char_names + def name ensure_api_user + @api.scope = 'char' begin - @api.Characters.characters.map { |char| char.name } - rescue EAAL::EAALError - [] + @api.CharacterSheet(characterID: userid).name + rescue EAAL::Exception::EAALError + '?' end end @@ -25,12 +26,21 @@ class User < ActiveRecord::Base ensure_api_user @api.scope = 'char' begin - @api.CharacterSheet(names: name).corporationID.to_i - rescue EAAL::EAALError + @api.CharacterSheet(characterID: userid).corporationID.to_i + rescue EAAL::Exception::EAALError 0 end end + def characters + ensure_api_user + begin + @api.Characters.characters + rescue EAAL::Exception::EAALError + [] + end + end + private def ensure_api_user @api ||= EAAL::API.new(api_key, api_verify) diff --git a/app/views/users/login.html.erb b/app/views/users/login.html.erb index 8f3b679..33e2e32 100644 --- a/app/views/users/login.html.erb +++ b/app/views/users/login.html.erb @@ -5,8 +5,8 @@ <%= form_tag do %> - - + + diff --git a/db/migrate/20150317052416_replace_name_with_id_in_user.rb b/db/migrate/20150317052416_replace_name_with_id_in_user.rb new file mode 100644 index 0000000..1b0993f --- /dev/null +++ b/db/migrate/20150317052416_replace_name_with_id_in_user.rb @@ -0,0 +1,6 @@ +class ReplaceNameWithIdInUser < ActiveRecord::Migration + def change + remove_column :users, :name, :string + add_column :users, :userid, :integer, first: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 09c6889..10b8969 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150316234044) do +ActiveRecord::Schema.define(version: 20150317052416) do create_table "admin_settings", force: :cascade do |t| t.string "key" @@ -19,7 +19,7 @@ ActiveRecord::Schema.define(version: 20150316234044) do end create_table "users", force: :cascade do |t| - t.string "name" + t.integer "userid" t.string "email" t.string "password_digest" t.string "api_key" diff --git a/lib/calefaction/eve.rb b/lib/calefaction/eve.rb index e891956..4abc23e 100644 --- a/lib/calefaction/eve.rb +++ b/lib/calefaction/eve.rb @@ -17,7 +17,7 @@ module Calefaction::EVE @@api.scope = 'corp' begin @@api.CorporationSheet(corporationID: corp_id).ticker - rescue EAAL::EAALError + rescue EAAL::Exception::EAALError nil end end diff --git a/lib/tasks/calefaction.rake b/lib/tasks/calefaction.rake index 5536fb5..4391e48 100644 --- a/lib/tasks/calefaction.rake +++ b/lib/tasks/calefaction.rake @@ -13,14 +13,14 @@ namespace :calefaction do if AdminSetting.get(:corp_id).nil? puts "The database does not contain the correct seed values. You need "\ - "to run\n`rake db:reset db:setup` first." + "to run\n`rake db:reset tmp:cache:clear` first." next end if AdminSetting.get(:corp_id).to_i > 0 puts "The database is not empty; you should change settings from "\ "within the\napplication. Alternatively, you can start over with\n"\ - "`rake db:reset db:setup calefaction:setup`." + "`rake db:reset tmp:cache:clear calefaction:setup`." next end @@ -43,7 +43,7 @@ namespace :calefaction do api_verify = STDIN.gets.chomp user = User.new(api_key: api_key, api_verify: api_verify, admin?: true) - chars = user.char_names + chars = user.characters if chars.empty? puts "The given API key is invalid, has no characters, or something "\ "else is wrong\nwith the EVE API right now..." @@ -51,20 +51,21 @@ namespace :calefaction do end if chars.length == 1 - user.name = chars.first - puts "\nUsing character: #{user.name}" + puts "\nUsing character: #{chars.first.name}" + user.userid = chars.first.characterID else puts "\nChoose a character:" - chars.each_with_index do |name, i| - puts " [#{i}]: #{name}" + chars.each_with_index do |char, i| + puts " [#{i}]: #{char.name}" end - puts "Enter the number next to your chosen character: " + print "Enter the number next to your chosen character: " loop do index = STDIN.gets.to_i - break if index >= 0 && index < chars.length - puts "Bad input; try again: " + if index >= 0 && index < chars.length + user.userid = chars[index].characterID and break + end + print "Bad input; try again: " end - user.name = chars[index] end unless user.member_of? corp_id @@ -72,9 +73,12 @@ namespace :calefaction do next end - print "\nEnter your email address (used for password resets; may be blank): " + print "\nEnter your email address (required; used to log in): " user.email = STDIN.gets.chomp - user.email = nil if user.email.empty? + if user.email.blank? + puts "\nInvalid email address. Stopping." + next + end print "\nEnter your new password (to log in to the website, **NOT** for EVE!): " user.password = STDIN.noecho(&:gets).chomp
<%= label_tag('username', 'Character name') %><%= text_field_tag('username') %><%= label_tag('email', 'Email address') %><%= email_field_tag('email') %>
<%= label_tag('password', 'Password') %>