project_policy.rb 11.1 KB
Newer Older
1
class ProjectPolicy < BasePolicy
2 3 4 5 6 7 8 9
  def self.create_read_update_admin(name)
    [
      :"create_#{name}",
      :"read_#{name}",
      :"update_#{name}",
      :"admin_#{name}"
    ]
  end
10

11 12
  desc "User is a project owner"
  condition :owner do
13 14
    (project.owner.present? && project.owner == @user) ||
      project.group&.has_owner?(@user)
15
  end
16

17 18 19
  desc "Project has public builds enabled"
  condition(:public_builds, scope: :subject) { project.public_builds? }

20
  # For guest access we use #team_member? so we can use
21 22 23 24 25
  # project.members, which gets cached in subject scope.
  # This is safe because team_access_level is guaranteed
  # by ProjectAuthorization's validation to be at minimum
  # GUEST
  desc "User has guest access"
26
  condition(:guest) { team_member? }
27

28 29
  desc "User has reporter access"
  condition(:reporter) { team_access_level >= Gitlab::Access::REPORTER }
30

31 32 33 34 35 36 37 38 39 40 41 42
  desc "User has developer access"
  condition(:developer) { team_access_level >= Gitlab::Access::DEVELOPER }

  desc "User has master access"
  condition(:master) { team_access_level >= Gitlab::Access::MASTER }

  desc "Project is public"
  condition(:public_project, scope: :subject) { project.public? }

  desc "Project is visible to internal users"
  condition(:internal_access) do
    project.internal? && !user.external?
43 44
  end

45 46 47 48 49 50 51 52 53 54 55
  desc "User is a member of the group"
  condition(:group_member, scope: :subject) { project_group_member? }

  desc "Project is archived"
  condition(:archived, scope: :subject) { project.archived? }

  condition(:default_issues_tracker, scope: :subject) { project.default_issues_tracker? }

  desc "Container registry is disabled"
  condition(:container_registry_disabled, scope: :subject) do
    !project.container_registry_enabled
56 57
  end

58 59 60 61 62 63
  desc "Project has an external wiki"
  condition(:has_external_wiki, scope: :subject) { project.has_external_wiki? }

  desc "Project has request access enabled"
  condition(:request_access_enabled, scope: :subject) { project.request_access_enabled }

64 65 66
  desc "Has merge requests allowing pushes to user"
  condition(:has_merge_requests_allowing_pushes, scope: :subject) do
    project.merge_requests_allowing_push_to_user(user).any?
67 68
  end

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  # We aren't checking `:read_issue` or `:read_merge_request` in this case
  # because it could be possible for a user to see an issuable-iid
  # (`:read_issue_iid` or `:read_merge_request_iid`) but then wouldn't be
  # allowed to read the actual issue after a more expensive `:read_issue`
  # check. These checks are intended to be used alongside
  # `:read_project_for_iids`.
  #
  # `:read_issue` & `:read_issue_iid` could diverge in gitlab-ee.
  condition(:issues_visible_to_user, score: 4) do
    @subject.feature_available?(:issues, @user)
  end

  condition(:merge_requests_visible_to_user, score: 4) do
    @subject.feature_available?(:merge_requests, @user)
  end

85 86 87 88 89 90 91 92 93 94 95 96 97
  features = %w[
    merge_requests
    issues
    repository
    snippets
    wiki
    builds
  ]

  features.each do |f|
    # these are scored high because they are unlikely
    desc "Project has #{f} disabled"
    condition(:"#{f}_disabled", score: 32) { !feature_available?(f.to_sym) }
98 99
  end

100 101 102 103
  # `:read_project` may be prevented in EE, but `:read_project_for_iids` should
  # not.
  rule { guest | admin }.enable :read_project_for_iids

104 105 106 107
  rule { guest }.enable :guest_access
  rule { reporter }.enable :reporter_access
  rule { developer }.enable :developer_access
  rule { master }.enable :master_access
108
  rule { owner | admin }.enable :owner_access
