Skip to content

Commit

Permalink
[webui][api] refactored permission check. Added condition for require…
Browse files Browse the repository at this point in the history
…d params. Handingly unknown request ids for comment
  • Loading branch information
Shayon Mukherjee authored and hennevogel committed Aug 19, 2013
1 parent b6d28dc commit f0b4f01
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 66 deletions.
12 changes: 6 additions & 6 deletions src/api/app/controllers/webui/comments_controller.rb
Expand Up @@ -38,32 +38,32 @@ def requests_new
end

def projects_edit
CommentProject.edit_comment(params)
CommentProject.edit(params)
render_ok
end

def packages_edit
CommentPackage.edit_comment(params)
CommentPackage.edit(params)
render_ok
end

def requests_edit
CommentRequest.edit_comment(params)
CommentRequest.edit(params)
render_ok
end

def projects_delete
CommentProject.delete_comment(params)
CommentProject.delete(params)
render_ok
end

def packages_delete
CommentPackage.delete_comment(params)
CommentPackage.delete(params)
render_ok
end

def requests_delete
CommentRequest.delete_comment(params)
CommentRequest.delete(params)
render_ok
end

Expand Down
60 changes: 31 additions & 29 deletions src/api/app/models/comment.rb
Expand Up @@ -3,14 +3,29 @@ class Comment < ActiveRecord::Base
belongs_to :package
belongs_to :bs_request

class NoDataEnteredError < APIException
setup 'no_data_entered', 403, "No data Entered"
class CommentNoDataEntered < APIException
setup 'comment_no_data_entered', 403, "No data Entered"
end
class NoUserFound < APIException
setup 'no_user_found', 403, "No user found"
class CommentNoUserFound < APIException
setup 'comment_no_user_found', 403, "No user found"
end
class WritePermissionError < APIException
setup "project_write_permission_error"
class CommentWritePermissionError < APIException
setup "comment_write_permission_error"
end

def self.fields_check!(params)
if params[:body].blank?
raise CommentNoDataEntered.new "You didn't add a body to the comment."
elsif params[:user].blank?
raise CommentNoUserFound.new "No user found. Sign in before continuing."
end
end

def self.permission_check!(params)
unless User.current.login == params[:user] || User.current.is_admin? || @object_permission_check
raise CommentWritePermissionError, "You don't have the permissions to modify the content."
end
fields_check!(params)
end

def self.save(params)
Expand All @@ -20,34 +35,21 @@ def self.save(params)
@comment['user'] = params[:user]
@comment['parent_id'] = params[:parent_id] if params[:parent_id]

if @comment['body'].blank?
raise NoDataEnteredError.new "You didn't add a body to the comment."
elsif !@comment['parent_id'] && @comment['title'].blank?
raise NoDataEnteredError.new "You didnt add a title to the comment"
elsif @comment['user'].blank?
raise NoUserFound.new "No user found. Sign in before continuing."
fields_check!(params)
if !params[:parent_id] && params[:title].blank?
raise CommentNoDataEntered.new "You didn't add a title to the comment."
end
end

def self.edit_comment(params)

if User.current.login == params[:user]
self.update(params[:comment_id],:body => params[:body])
else
raise WritePermissionError, "You don't have the permissions to modify the content."
end

if params[:body].blank?
raise NoDataEnteredError.new "You didn't add a body to the comment."
end
def self.edit(params)
permission_check!(params)
self.update(params[:comment_id],:body => params[:body])
end

def self.delete_comment(params)
if @object_permission_check
self.update(params[:comment_id],:body => "Comment deleted.")
else
raise WritePermissionError, "You don't have the permissions to modify the content."
end
def self.delete(params)
params[:body] = "Comment deleted."
permission_check!(params)
self.update(params[:comment_id],:body => params[:body] , :user => params[:user])
end

end
4 changes: 2 additions & 2 deletions src/api/app/models/comment_package.rb
Expand Up @@ -6,9 +6,9 @@ def self.save(params)
CommentPackage.create(@comment)
end

def self.delete_comment(params)
def self.permission_check!(params)
package = Package.get_by_project_and_name(params[:project], params[:package])
@object_permission_check = (User.current.can_modify_package?(package) || User.current.is_admin? || User.current.login == params[:user])
@object_permission_check = User.current.can_modify_package?(package)
super
end
end
4 changes: 2 additions & 2 deletions src/api/app/models/comment_project.rb
Expand Up @@ -7,9 +7,9 @@ def self.save(params)
CommentProject.create(@comment)
end

def self.delete_comment(params)
def self.permission_check!(params)
project = Project.get_by_name(params[:project])
@object_permission_check = (User.current.can_modify_project?(project) || User.current.is_admin? || User.current.login == params[:user])
@object_permission_check = User.current.can_modify_project?(project)
super
end
end
5 changes: 0 additions & 5 deletions src/api/app/models/comment_request.rb
Expand Up @@ -4,9 +4,4 @@ def self.save(params)
@comment['bs_request_id'] = params[:request_id]
CommentRequest.create(@comment)
end

