diff --git a/Gemfile b/Gemfile index acf0d78..9a01326 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ ruby '2.2.3' gem 'rails', '4.2.5' gem 'bootstrap_form' +gem 'cancancan' gem 'carrierwave', github: 'carrierwaveuploader/carrierwave' gem 'devise' gem 'http_logger' diff --git a/Gemfile.lock b/Gemfile.lock index 2f382e7..e10418d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,7 @@ GEM bootstrap_form (2.3.0) builder (3.2.2) byebug (8.2.1) + cancancan (1.13.1) capybara (2.5.0) mime-types (>= 1.16) nokogiri (>= 1.3.3) @@ -267,6 +268,7 @@ DEPENDENCIES annotate bootstrap_form byebug + cancancan carrierwave! coveralls devise diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1eeeef4..9c20a99 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,6 +3,10 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + rescue_from CanCan::AccessDenied do |exception| + redirect_to root_url, alert: exception.message + end + def new_session_path(_scope) root_path end diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 4ad15f1..a6285a1 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -1,5 +1,14 @@ class SubmissionsController < ApplicationController before_action :authenticate_user! + load_and_authorize_resource + + def index + if current_user.admin? + @submissions = Submission.all.order(created_at: :desc) + else + @submissions = current_user.submissions.order(created_at: :desc) + end + end def new @submission = Submission.new @@ -12,12 +21,16 @@ def create if @submission.save process_submission(@submission) flash.notice = 'Your Submission is now in progress.' - redirect_to root_path + redirect_to submissions_path else render 'new' end end + def package + send_file(Submission.find_by_id(params[:id]).sword_path) + end + private def process_submission(submission) diff --git a/app/models/ability.rb b/app/models/ability.rb new file mode 100644 index 0000000..45519fe --- /dev/null +++ b/app/models/ability.rb @@ -0,0 +1,10 @@ +class Ability + include CanCan::Ability + + def initialize(user) + can :manage, Submission if user.admin? + can [:create, :read], Submission, user: user + # See the wiki for details: + # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities + end +end diff --git a/app/models/user.rb b/app/models/user.rb index fdc0b96..364cdc6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,7 @@ # uid :string not null # created_at :datetime not null # updated_at :datetime not null +# admin :boolean # class User < ActiveRecord::Base diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 668c596..6950e4d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -34,6 +34,7 @@