Project

General

Profile

Feature #3346 » cross-project_revision_and_commit_links_v3.diff

Patch v3 - Fix link_to_if_authorized and link_to_remote_if_authorized helpers because it breaks contextual links - Babar O'Cap, 2009-08-20 09:26

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
......
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
              title = truncate_single_line(changeset.comments, :length => 100) if authorize_for('repositories', 'revision', link_project)
462
              link = link_to h("#{revproj}:r#{oid}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :rev => oid},
463
                                                      :class => 'changeset',
464
                                                      :title => title
465
            end            
466
          end
454 467
        elsif sep == '#'
455 468
          oid = oid.to_i
456 469
          case prefix
......
497 510
                                              :class => 'version'
498 511
            end
499 512
          when 'commit'
513
            if revproj.nil?
500 514
            if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
501 515
              link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
502 516
                                           :class => 'changeset',
503 517
                                           :title => truncate_single_line(changeset.comments, :length => 100)
504 518
            end
519
            else
520
              revproj.gsub!(/[\:"]/,'')
521
              link_project = Project.find_by_name(h(revproj)) || Project.find_by_identifier(h(revproj))
522
              if link_project && (changeset = link_project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
523
                title = truncate_single_line(changeset.comments, :length => 100) if authorize_for('repositories', 'revision', link_project)
524
                link = link_to h("#{revproj}:#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => link_project, :rev => changeset.revision},
525
                                                        :class => 'changeset',
526
                                                        :title => title
527
              end
528
            end
505 529
          when 'source', 'export'
506 530
            if project && project.repository
507 531
              name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
......
521 545
          end
522 546
        end
523 547
      end
524
      leading + (link || "#{prefix}#{sep}#{oid}")
548
      leading + (link || (revproj ? "#{revproj}:" : "") + "#{prefix}#{sep}#{oid}")
525 549
    end
526 550

  
527 551
    text
(5-5/7)