def self.delete_comment(params)
@object_permission_check = (User.current.is_admin? || User.current.login == params[:user])
super
end
end
6 changes: 3 additions & 3 deletions src/api/config/routes.rb
Expand Up @@ -355,9 +355,9 @@
post 'comments/package/:project/:package/new' => 'comments#packages_new', constraints: cons
post 'comments/request/:id/new' => 'comments#requests_new', constraints: cons

put 'comments/project/:project/update' => 'comments#projects_edit', constraints: cons
put 'comments/package/:project/:package/update' => 'comments#packages_edit', constraints: cons
put 'comments/request/:id/update' => 'comments#requests_edit', constraints: cons
put 'comments/project/:project/edit' => 'comments#projects_edit', constraints: cons
put 'comments/package/:project/:package/edit' => 'comments#packages_edit', constraints: cons
put 'comments/request/:id/edit' => 'comments#requests_edit', constraints: cons

put 'comments/project/:project/delete' => 'comments#projects_delete', constraints: cons
put 'comments/package/:project/:package/delete' => 'comments#packages_delete', constraints: cons
Expand Down
24 changes: 12 additions & 12 deletions src/api/test/functional/comments_controller_test.rb
Expand Up @@ -29,65 +29,65 @@ def test_update_permissions_for_comments_on_project
reset_auth
prepare_request_with_user "tom", "thunder"

