Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Queues - Media Ranker OAuth - Sahana Murthy #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@

# Ignore Byebug command history file.
.byebug_history

.env
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ gem 'jbuilder', '~> 2.5'
# Use the Foundation CSS framework
gem 'foundation-rails'

gem "omniauth"
gem "omniauth-github"

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
Expand All @@ -48,6 +51,8 @@ group :development, :test do

# Use pry for rails console
gem 'pry-rails'

gem 'binding_of_caller'
end

group :test do
Expand All @@ -64,6 +69,7 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
32 changes: 32 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ GEM
erubis (>= 2.6.6)
rack (>= 0.9.0)
bindex (0.5.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.3)
byebug (9.0.6)
coderay (1.1.1)
Expand All @@ -60,15 +62,23 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
debug_inspector (0.0.2)
dotenv (2.2.0)
dotenv-rails (2.2.0)
dotenv (= 2.2.0)
railties (>= 3.2, < 5.1)
erubis (2.7.0)
execjs (2.7.0)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
foundation-rails (6.3.0.0)
railties (>= 3.1.0)
sass (>= 3.3.0, < 3.5)
sprockets-es6 (>= 0.9.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
hashie (3.5.5)
i18n (0.8.1)
jbuilder (2.6.3)
activesupport (>= 3.0.0, < 5.2)
Expand All @@ -77,6 +87,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jwt (1.5.6)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -104,9 +115,26 @@ GEM
minitest (~> 5.0)
rails (>= 4.1)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.0.0)
nokogiri (1.7.1)
mini_portile2 (~> 2.1.0)
oauth2 (1.3.1)
faraday (>= 0.8, < 0.12)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.6.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
omniauth-github (1.2.3)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
pg (0.20.0)
pry (0.10.4)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -194,8 +222,10 @@ PLATFORMS

DEPENDENCIES
better_errors
binding_of_caller
byebug
coffee-rails (~> 4.2)
dotenv-rails
foundation-rails
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -204,6 +234,8 @@ DEPENDENCIES
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
Expand Down
17 changes: 13 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :find_user
before_action :lookup_user

def render_404
# DPR: supposedly this will actually render a 404 page in production
raise ActionController::RoutingError.new('Not Found')
end

private
def find_user
if session[:user_id]
@login_user = User.find_by(id: session[:user_id])
def lookup_user
unless session[:user_id].nil?
@current_user = User.find_by(id: session[:user_id])
end
end

def require_login
lookup_user
if @current_user.nil?
flash[:message] = "You must be logged in to see that page"
# In a before_action filter, this will prevent the action from running at all
redirect_to root_path
end
end
end
65 changes: 46 additions & 19 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
class SessionsController < ApplicationController
def login_form
end

def login
username = params[:username]
if username and user = User.find_by(username: username)
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully logged in as existing user #{user.username}"
else
user = User.new(username: username)
def auth_callback
auth_hash = request.env['omniauth.auth']

# Log in a returning user
user = User.find_by(oauth_provider: params[:provider], oauth_uid: auth_hash["uid"])

if user.nil?
# Don't know this user - build a new user
user = User.from_github(auth_hash)

if user.save
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
flash[:message] = "Successfully logged in as new user #{user.username}"
else
flash.now[:status] = :failure
flash.now[:result_text] = "Could not log in"
flash.now[:messages] = user.errors.messages
render "login_form", status: :bad_request
return
flash[:message] = "Could not log in"
user.errors.messages.each do |field, problem|
flash[:field] = problem.join(", ")
end
end
else
# Welcome back!
session[:user_id] = user.id
flash[:message] = "Welcome back, #{user.username}"
end
redirect_to root_path
end

# def login_form
# end
#
# def login
# username = params[:username]
# if username and user = User.find_by(username: username)
# session[:user_id] = user.id
# flash[:status] = :success
# flash[:result_text] = "Successfully logged in as existing user #{user.username}"
# else
# user = User.new(username: username)
# if user.save
# session[:user_id] = user.id
# flash[:status] = :success
# flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
# else
# flash.now[:status] = :failure
# flash.now[:result_text] = "Could not log in"
# flash.now[:messages] = user.errors.messages
# render "login_form", status: :bad_request
# return
# end
# end
# redirect_to root_path
# end
#
def logout
session[:user_id] = nil
flash[:status] = :success
# flash[:status] = :success
flash[:result_text] = "Successfully logged out"
redirect_to root_path
end
Expand Down
27 changes: 27 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ def index
@users = User.all
end

