Skip to content

Commit

Permalink
Add initial policy for BsRequest
Browse files Browse the repository at this point in the history
To keep up with on-going code improvement, I would like to propose
an initial Pundit policy for BsRequest
  • Loading branch information
vpereira authored and coolo committed Nov 14, 2018
1 parent 61dd9d2 commit 25a74af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/api/app/controllers/request_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,21 @@ def destroy

# POST /request?cmd=create
def request_create
xml = nil
BsRequest.transaction do
parsed_xml = Xmlhash.parse(request.raw_post.to_s)

raise SaveError, 'Failed parsing the request xml' unless parsed_xml
raise SaveError, 'Request ID attribute not allowed when creating a request' if parsed_xml['id']

@req = BsRequest.new_from_hash(parsed_xml)
@req = BsRequest.new_from_xml(request.raw_post.to_s)
authorize @req, :create?
@req.set_add_revision if params[:addrevision].present?
@req.set_ignore_build_state if params[:ignore_build_state].present?
@req.save!

xml = @req.render_xml
Suse::Validator.validate(:request, xml)
Suse::Validator.validate(:request, @req.render_xml)
end

# cache the diff (in the backend)
@req.bs_request_actions.each do |a|
BsRequestActionWebuiInfosJob.perform_later(a)
end

render xml: xml
render xml: @req.render_xml
end

def request_command_diff
Expand Down
14 changes: 14 additions & 0 deletions src/api/app/policies/bs_request_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class BsRequestPolicy < ApplicationPolicy
def initialize(user, record)
raise Pundit::NotAuthorizedError, 'record does not exist' unless record
@user = user
@record = record
end

def create?
# new request should not have an id (BsRequest#number)
return false if @record.number
# dont let user set approver other than himself unless he is admin
![nil, @user.login].include?(@record.approver) && !@user.is_admin? ? false : true
end
end

0 comments on commit 25a74af

Please sign in to comment.