Project

General

Profile

Defect #34029 » patch.diff

Vincent Robert, 2020-09-24 15:06

View differences:

app/models/principal.rb
143 143
    project.is_a?(Project) && project_ids.include?(project.id)
144 144
  end
145 145

  
146
  def roles
147
    @roles ||= Role.joins(members: :project).where(["#{Project.table_name}.status <> ?", Project::STATUS_ARCHIVED]).where(Member.arel_table[:user_id].eq(id)).distinct
148
  end
149

  
146 150
  # Returns an array of the project ids that the principal is a member of
147 151
  def project_ids
148 152
    @project_ids ||= super.freeze
app/models/user.rb
588 588
    @membership_by_project_id[project_id]
589 589
  end
590 590

  
591
  def roles
592
    @roles ||= Role.joins(members: :project).where(["#{Project.table_name}.status <> ?", Project::STATUS_ARCHIVED]).where(Member.arel_table[:user_id].eq(id)).distinct
593
  end
594

  
595 591
  # Returns the user's bult-in role
596 592
  def builtin_role
597 593
    @builtin_role ||= Role.non_member
......
723 719
      return true if admin?
724 720

  
725 721
      # authorize if user has at least one role that has this permission
726
      roles = self.roles.to_a | [builtin_role]
722
      roles = self.roles.to_a | [builtin_role] | Group.non_member.roles.to_a | Group.anonymous.roles.to_a
727 723
      roles.any? {|role|
728 724
        role.allowed_to?(action) &&
729 725
        (block_given? ? yield(role, self) : true)
test/system/issues_test.rb
152 152
    assert_equal 'Some description', issue.attachments.first.description
153 153
  end
154 154

  
155
  def test_create_issue_with_attachment_when_user_is_not_a_member
156
    set_tmp_attachments_directory
157

  
158
    # Set no permission to non-member role
159
    non_member_role = Role.where(:builtin => Role::BUILTIN_NON_MEMBER).first
160
    non_member_role.permissions = []
161
    non_member_role.save
162

  
163
    # Set role "Reporter" to non-member users on project ecookbook
164
    membership = Member.find_or_create_by(user_id: Group.non_member.id, project_id: 1)
165
    membership.roles = [Role.find(3)] # Reporter
166
    membership.save
167

  
168
    log_user('someone', 'foo')
169

  
170
    issue = new_record(Issue) do
171
      visit '/projects/ecookbook/issues/new'
172
      fill_in 'Subject', :with => 'Issue with attachment'
173
      attach_file 'attachments[dummy][file]', Rails.root.join('test/fixtures/files/testfile.txt')
174
      fill_in 'attachments[1][description]', :with => 'Some description'
175
      click_on 'Create'
176
    end
177
    assert_equal 1, issue.attachments.count
178
    assert_equal 'Some description', issue.attachments.first.description
179
  end
180

  
155 181
  def test_create_issue_with_new_target_version
156 182
    log_user('jsmith', 'jsmith')
157 183

  
(2-2/3)