Преглед на файлове

Auth with emails, not names; don't store char names; bugfixes.

old-ruby
Ben Kurtovic преди 9 години
родител
ревизия
e142bd0f29
променени са 7 файла, в които са добавени 48 реда и са изтрити 28 реда
  1. +4
    -4
      app/controllers/users_controller.rb
  2. +16
    -6
      app/models/user.rb
  3. +2
    -2
      app/views/users/login.html.erb
  4. +6
    -0
      db/migrate/20150317052416_replace_name_with_id_in_user.rb
  5. +2
    -2
      db/schema.rb
  6. +1
    -1
      lib/calefaction/eve.rb
  7. +17
    -13
      lib/tasks/calefaction.rake

+ 4
- 4
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



+ 16
- 6
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)


+ 2
- 2
app/views/users/login.html.erb Целия файл

@@ -5,8 +5,8 @@
<%= form_tag do %>
<table>
<tr>
<td><%= label_tag('username', 'Character name') %></td>
<td><%= text_field_tag('username') %></td>
<td><%= label_tag('email', 'Email address') %></td>
<td><%= email_field_tag('email') %></td>
</tr>
<tr>
<td><%= label_tag('password', 'Password') %></td>


+ 6
- 0
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

+ 2
- 2
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"


+ 1
- 1
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


+ 17
- 13
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


Зареждане…
Отказ
Запис