109

110
  rule { can?(:owner_access) }.policy do
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    enable :guest_access
    enable :reporter_access
    enable :developer_access
    enable :master_access

    enable :change_namespace
    enable :change_visibility_level
    enable :rename_project
    enable :remove_project
    enable :archive_project
    enable :remove_fork_project
    enable :destroy_merge_request
    enable :destroy_issue
    enable :remove_pages
  end
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  rule { can?(:guest_access) }.policy do
    enable :read_project
    enable :read_board
    enable :read_list
    enable :read_wiki
    enable :read_issue
    enable :read_label
    enable :read_milestone
    enable :read_project_snippet
    enable :read_project_member
    enable :read_note
    enable :create_project
    enable :create_issue
    enable :create_note
    enable :upload_file
    enable :read_cycle_analytics
  end
144

145
  # These abilities are not allowed to admins that are not members of the project,
146
  # that's why they are defined separately.
147
  rule { guest & can?(:download_code) }.enable :build_download_code
148
  rule { guest & can?(:read_container_image) }.enable :project_read_container_image
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  rule { can?(:reporter_access) }.policy do
    enable :download_code
    enable :download_wiki_code
    enable :fork_project
    enable :create_project_snippet
    enable :update_issue
    enable :admin_issue
    enable :admin_label
    enable :admin_list
    enable :read_commit_status
    enable :read_build
    enable :read_container_image
    enable :read_pipeline
    enable :read_pipeline_schedule
    enable :read_environment
    enable :read_deployment
    enable :read_merge_request
  end
168

169 170
  # We define `:public_user_access` separately because there are cases in gitlab-ee
  # where we enable or prevent it based on other coditions.
171 172
  rule { (~anonymous & public_project) | internal_access }.policy do
    enable :public_user_access
173
    enable :read_project_for_iids
174
  end
175

176
  rule { can?(:public_user_access) }.policy do
177
    enable :public_access
178
    enable :guest_access
179 180 181

    enable :fork_project
    enable :build_download_code
182
    enable :project_read_container_image
183 184
    enable :request_access
  end
185

186 187 188 189 190
  rule { owner | admin | guest | group_member }.prevent :request_access
  rule { ~request_access_enabled }.prevent :request_access

  rule { can?(:developer_access) }.policy do
    enable :admin_merge_request
191
    enable :admin_milestone
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    enable :update_merge_request
    enable :create_commit_status
    enable :update_commit_status
    enable :create_build
    enable :update_build
    enable :create_pipeline
    enable :update_pipeline
    enable :create_pipeline_schedule
    enable :create_merge_request
    enable :create_wiki
    enable :push_code
    enable :resolve_note
    enable :create_container_image
    enable :update_container_image
    enable :create_environment
    enable :create_deployment
208 209
  end

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  rule { can?(:master_access) }.policy do
    enable :delete_protected_branch
    enable :update_project_snippet
    enable :update_environment
    enable :update_deployment
    enable :admin_project_snippet
    enable :admin_project_member
    enable :admin_note
    enable :admin_wiki
    enable :admin_project
    enable :admin_commit_status
    enable :admin_build
    enable :admin_container_image
    enable :admin_pipeline
    enable :admin_environment
    enable :admin_deployment
    enable :admin_pages
    enable :read_pages
    enable :update_pages
229 230
    enable :read_cluster
    enable :create_cluster
231
  end
232

233 234 235 236 237 238
  rule { archived }.policy do
    prevent :create_merge_request
    prevent :push_code
    prevent :delete_protected_branch
    prevent :update_merge_request
    prevent :admin_merge_request
239 240
  end

241 242
  rule { merge_requests_disabled | repository_disabled }.policy do
    prevent(*create_read_update_admin(:merge_request))
