This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
/
repository_policy.rb
68 lines (57 loc) · 1.87 KB
/
repository_policy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
class RepositoryPolicy
attr_reader :user, :repository, :namespace
def initialize(user, repository)
@user = user
@repository = repository
@namespace = repository.namespace
end
def show?
return namespace.visibility_public? unless @user
@user.admin? ||
namespace.visibility_public? ||
namespace.visibility_protected? ||
namespace.team.users.exists?(user.id)
end
# Returns true if the repository can be destroyed.
def destroy?
raise Pundit::NotAuthorizedError, "must be logged in" unless user
is_owner = namespace.team.owners.exists?(user.id)
is_contributor = namespace.team.contributors.exists?(user.id)
can_contributor_delete = APP_CONFIG["delete"]["contributors"] && is_contributor
delete_enabled? && (@user.admin? || is_owner || can_contributor_delete)
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.nil?
@scope
.joins(namespace: { team: :users })
.where("namespaces.visibility = :namespace_visibility",
namespace_visibility: Namespace.visibilities[:visibility_public])
.distinct
elsif user.admin?
@scope.all
else
# Show repositories only if the repository is public or
# the repository belongs to the current_user
@scope
.joins(namespace: { team: :users })
.where("namespaces.visibility = :namespace_visibility OR " \
"users.id = :user_id",
namespace_visibility: Namespace.visibilities[:visibility_public],
user_id: @user.id)
.distinct
end
end
end
protected
# Returns true if delete is enabled
def delete_enabled?
APP_CONFIG.enabled?("delete")
end
end