Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

move authorization methods into module

ulferts8d3b7779417b
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 605e6a9143f..3310a9db849 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -241,42 +241,6 @@ class ApplicationController < ActionController::Base
end
end
- # Authorize the user for the requested controller action.
- # To be used in before_action hooks
- def authorize(ctrl = params[:controller], action = params[:action])
- do_authorize({ controller: ctrl, action: }, global: false)
- end
-
- # Authorize the user for the requested controller action outside a project
- # To be used in before_action hooks
- def authorize_global
- action = { controller: params[:controller], action: params[:action] }
- do_authorize(action, global: true)
- end
-
- # Deny access if user is not allowed to do the specified action.
- #
- # Action can be:
- # * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })
- # * a permission Symbol (eg. :edit_project)
- def do_authorize(action, global: false) # rubocop:disable Metrics/PerceivedComplexity
- is_authorized = if global
- User.current.allowed_based_on_permission_context?(action)
- else
- User.current.allowed_based_on_permission_context?(action, project: @project || @projects,
- entity: @work_package || @work_packages)
- end
-
- unless is_authorized
- if @project&.archived?
- render_403 message: :notice_not_authorized_archived_project
- else
- deny_access
- end
- end
- is_authorized
- end
-
# Find project of id params[:id]
# Note: find() is Project.friendly.find()
def find_project
@@ -293,16 +257,6 @@ class ApplicationController < ActionController::Base
render_404
end
- # Find a project based on params[:project_id]
- # TODO: some subclasses override this, see about merging their logic
- def authorize_in_optional_project
- @project = Project.find(params[:project_id]) if params[:project_id].present?
-
- do_authorize({ controller: params[:controller], action: params[:action] }, global: params[:project_id].blank?)
- rescue ActiveRecord::RecordNotFound
- render_404
- end
-
# Finds and sets @project based on @object.project
def find_project_from_association
render_404 if @object.blank?
diff --git a/app/controllers/concerns/accounts/authorization.rb b/app/controllers/concerns/accounts/authorization.rb
index cfed68a66b2..7c7ffddabb4 100644
--- a/app/controllers/concerns/accounts/authorization.rb
+++ b/app/controllers/concerns/accounts/authorization.rb
@@ -43,11 +43,7 @@ module Accounts::Authorization
}
end
- def require_admin
- return unless require_login
-
- render_403 unless current_user.admin?
- end
+ private
def authorization_check_required
unless authorization_is_ensured?(params[:action])
@@ -66,7 +62,57 @@ module Accounts::Authorization
end
end
- private
+ # Authorize the user for the requested controller action.
+ # To be used in before_action hooks
+ def authorize(ctrl = params[:controller], action = params[:action])
+ do_authorize({ controller: ctrl, action: }, global: false)
+ end
+
+ # Authorize the user for the requested controller action outside a project
+ # To be used in before_action hooks
+ def authorize_global
+ action = { controller: params[:controller], action: params[:action] }
+ do_authorize(action, global: true)
+ end
+
+ # Find a project based on params[:project_id]
+ def authorize_in_optional_project
+ @project = Project.find(params[:project_id]) if params[:project_id].present?
+
+ do_authorize({ controller: params[:controller], action: params[:action] }, global: params[:project_id].blank?)
+ rescue ActiveRecord::RecordNotFound
+ render_404
+ end
+
+ # Deny access if user is not allowed to do the specified action.
+ #
+ # Action can be:
+ # * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })
+ # * a permission Symbol (eg. :edit_project)
+ def do_authorize(action, global: false) # rubocop:disable Metrics/PerceivedComplexity
+ is_authorized = if global
+ User.current.allowed_based_on_permission_context?(action)
+ else
+ User.current.allowed_based_on_permission_context?(action,
+ project: @project || @projects,
+ entity: @work_package || @work_packages)
+ end
+
+ unless is_authorized
+ if @project&.archived?
+ render_403 message: :notice_not_authorized_archived_project
+ else
+ deny_access
+ end
+ end
+ is_authorized
+ end
+
+ def require_admin
+ return unless require_login
+
+ render_403 unless current_user.admin?
+ end
def authorization_is_ensured?(action)
return false if authorization_ensured.nil?