diff --git a/app/controllers/concerns/submission_controller.rb b/app/controllers/concerns/submission_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..3bb83748a7f7e4aa38768f79fcb1171ea56b2c66 --- /dev/null +++ b/app/controllers/concerns/submission_controller.rb @@ -0,0 +1,35 @@ +module SubmissionController + extend ActiveSupport::Concern + include ::Paginator + + included do + before_action :set_new_submission, only: :submit + before_action :authorize! + end + + def submissions + learning_objects = paginate LearningObject.where(state: LearningObject.states[:submitted]) + render json: learning_objects + end + + def submit + return render status: :ok if @learning_object.submitted? + + if @learning_object.update(state: LearningObject.states[:submitted]) + render json: @learning_object, status: :ok + else + render json: @learning_object.errors, status: :unprocessable_entity + end + end + + private + + def submitted + return @learning_object.submitted? + end + + def set_new_submission + @learning_object = LearningObject.find(params[:id]) + end + +end diff --git a/app/controllers/v1/learning_objects/publishes_controller.rb b/app/controllers/v1/learning_objects/publishes_controller.rb index 2686c5ebd97f196f847ca22f6c30ed7e1a0adcaf..d836d19044578e11a2b824d61d8ceb89f1b01cfc 100644 --- a/app/controllers/v1/learning_objects/publishes_controller.rb +++ b/app/controllers/v1/learning_objects/publishes_controller.rb @@ -20,7 +20,7 @@ class V1::LearningObjects::PublishesController < ApplicationController end def authorize! - authorize(@learning_object || LearningObject.new, :update?) + authorize(@learning_object, :publish?) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/controllers/v1/learning_objects_controller.rb b/app/controllers/v1/learning_objects_controller.rb index 2b274844ece406954e64eaf05ddf56eb59d880ad..739e9489a62ef5c4d7fc991f196357327e754892 100644 --- a/app/controllers/v1/learning_objects_controller.rb +++ b/app/controllers/v1/learning_objects_controller.rb @@ -9,10 +9,12 @@ class V1::LearningObjectsController < ApplicationController include ::HighlightsController include ::SubjectableController include ::StageableController + include ::SubmissionController - before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging] + before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging, :submit, :submission] before_action :set_learning_object, only: [:show, :update, :destroy, :subjecting, :unsubjecting, :add_stages, :remove_stages] - before_action :set_new_learning_object, only: :index + before_action :set_new_learning_object, only: [:index, :submissions] + before_action :set_new_submission, only: :submit before_action :authorize!, except: [:create, :tagging, :untagging, :download, :magnetlink] before_action :set_paper_trail_whodunnit, except: [:index, :show] diff --git a/app/models/concerns/stateful.rb b/app/models/concerns/stateful.rb index 7ca2f074779ec9894ee4f4b2166a637ff92daf41..eb3727e5665c3853c8e919bdbf8077b3a983c4b8 100644 --- a/app/models/concerns/stateful.rb +++ b/app/models/concerns/stateful.rb @@ -2,6 +2,6 @@ module Stateful extend ActiveSupport::Concern included do - enum state: { draft: 0, published: 1, suspended: 2 } + enum state: { draft: 0, published: 1, suspended: 2, submitted: 3 } end end diff --git a/app/models/user.rb b/app/models/user.rb index 1399f3b6c95e4b49413c25b5634e0eed3bc39b92..78ce1d7f2da76c969ff3ac5b7361d62a1bb1f6e8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -99,6 +99,13 @@ class User < ApplicationRecord false end + def is_curator? + roles.each do |role| + return true if role.name == 'curator' + end + false + end + def is_moderator? roles.each do |role| return true if role.name == 'moderator' diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 8445518d31b69544549342d4e8d3713371ef7266..d33251570ec0db82ca4109a3b286f2f2ace5e0e7 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -59,6 +59,10 @@ class ApplicationPolicy user.is_admin? || user.is_moderator? end + def user_can_curate? + user.is_curator? + end + class Scope < ApplicationPolicy attr_reader :user, :scope diff --git a/app/policies/learning_object_policy.rb b/app/policies/learning_object_policy.rb index b223bcaaf43220d5a70e59d189c4a3ef2084b60e..70dd4d64f957066660dcbf48f3ad5c766f49c2f1 100644 --- a/app/policies/learning_object_policy.rb +++ b/app/policies/learning_object_policy.rb @@ -4,6 +4,7 @@ class LearningObjectPolicy < ApplicationPolicy include TaggablePolicy include SubjectablePolicy include StageablePolicy + include SubmissionPolicy class Scope < Scope def resolve @@ -25,6 +26,10 @@ class LearningObjectPolicy < ApplicationPolicy record if owns? end + def publish? + record if user_can_curate? && record.submitted? + end + def destroy? record if owns? end diff --git a/app/policies/submission_policy.rb b/app/policies/submission_policy.rb new file mode 100644 index 0000000000000000000000000000000000000000..f3c1ced3e0c503282aaf38468d4551d4152f7e79 --- /dev/null +++ b/app/policies/submission_policy.rb @@ -0,0 +1,12 @@ +module SubmissionPolicy + + def submit? + record if owns? + end + + def submissions? + return false if user.nil? + record if user_can_curate? + end + +end diff --git a/config/routes.rb b/config/routes.rb index 2ef9a747bce42a72c79b623e5ba1d374077703ee..fc31bc499df0cf65079e6c64857949e34f68477f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,6 +58,15 @@ Rails.application.routes.draw do end end + concern :submission do + collection do + get :submissions + end + member do + post :submit + end + end + concern :highlights do collection do get :this_week @@ -124,7 +133,7 @@ Rails.application.routes.draw do end end - resources :learning_objects, concerns: [:sociable, :downloadable, :reviewable, :taggable, :versionable, :deletable, :highlights, :subjectable, :stageable] do + resources :learning_objects, concerns: [:sociable, :downloadable, :reviewable, :taggable, :versionable, :deletable, :highlights, :subjectable, :stageable, :submission] do member do resource :chunk, module: 'learning_objects', only: [:create, :show] resource :upload, module: 'learning_objects', only: :create