From fcfc56aab5bd6715995aa135bce7f9caa97c8f49 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 14 Mar 2015 21:33:25 -0500 Subject: [PATCH] Make user setup work fully, thanks to EAAL. --- Gemfile | 1 + Gemfile.lock | 10 +++++++ app/models/user.rb | 24 ++++++++++++++-- config/initializers/eaal.rb | 3 ++ lib/tasks/calefaction.rake | 70 +++++++++++++++++++++++++++++---------------- 5 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 config/initializers/eaal.rb diff --git a/Gemfile b/Gemfile index 6c975d8..0575e26 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ gem 'sqlite3' gem 'uglifier', '>= 1.3.0' gem 'jquery-rails' gem 'bcrypt', '~> 3.1.7' +gem 'eaal', '~> 0.1.12' # At this point, avoid using SASS or CoffeeScript # gem 'sass-rails', '~> 5.0' diff --git a/Gemfile.lock b/Gemfile.lock index eb79d67..f9af89d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -48,11 +48,18 @@ GEM columnize (0.9.0) debug_inspector (0.0.2) debugger-linecache (1.2.0) + eaal (0.1.12) + faraday (>= 0.8.4) + hpricot (>= 0.6) + memcache-client (>= 1.7.1) erubis (2.7.0) execjs (2.4.0) + faraday (0.9.1) + multipart-post (>= 1.2, < 3) globalid (0.3.3) activesupport (>= 4.1.0) hike (1.2.3) + hpricot (0.8.6) i18n (0.7.0) jquery-rails (4.0.3) rails-dom-testing (~> 1.0) @@ -63,10 +70,12 @@ GEM nokogiri (>= 1.5.9) mail (2.6.3) mime-types (>= 1.16, < 3) + memcache-client (1.8.5) mime-types (2.4.3) mini_portile (0.6.2) minitest (5.5.1) multi_json (1.11.0) + multipart-post (2.0.0) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) rack (1.6.0) @@ -128,6 +137,7 @@ PLATFORMS DEPENDENCIES bcrypt (~> 3.1.7) byebug + eaal (~> 0.1.12) jquery-rails rails (= 4.2.0) sqlite3 diff --git a/app/models/user.rb b/app/models/user.rb index cf5a20d..cab51c1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,9 +1,29 @@ -# require 'calefaction/api/eveonline' +require 'eaal' class User < ActiveRecord::Base has_secure_password + def char_names + ensure_api_user + begin + @api.Characters.characters.map { |char| char.name } + rescue EAAL::EAALError + [] + end + end + def member_of?(corp_id) - false + ensure_api_user + @api.scope = 'char' + begin + @api.CharacterSheet(names: name).corporationID.to_i == corp_id + rescue EAAL::EAALError + false + end + end + + private + def ensure_api_user + @api ||= EAAL::API.new(api_key, api_verify) end end diff --git a/config/initializers/eaal.rb b/config/initializers/eaal.rb new file mode 100644 index 0000000..3fdd1c1 --- /dev/null +++ b/config/initializers/eaal.rb @@ -0,0 +1,3 @@ +require 'eaal' + +EAAL.cache = EAAL::Cache::FileCache.new("#{Rails.root}/tmp/eaal") diff --git a/lib/tasks/calefaction.rake b/lib/tasks/calefaction.rake index b70364d..6d0dd0f 100644 --- a/lib/tasks/calefaction.rake +++ b/lib/tasks/calefaction.rake @@ -8,48 +8,68 @@ namespace :calefaction do print "\nEnter your corporation's ID (this is visible in the URL for "\ "your corp's page on \nzKillboard, among other places): " - corp_id = STDIN.gets.chomp.to_i + corp_id = STDIN.gets.to_i if corp_id <= 0 puts 'Corporation ID must be a positive integer. Stopping.' next end - print "\nEnter your character's name: " - user_name = STDIN.gets.chomp + print "\nEnter your character's API key ID (create one at\n"\ + "https://community.eveonline.com/support/api-key/createpredefined?accessMask=8): " + user_api_key = STDIN.gets.chomp + + print "\nEnter the verification code for the key you just entered: " + user_api_verify = STDIN.gets.chomp + + user = User.new(api_key: user_api_key, api_verify: user_api_verify, + is_admin: true, is_corp: true) + chars = user.char_names + if chars.empty? + puts "The given API key is invalid, has no characters, or something "\ + "else is wrong\nwith the EVE API right now..." + next + end + + if chars.length == 1 + user.name = chars.first + else + puts "\nChoose a character:" + chars.each_with_index do |name, i| + puts " [#{i}]: #{name}" + end + puts "Enter the number next to your chosen character: " + loop do + index = STDIN.gets.to_i + break unless index >= chars.length || index < 0 + puts "Bad input; try again: " + end + user.name = chars[index] + end + + unless user.member_of? corp_id + puts 'You are not a member of the given corporation. Stopping.' + next + end print "\nEnter your email address (used for password resets; may be blank): " - user_email = STDIN.gets.chomp - user_email = nil if user_email.empty? + user.email = STDIN.gets.chomp + user.email = nil if user.email.empty? print "\nEnter your new password (to log in to the website, **NOT** for EVE!): " - user_pass = STDIN.noecho(&:gets).chomp + user.password = STDIN.noecho(&:gets).chomp puts print "\nConfirm the password: " - if user_pass != STDIN.noecho(&:gets).chomp + if user.password != STDIN.noecho(&:gets).chomp puts "\nPasswords do not match. Stopping." next end puts - print "\nEnter your character's API key ID (create one at\n"\ - "https://community.eveonline.com/support/api-key/createpredefined?accessMask=8): " - user_api_key = STDIN.gets.chomp - - print "\nEnter the verification code for the key you just entered: " - user_api_verify = STDIN.gets.chomp + AdminSetting.where(key: %w(corp_name site_name)).update_all(value: corp_name) + AdminSetting.find_by(key: 'corp_id').update(value: corp_id) + user.save - User.transaction do - AdminSetting.where(key: %w(corp_name site_name)).update_all(value: corp_name) - AdminSetting.find_by(key: 'corp_id').update(value: corp_id) - user = User.new(name: user_name, email: user_email, password: user_pass, - api_key: user_api_key, api_verify: user_api_verify, - is_admin: true, is_corp: true) - unless user.member_of? corp_id - puts 'You are not a member of the given corporation. Stopping.' - raise ActiveRecord::Rollback - end - user.save - end + puts "\nDone!" end end