From 92b10037cd13a6107047aa6bfd3a87e00b88cebd Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 13 Mar 2015 23:21:52 -0500 Subject: [PATCH] Finish most of the user model, add a thing to init the db and create an admin. --- Gemfile | 4 +- Gemfile.lock | 2 + README.md | 4 +- app/controllers/users_controller.rb | 8 +++- app/helpers/tools_helper.rb | 8 ++++ app/models/.keep | 0 app/models/concerns/.keep | 0 app/models/concerns/encryptor.rb | 20 ++++++++ app/models/user.rb | 16 +++++++ app/views/admin/index.html.erb | 2 +- app/views/shared/_header.html.erb | 2 +- app/views/tools/index.html.erb | 4 -- app/views/users/create.html.erb | 4 -- app/views/users/signup.html.erb | 4 ++ config/routes.rb | 6 ++- ...ate_users.rb => 20150314013929_create_users.rb} | 5 +- db/schema.rb | 11 +++-- lib/tasks/.keep | 0 lib/tasks/calefaction.rake | 55 ++++++++++++++++++++++ test/controllers/.keep | 0 test/fixtures/.keep | 0 test/fixtures/users.yml | 8 +++- test/models/.keep | 0 23 files changed, 139 insertions(+), 24 deletions(-) delete mode 100644 app/models/.keep delete mode 100644 app/models/concerns/.keep create mode 100644 app/models/concerns/encryptor.rb delete mode 100644 app/views/users/create.html.erb create mode 100644 app/views/users/signup.html.erb rename db/migrate/{20150311175036_create_users.rb => 20150314013929_create_users.rb} (65%) delete mode 100644 lib/tasks/.keep create mode 100644 lib/tasks/calefaction.rake delete mode 100644 test/controllers/.keep delete mode 100644 test/fixtures/.keep delete mode 100644 test/models/.keep diff --git a/Gemfile b/Gemfile index 0b3c36f..6c975d8 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gem 'rails', '4.2.0' gem 'sqlite3' gem 'uglifier', '>= 1.3.0' gem 'jquery-rails' +gem 'bcrypt', '~> 3.1.7' # At this point, avoid using SASS or CoffeeScript # gem 'sass-rails', '~> 5.0' @@ -21,9 +22,6 @@ gem 'jquery-rails' # bundle exec rake doc:rails generates the API under doc/api. # gem 'sdoc', '~> 0.4.0', group: :doc -# Use ActiveModel has_secure_password -# gem 'bcrypt', '~> 3.1.7' - group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index a9e4335..eb79d67 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,6 +37,7 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) arel (6.0.0) + bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) @@ -125,6 +126,7 @@ PLATFORMS ruby DEPENDENCIES + bcrypt (~> 3.1.7) byebug jquery-rails rails (= 4.2.0) diff --git a/README.md b/README.md index 413dcf4..a096ee7 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,6 @@ Installing calefaction can be deployed as a standard Rails app. -- describe db setup here... +To set up the database and create your admin account, run: + + rake db:setup calefaction:setup diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d516673..02d3f97 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,11 @@ class UsersController < ApplicationController - def create + def login end - def login + def signup + end + + def create + render 'signup' end end diff --git a/app/helpers/tools_helper.rb b/app/helpers/tools_helper.rb index 6f87959..98e1e37 100644 --- a/app/helpers/tools_helper.rb +++ b/app/helpers/tools_helper.rb @@ -1,2 +1,10 @@ module ToolsHelper + TOOLS = [ + {:name => :campaigns}, + {:name => :recruitment}, + {:name => :stratmap}, + {:name => :tspsolver}, + {:name => :navyinfo}, + {:name => :combatsim} + ] end diff --git a/app/models/.keep b/app/models/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/app/models/concerns/encryptor.rb b/app/models/concerns/encryptor.rb new file mode 100644 index 0000000..7071fc1 --- /dev/null +++ b/app/models/concerns/encryptor.rb @@ -0,0 +1,20 @@ +module Encryptor + extend ActiveSupport::Concern + + class_methods do + def encrypt(value) + ensure_encryptor + @crypt.encrypt_and_sign(value) + end + + def decrypt(value) + ensure_encryptor + @crypt.decrypt_and_verify(value) + end + + private + def ensure_encryptor + @crypt ||= ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base) + end + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 4a57cf0..d12be1b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,18 @@ +# require 'calefaction/api/eveonline' + class User < ActiveRecord::Base + include Encryptor + has_secure_password + + def api_verify + self.class.decrypt(super()) + end + + def api_verify=(value) + super(self.class.encrypt(value)) + end + + def member_of?(corp_id) + false + end end diff --git a/app/views/admin/index.html.erb b/app/views/admin/index.html.erb index 103ae36..4134a7a 100644 --- a/app/views/admin/index.html.erb +++ b/app/views/admin/index.html.erb @@ -1,6 +1,6 @@ <% provide(:title, 'Admin') %> -

