Move WP edit authorization to model

pull/2081/head
Hagen Schink 10 years ago
parent 53f42d7649
commit b36ec41979
  1. 6
      lib/api/v3/work_packages/work_package_model.rb
  2. 1
      lib/api/v3/work_packages/work_packages_api.rb

@ -44,6 +44,7 @@ module API
super(object) super(object)
@user = user @user = user
@can = WorkPackagePolicy.new(user)
end end
# N.B. required by ActionView::Helpers::UrlHelper # N.B. required by ActionView::Helpers::UrlHelper
@ -163,6 +164,7 @@ module API
model.closed? model.closed?
end end
validate :user_allowed_to_edit
validates_presence_of :subject, :project_id, :type, :author, :status validates_presence_of :subject, :project_id, :type, :author, :status
validates_length_of :subject, maximum: 255 validates_length_of :subject, maximum: 255
validate :milestone_constraint validate :milestone_constraint
@ -170,6 +172,10 @@ module API
private private
def user_allowed_to_edit
fail ::API::Errors::Unauthorized unless @can.allowed?(model, :edit)
end
def milestone_constraint def milestone_constraint
errors.add :parent_id, :cannot_be_milestone if model.parent && model.parent.is_milestone? errors.add :parent_id, :cannot_be_milestone if model.parent && model.parent.is_milestone?
end end

@ -78,7 +78,6 @@ module API
end end
patch do patch do
authorize(:edit_work_packages, context: @work_package.project)
authorize(:manage_subtasks, context: @work_package.project) if work_package_attributes.has_key? 'parentId' authorize(:manage_subtasks, context: @work_package.project) if work_package_attributes.has_key? 'parentId'
check_lock_version # fails if lock version is missing check_lock_version # fails if lock version is missing
check_work_package_attributes # fails if request contains read-only attributes check_work_package_attributes # fails if request contains read-only attributes

Loading…
Cancel
Save