# def auth_callback
# auth_hash = request.env['omniauth.auth']
#
# # Log in a returning user
# user = User.find_by(oauth_provider: params[:provider], oauth_uid: auth_hash["uid"])
#
# if user.nil?
# # Don't know this user - build a new user
# user = User.from_github(auth_hash)
#
# if user.save
# session[:user_id] = user.id
# flash[:message] = "Successfully logged in as new user #{user.username}"
# else
# flash[:message] = "Could not log in"
# user.errors.messages.each do |field, problem|
# flash[:field] = problem.join(", ")
# end
# end
# else
# # Welcome back!
# session[:user_id] = user.id
# flash[:message] = "Welcome back, #{user.username}"
# end
# redirect_to root_path
# end

def show
@user = User.find_by(id: params[:id])
render_404 unless @user
Expand Down
39 changes: 26 additions & 13 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class WorksController < ApplicationController
# We should always be able to tell what category
# of work we're dealing with
before_action :require_login, except: [:root]
before_action :category_from_url, only: [:index, :new, :create]
before_action :category_from_work, except: [:root, :index, :new, :create]

Expand All @@ -22,6 +23,8 @@ def new

def create
@work = Work.new(media_params)
@work.user = @current_user

if @work.save
flash[:status] = :success
flash[:result_text] = "Successfully created #{@media_category.singularize} #{@work.id}"
Expand All @@ -42,24 +45,34 @@ def edit
end

def update
@work.update_attributes(media_params)
if @work.save
flash[:status] = :success
flash[:result_text] = "Successfully updated #{@media_category.singularize} #{@work.id}"
redirect_to works_path(@media_category)
if @work.can_edit?(@current_user) == true
@work.update_attributes(media_params)
if @work.save
flash[:status] = :success
flash[:result_text] = "Successfully updated #{@media_category.singularize} #{@work.id}"
redirect_to works_path(@media_category)
else
flash.now[:status] = :failure
flash.now[:result_text] = "Could not update #{@media_category.singularize}"
flash.now[:messages] = @work.errors.messages
render :edit, status: :not_found
end
else
flash.now[:status] = :failure
flash.now[:result_text] = "Could not update #{@media_category.singularize}"
flash.now[:messages] = @work.errors.messages
render :edit, status: :not_found
flash[:message] = "You are not authorized to edit this work"
redirect_to work_path
end
end

def destroy
@work.destroy
flash[:status] = :success
flash[:result_text] = "Successfully destroyed #{@media_category.singularize} #{@work.id}"
redirect_to root_path
if @work.can_edit?(@current_user) == true
@work.destroy
flash[:status] = :success
flash[:result_text] = "Successfully destroyed #{@media_category.singularize} #{@work.id}"
redirect_to root_path
else
flash[:message] = "You are not authorized to edit this work"
redirect_to work_path
end
end

def upvote
Expand Down
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class User < ApplicationRecord
has_many :votes
has_many :ranked_works, through: :votes, source: :work
has_many :works

validates :username, uniqueness: true, presence: true

def self.from_github(auth_hash)
user = User.new
user.username = auth_hash["info"]["nickname"]
user.email = auth_hash["info"]["email"]
user.oauth_uid = auth_hash["uid"]
user.oauth_provider = "github"
return user
end
end
9 changes: 9 additions & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Work < ApplicationRecord
has_many :votes, dependent: :destroy
has_many :ranking_users, through: :votes, source: :user
belongs_to :user

validates :category, presence: true,
inclusion: { in: %w(album book movie) }
Expand Down Expand Up @@ -34,6 +35,14 @@ def self.top_ten(category)
where(category: category).order(vote_count: :desc).limit(10)
end

def can_edit?(current_user)
if self.user == current_user
return true
else
return false
end
end

private
def fix_category
if self.category
Expand Down
Loading