put "/webui/comments/project/BaseDistro/update", {:comment_id => 100, :user => 'tom', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/project/BaseDistro/delete", {:comment_id => 100, :user => 'tom', :body => "Comment deleted"}
assert_response 200

# Test to see if another user can delete a comment he/she is not associated with
prepare_request_with_user "tom", "thunder"

put "/webui/comments/project/BaseDistro/delete", {:comment_id => 100, :user => 'Iggy',:project => "BaseDistro", :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/project/BaseDistro/delete", {:comment_id => 100, :user => 'Iggy',:project => "BaseDistro", :body => "Comment deleted"}
assert_response 400

# Test to see check permission on editing comments

put "/webui/comments/project/BaseDistro/update", {:comment_id => 100, :user => 'Iggy',:project => "BaseDistro", :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/project/BaseDistro/edit", {:comment_id => 100, :user => 'Iggy',:project => "BaseDistro", :body => "Hurray this is a comment"}
assert_response 400

put "/webui/comments/project/BaseDistro/update", {:comment_id => 100, :user => 'tom',:project => "BaseDistro", :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/project/BaseDistro/edit", {:comment_id => 100, :user => 'tom',:project => "BaseDistro", :body => "Hurray this is a comment 2"}
assert_response 200
end

def test_update_permissions_for_comments_on_package
reset_auth
prepare_request_with_user "tom", "thunder"

put "/webui/comments/package/BaseDistro/pack1/update", {:comment_id => 102, :user => 'tom', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/package/BaseDistro/pack1/delete", {:comment_id => 102, :user => 'tom', :body => "Comment deleted"}
assert_response 200

# Test to see if another user can delete a comment he/she is not associated with
prepare_request_with_user "tom", "thunder"

put "/webui/comments/package/BaseDistro/pack1/delete", {:comment_id => 102, :user => 'Iggy', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/package/BaseDistro/pack1/delete", {:comment_id => 102, :user => 'Iggy', :body => "Comment deleted"}
assert_response 400

# Test to see check permission on editing comments

put "/webui/comments/package/BaseDistro/pack1/update", {:comment_id => 102, :user => 'Iggy', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/package/BaseDistro/pack1/edit", {:comment_id => 102, :user => 'Iggy', :body => "Some comment"}
assert_response 400

put "/webui/comments/package/BaseDistro/pack1/update", {:comment_id => 102, :user => 'tom', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/package/BaseDistro/pack1/edit", {:comment_id => 102, :user => 'tom', :body => "Some comment from the dark knight"}
assert_response 200
end

def test_update_permissions_for_comments_on_request
reset_auth
prepare_request_with_user "tom", "thunder"

put "/webui/comments/request/1000/update", {:comment_id => 103, :user => 'tom', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/request/1000/delete", {:comment_id => 103, :user => 'tom', :body => "Comment deleted"}
assert_response 200

# Test to see if another user can delete a comment he/she is not associated with
prepare_request_with_user "tom", "thunder"

put "/webui/comments/request/1000/delete", {:comment_id => 103, :user => 'Iggy', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/request/1000/delete", {:comment_id => 103, :user => 'Iggy', :body => "Comment deleted"}
assert_response 400

# Test to see check permission on editing comments

put "/webui/comments/request/1000/update", {:comment_id => 103, :user => 'Iggy', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/request/1000/edit", {:comment_id => 103, :user => 'Iggy', :body => "Comment from the president"}
assert_response 400

put "/webui/comments/request/1000/update", {:comment_id => 103, :user => 'tom', :title => "This is a title", :body => "Comment deleted"}
put "/webui/comments/request/1000/edit", {:comment_id => 103, :user => 'tom', :body => "Comment from anony"}
assert_response 200
end

Expand Down
4 changes: 4 additions & 0 deletions src/webui/app/controllers/package_controller.rb
Expand Up @@ -1093,6 +1093,8 @@ def comments
end

def save_comments
required_parameters :project, :package, :user, :body
required_parameters :title if !params[:parent_id]
begin
params[:project] = @project.name
params[:package] = @package.name
Expand All @@ -1112,6 +1114,7 @@ def save_comments
end

def edit_comments
required_parameters :project, :package, :comment_id
begin
unless params[:update] == 'true'
params[:project] = @project.name
Expand All @@ -1136,6 +1139,7 @@ def edit_comments
end

def delete_comments
required_parameters :user, :comment_id
begin
params[:project] = @project.name
params[:package] = @package.name
Expand Down
4 changes: 4 additions & 0 deletions src/webui/app/controllers/project_controller.rb
Expand Up @@ -1278,6 +1278,8 @@ def comments
end

def save_comments
required_parameters :project, :user, :body
required_parameters :title if !params[:parent_id]
begin
params[:project] = @project.name
ApiDetails.save_comments(:save_comments_for_projects, params)
Expand All @@ -1296,6 +1298,7 @@ def save_comments
end

def edit_comments
required_parameters :project, :comment_id
begin
unless params[:update] == 'true'
params[:project] = @project.name
Expand All @@ -1319,6 +1322,7 @@ def edit_comments
end

def delete_comments
required_parameters :user, :comment_id
begin
params[:project] = @project.name
ApiDetails.update_comments(:delete_comments_for_projects, params)
Expand Down
12 changes: 12 additions & 0 deletions src/webui/app/controllers/request_controller.rb
Expand Up @@ -291,6 +291,14 @@ def set_incident


def comments
# avoiding display of comment section for unnecessary request ids
begin
@req = ApiDetails.read(:request, params[:id])
rescue ApiDetails::NotFoundError
flash[:error] = "Can't find request #{params[:id]}"
redirect_back_or_to :controller => "home", :action => "requests" and return
end

unless params[:reply] == 'true'
@comment = ApiDetails.read(:comments_by_request, params[:id])
@comments_as_thread = sort_comments(@comment)
Expand All @@ -300,6 +308,8 @@ def comments
end

def save_comments
required_parameters :id, :user, :body
required_parameters :title if !params[:parent_id]
begin
params[:request_id] = params[:id]
ApiDetails.save_comments(:save_comments_for_requests, params)
Expand All @@ -318,6 +328,7 @@ def save_comments
end

def edit_comments
required_parameters :id, :comment_id
begin
unless params[:update] == 'true'
params[:request_id] = params[:id]
Expand All @@ -341,6 +352,7 @@ def edit_comments
end

def delete_comments
required_parameters :user, :comment_id
begin
params[:request_id] = params[:id]
ApiDetails.update_comments(:delete_comments_for_requests, params)
Expand Down
6 changes: 3 additions & 3 deletions src/webui/app/models/api_details.rb
Expand Up @@ -40,9 +40,9 @@ def self.save_comments(route_name, params)
def self.update_comments(route_name, params)
uri = "/webui/" +
case route_name.to_sym
when :edit_comments_for_projects then "comments/project/#{params[:project]}/update"
when :edit_comments_for_packages then "comments/package/#{params[:project]}/#{params[:package]}/update"
when :edit_comments_for_requests then "comments/request/#{params[:request_id]}/update"
when :edit_comments_for_projects then "comments/project/#{params[:project]}/edit"
when :edit_comments_for_packages then "comments/package/#{params[:project]}/#{params[:package]}/edit"
when :edit_comments_for_requests then "comments/request/#{params[:request_id]}/edit"

when :delete_comments_for_projects then "comments/project/#{params[:project]}/delete"
when :delete_comments_for_packages then "comments/package/#{params[:project]}/#{params[:package]}/delete"
Expand Down
3 changes: 1 addition & 2 deletions src/webui/app/views/package/comments.html.erb
Expand Up @@ -5,7 +5,6 @@
<div class="grid_16 alpha omega box box-shadow">
<%= render :partial => "package/tabs" %>
<h3><%= @pagetitle %></h3>
<%=render :partial => "shared/parent_comments"%>
</div>

<%= render :partial => "shared/parent_comments"%>
<%=render :partial => "shared/new_comment"%>
3 changes: 1 addition & 2 deletions src/webui/app/views/request/comments.html.erb
Expand Up @@ -5,7 +5,6 @@
<div class="grid_16 alpha omega box box-shadow">
<%= render :partial => "request/tabs" %>
<h3><%= @pagetitle %></h3>
<%= render :partial => "shared/parent_comments"%>
</div>

<%= render :partial => "shared/parent_comments"%>
<%=render :partial => "shared/new_comment"%>

0 comments on commit f0b4f01

Please sign in to comment.