cross-project_revision_and_commit_links_v2.diff

Support for cross-project revision and commit links v2 - Babar O'Cap, 2009-08-18 17:22

Download (6.68 KB)

View differences:

C:\Depots\redmine\app\helpers\application_helper.rb
29 29
  def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30 30

  
31 31
  # Return true if user is authorized for controller/action, otherwise false
32
  def authorize_for(controller, action)
33
    User.current.allowed_to?({:controller => controller, :action => action}, @project)
32
  def authorize_for(controller, action, project = @project)
33
    User.current.allowed_to?({:controller => controller, :action => action}, project)
34 34
  end
35 35

  
36 36
  # Display a link if user is authorized
37 37
  def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
38
    link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
38
    link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action], options[:project])
39 39
  end
40 40

  
41 41
  # Display a link to remote if user is authorized
42 42
  def link_to_remote_if_authorized(name, options = {}, html_options = nil)
43 43
    url = options[:url] || {}
44
    link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
44
    link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action], options[:project])
45 45
  end
46 46

  
47 47
  # Display a link to user's account page
......
422 422
    #     #52 -> Link to issue #52
423 423
    #   Changesets:
424 424
    #     r52 -> Link to revision 52
425
    #     r:project:52 -> Link to revision 52 of an other project, using project name or identifier
425 426
    #     commit:a85130f -> Link to scmid starting with a85130f
427
    #     commit:project:a85130f -> Link to scmid starting with a85130f of an other project, using project name or identifier
426 428
    #   Documents:
427 429
    #     document#17 -> Link to document with id 17
428 430
    #     document:Greetings -> Link to the document with title "Greetings"
......
441 443
    #     export:some/file -> Force the download of the file
442 444
    #  Forum messages:
443 445
    #     message#1218 -> Link to message with id 1218
444
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
445
      leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
446
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(:[^\s<>][^\:\r\n\b]+[^\s<>]:)?(\d+)|(:)([^\s<>][^\:\r\n\b]+[^\s<>]:)?([^"\:\s<>][^\:\s\r\n\b<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
447
      leading, esc, prefix, sep, revproj, oid = $1, $2, $3, $8 || $5, $9 || $6, $10 || $7
446 448
      link = nil
447 449
      if esc.nil?
448 450
        if prefix.nil? && sep == 'r'
451
          if revproj.nil?
449 452
          if project && (changeset = project.changesets.find_by_revision(oid))
450 453
            link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
451 454
                                      :class => 'changeset',
452 455
                                      :title => truncate_single_line(changeset.comments, :length => 100))
453 456
          end
457
          else
458
            revproj.gsub!(/[\:"]/,'')
459
            link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
460
            if link_project && (changeset = link_project.changesets.find_by_revision(oid))              
461
              name = h("#{revproj}:r#{oid}")
462
              options = {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :project => link_project, :rev => oid}
463
              html_options =  {:class => 'changeset',
464
                :title => truncate_single_line(changeset.comments, :length => 100)}
465
              link = link_to_if_authorized(name, options, html_options) || (options.delete(:project) && html_options.delete(:title) && link_to(name, options, html_options))
466
            end            
467
          end
454 468
        elsif sep == '#'
455 469
          oid = oid.to_i
456 470
          case prefix
......
497 511
                                              :class => 'version'
498 512
            end
499 513
          when 'commit'
514
            if revproj.nil?
500 515
            if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
501 516
              link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
502 517
                                           :class => 'changeset',
503 518
                                           :title => truncate_single_line(changeset.comments, :length => 100)
504 519
            end
520
            else
521
              revproj.gsub!(/[\:"]/,'')
522
              link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
523
              if link_project && (changeset = link_project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
524
                name = h("#{revproj}:#{name}")
525
                options = {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :project => link_project, :rev => changeset.revision}
526
                html_options =  {:class => 'changeset',
527
                :title => truncate_single_line(changeset.comments, :length => 100)}
528
                link = link_to_if_authorized(name, options, html_options) || (options.delete(:project) && html_options.delete(:title) && link_to(name, options, html_options))
529
              end
530
            end
505 531
          when 'source', 'export'
506 532
            if project && project.repository
507 533
              name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
......
521 547
          end
522 548
        end
523 549
      end
524
      leading + (link || "#{prefix}#{sep}#{oid}")
550
      leading + (link || (revproj ? "#{revproj}:" : "") + "#{prefix}#{sep}#{oid}")
525 551
    end
526 552

  
527 553
    text
......
626 652
  # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
627 653
  def avatar(user, options = { })
628 654
    if Setting.gravatar_enabled?
629
      options.merge!({:ssl => Setting.protocol == 'https'})
630 655
      email = nil
631 656
      if user.respond_to?(:mail)
632 657
        email = user.mail