Admin#index

+

Admin Settings

<%= form_tag do %> diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index 94b0c84..333715f 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -6,7 +6,7 @@ <% end %> - <%= link_to "Signup", controller: "users", action: "create" %> + <%= link_to "Signup", controller: "users", action: "signup" %> • <%= link_to "Login", controller: "users", action: "login" %> • diff --git a/app/views/tools/index.html.erb b/app/views/tools/index.html.erb index 8027295..ce9f6de 100644 --- a/app/views/tools/index.html.erb +++ b/app/views/tools/index.html.erb @@ -1,5 +1 @@ -

Tools#index

- -

Tools: ...

-

<%= AdminSetting.get('description') %>

diff --git a/app/views/users/create.html.erb b/app/views/users/create.html.erb deleted file mode 100644 index 49927e6..0000000 --- a/app/views/users/create.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -<% provide(:title, 'Signup') %> - -

Users#create

-

Find me in app/views/users/create.html.erb

diff --git a/app/views/users/signup.html.erb b/app/views/users/signup.html.erb new file mode 100644 index 0000000..115fe7f --- /dev/null +++ b/app/views/users/signup.html.erb @@ -0,0 +1,4 @@ +<% provide(:title, 'Signup') %> + +

Users#signup

+

Find me in app/views/users/signup.html.erb

diff --git a/config/routes.rb b/config/routes.rb index 9931e04..4d0d976 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,16 @@ Rails.application.routes.draw do root 'tools#index' - get '/signup' => 'users#create' get '/login' => 'users#login' + get '/signup' => 'users#signup' + post '/signup' => 'users#create' get '/admin' => 'admin#index' post '/admin' => 'admin#update' + # routes for each tool go here, e.g.: + # get 'tools#campaigns' + # Example of named route that can be invoked with purchase_url(id: product.id) # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase diff --git a/db/migrate/20150311175036_create_users.rb b/db/migrate/20150314013929_create_users.rb similarity index 65% rename from db/migrate/20150311175036_create_users.rb rename to db/migrate/20150314013929_create_users.rb index a19be02..4d7a511 100644 --- a/db/migrate/20150311175036_create_users.rb +++ b/db/migrate/20150314013929_create_users.rb @@ -3,8 +3,11 @@ class CreateUsers < ActiveRecord::Migration create_table :users do |t| t.string :name t.string :email - t.string :password_hash + t.string :password_digest + t.string :api_key + t.string :api_verify t.boolean :is_admin + t.boolean :is_corp t.timestamps null: false end diff --git a/db/schema.rb b/db/schema.rb index eca9a72..32785a9 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: 20150313054643) do +ActiveRecord::Schema.define(version: 20150314013929) do create_table "admin_settings", force: :cascade do |t| t.string "key" @@ -21,10 +21,13 @@ ActiveRecord::Schema.define(version: 20150313054643) do create_table "users", force: :cascade do |t| t.string "name" t.string "email" - t.string "password_hash" + t.string "password_digest" + t.string "api_key" + t.string "api_verify" t.boolean "is_admin" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.boolean "is_corp" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end end diff --git a/lib/tasks/.keep b/lib/tasks/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/lib/tasks/calefaction.rake b/lib/tasks/calefaction.rake new file mode 100644 index 0000000..b70364d --- /dev/null +++ b/lib/tasks/calefaction.rake @@ -0,0 +1,55 @@ +require 'io/console' + +namespace :calefaction do + desc "Sets some initial database values and creates an admin user" + task setup: :environment do + print "Enter your corporation's name: " + corp_name = STDIN.gets.chomp + + 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 + 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 email address (used for password resets; may be blank): " + 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 + puts + + print "\nConfirm the password: " + if user_pass != 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 + + 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 + end +end diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index bea6138..05c0f76 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -3,11 +3,15 @@ one: name: MyString email: MyString - password_hash: MyString + password_digest: <%= BCrypt::Password.create('secret') %> + api_key: MyString is_admin: false + is_corp: false two: name: MyString email: MyString - password_hash: MyString + password_digest: <%= BCrypt::Password.create('secret') %> + api_key: MyString is_admin: false + is_corp: false diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29..0000000