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

Add methods to ApplicationController to use new permission checks

Klaus Zanders8951241c60ce
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 08b870b8d07..5e06b5186a0 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -231,15 +231,77 @@ class ApplicationController < ActionController::Base
end
end
+ def authorize_in_project(ctrl = params[:controller], action = params[:action])
+ authorization_project = @project || @projects
+ allowed = User.current.allowed_in_project?({ controller: ctrl, action: }, authorization_project)
+
+ unless allowed
+ if @project&.archived?
+ render_403 message: :notice_not_authorized_archived_project
+ else
+ deny_access
+ end
+ end
+
+ allowed
+ end
+
+ def authorize_globally(ctrl = params[:controller], action = params[:action])
+ allowed = User.current.allowed_globally?({ controller: ctrl, action: })
+
+ deny_access unless allowed
+
+ allowed
+ end
+
+ def authorize_in_any_project(ctrl = params[:controller], action = params[:action])
+ allowed = User.current.allowed_in_any_project?({ controller: ctrl, action: })
+
+ deny_access unless allowed
+
+ allowed
+ end
+
+ def authorize_in_model(ctrl = params[:controller], action = params[:action])
+ model = instance_variable_get("@#{model_object.to_s.underscore}")
+
+ allowed = User.current.allowed_in_entity?({ controller: ctrl, action: }, model, model_object)
+
+ unless allowed
+ if model.respond_to?(:project) && model.project&.archived?
+ render_403 message: :notice_not_authorized_archived_project
+ else
+ deny_access
+ end
+ end
+
+ allowed
+ end
+
+ def authorize_in_any_entity(ctrl = params[:controller], action = params[:action])
+ allowed = if @project
+ User.current.allowed_in_any_entity?({ controller: ctrl, action: }, in_project: @project)
+ else
+ User.current.allowed_in_any_entity?({ controller: ctrl, action: })
+ end
+
+ deny_access unless allowed
+
+ allowed
+ end
+
# Authorize the user for the requested controller action.
# To be used in before_action hooks
def authorize(ctrl = params[:controller], action = params[:action])
+ OpenProject::Deprecation.deprecate_method(ApplicationController, :authorize)
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
+ OpenProject::Deprecation.deprecate_method(ApplicationController, :authorize_global)
+
action = { controller: params[:controller], action: params[:action] }
do_authorize(action, global: true)
end
@@ -250,6 +312,7 @@ class ApplicationController < ActionController::Base
# * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })
# * a permission Symbol (eg. :edit_project)
def do_authorize(action, global: false)
+ OpenProject::Deprecation.deprecate_method(ApplicationController, :do_authorize)
context = @project || @projects
is_authorized = User.current.allowed_to?(action, context, global:)
diff --git a/app/models/users/permission_checks.rb b/app/models/users/permission_checks.rb
index 8be654c8b93..707e4bb6880 100644
--- a/app/models/users/permission_checks.rb
+++ b/app/models/users/permission_checks.rb
@@ -119,7 +119,7 @@ module Users::PermissionChecks
# Old allowed_to? interface. Marked as deprecated, should be removed at some point ... Guessing 14.0?
def allowed_to?(action, context, global: false)
- # OpenProject::Deprecation.deprecate_method(User, :allowed_to?)
+ OpenProject::Deprecation.deprecate_method(User, :allowed_to?)
user_allowed_service.call(action, context, global:)
end