2011-05-29 13:11:52 -07:00
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
2024-07-30 13:42:36 +02:00
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
2011-05-29 13:11:52 -07:00
# This program is free software; you can redistribute it and/or
2013-06-05 16:27:56 +02:00
# modify it under the terms of the GNU General Public License version 3.
2011-05-30 20:52:25 +02:00
#
2013-09-16 17:59:31 +02:00
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2013-09-16 17:59:31 +02:00
# 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.
#
2021-09-02 21:49:06 +02:00
# See COPYRIGHT and LICENSE files for more details.
2011-05-29 13:11:52 -07:00
#++
2020-04-27 08:04:28 +02:00
require Rails . root . join ( " config/constants/open_project/activity " )
2007-09-23 17:19:27 +00:00
module Redmine # :nodoc:
2011-08-25 18:46:53 +02:00
class PluginError < StandardError
attr_reader :plugin_id
2021-02-11 16:02:18 +01:00
2014-11-03 21:53:03 +01:00
def initialize ( plug_id = nil )
2011-08-25 18:46:53 +02:00
super
@plugin_id = plug_id
end
end
2021-02-11 16:02:18 +01:00
2011-08-25 18:46:53 +02:00
class PluginNotFound < PluginError
def to_s
" Missing the plugin #{ @plugin_id } "
end
end
2021-02-11 16:02:18 +01:00
2011-08-25 18:46:53 +02:00
class PluginCircularDependency < PluginError
def to_s
" Circular plugin dependency in #{ @plugin_id } "
end
end
2021-02-11 16:02:18 +01:00
2011-08-25 18:46:53 +02:00
class PluginRequirementError < PluginError ; end
2011-05-30 20:52:25 +02:00
2007-09-23 17:19:27 +00:00
# Base class for Redmine plugins.
# Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# Redmine::Plugin.register :example do
# name 'Example plugin'
# author 'John Smith'
# description 'This is an example plugin for Redmine'
# version '0.0.1'
2015-10-29 19:12:02 +01:00
# settings default: {'foo'=>'bar'}, partial: 'settings/settings'
2007-09-23 17:19:27 +00:00
# end
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# === Plugin attributes
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# +settings+ is an optional attribute that let the plugin be configurable.
# It must be a hash with the following keys:
# * <tt>:default</tt>: default value for the plugin settings
# * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
# Example:
2015-10-29 19:12:02 +01:00
# settings default: {'foo'=>'bar'}, partial: 'settings/settings'
2007-09-23 17:19:27 +00:00
# In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# When rendered, the plugin settings value is available as the local variable +settings+
class Plugin
2011-08-25 18:46:53 +02:00
@registered_plugins = ActiveSupport :: OrderedHash . new
@deferred_plugins = { }
2012-07-30 16:55:01 +02:00
cattr_accessor :public_directory
2023-03-24 18:07:29 +01:00
self . public_directory = Rails . public_path . join ( " plugin_assets " )
2012-07-30 16:55:01 +02:00
2007-09-23 17:19:27 +00:00
class << self
2011-08-25 18:46:53 +02:00
attr_reader :registered_plugins , :deferred_plugins
2007-09-23 17:19:27 +00:00
private :new
def def_field ( * names )
2011-05-30 20:52:25 +02:00
class_eval do
2007-09-23 17:19:27 +00:00
names . each do | name |
2011-05-30 20:52:25 +02:00
define_method ( name ) do | * args |
2024-01-04 17:01:17 +01:00
args . empty? ? instance_variable_get ( :" @ #{ name } " ) : instance_variable_set ( :" @ #{ name } " , * args )
2007-09-23 17:19:27 +00:00
end
end
end
end
end
2023-09-04 12:06:15 +02:00
def_field :gem_name , :url , :author , :author_url , :version , :settings , :bundled
alias :bundled? :bundled
2008-11-16 15:22:48 +00:00
attr_reader :id
2011-05-30 20:52:25 +02:00
2007-09-23 17:19:27 +00:00
# Plugin constructor
2008-11-16 15:22:48 +00:00
def self . register ( id , & block )
2011-08-25 18:46:53 +02:00
id = id . to_sym
2008-11-16 15:22:48 +00:00
p = new ( id )
2007-09-23 17:19:27 +00:00
p . instance_eval ( & block )
2012-10-18 15:21:15 +02:00
2008-11-16 15:38:37 +00:00
registered_plugins [ id ] = p
2011-08-25 18:46:53 +02:00
2012-10-18 15:20:10 +02:00
if p . settings
2022-03-14 10:00:49 +01:00
Settings :: Definition . add ( " plugin_ #{ id } " ,
2022-05-16 11:11:32 +02:00
default : p . settings [ :default ] ,
2022-04-27 14:36:25 +02:00
format : :hash ,
env_alias : p . settings [ :env_alias ] )
2012-10-18 15:20:10 +02:00
end
2011-08-25 18:46:53 +02:00
# If there are plugins waiting for us to be loaded, we try loading those, again
if deferred_plugins [ id ]
deferred_plugins [ id ] . each do | ary |
plugin_id , block = ary
register ( plugin_id , & block )
end
deferred_plugins . delete ( id )
end
2019-06-24 09:26:20 +02:00
p
2011-08-25 18:46:53 +02:00
rescue PluginNotFound = > e
# find circular dependencies
2014-11-03 21:53:03 +01:00
raise PluginCircularDependency . new ( id ) if dependencies_for ( e . plugin_id ) . include? ( id )
2019-06-24 09:26:20 +02:00
2022-10-13 13:13:45 +02:00
raise
2011-08-25 18:46:53 +02:00
end
2020-08-18 08:24:34 +02:00
def name ( * args )
2023-09-06 10:58:27 +02:00
name = args . empty? ? instance_variable_get ( :@name ) : instance_variable_set ( :@name , * args )
2020-08-18 08:24:34 +02:00
2023-09-04 12:06:15 +02:00
return :: I18n . t ( name ) if name . is_a? ( Symbol )
return name if name
translated_name = :: I18n . t ( " plugin_ #{ id } .name " , default : nil )
translated_name || gemspec & . summary || id . to_s . humanize
end
def description ( * args )
2023-09-06 10:58:27 +02:00
description = args . empty? ? instance_variable_get ( :@description ) : instance_variable_set ( :@description , * args )
2023-09-04 12:06:15 +02:00
description || :: I18n . t ( " plugin_ #{ id } .description " , default : gemspec & . description )
end
def gemspec
Gem . loaded_specs [ gem_name ]
2020-08-18 08:24:34 +02:00
end
2011-08-25 18:46:53 +02:00
# returns an array of all dependencies we know of for plugin id
# (might not be complete at all times!)
def self . dependencies_for ( id )
2014-11-04 11:07:50 +01:00
direct_deps = deferred_plugins . keys . find_all { | k | deferred_plugins [ k ] . map ( & :first ) . include? ( id ) }
2014-11-03 21:53:03 +01:00
direct_deps . inject ( [ ] ) do | deps , v |
deps << v
deps += dependencies_for ( v )
2022-05-31 11:55:27 +02:00
end
2008-11-16 15:38:37 +00:00
end
2011-05-30 20:52:25 +02:00
2011-08-25 18:46:53 +02:00
# Returns an array of all registered plugins
2008-11-16 15:38:37 +00:00
def self . all
2011-08-25 18:46:53 +02:00
registered_plugins . values
2008-11-16 15:38:37 +00:00
end
2011-05-30 20:52:25 +02:00
2023-09-04 12:06:15 +02:00
def self . not_bundled
2011-08-25 18:46:53 +02:00
registered_plugins
2024-03-21 11:31:17 +01:00
. values
. reject ( & :bundled )
end
2008-11-16 15:38:37 +00:00
# Finds a plugin by its id
2008-11-16 16:08:25 +00:00
# Returns a PluginNotFound exception if the plugin doesn't exist
2008-11-16 15:38:37 +00:00
def self . find ( id )
2011-08-25 18:46:53 +02:00
registered_plugins [ id . to_sym ] || raise ( PluginNotFound . new ( id . to_sym ) )
2008-11-16 15:22:48 +00:00
end
2011-05-30 20:52:25 +02:00
2008-11-16 16:08:25 +00:00
# Clears the registered plugins hash
# It doesn't unload installed plugins
def self . clear
@registered_plugins = { }
end
2010-10-25 23:32:01 +00:00
# Checks if a plugin is installed
#
# @param [String] id name of the plugin
def self . installed? ( id )
registered_plugins [ id . to_sym ] . present?
end
2011-05-30 20:52:25 +02:00
2008-11-16 15:22:48 +00:00
def initialize ( id )
@id = id . to_sym
end
2011-05-30 20:52:25 +02:00
2021-02-11 16:02:18 +01:00
def <=> ( other )
id . to_s < = > other . id . to_s
2007-09-23 17:19:27 +00:00
end
2011-05-30 20:52:25 +02:00
2013-06-13 12:05:27 +02:00
# Sets a requirement on the OpenProject version.
# Raises a PluginRequirementError exception if the requirement is not met.
#
# It uses the same syntax as rubygems requirements.
# Examples
# # Requires exactly OpenProject 1.1.1
# requires_openproject "1.1.1"
# requires_openproject "= 1.1.1"
# # Requires OpenProject 1.1.x
# requires_openproject "~> 1.1.0"
# # Requires OpenProject between 1.1.0 and 1.1.5 or higher
# requires_openproject ">= 1.1.0", "<= 1.1.5"
2023-09-04 12:06:15 +02:00
def requires_openproject ( * )
required_version = Gem :: Requirement . new ( * )
2013-06-13 12:05:27 +02:00
op_version = Gem :: Version . new ( OpenProject :: VERSION . to_semver )
unless required_version . satisfied_by? op_version
raise PluginRequirementError . new ( " #{ id } plugin requires OpenProject version #{ required_version } but current version is #{ op_version } . " )
end
2021-02-11 16:02:18 +01:00
2013-06-13 12:05:27 +02:00
true
end
2009-12-16 02:07:46 +00:00
# Raises a PluginRequirementError exception if the requirement is not met
#
# Examples
# # Requires a plugin named :foo version 0.7.3 or higher
2015-10-29 19:12:02 +01:00
# requires_redmine_plugin :foo, version_or_higher: '0.7.3'
2009-12-16 02:07:46 +00:00
# requires_redmine_plugin :foo, '0.7.3'
#
# # Requires a specific version of a Redmine plugin
2015-10-29 19:12:02 +01:00
# requires_redmine_plugin :foo, version: '0.7.3' # 0.7.3 only
# requires_redmine_plugin :foo, version: ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
2009-12-16 02:07:46 +00:00
def requires_redmine_plugin ( plugin_name , arg )
2014-11-03 21:11:16 +01:00
arg = { version_or_higher : arg } unless arg . is_a? ( Hash )
2009-12-16 02:07:46 +00:00
arg . assert_valid_keys ( :version , :version_or_higher )
plugin = Plugin . find ( plugin_name )
2014-11-04 11:07:50 +01:00
current = plugin . version . split ( " . " ) . map ( & :to_i )
2009-12-16 02:07:46 +00:00
arg . each do | k , v |
v = [ ] << v unless v . is_a? ( Array )
2014-11-04 11:07:50 +01:00
versions = v . map { | s | s . split ( " . " ) . map ( & :to_i ) }
2009-12-16 02:07:46 +00:00
case k
when :version_or_higher
raise ArgumentError . new ( " wrong number of versions ( #{ versions . size } for 1) " ) unless versions . size == 1
unless ( current < = > versions . first ) > = 0
raise PluginRequirementError . new ( " #{ id } plugin requires the #{ plugin_name } plugin #{ v } or higher but current is #{ current . join ( '.' ) } " )
end
when :version
2014-11-03 21:53:03 +01:00
unless versions . include? ( current . slice ( 0 , 3 ) )
2009-12-16 02:07:46 +00:00
raise PluginRequirementError . new ( " #{ id } plugin requires one the following versions of #{ plugin_name } : #{ v . join ( ', ' ) } but current is #{ current . join ( '.' ) } " )
end
end
end
true
end
2007-09-23 17:19:27 +00:00
# Adds an item to the given +menu+.
# The +id+ parameter (equals to the project id) is automatically added to the url.
2015-10-29 19:12:02 +01:00
# menu :project_menu, :plugin_example, { controller: '/example', action: 'say_hello' }, caption: 'Sample'
2011-05-30 20:52:25 +02:00
#
2008-02-22 18:19:00 +00:00
# +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
2011-05-30 20:52:25 +02:00
#
2014-11-03 21:53:03 +01:00
def menu ( menu_name , item , url , options = { } )
2011-11-25 14:52:33 +01:00
Redmine :: MenuManager . map ( menu_name ) do | menu |
menu . push ( item , url , options )
end
2008-10-25 09:55:31 +00:00
end
alias :add_menu_item :menu
2011-05-30 20:52:25 +02:00
2023-03-28 16:58:00 +02:00
def configure_menu ( menu_name , & )
2023-03-24 18:07:29 +01:00
Redmine :: MenuManager . map ( menu_name , & )
end
2008-10-25 09:55:31 +00:00
# Removes +item+ from the given +menu+.
2011-11-25 14:52:33 +01:00
def delete_menu_item ( menu_name , item )
2015-06-24 14:09:03 +02:00
hide_menu_item ( menu_name , item )
2015-06-24 14:03:37 +02:00
end
# Allows to hide an existing +item+ in a menu.
2015-06-24 14:09:03 +02:00
#
# +hide_if+ parameter can be a lambda accepting a project, the item will only be hidden if
# the condition evaluates to true.
2021-02-11 16:02:18 +01:00
def hide_menu_item ( menu_name , item , hide_if : - > ( * ) { true } )
2015-06-24 14:03:37 +02:00
Redmine :: MenuManager . map ( menu_name ) do | menu |
2021-02-11 16:02:18 +01:00
menu . add_condition ( item , - > ( project ) { ! hide_if . call ( project ) } )
2015-06-24 14:03:37 +02:00
end
2007-09-23 17:19:27 +00:00
end
2019-07-29 13:00:00 +02:00
def rename_menu_item ( menu_name , item , options )
Redmine :: MenuManager . map ( menu_name ) do | menu |
menu_item = menu . find ( item )
menu_item . caption = options [ :caption ]
menu_item . icon = options [ :icon ]
menu_item . badge = options [ :badge ]
2019-08-02 17:27:37 +02:00
menu_item . url = options [ :url ]
2019-07-29 13:00:00 +02:00
end
end
2007-09-23 17:19:27 +00:00
# Defines a permission called +name+ for the given +actions+.
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
2015-10-29 19:12:02 +01:00
# permission :destroy_contacts, { contacts: :destroy }
# permission :view_contacts, { contacts: [:index, :show] }
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# The +options+ argument can be used to make the permission public (implicitly given to any user)
# or to restrict users the permission can be given to.
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# Examples
# # A permission that is implicitly given to any user
# # This permission won't appear on the Roles & Permissions setup screen
2015-10-29 19:12:02 +01:00
# permission :say_hello, { example: :say_hello }, public: true
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# # A permission that can be given to any user
2015-10-29 19:12:02 +01:00
# permission :say_hello, { example: :say_hello }
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# # A permission that can be given to registered users only
2015-10-29 19:12:02 +01:00
# permission :say_hello, { example: :say_hello }, require: :loggedin
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# # A permission that can be given to project members only
2015-10-29 19:12:02 +01:00
# permission :say_hello, { example: :say_hello }, require: :member
2007-09-23 17:19:27 +00:00
def permission ( name , actions , options = { } )
2019-05-09 21:11:01 +02:00
if @project_scope
2019-08-28 10:12:43 +02:00
mod , mod_options = @project_scope
2021-02-11 16:02:18 +01:00
OpenProject :: AccessControl . map do | map |
map . project_module ( mod , mod_options ) do | map |
2022-03-01 17:50:33 +01:00
map . permission ( name , actions , ** options )
2021-02-11 16:02:18 +01:00
end
end
2007-09-23 17:19:27 +00:00
else
2022-03-01 17:50:33 +01:00
OpenProject :: AccessControl . map { | map | map . permission ( name , actions , ** options ) }
2007-09-23 17:19:27 +00:00
end
end
2011-05-30 20:52:25 +02:00
2007-09-23 17:19:27 +00:00
# Defines a project module, that can be enabled/disabled for each project.
# Permissions defined inside +block+ will be bind to the module.
2011-05-30 20:52:25 +02:00
#
2007-09-23 17:19:27 +00:00
# project_module :things do
2015-10-29 19:12:02 +01:00
# permission :view_contacts, { contacts: [:list, :show] }, public: true
# permission :destroy_contacts, { contacts: :destroy }
2007-09-23 17:19:27 +00:00
# end
2019-05-09 21:11:01 +02:00
def project_module ( name , options = { } , & )
2022-04-01 08:58:56 +02:00
plugin = self
Rails . application . reloader . to_prepare do
2022-05-17 22:11:51 +02:00
plugin . instance_eval { @project_scope = [ name , options ] }
2022-04-01 08:58:56 +02:00
plugin . instance_eval ( & )
end
2019-05-09 21:11:01 +02:00
ensure
2022-05-17 22:11:51 +02:00
plugin . instance_eval { @project_scope = nil }
2007-09-23 17:19:27 +00:00
end
2011-05-30 20:52:25 +02:00
2008-07-27 18:38:31 +00:00
# Registers an activity provider.
#
# Options:
# * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
# * <tt>:default</tt> - setting this option to false will make the events not displayed by default
2011-05-30 20:52:25 +02:00
#
2008-07-27 18:38:31 +00:00
# A model can provide several activity event types.
2011-05-30 20:52:25 +02:00
#
2008-07-27 18:38:31 +00:00
# Examples:
# register :news
2015-10-29 19:12:02 +01:00
# register :scrums, class_name: 'Meeting'
# register :issues, class_name: ['Issue', 'Journal']
2011-05-30 20:52:25 +02:00
#
2008-07-27 18:38:31 +00:00
# Retrieving events:
# Associated model(s) must implement the find_events class method.
2011-05-30 20:52:25 +02:00
#
2014-04-04 23:19:39 +02:00
# The following call should return all the scrum events visible by current user that occurred in the 5 last days:
2008-07-27 18:38:31 +00:00
# Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
2015-10-29 19:12:02 +01:00
# Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, project: foo) # events for project foo only
2011-05-30 20:52:25 +02:00
#
2008-07-27 18:38:31 +00:00
# Note that :view_scrums permission is required to view these events in the activity view.
2023-09-04 12:06:15 +02:00
def activity_provider ( * )
2025-03-01 21:02:09 +01:00
ActiveSupport :: Deprecation . new . warn ( " Use ActsAsOpEngine # activity_provider instead. " )
2023-09-04 12:06:15 +02:00
OpenProject :: Activity . register ( * )
2008-07-27 18:38:31 +00:00
end
2011-05-30 20:52:25 +02:00
2008-10-27 11:08:29 +00:00
# Registers a wiki formatter.
#
# Parameters:
# * +name+ - human-readable name
# * +formatter+ - formatter class, which should have an instance method +to_html+
# * +helper+ - helper module, which will be included by wiki pages
def wiki_format_provider ( name , formatter , helper )
Redmine :: WikiFormatting . register ( name , formatter , helper )
end
2007-09-23 17:19:27 +00:00
# Returns +true+ if the plugin can be configured.
def configurable?
2023-03-24 18:07:29 +01:00
settings . is_a? ( Hash ) && settings [ :partial ] . present?
2007-09-23 17:19:27 +00:00
end
2012-08-03 16:50:52 +02:00
def mirror_assets
source = assets_directory
destination = public_directory
return unless File . directory? ( source )
2023-03-24 18:07:29 +01:00
source_files = Dir [ " #{ source } /**/* " ]
2012-08-03 16:50:52 +02:00
source_dirs = source_files . select { | d | File . directory? ( d ) }
source_files -= source_dirs
unless source_files . empty?
base_target_dir = File . join ( destination , File . dirname ( source_files . first ) . gsub ( source , " " ) )
FileUtils . mkdir_p ( base_target_dir )
end
source_dirs . each do | dir |
# strip down these paths so we have simple, relative paths we can
# add to the destination
target_dir = File . join ( destination , dir . gsub ( source , " " ) )
begin
FileUtils . mkdir_p ( target_dir )
2021-02-11 16:02:18 +01:00
rescue StandardError = > e
2012-08-03 16:50:52 +02:00
raise " Could not create directory #{ target_dir } : \n " + e
end
end
source_files . each do | file |
2021-02-11 16:02:18 +01:00
target = File . join ( destination , file . gsub ( source , " " ) )
unless File . exist? ( target ) && FileUtils . identical? ( file , target )
FileUtils . cp ( file , target )
2012-08-03 16:50:52 +02:00
end
2021-02-11 16:02:18 +01:00
rescue StandardError = > e
raise " Could not copy #{ file } to #{ target } : \n " + e
2012-08-03 16:50:52 +02:00
end
end
# Mirrors assets from one or all plugins to public/plugin_assets
2014-11-03 21:53:03 +01:00
def self . mirror_assets ( name = nil )
2012-08-03 16:50:52 +02:00
if name . present?
find ( name ) . mirror_assets
else
2014-11-03 21:53:03 +01:00
all . each ( & :mirror_assets )
2012-08-03 16:50:52 +02:00
end
end
2007-09-23 17:19:27 +00:00
end
end