sqlite3, test coverage improvements, auto auth

This commit is contained in:
2015-10-23 19:34:59 -05:00
parent 41ceccc5b5
commit 7ddf93578e
15 changed files with 143 additions and 83 deletions

View File

@ -10,21 +10,44 @@ class OauthsController < ApplicationController
@user = login_from(provider)
if @user
redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
redirect_to root_path, notice: login_msg(@user, provider)
else
msg = "Your account must be pre-approved. Please contact the administrator."
redirect_to root_path, notice: msg
@user = auth_and_login(provider)
redirect_to root_path, notice: login_msg(@user, provider)
end
end
private
# def create_and_login provider
# @user = create_from(provider)
# reset_session # protect from session fixation attack
# auto_login(@user)
# redirect_to root_path, notice: "Logged in from #{provider.titleize}!"
# end
def login_msg user, provider = 'oAuth'
if user.active?
"Logged in from #{provider.titleize}!"
else
"Your account must be activated by an administrator."
end
end
def auth_and_login provider
user = create_auth_from(provider, auth_info)
reset_session # protect from session fixation attack
auto_login(user) if user.active?
user
end
def create_auth_from provider, auth
user = Person.find_by_email auth[:user_info]["email"]
user.authentications.create(provider: provider, uid: auth[:uid])
user
end
def auth_info
@auth_info ||= google_hash
end
def google_hash
ga = Sorcery::Providers::Google.new
ga.get_user_hash access_token
end
def auth_params
params.permit(:code, :provider)

View File

@ -1,3 +1,4 @@
class Authentication < ActiveRecord::Base
belongs_to :user
belongs_to :person
validates :uid, presence: true, uniqueness: { scope: :provider }
end

View File

@ -11,7 +11,7 @@ class Person < ActiveRecord::Base
validates :phone, presence: true
scope :with_name, lambda { |name|
where("concat(first_name, ' ', last_name) RLIKE ?", name)
where("first_name || ' ' || last_name LIKE ?", "%#{name}%")
}
scope :just_parents, lambda {
@ -34,6 +34,10 @@ class Person < ActiveRecord::Base
id
end
def active?
activation_state == "active"
end
private
## SorceryCore expects the model to hold a crypted_password field

View File

@ -1,3 +1,3 @@
%h2 Edit #{@user.name}
= render partial: 'form', locals: {form_action: edit_user_path}
= render partial: 'form', locals: { form_action: edit_user_path }

View File

@ -1,3 +1,3 @@
%h2 Register a new User
= render partial: 'form', locals: {form_action: add_user_path}
= render partial: 'form', locals: { form_action: add_user_path }