Project

General

Profile

Feature #38048 » 38048.patch

Felix Schäfer, 2022-12-14 22:00

View differences:

app/models/project.rb
823 823
    'name',
824 824
    'description',
825 825
    'homepage',
826
    'is_public',
827 826
    'identifier',
828 827
    'custom_field_values',
829 828
    'custom_fields',
......
834 833
    'default_issue_query_id',
835 834
    'default_assigned_to_id')
836 835

  
836
  safe_attributes(
837
    'is_public',
838
    :if =>
839
      lambda do |project, user|
840
        if project.new_record?
841
          if user.admin?
842
            true
843
          else
844
            default_member_role&.has_permission?(:select_project_publicity)
845
          end
846
        else
847
          user.allowed_to?(:select_project_publicity, project)
848
        end
849
      end
850
  )
851

  
837 852
  safe_attributes(
838 853
    'enabled_module_names',
839 854
    :if =>
app/views/projects/_form.html.erb
11 11
<% end %></p>
12 12
<p><%= f.text_field :homepage, :size => 60 %></p>
13 13
<p>
14
  <%= f.check_box :is_public %>
14
  <%= f.check_box :is_public, :disabled => !@project.safe_attribute?(:is_public) %>
15 15
  <em class="info"><%= Setting.login_required? ? l(:text_project_is_public_non_member) : l(:text_project_is_public_anonymous) %></em>
16 16
</p>
17 17

  
config/locales/de.yml
957 957
  permission_rename_wiki_pages: Wiki-Seiten umbenennen
958 958
  permission_save_queries: Filter speichern
959 959
  permission_select_project_modules: Projektmodule auswählen
960
  permission_select_project_publicity: Projekt als privat oder öffentlich markieren
960 961
  permission_set_issues_private: Tickets als privat oder öffentlich markieren
961 962
  permission_set_notes_private: Kommentar als privat markieren
962 963
  permission_set_own_issues_private: Eigene Tickets als privat oder öffentlich markieren
config/locales/en.yml
522 522
  permission_edit_project: Edit project
523 523
  permission_close_project: Close / reopen the project
524 524
  permission_delete_project: Delete the project
525
  permission_select_project_publicity: Set project public or private
525 526
  permission_select_project_modules: Select project modules
526 527
  permission_manage_members: Manage members
527 528
  permission_manage_project_activities: Manage project activities
db/migrate/20221214173537_add_select_project_publicity_permission.rb
1
class AddSelectProjectPublicityPermission < ActiveRecord::Migration[5.2]
2
  def up
3
    Role.find_each do |r|
4
      r.add_permission!(:select_project_publicity) if r.permissions.include?(:edit_project)
5
    end
6
  end
7

  
8
  def down
9
    Role.find_each do |r|
10
      r.remove_permission!(:select_project_publicity)
11
    end
12
  end
13

  
14
  def run_in_request?; true end
15
end
lib/redmine/preparation.rb
39 39
        map.permission :edit_project, {:projects => [:settings, :edit, :update]}, :require => :member
40 40
        map.permission :close_project, {:projects => [:close, :reopen]}, :require => :member, :read => true
41 41
        map.permission :delete_project, {:projects => :destroy}, :require => :member, :read => true
42
        map.permission :select_project_publicity, {}, :require => :member
42 43
        map.permission :select_project_modules, {:projects => :modules}, :require => :member
43 44
        map.permission :view_members, {:members => [:index, :show]}, :public => true, :read => true
44 45
        map.permission :manage_members, {:projects => :settings, :members => [:index, :show, :new, :create, :edit, :update, :destroy, :autocomplete]}, :require => :member
test/fixtures/roles.yml
11 11
    - :edit_project
12 12
    - :close_project
13 13
    - :delete_project
14
    - :select_project_publicity
14 15
    - :select_project_modules
15 16
    - :manage_members
16 17
    - :manage_versions
......
85 86
    ---
86 87
    - :edit_project
87 88
    - :delete_project
89
    - :select_project_publicity
88 90
    - :manage_members
89 91
    - :manage_versions
90 92
    - :manage_categories
......
137 139
  permissions: |
138 140
    ---
139 141
    - :edit_project
142
    - :select_project_publicity
140 143
    - :manage_members
141 144
    - :manage_versions
142 145
    - :manage_categories
test/functional/projects_controller_test.rb
407 407
    end
408 408
  end
409 409

  
410
  def test_new_by_non_admin_should_enable_setting_public_if_default_role_is_allowed_to_set_public
411
    Role.non_member.add_permission!(:add_project)
412
    default_role = Role.generate!(permissions: [:add_project])
413
    user = User.generate!
414
    @request.session[:user_id] = user.id
415

  
416
    with_settings new_project_user_role_id: default_role.id.to_s do
417
      get :new
418
      assert_select 'input[name=?][disabled=disabled]', 'project[is_public]'
419

  
420
      default_role.add_permission!(:select_project_publicity)
421
      get :new
422
      assert_select 'input[name=?]:not([disabled])', 'project[is_public]'
423
    end
424
  end
425

  
410 426
  def test_new_should_not_display_invalid_search_link
411 427
    @request.session[:user_id] = 1
412 428

  
......
504 520
          :name => "blog",
505 521
          :description => "weblog",
506 522
          :identifier => "blog",
507
          :is_public => 1,
508 523
          :custom_field_values => {
509 524
            '3' => 'Beta'
510 525
          },
......
518 533
    project = Project.find_by_name('blog')
519 534
    assert_kind_of Project, project
520 535
    assert_equal 'weblog', project.description
521
    assert_equal true, project.is_public?
522 536
    assert_equal [1, 3], project.trackers.map(&:id).sort
523 537
    assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
524 538

  
......
527 541
    assert_equal 1, project.members.size
528 542
  end
529 543

  
544
  test "#create by user without select_project_publicity permission should not create a new private project" do
545
    Role.non_member.add_permission! :add_project
546
    default_role = Project.default_member_role
547
    default_role.remove_permission!(:select_project_publicity)
548
    puts  Setting.default_projects_public?
549
    @request.session[:user_id] = 9
550

  
551
    post(
552
      :create, :params => {
553
        :project => {
554
          :name => "blog",
555
          :identifier => "blog",
556
          :enabled_module_names => ['issue_tracking', 'news', 'repository'],
557
          :is_public => 0
558
        }
559
      }
560
    )
561

  
562
    project = Project.find_by_name('blog')
563
    assert_equal true, project.is_public?
564
  end
565

  
566
  test "#create by non-admin user with add_project and select_project_publicity permission should create a new private project" do
567
    @request.session[:user_id] = 2
568

  
569
    post(
570
      :create, :params => {
571
        :project => {
572
          :name => "blog",
573
          :identifier => "blog",
574
          :enabled_module_names => ['issue_tracking', 'news', 'repository'],
575
          :is_public => 0
576
        }
577
      }
578
    )
579

  
580
    project = Project.find_by_name('blog')
581
    assert_equal false, project.is_public?
582
  end
583

  
530 584
  test "#create by non-admin user with add_project permission should fail with parent_id" do
531 585
    Role.non_member.add_permission! :add_project
532 586
    User.find(9).update! :language => 'en'
    (1-1/1)