243 244
  end

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  rule { issues_disabled & merge_requests_disabled }.policy do
    prevent(*create_read_update_admin(:label))
    prevent(*create_read_update_admin(:milestone))
  end

  rule { snippets_disabled }.policy do
    prevent(*create_read_update_admin(:project_snippet))
  end

  rule { wiki_disabled & ~has_external_wiki }.policy do
    prevent(*create_read_update_admin(:wiki))
    prevent(:download_wiki_code)
  end

  rule { builds_disabled | repository_disabled }.policy do
    prevent(*create_read_update_admin(:build))
    prevent(*(create_read_update_admin(:pipeline) - [:read_pipeline]))
    prevent(*create_read_update_admin(:pipeline_schedule))
    prevent(*create_read_update_admin(:environment))
    prevent(*create_read_update_admin(:deployment))
  end

  rule { repository_disabled }.policy do
    prevent :push_code
    prevent :download_code
    prevent :fork_project
    prevent :read_commit_status
  end

  rule { container_registry_disabled }.policy do
    prevent(*create_read_update_admin(:container_image))
  end

  rule { anonymous & ~public_project }.prevent_all
279 280 281 282 283

  rule { public_project }.policy do
    enable :public_access
    enable :read_project_for_iids
  end
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

  rule { can?(:public_access) }.policy do
    enable :read_project
    enable :read_board
    enable :read_list
    enable :read_wiki
    enable :read_label
    enable :read_milestone
    enable :read_project_snippet
    enable :read_project_member
    enable :read_merge_request
    enable :read_note
    enable :read_pipeline
    enable :read_pipeline_schedule
    enable :read_commit_status
    enable :read_container_image
    enable :download_code
    enable :download_wiki_code
    enable :read_cycle_analytics

    # NOTE: may be overridden by IssuePolicy
    enable :read_issue
  end

  rule { public_builds }.policy do
    enable :read_build
  end

  rule { public_builds & can?(:guest_access) }.policy do
    enable :read_pipeline
    enable :read_pipeline_schedule
  end

  rule { issues_disabled }.policy do
    prevent :create_issue
    prevent :update_issue
    prevent :admin_issue
    prevent :read_issue
322
  end
323

324
  # These rules are included to allow maintainers of projects to push to certain
325 326
  # to run pipelines for the branches they have access to.
  rule { can?(:public_access) & has_merge_requests_allowing_pushes }.policy do
327 328 329 330 331 332
    enable :create_build
    enable :update_build
    enable :create_pipeline
    enable :update_pipeline
  end

333 334 335 336 337 338 339 340
  rule do
    (can?(:read_project_for_iids) & issues_visible_to_user) | can?(:read_issue)
  end.enable :read_issue_iid

  rule do
    (can?(:read_project_for_iids) & merge_requests_visible_to_user) | can?(:read_merge_request)
  end.enable :read_merge_request_iid

341 342
  private

343
  def team_member?
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    return false if @user.nil?

    greedy_load_subject = false

    # when scoping by subject, we want to be greedy
    # and load *all* the members with one query.
    greedy_load_subject ||= DeclarativePolicy.preferred_scope == :subject

    # in this case we're likely to have loaded #members already
    # anyways, and #member? would fail with an error
    greedy_load_subject ||= !@user.persisted?

    if greedy_load_subject
      project.team.members.include?(user)
    else
      # otherwise we just make a specific query for
      # this particular user.
      team_access_level >= Gitlab::Access::GUEST
    end
  end

  def project_group_member?
    return false if @user.nil?

368 369
    project.group &&
      (
370 371
        project.group.members_with_parents.exists?(user_id: @user.id) ||
        project.group.requesters.exists?(user_id: @user.id)
372 373 374
      )
  end

375 376
  def team_access_level
    return -1 if @user.nil?
377

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    # NOTE: max_member_access has its own cache
    project.team.max_member_access(@user.id)
  end

  def feature_available?(feature)
    case project.project_feature.access_level(feature)
    when ProjectFeature::DISABLED
      false
    when ProjectFeature::PRIVATE
      guest? || admin?
    else
      true
    end
  end

  def project
    @subject
395
  end
396
end