You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

menu_manager.rb 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. module MenuManager
  20. # @private
  21. class MenuError < StandardError
  22. end
  23. module MenuController
  24. def self.included(base)
  25. base.class_attribute :main_menu
  26. base.main_menu = true
  27. base.extend(ClassMethods)
  28. end
  29. module ClassMethods
  30. @@menu_items = Hash.new {|hash, key| hash[key] = {:default => key, :actions => {}}}
  31. mattr_accessor :menu_items
  32. # Set the menu item name for a controller or specific actions
  33. # Examples:
  34. # * menu_item :tickets # => sets the menu name to :tickets for the whole controller
  35. # * menu_item :tickets, :only => :list # => sets the menu name to :tickets for the 'list' action only
  36. # * menu_item :tickets, :only => [:list, :show] # => sets the menu name to :tickets for 2 actions only
  37. #
  38. # The default menu item name for a controller is controller_name by default
  39. # Eg. the default menu item name for ProjectsController is :projects
  40. def menu_item(id, options = {})
  41. if actions = options[:only]
  42. actions = [] << actions unless actions.is_a?(Array)
  43. actions.each {|a| menu_items[controller_name.to_sym][:actions][a.to_sym] = id}
  44. else
  45. menu_items[controller_name.to_sym][:default] = id
  46. end
  47. end
  48. end
  49. def menu_items
  50. self.class.menu_items
  51. end
  52. def current_menu(project)
  53. if project && !project.new_record?
  54. :project_menu
  55. elsif self.class.main_menu
  56. :application_menu
  57. end
  58. end
  59. # Returns the menu item name according to the current action
  60. def current_menu_item
  61. @current_menu_item ||= menu_items[controller_name.to_sym][:actions][action_name.to_sym] ||
  62. menu_items[controller_name.to_sym][:default]
  63. end
  64. # Redirects user to the menu item
  65. # Returns false if user is not authorized
  66. def redirect_to_menu_item(name)
  67. redirect_to_project_menu_item(nil, name)
  68. end
  69. # Redirects user to the menu item of the given project
  70. # Returns false if user is not authorized
  71. def redirect_to_project_menu_item(project, name)
  72. menu = project.nil? ? :application_menu : :project_menu
  73. item = Redmine::MenuManager.items(menu).detect {|i| i.name.to_s == name.to_s}
  74. if item && item.allowed?(User.current, project)
  75. url = item.url
  76. url = {item.param => project}.merge(url) if project
  77. redirect_to url
  78. return true
  79. end
  80. false
  81. end
  82. end
  83. module MenuHelper
  84. # Returns the current menu item name
  85. def current_menu_item
  86. controller.current_menu_item
  87. end
  88. # Renders the application main menu
  89. def render_main_menu(project)
  90. if menu_name = controller.current_menu(project)
  91. render_menu(menu_name, project)
  92. end
  93. end
  94. def display_main_menu?(project)
  95. menu_name = controller.current_menu(project)
  96. menu_name.present? && Redmine::MenuManager.items(menu_name).children.present?
  97. end
  98. def render_menu(menu, project=nil)
  99. links = []
  100. menu_items_for(menu, project) do |node|
  101. links << render_menu_node(node, project)
  102. end
  103. links.empty? ? nil : content_tag('ul', links.join.html_safe)
  104. end
  105. def render_menu_node(node, project=nil)
  106. if node.children.present? || !node.child_menus.nil?
  107. return render_menu_node_with_children(node, project)
  108. else
  109. caption, url, selected = extract_node_details(node, project)
  110. return content_tag('li',
  111. render_single_menu_node(node, caption, url, selected))
  112. end
  113. end
  114. def render_menu_node_with_children(node, project=nil)
  115. caption, url, selected = extract_node_details(node, project)
  116. html = [].tap do |html|
  117. html << '<li>'
  118. # Parent
  119. html << render_single_menu_node(node, caption, url, selected)
  120. # Standard children
  121. standard_children_list = "".html_safe.tap do |child_html|
  122. node.children.each do |child|
  123. child_html << render_menu_node(child, project) if allowed_node?(child, User.current, project)
  124. end
  125. end
  126. html << content_tag(:ul, standard_children_list, :class => 'menu-children') unless standard_children_list.empty?
  127. # Unattached children
  128. unattached_children_list = render_unattached_children_menu(node, project)
  129. html << content_tag(:ul, unattached_children_list, :class => 'menu-children unattached') unless unattached_children_list.blank?
  130. html << '</li>'
  131. end
  132. return html.join("\n").html_safe
  133. end
  134. # Returns a list of unattached children menu items
  135. def render_unattached_children_menu(node, project)
  136. return nil unless node.child_menus
  137. "".html_safe.tap do |child_html|
  138. unattached_children = node.child_menus.call(project)
  139. # Tree nodes support #each so we need to do object detection
  140. if unattached_children.is_a? Array
  141. unattached_children.each do |child|
  142. child_html << content_tag(:li, render_unattached_menu_item(child, project)) if allowed_node?(child, User.current, project)
  143. end
  144. else
  145. raise MenuError, ":child_menus must be an array of MenuItems"
  146. end
  147. end
  148. end
  149. def render_single_menu_node(item, caption, url, selected)
  150. options = item.html_options(:selected => selected)
  151. # virtual nodes are only there for their children to be displayed in the menu
  152. # and should not do anything on click, except if otherwise defined elsewhere
  153. if url.blank?
  154. url = '#'
  155. options.reverse_merge!(:onclick => 'return false;')
  156. end
  157. link_to(h(caption), use_absolute_controller(url), options)
  158. end
  159. def render_unattached_menu_item(menu_item, project)
  160. raise MenuError, ":child_menus must be an array of MenuItems" unless menu_item.is_a? MenuItem
  161. if menu_item.allowed?(User.current, project)
  162. link_to(menu_item.caption, use_absolute_controller(menu_item.url), menu_item.html_options)
  163. end
  164. end
  165. def menu_items_for(menu, project=nil)
  166. items = []
  167. Redmine::MenuManager.items(menu).root.children.each do |node|
  168. if node.allowed?(User.current, project)
  169. if block_given?
  170. yield node
  171. else
  172. items << node # TODO: not used?
  173. end
  174. end
  175. end
  176. return block_given? ? nil : items
  177. end
  178. def extract_node_details(node, project=nil)
  179. item = node
  180. url =
  181. case item.url
  182. when Hash
  183. project.nil? ? item.url : {item.param => project}.merge(item.url)
  184. when Symbol
  185. if project
  186. send(item.url, project)
  187. else
  188. send(item.url)
  189. end
  190. else
  191. item.url
  192. end
  193. caption = item.caption(project)
  194. return [caption, url, (current_menu_item == item.name)]
  195. end
  196. # See MenuItem#allowed?
  197. def allowed_node?(node, user, project)
  198. unless node.is_a? MenuItem
  199. raise MenuError, ":child_menus must be an array of MenuItems"
  200. end
  201. node.allowed?(user, project)
  202. end
  203. # Prevent hash type URLs (e.g. {controller: 'foo', action: 'bar}) from being namespaced
  204. # when menus are rendered from views in namespaced controllers in plugins or engines
  205. def use_absolute_controller(url)
  206. if url.is_a?(Hash) && url[:controller].present? && !url[:controller].start_with?('/')
  207. url[:controller] = "/#{url[:controller]}"
  208. end
  209. url
  210. end
  211. end
  212. class << self
  213. def map(menu_name)
  214. @items ||= {}
  215. mapper = Mapper.new(menu_name.to_sym, @items)
  216. if block_given?
  217. yield mapper
  218. else
  219. mapper
  220. end
  221. end
  222. def items(menu_name)
  223. @items[menu_name.to_sym] || MenuNode.new(:root, {})
  224. end
  225. end
  226. class Mapper
  227. attr_reader :menu, :menu_items
  228. def initialize(menu, items)
  229. items[menu] ||= MenuNode.new(:root, {})
  230. @menu = menu
  231. @menu_items = items[menu]
  232. end
  233. # Adds an item at the end of the menu. Available options:
  234. # * param: the parameter name that is used for the project id (default is :id)
  235. # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
  236. # * caption that can be:
  237. # * a localized string Symbol
  238. # * a String
  239. # * a Proc that can take the project as argument
  240. # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
  241. # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
  242. # * children: a Proc that is called before rendering the item. The Proc should return an array of MenuItems, which will be added as children to this item.
  243. # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
  244. # * last: menu item will stay at the end (eg. :last => true)
  245. # * html_options: a hash of html options that are passed to link_to
  246. def push(name, url, options={})
  247. options = options.dup
  248. if options[:parent]
  249. subtree = self.find(options[:parent])
  250. target_root = subtree || @menu_items.root
  251. else
  252. target_root = @menu_items.root
  253. end
  254. # menu item position
  255. if first = options.delete(:first)
  256. target_root.prepend(MenuItem.new(name, url, options))
  257. elsif before = options.delete(:before)
  258. if exists?(before)
  259. target_root.add_at(MenuItem.new(name, url, options), position_of(before))
  260. else
  261. target_root.add(MenuItem.new(name, url, options))
  262. end
  263. elsif after = options.delete(:after)
  264. if exists?(after)
  265. target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1)
  266. else
  267. target_root.add(MenuItem.new(name, url, options))
  268. end
  269. elsif options[:last] # don't delete, needs to be stored
  270. target_root.add_last(MenuItem.new(name, url, options))
  271. else
  272. target_root.add(MenuItem.new(name, url, options))
  273. end
  274. end
  275. # Removes a menu item
  276. def delete(name)
  277. if found = self.find(name)
  278. @menu_items.remove!(found)
  279. end
  280. end
  281. # Checks if a menu item exists
  282. def exists?(name)
  283. @menu_items.any? {|node| node.name == name}
  284. end
  285. def find(name)
  286. @menu_items.find {|node| node.name == name}
  287. end
  288. def position_of(name)
  289. @menu_items.each do |node|
  290. if node.name == name
  291. return node.position
  292. end
  293. end
  294. end
  295. end
  296. class MenuNode
  297. include Enumerable
  298. attr_accessor :parent
  299. attr_reader :last_items_count, :name
  300. def initialize(name, content = nil)
  301. @name = name
  302. @children = []
  303. @last_items_count = 0
  304. end
  305. def children
  306. if block_given?
  307. @children.each {|child| yield child}
  308. else
  309. @children
  310. end
  311. end
  312. # Returns the number of descendants + 1
  313. def size
  314. @children.inject(1) {|sum, node| sum + node.size}
  315. end
  316. def each(&block)
  317. yield self
  318. children {|child| child.each(&block)}
  319. end
  320. # Adds a child at first position
  321. def prepend(child)
  322. add_at(child, 0)
  323. end
  324. # Adds a child at given position
  325. def add_at(child, position)
  326. raise "Child already added" if find {|node| node.name == child.name}
  327. @children = @children.insert(position, child)
  328. child.parent = self
  329. child
  330. end
  331. # Adds a child as last child
  332. def add_last(child)
  333. add_at(child, -1)
  334. @last_items_count += 1
  335. child
  336. end
  337. # Adds a child
  338. def add(child)
  339. position = @children.size - @last_items_count
  340. add_at(child, position)
  341. end
  342. alias :<< :add
  343. # Removes a child
  344. def remove!(child)
  345. @children.delete(child)
  346. @last_items_count -= +1 if child && child.last
  347. child.parent = nil
  348. child
  349. end
  350. # Returns the position for this node in it's parent
  351. def position
  352. self.parent.children.index(self)
  353. end
  354. # Returns the root for this node
  355. def root
  356. root = self
  357. root = root.parent while root.parent
  358. root
  359. end
  360. end
  361. class MenuItem < MenuNode
  362. include Redmine::I18n
  363. attr_reader :name, :url, :param, :condition, :parent,
  364. :child_menus, :last, :permission
  365. def initialize(name, url, options={})
  366. if options[:if] && !options[:if].respond_to?(:call)
  367. raise ArgumentError, "Invalid option :if for menu item '#{name}'"
  368. end
  369. if options[:html] && !options[:html].is_a?(Hash)
  370. raise ArgumentError, "Invalid option :html for menu item '#{name}'"
  371. end
  372. if options[:parent] == name.to_sym
  373. raise ArgumentError, "Cannot set the :parent to be the same as this item"
  374. end
  375. if options[:children] && !options[:children].respond_to?(:call)
  376. raise ArgumentError, "Invalid option :children for menu item '#{name}'"
  377. end
  378. @name = name
  379. @url = url
  380. @condition = options[:if]
  381. @permission = options[:permission]
  382. @permission ||= false if options.key?(:permission)
  383. @param = options[:param] || :id
  384. @caption = options[:caption]
  385. @html_options = options[:html] || {}
  386. # Adds a unique class to each menu item based on its name
  387. @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
  388. @parent = options[:parent]
  389. @child_menus = options[:children]
  390. @last = options[:last] || false
  391. super @name.to_sym
  392. end
  393. def caption(project=nil)
  394. if @caption.is_a?(Proc)
  395. c = @caption.call(project).to_s
  396. c = @name.to_s.humanize if c.blank?
  397. c
  398. else
  399. if @caption.nil?
  400. l_or_humanize(name, :prefix => 'label_')
  401. else
  402. @caption.is_a?(Symbol) ? l(@caption) : @caption
  403. end
  404. end
  405. end
  406. def html_options(options={})
  407. if options[:selected]
  408. o = @html_options.dup
  409. o[:class] += ' selected'
  410. o
  411. else
  412. @html_options
  413. end
  414. end
  415. # Checks if a user is allowed to access the menu item by:
  416. #
  417. # * Checking the permission or the url target (project only)
  418. # * Checking the conditions of the item
  419. def allowed?(user, project)
  420. if url.blank?
  421. # this is a virtual node that is only there for its children to be diplayed in the menu
  422. # it is considered an allowed node if at least one of the children is allowed
  423. all_children = children
  424. all_children += child_menus.call(project) if child_menus
  425. unless all_children.detect{|child| child.allowed?(user, project)}
  426. return false
  427. end
  428. elsif user && project
  429. if permission
  430. unless user.allowed_to?(permission, project)
  431. return false
  432. end
  433. elsif permission.nil? && url.is_a?(Hash)
  434. unless user.allowed_to?(url, project)
  435. return false
  436. end
  437. end
  438. end
  439. if condition && !condition.call(project)
  440. # Condition that doesn't pass
  441. return false
  442. end
  443. return true
  444. end
  445. end
  446. end
  447. end