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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Extract all permission related methods into User::PermissionChecks concern
Klaus Zandersd528841a314f
diff --git a/app/models/user.rb b/app/models/user.rbindex 8f4def4ee68..4ace62195fd 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -41,6 +41,7 @@ class User < Principalinclude ::Associations::Groupableinclude ::Users::Avatars+ include ::Users::PermissionChecksextend DeprecatedAliashas_many :watches, class_name: 'Watcher',@@ -176,8 +177,12 @@ class User < Principalenddef reload(*args)+<<<<<<< HEAD@user_allowed_service = nil@project_role_cache = nil+=======+ @name = nil+>>>>>>> 088d0fb8ef (Extract all permission related methods into User::PermissionChecks concern)superend@@ -470,12 +475,6 @@ class User < Principal!logged?end- # Return user's roles for project- def roles_for_project(project)- project_role_cache.fetch(project)- end- alias :roles :roles_for_project-# Cheap version of Project.visible.countdef number_of_known_projectsif admin?@@ -485,33 +484,6 @@ class User < Principalendend- # Return true if the user is a member of project- def member_of?(project)- roles_for_project(project).any?(&:member?)- end-- def self.allowed(action, project)- Authorization.users(action, project)- end-- def self.allowed_members(action, project)- Authorization.users(action, project).where.not(members: { id: nil })- end-- def allowed_to?(action, context, global: false)- user_allowed_service.call(action, context, global:)- end-- def allowed_to_in_project?(action, project)- allowed_to?(action, project)- end-- def allowed_to_globally?(action)- allowed_to?(action, nil, global: true)- end-- delegate :preload_projects_allowed_to, to: :user_allowed_service-def reported_work_package_countWorkPackage.on_active_project.with_author(self).visible.countend@@ -623,14 +595,6 @@ class User < Principal[skip_suffix_check, regexp]end- def user_allowed_service- @user_allowed_service ||= ::Authorization::UserAllowedService.new(self, role_cache: project_role_cache)- end-- def project_role_cache- @project_role_cache ||= ::Users::ProjectRoleCache.new(self)- end-def former_passwords_include?(password)return false if Setting[:password_count_former_banned].to_i == 0diff --git a/app/models/users/permission_checks.rb b/app/models/users/permission_checks.rbnew file mode 100644index 00000000000..e98eb62f76a--- /dev/null+++ b/app/models/users/permission_checks.rb@@ -0,0 +1,118 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Users::PermissionChecks+ extend ActiveSupport::Concern++ included do+ delegate :preload_projects_allowed_to, to: :user_allowed_service++ # Some Ruby magic. Create methods for each entity we can have memberships on automatically+ # i.e. allowed_in_work_package? and allowed_in_any_work_package?+ Member::ALLOWED_ENTITIES.each do |entity_model_name|+ entity_name_underscored = entity_model_name.underscore+ entity_class = entity_model_name.constantize++ define_method "allowed_in_#{entity_name_underscored}?" do |permission, entity|+ allowed_in_entity?(permission, entity, entity_class)+ end++ define_method "allowed_in_any_#{entity_name_underscored}?" do |permission, in_project: nil|+ allowed_in_any_entity?(permission, entity_class, in_project:)+ end+ end+ end++ class_methods do+ def allowed(action, project)+ Authorization.users(action, project)+ end++ def allowed_members(action, project)+ Authorization.users(action, project).where.not(members: { id: nil })+ end+ end++ def reload(*args)+ @user_allowed_service = nil+ @project_role_cache = nil+ @user_permissible_service = nil++ super+ end++ # All the new methos to check for permissions. This will completely replace the old interface:+ delegate :allowed_globally?,+ :allowed_in_project?,+ :allowed_in_any_project?,+ :allowed_in_entity?,+ :allowed_in_any_entity?,+ to: :user_permissible_service++ # Return user's roles for project+ def roles_for_project(project)+ project_role_cache.fetch(project)+ end+ alias :roles :roles_for_project++ # Return true if the user is a member of project+ def member_of?(project)+ roles_for_project(project).any?(&:member?)+ end++ # 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?)+ user_allowed_service.call(action, context, global:)+ end++ def allowed_to_in_project?(action, project)+ # OpenProject::Deprecation.replaced(:allowed_to_in_project?, :allowed_in_project?, caller)+ allowed_to?(action, project)+ end++ def allowed_to_globally?(action)+ # OpenProject::Deprecation.replaced(:allowed_to_globally?, :allowed_globally?, caller)+ allowed_to?(action, nil, global: true)+ end++ private++ def user_allowed_service+ @user_allowed_service ||= ::Authorization::UserAllowedService.new(self, role_cache: project_role_cache)+ end++ def user_permissible_service+ @user_permissible_service ||= ::Authorization::UserPermissibleService.new(self)+ end++ def project_role_cache+ @project_role_cache ||= ::Users::ProjectRoleCache.new(self)+ end+end