From ce0c32ea024a9d2fa242b156803413eabc46ebdf Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Tue, 14 Dec 2010 09:16:45 -0800 Subject: [PATCH] Refactor: create the permission name dynamically to support other classes --- app/controllers/watchers_controller.rb | 32 +++++++++++--------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 2f596caf..e5c9b6fa 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -100,26 +100,22 @@ private end def authorize_access_to_object - allowed = false - - case @watched.class.to_s - when "Issue" - if params[:action] == 'new' - allowed = true if User.current.allowed_to?(:add_issue_watchers, @project) - end - if params[:action] == 'destroy' - allowed = true if User.current.allowed_to?(:delete_issue_watchers, @project) - end - when "WikiPage" - if params[:action] == 'new' - allowed = true if User.current.allowed_to?(:add_wiki_page_watchers, @project) - end - if params[:action] == 'destroy' - allowed = true if User.current.allowed_to?(:delete_wiki_page_watchers, @project) - end + permission = '' + case params[:action] + when 'new' + permission << 'add_' + when 'destroy' + permission << 'delete_' end - deny_access unless allowed + # Ends up like: :delete_wiki_page_watchers + permission << "#{@watched.class.name.underscore}_watchers" + + if User.current.allowed_to?(permission.to_sym, @project) + return true + else + deny_access + end end end