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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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), 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, 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. end
  204. class << self
  205. def map(menu_name)
  206. @items ||= {}
  207. mapper = Mapper.new(menu_name.to_sym, @items)
  208. if block_given?
  209. yield mapper
  210. else
  211. mapper
  212. end
  213. end
  214. def items(menu_name)
  215. @items[menu_name.to_sym] || MenuNode.new(:root, {})
  216. end
  217. end
  218. class Mapper
  219. attr_reader :menu, :menu_items
  220. def initialize(menu, items)
  221. items[menu] ||= MenuNode.new(:root, {})
  222. @menu = menu
  223. @menu_items = items[menu]
  224. end
  225. # Adds an item at the end of the menu. Available options:
  226. # * param: the parameter name that is used for the project id (default is :id)
  227. # * if: a Proc that is called before rendering the item, the item is displayed only if it returns true
  228. # * caption that can be:
  229. # * a localized string Symbol
  230. # * a String
  231. # * a Proc that can take the project as argument
  232. # * before, after: specify where the menu item should be inserted (eg. :after => :activity)
  233. # * parent: menu item will be added as a child of another named menu (eg. :parent => :issues)
  234. # * 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.
  235. # eg. :children => Proc.new {|project| [Redmine::MenuManager::MenuItem.new(...)] }
  236. # * last: menu item will stay at the end (eg. :last => true)
  237. # * html_options: a hash of html options that are passed to link_to
  238. def push(name, url, options={})
  239. options = options.dup
  240. if options[:parent]
  241. subtree = self.find(options[:parent])
  242. if subtree
  243. target_root = subtree
  244. else
  245. target_root = @menu_items.root
  246. end
  247. else
  248. target_root = @menu_items.root
  249. end
  250. # menu item position
  251. if first = options.delete(:first)
  252. target_root.prepend(MenuItem.new(name, url, options))
  253. elsif before = options.delete(:before)
  254. if exists?(before)
  255. target_root.add_at(MenuItem.new(name, url, options), position_of(before))
  256. else
  257. target_root.add(MenuItem.new(name, url, options))
  258. end
  259. elsif after = options.delete(:after)
  260. if exists?(after)
  261. target_root.add_at(MenuItem.new(name, url, options), position_of(after) + 1)
  262. else
  263. target_root.add(MenuItem.new(name, url, options))
  264. end
  265. elsif options[:last] # don't delete, needs to be stored
  266. target_root.add_last(MenuItem.new(name, url, options))
  267. else
  268. target_root.add(MenuItem.new(name, url, options))
  269. end
  270. end
  271. # Removes a menu item
  272. def delete(name)
  273. if found = self.find(name)
  274. @menu_items.remove!(found)
  275. end
  276. end
  277. # Checks if a menu item exists
  278. def exists?(name)
  279. @menu_items.any? {|node| node.name == name}
  280. end
  281. def find(name)
  282. @menu_items.find {|node| node.name == name}
  283. end
  284. def position_of(name)
  285. @menu_items.each do |node|
  286. if node.name == name
  287. return node.position
  288. end
  289. end
  290. end
  291. end
  292. class MenuNode
  293. include Enumerable
  294. attr_accessor :parent
  295. attr_reader :last_items_count, :name
  296. def initialize(name, content = nil)
  297. @name = name
  298. @children = []
  299. @last_items_count = 0
  300. end
  301. def children
  302. if block_given?
  303. @children.each {|child| yield child}
  304. else
  305. @children
  306. end
  307. end
  308. # Returns the number of descendants + 1
  309. def size
  310. @children.inject(1) {|sum, node| sum + node.size}
  311. end
  312. def each(&block)
  313. yield self
  314. children {|child| child.each(&block)}
  315. end
  316. # Adds a child at first position
  317. def prepend(child)
  318. add_at(child, 0)
  319. end
  320. # Adds a child at given position
  321. def add_at(child, position)
  322. raise "Child already added" if find {|node| node.name == child.name}
  323. @children = @children.insert(position, child)
  324. child.parent = self
  325. child
  326. end
  327. # Adds a child as last child
  328. def add_last(child)
  329. add_at(child, -1)
  330. @last_items_count += 1
  331. child
  332. end
  333. # Adds a child
  334. def add(child)
  335. position = @children.size - @last_items_count
  336. add_at(child, position)
  337. end
  338. alias :<< :add
  339. # Removes a child
  340. def remove!(child)
  341. @children.delete(child)
  342. @last_items_count -= +1 if child && child.last
  343. child.parent = nil
  344. child
  345. end
  346. # Returns the position for this node in it's parent
  347. def position
  348. self.parent.children.index(self)
  349. end
  350. # Returns the root for this node
  351. def root
  352. root = self
  353. root = root.parent while root.parent
  354. root
  355. end
  356. end
  357. class MenuItem < MenuNode
  358. include Redmine::I18n
  359. attr_reader :name, :url, :param, :condition, :parent,
  360. :child_menus, :last, :permission
  361. def initialize(name, url, options={})
  362. if options[:if] && !options[:if].respond_to?(:call)
  363. raise ArgumentError, "Invalid option :if for menu item '#{name}'"
  364. end
  365. if options[:html] && !options[:html].is_a?(Hash)
  366. raise ArgumentError, "Invalid option :html for menu item '#{name}'"
  367. end
  368. if options[:parent] == name.to_sym
  369. raise ArgumentError, "Cannot set the :parent to be the same as this item"
  370. end
  371. if options[:children] && !options[:children].respond_to?(:call)
  372. raise ArgumentError, "Invalid option :children for menu item '#{name}'"
  373. end
  374. @name = name
  375. @url = url
  376. @condition = options[:if]
  377. @permission = options[:permission]
  378. @permission ||= false if options.key?(:permission)
  379. @param = options[:param] || :id
  380. @caption = options[:caption]
  381. @html_options = options[:html] || {}
  382. # Adds a unique class to each menu item based on its name
  383. @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ')
  384. @parent = options[:parent]
  385. @child_menus = options[:children]
  386. @last = options[:last] || false
  387. super @name.to_sym
  388. end
  389. def caption(project=nil)
  390. if @caption.is_a?(Proc)
  391. c = @caption.call(project).to_s
  392. c = @name.to_s.humanize if c.blank?
  393. c
  394. else
  395. if @caption.nil?
  396. l_or_humanize(name, :prefix => 'label_')
  397. else
  398. @caption.is_a?(Symbol) ? l(@caption) : @caption
  399. end
  400. end
  401. end
  402. def html_options(options={})
  403. if options[:selected]
  404. o = @html_options.dup
  405. o[:class] += ' selected'
  406. o
  407. else
  408. @html_options
  409. end
  410. end
  411. # Checks if a user is allowed to access the menu item by:
  412. #
  413. # * Checking the permission or the url target (project only)
  414. # * Checking the conditions of the item
  415. def allowed?(user, project)
  416. if url.blank?
  417. # this is a virtual node that is only there for its children to be diplayed in the menu
  418. # it is considered an allowed node if at least one of the children is allowed
  419. all_children = children
  420. all_children += child_menus.call(project) if child_menus
  421. unless all_children.detect{|child| child.allowed?(user, project)}
  422. return false
  423. end
  424. elsif user && project
  425. if permission
  426. unless user.allowed_to?(permission, project)
  427. return false
  428. end
  429. elsif permission.nil? && url.is_a?(Hash)
  430. unless user.allowed_to?(url, project)
  431. return false
  432. end
  433. end
  434. end
  435. if condition && !condition.call(project)
  436. # Condition that doesn't pass
  437. return false
  438. end
  439. return true
  440. end
  441. end
  442. end
  443. end