Chris@909
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 module Redmine #:nodoc:
|
Chris@0
|
19
|
Chris@0
|
20 class PluginNotFound < StandardError; end
|
Chris@0
|
21 class PluginRequirementError < StandardError; end
|
Chris@909
|
22
|
Chris@0
|
23 # Base class for Redmine plugins.
|
Chris@0
|
24 # Plugins are registered using the <tt>register</tt> class method that acts as the public constructor.
|
Chris@909
|
25 #
|
Chris@0
|
26 # Redmine::Plugin.register :example do
|
Chris@0
|
27 # name 'Example plugin'
|
Chris@0
|
28 # author 'John Smith'
|
Chris@0
|
29 # description 'This is an example plugin for Redmine'
|
Chris@0
|
30 # version '0.0.1'
|
Chris@0
|
31 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
|
Chris@0
|
32 # end
|
Chris@909
|
33 #
|
Chris@0
|
34 # === Plugin attributes
|
Chris@909
|
35 #
|
Chris@0
|
36 # +settings+ is an optional attribute that let the plugin be configurable.
|
Chris@0
|
37 # It must be a hash with the following keys:
|
Chris@0
|
38 # * <tt>:default</tt>: default value for the plugin settings
|
Chris@0
|
39 # * <tt>:partial</tt>: path of the configuration partial view, relative to the plugin <tt>app/views</tt> directory
|
Chris@0
|
40 # Example:
|
Chris@0
|
41 # settings :default => {'foo'=>'bar'}, :partial => 'settings/settings'
|
Chris@0
|
42 # In this example, the settings partial will be found here in the plugin directory: <tt>app/views/settings/_settings.rhtml</tt>.
|
Chris@909
|
43 #
|
Chris@0
|
44 # When rendered, the plugin settings value is available as the local variable +settings+
|
Chris@0
|
45 class Plugin
|
Chris@0
|
46 @registered_plugins = {}
|
Chris@0
|
47 class << self
|
Chris@0
|
48 attr_reader :registered_plugins
|
Chris@0
|
49 private :new
|
Chris@0
|
50
|
Chris@0
|
51 def def_field(*names)
|
Chris@909
|
52 class_eval do
|
Chris@0
|
53 names.each do |name|
|
Chris@909
|
54 define_method(name) do |*args|
|
Chris@0
|
55 args.empty? ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", *args)
|
Chris@0
|
56 end
|
Chris@0
|
57 end
|
Chris@0
|
58 end
|
Chris@0
|
59 end
|
Chris@0
|
60 end
|
Chris@0
|
61 def_field :name, :description, :url, :author, :author_url, :version, :settings
|
Chris@0
|
62 attr_reader :id
|
Chris@909
|
63
|
Chris@0
|
64 # Plugin constructor
|
Chris@0
|
65 def self.register(id, &block)
|
Chris@0
|
66 p = new(id)
|
Chris@0
|
67 p.instance_eval(&block)
|
Chris@0
|
68 # Set a default name if it was not provided during registration
|
Chris@0
|
69 p.name(id.to_s.humanize) if p.name.nil?
|
Chris@0
|
70 # Adds plugin locales if any
|
Chris@0
|
71 # YAML translation files should be found under <plugin>/config/locales/
|
Chris@909
|
72 ::I18n.load_path += Dir.glob(File.join(Rails.root, 'vendor', 'plugins', id.to_s, 'config', 'locales', '*.yml'))
|
Chris@0
|
73 registered_plugins[id] = p
|
Chris@0
|
74 end
|
Chris@909
|
75
|
Chris@909
|
76 # Returns an array of all registered plugins
|
Chris@0
|
77 def self.all
|
Chris@0
|
78 registered_plugins.values.sort
|
Chris@0
|
79 end
|
Chris@909
|
80
|
Chris@0
|
81 # Finds a plugin by its id
|
Chris@0
|
82 # Returns a PluginNotFound exception if the plugin doesn't exist
|
Chris@0
|
83 def self.find(id)
|
Chris@0
|
84 registered_plugins[id.to_sym] || raise(PluginNotFound)
|
Chris@0
|
85 end
|
Chris@909
|
86
|
Chris@0
|
87 # Clears the registered plugins hash
|
Chris@0
|
88 # It doesn't unload installed plugins
|
Chris@0
|
89 def self.clear
|
Chris@0
|
90 @registered_plugins = {}
|
Chris@0
|
91 end
|
chris@37
|
92
|
chris@37
|
93 # Checks if a plugin is installed
|
chris@37
|
94 #
|
chris@37
|
95 # @param [String] id name of the plugin
|
chris@37
|
96 def self.installed?(id)
|
chris@37
|
97 registered_plugins[id.to_sym].present?
|
chris@37
|
98 end
|
Chris@909
|
99
|
Chris@0
|
100 def initialize(id)
|
Chris@0
|
101 @id = id.to_sym
|
Chris@0
|
102 end
|
Chris@909
|
103
|
Chris@0
|
104 def <=>(plugin)
|
Chris@0
|
105 self.id.to_s <=> plugin.id.to_s
|
Chris@0
|
106 end
|
Chris@909
|
107
|
Chris@0
|
108 # Sets a requirement on Redmine version
|
Chris@0
|
109 # Raises a PluginRequirementError exception if the requirement is not met
|
Chris@0
|
110 #
|
Chris@0
|
111 # Examples
|
Chris@0
|
112 # # Requires Redmine 0.7.3 or higher
|
Chris@0
|
113 # requires_redmine :version_or_higher => '0.7.3'
|
Chris@0
|
114 # requires_redmine '0.7.3'
|
Chris@0
|
115 #
|
Chris@0
|
116 # # Requires a specific Redmine version
|
Chris@0
|
117 # requires_redmine :version => '0.7.3' # 0.7.3 only
|
Chris@0
|
118 # requires_redmine :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
|
Chris@0
|
119 def requires_redmine(arg)
|
Chris@0
|
120 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
|
Chris@0
|
121 arg.assert_valid_keys(:version, :version_or_higher)
|
Chris@909
|
122
|
Chris@0
|
123 current = Redmine::VERSION.to_a
|
Chris@0
|
124 arg.each do |k, v|
|
Chris@0
|
125 v = [] << v unless v.is_a?(Array)
|
Chris@0
|
126 versions = v.collect {|s| s.split('.').collect(&:to_i)}
|
Chris@0
|
127 case k
|
Chris@0
|
128 when :version_or_higher
|
Chris@0
|
129 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
|
Chris@0
|
130 unless (current <=> versions.first) >= 0
|
Chris@0
|
131 raise PluginRequirementError.new("#{id} plugin requires Redmine #{v} or higher but current is #{current.join('.')}")
|
Chris@0
|
132 end
|
Chris@0
|
133 when :version
|
Chris@0
|
134 unless versions.include?(current.slice(0,3))
|
Chris@0
|
135 raise PluginRequirementError.new("#{id} plugin requires one the following Redmine versions: #{v.join(', ')} but current is #{current.join('.')}")
|
Chris@0
|
136 end
|
Chris@0
|
137 end
|
Chris@0
|
138 end
|
Chris@0
|
139 true
|
Chris@0
|
140 end
|
Chris@0
|
141
|
Chris@0
|
142 # Sets a requirement on a Redmine plugin version
|
Chris@0
|
143 # Raises a PluginRequirementError exception if the requirement is not met
|
Chris@0
|
144 #
|
Chris@0
|
145 # Examples
|
Chris@0
|
146 # # Requires a plugin named :foo version 0.7.3 or higher
|
Chris@0
|
147 # requires_redmine_plugin :foo, :version_or_higher => '0.7.3'
|
Chris@0
|
148 # requires_redmine_plugin :foo, '0.7.3'
|
Chris@0
|
149 #
|
Chris@0
|
150 # # Requires a specific version of a Redmine plugin
|
Chris@0
|
151 # requires_redmine_plugin :foo, :version => '0.7.3' # 0.7.3 only
|
Chris@0
|
152 # requires_redmine_plugin :foo, :version => ['0.7.3', '0.8.0'] # 0.7.3 or 0.8.0
|
Chris@0
|
153 def requires_redmine_plugin(plugin_name, arg)
|
Chris@0
|
154 arg = { :version_or_higher => arg } unless arg.is_a?(Hash)
|
Chris@0
|
155 arg.assert_valid_keys(:version, :version_or_higher)
|
Chris@0
|
156
|
Chris@0
|
157 plugin = Plugin.find(plugin_name)
|
Chris@0
|
158 current = plugin.version.split('.').collect(&:to_i)
|
Chris@0
|
159
|
Chris@0
|
160 arg.each do |k, v|
|
Chris@0
|
161 v = [] << v unless v.is_a?(Array)
|
Chris@0
|
162 versions = v.collect {|s| s.split('.').collect(&:to_i)}
|
Chris@0
|
163 case k
|
Chris@0
|
164 when :version_or_higher
|
Chris@0
|
165 raise ArgumentError.new("wrong number of versions (#{versions.size} for 1)") unless versions.size == 1
|
Chris@0
|
166 unless (current <=> versions.first) >= 0
|
Chris@0
|
167 raise PluginRequirementError.new("#{id} plugin requires the #{plugin_name} plugin #{v} or higher but current is #{current.join('.')}")
|
Chris@0
|
168 end
|
Chris@0
|
169 when :version
|
Chris@0
|
170 unless versions.include?(current.slice(0,3))
|
Chris@0
|
171 raise PluginRequirementError.new("#{id} plugin requires one the following versions of #{plugin_name}: #{v.join(', ')} but current is #{current.join('.')}")
|
Chris@0
|
172 end
|
Chris@0
|
173 end
|
Chris@0
|
174 end
|
Chris@0
|
175 true
|
Chris@0
|
176 end
|
Chris@0
|
177
|
Chris@0
|
178 # Adds an item to the given +menu+.
|
Chris@0
|
179 # The +id+ parameter (equals to the project id) is automatically added to the url.
|
Chris@0
|
180 # menu :project_menu, :plugin_example, { :controller => 'example', :action => 'say_hello' }, :caption => 'Sample'
|
Chris@909
|
181 #
|
Chris@0
|
182 # +name+ parameter can be: :top_menu, :account_menu, :application_menu or :project_menu
|
Chris@909
|
183 #
|
Chris@0
|
184 def menu(menu, item, url, options={})
|
Chris@0
|
185 Redmine::MenuManager.map(menu).push(item, url, options)
|
Chris@0
|
186 end
|
Chris@0
|
187 alias :add_menu_item :menu
|
Chris@909
|
188
|
Chris@0
|
189 # Removes +item+ from the given +menu+.
|
Chris@0
|
190 def delete_menu_item(menu, item)
|
Chris@0
|
191 Redmine::MenuManager.map(menu).delete(item)
|
Chris@0
|
192 end
|
Chris@0
|
193
|
Chris@0
|
194 # Defines a permission called +name+ for the given +actions+.
|
Chris@909
|
195 #
|
Chris@0
|
196 # The +actions+ argument is a hash with controllers as keys and actions as values (a single value or an array):
|
Chris@0
|
197 # permission :destroy_contacts, { :contacts => :destroy }
|
Chris@0
|
198 # permission :view_contacts, { :contacts => [:index, :show] }
|
Chris@909
|
199 #
|
Chris@0
|
200 # The +options+ argument can be used to make the permission public (implicitly given to any user)
|
Chris@0
|
201 # or to restrict users the permission can be given to.
|
Chris@909
|
202 #
|
Chris@0
|
203 # Examples
|
Chris@0
|
204 # # A permission that is implicitly given to any user
|
Chris@0
|
205 # # This permission won't appear on the Roles & Permissions setup screen
|
Chris@0
|
206 # permission :say_hello, { :example => :say_hello }, :public => true
|
Chris@909
|
207 #
|
Chris@0
|
208 # # A permission that can be given to any user
|
Chris@0
|
209 # permission :say_hello, { :example => :say_hello }
|
Chris@909
|
210 #
|
Chris@0
|
211 # # A permission that can be given to registered users only
|
Chris@0
|
212 # permission :say_hello, { :example => :say_hello }, :require => :loggedin
|
Chris@909
|
213 #
|
Chris@0
|
214 # # A permission that can be given to project members only
|
Chris@0
|
215 # permission :say_hello, { :example => :say_hello }, :require => :member
|
Chris@0
|
216 def permission(name, actions, options = {})
|
Chris@0
|
217 if @project_module
|
Chris@0
|
218 Redmine::AccessControl.map {|map| map.project_module(@project_module) {|map|map.permission(name, actions, options)}}
|
Chris@0
|
219 else
|
Chris@0
|
220 Redmine::AccessControl.map {|map| map.permission(name, actions, options)}
|
Chris@0
|
221 end
|
Chris@0
|
222 end
|
Chris@909
|
223
|
Chris@0
|
224 # Defines a project module, that can be enabled/disabled for each project.
|
Chris@0
|
225 # Permissions defined inside +block+ will be bind to the module.
|
Chris@909
|
226 #
|
Chris@0
|
227 # project_module :things do
|
Chris@0
|
228 # permission :view_contacts, { :contacts => [:list, :show] }, :public => true
|
Chris@0
|
229 # permission :destroy_contacts, { :contacts => :destroy }
|
Chris@0
|
230 # end
|
Chris@0
|
231 def project_module(name, &block)
|
Chris@0
|
232 @project_module = name
|
Chris@0
|
233 self.instance_eval(&block)
|
Chris@0
|
234 @project_module = nil
|
Chris@0
|
235 end
|
Chris@909
|
236
|
Chris@0
|
237 # Registers an activity provider.
|
Chris@0
|
238 #
|
Chris@0
|
239 # Options:
|
Chris@0
|
240 # * <tt>:class_name</tt> - one or more model(s) that provide these events (inferred from event_type by default)
|
Chris@0
|
241 # * <tt>:default</tt> - setting this option to false will make the events not displayed by default
|
Chris@909
|
242 #
|
Chris@0
|
243 # A model can provide several activity event types.
|
Chris@909
|
244 #
|
Chris@0
|
245 # Examples:
|
Chris@0
|
246 # register :news
|
Chris@0
|
247 # register :scrums, :class_name => 'Meeting'
|
Chris@0
|
248 # register :issues, :class_name => ['Issue', 'Journal']
|
Chris@909
|
249 #
|
Chris@0
|
250 # Retrieving events:
|
Chris@0
|
251 # Associated model(s) must implement the find_events class method.
|
Chris@0
|
252 # ActiveRecord models can use acts_as_activity_provider as a way to implement this class method.
|
Chris@909
|
253 #
|
Chris@909
|
254 # The following call should return all the scrum events visible by current user that occured in the 5 last days:
|
Chris@0
|
255 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today)
|
Chris@0
|
256 # Meeting.find_events('scrums', User.current, 5.days.ago, Date.today, :project => foo) # events for project foo only
|
Chris@909
|
257 #
|
Chris@0
|
258 # Note that :view_scrums permission is required to view these events in the activity view.
|
Chris@0
|
259 def activity_provider(*args)
|
Chris@0
|
260 Redmine::Activity.register(*args)
|
Chris@0
|
261 end
|
Chris@909
|
262
|
Chris@0
|
263 # Registers a wiki formatter.
|
Chris@0
|
264 #
|
Chris@0
|
265 # Parameters:
|
Chris@0
|
266 # * +name+ - human-readable name
|
Chris@0
|
267 # * +formatter+ - formatter class, which should have an instance method +to_html+
|
Chris@0
|
268 # * +helper+ - helper module, which will be included by wiki pages
|
Chris@0
|
269 def wiki_format_provider(name, formatter, helper)
|
Chris@0
|
270 Redmine::WikiFormatting.register(name, formatter, helper)
|
Chris@0
|
271 end
|
Chris@0
|
272
|
Chris@0
|
273 # Returns +true+ if the plugin can be configured.
|
Chris@0
|
274 def configurable?
|
Chris@0
|
275 settings && settings.is_a?(Hash) && !settings[:partial].blank?
|
Chris@0
|
276 end
|
Chris@0
|
277 end
|
Chris@0
|
278 end
|