Skip to content

Commit 4afb400

Browse files
authored
Merge pull request #62 from fastruby/IIRR-30-and-IIRR-31
IIRR-30 | IIRR-31: Fix auth bug and enable puzzle cloning
2 parents 912ff92 + 277248f commit 4afb400

15 files changed

Lines changed: 103 additions & 7 deletions

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ GEM
101101
bootstrap-sass (3.4.1)
102102
autoprefixer-rails (>= 5.2.1)
103103
sassc (>= 2.0.0)
104-
brakeman (7.1.0)
104+
brakeman (8.0.1)
105105
racc
106106
builder (3.3.0)
107107
capybara (3.40.0)

app/controllers/application_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ class ApplicationController < ActionController::Base
22
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
33
allow_browser versions: :modern
44
before_action :check_session_expiry
5+
before_action :check_user_token
56

67
private
78

9+
def check_user_token
10+
unless session[:user_token]
11+
render "puzzles/login"
12+
end
13+
end
14+
815
def check_session_expiry
916
if session[:expires_at].present? && Time.current > session[:expires_at]
1017
reset_session
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Puzzles::ClonesController < ApplicationController
2+
def create
3+
original_puzzle = Puzzle.find(params[:puzzle_id])
4+
attributes = original_puzzle.attributes.slice("question", "answer", "explanation", "link", "suggested_by")
5+
cloned_puzzle = Puzzle.new(attributes.merge(original_puzzle:, state: params.fetch(:state, "pending")))
6+
7+
if cloned_puzzle.save
8+
redirect_to puzzles_path, notice: "Puzzle cloned. You can now edit the new puzzle."
9+
else
10+
redirect_to puzzles_path, alert: "Failed to clone puzzle."
11+
end
12+
end
13+
end

app/controllers/puzzles_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
class PuzzlesController < ApplicationController
22
def index
3-
unless session[:user_token]
4-
render "login"
5-
end
6-
73
@pending_puzzles = Puzzle.pending
84
@approved_puzzles = Puzzle.approved
95
@rejected_puzzles = Puzzle.rejected

app/controllers/sessions_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class SessionsController < ApplicationController
2+
skip_before_action :check_user_token
3+
24
def create
35
auth = request.env["omniauth.auth"]
46
user_email = auth.info.email
57

6-
domain_allowlist = ENV.fetch("DOMAIN_ALLOWLIST").split(",").map(&:strip)
8+
domain_allowlist = ENV.fetch("DOMAIN_ALLOWLIST", "").split(",").map(&:strip)
79
if domain_allowlist.present?
810
unless domain_allowlist.any? { |domain| user_email.end_with?("@#{domain}") }
911
reset_session

app/controllers/slack/application_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Slack::ApplicationController < ApplicationController
22
skip_before_action :verify_authenticity_token
3+
skip_before_action :check_user_token
4+
35
before_action :valid_slack_request?
46

57
private

app/models/puzzle.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ class Puzzle < ApplicationRecord
33
enum :state, { approved: 0, rejected: 1, pending: 2, archived: 3 }
44
has_many :answers
55

6+
belongs_to :original_puzzle, class_name: "Puzzle", optional: true
7+
68
validates :question, presence: true
79

810
scope :archived, -> { where(state: :archived).order(sent_at: :desc) }

app/views/puzzles/_puzzles_table.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<%= button_to 'Pending', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
4343
<% elsif actions == :archived %>
4444
<%= button_to 'Un-Archive', puzzle_state_path(puzzle, state: :pending), method: :patch, form_class: 'inline-form', class: 'btn pending-btn' %>
45+
<%= button_to 'Clone', puzzle_clone_path(puzzle, state: :pending), method: :post, form_class: 'inline-form', class: 'btn pending-btn' %>
4546
<% end %>
4647
</td>
4748
</tr>

config/environments/test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@
5050

5151
# Raise error when a before_action's only/except options reference missing actions.
5252
config.action_controller.raise_on_missing_callback_actions = true
53+
54+
# Once you have enabled test mode, all requests to OmniAuth will be short circuited to use
55+
# the mock authentication hash.
56+
# See: https://github.com/omniauth/omniauth/wiki/Integration-Testing
57+
OmniAuth.config.test_mode = true
5358
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Rails.application.routes.draw do
22
resources :puzzles, only: [ :index, :edit, :update ] do
33
resource :state, only: [ :update ], module: :puzzles
4+
resource :clone, only: [ :create ], module: :puzzles
45
end
56
resources :sessions, only: [ :create, :destroy ]
67

0 commit comments

Comments
 (0)