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