Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
3656d3b8
Commit
3656d3b8
authored
Aug 16, 2016
by
http://jneen.net/
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add automatic detection of the policy class
parent
16fe6dc7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
16 deletions
+18
-16
app/models/ability.rb
app/models/ability.rb
+3
-15
app/policies/base_policy.rb
app/policies/base_policy.rb
+15
-1
No files found.
app/models/ability.rb
View file @
3656d3b8
...
@@ -61,6 +61,9 @@ class Ability
...
@@ -61,6 +61,9 @@ class Ability
private
private
def
uncached_allowed
(
user
,
subject
)
def
uncached_allowed
(
user
,
subject
)
policy_class
=
BasePolicy
.
class_for
(
subject
)
rescue
nil
return
policy_class
.
abilities
(
user
,
subject
)
if
policy_class
return
anonymous_abilities
(
subject
)
if
user
.
nil?
return
anonymous_abilities
(
subject
)
if
user
.
nil?
return
[]
unless
user
.
is_a?
(
User
)
return
[]
unless
user
.
is_a?
(
User
)
return
[]
if
user
.
blocked?
return
[]
if
user
.
blocked?
...
@@ -70,13 +73,6 @@ class Ability
...
@@ -70,13 +73,6 @@ class Ability
def
abilities_by_subject_class
(
user
:,
subject
:)
def
abilities_by_subject_class
(
user
:,
subject
:)
case
subject
case
subject
when
Project
then
ProjectPolicy
.
abilities
(
user
,
subject
)
when
Issue
then
IssuePolicy
.
abilities
(
user
,
subject
)
when
MergeRequest
then
MergeRequestPolicy
.
abilities
(
user
,
subject
)
when
Ci
::
Build
then
Ci
::
BuildPolicy
.
abilities
(
user
,
subject
)
when
CommitStatus
then
CommitStatus
.
abilities
(
user
,
subject
)
when
Note
then
note_abilities
(
user
,
subject
)
when
ProjectSnippet
then
project_snippet_abilities
(
user
,
subject
)
when
ProjectSnippet
then
project_snippet_abilities
(
user
,
subject
)
when
PersonalSnippet
then
personal_snippet_abilities
(
user
,
subject
)
when
PersonalSnippet
then
personal_snippet_abilities
(
user
,
subject
)
when
Group
then
group_abilities
(
user
,
subject
)
when
Group
then
group_abilities
(
user
,
subject
)
...
@@ -96,14 +92,6 @@ class Ability
...
@@ -96,14 +92,6 @@ class Ability
anonymous_personal_snippet_abilities
(
subject
)
anonymous_personal_snippet_abilities
(
subject
)
elsif
subject
.
is_a?
(
ProjectSnippet
)
elsif
subject
.
is_a?
(
ProjectSnippet
)
anonymous_project_snippet_abilities
(
subject
)
anonymous_project_snippet_abilities
(
subject
)
elsif
subject
.
is_a?
(
CommitStatus
)
anonymous_commit_status_abilities
(
subject
)
elsif
subject
.
is_a?
(
Project
)
ProjectPolicy
.
abilities
(
nil
,
subject
)
elsif
subject
.
is_a?
(
Issue
)
IssuePolicy
.
abilities
(
nil
,
subject
)
elsif
subject
.
is_a?
(
MergeRequest
)
MergeRequestPolicy
.
abilities
(
nil
,
subject
)
elsif
subject
.
respond_to?
(
:project
)
elsif
subject
.
respond_to?
(
:project
)
ProjectPolicy
.
abilities
(
nil
,
subject
.
project
)
ProjectPolicy
.
abilities
(
nil
,
subject
.
project
)
elsif
subject
.
is_a?
(
Group
)
||
subject
.
respond_to?
(
:group
)
elsif
subject
.
is_a?
(
Group
)
||
subject
.
respond_to?
(
:group
)
...
...
app/policies/base_policy.rb
View file @
3656d3b8
...
@@ -4,7 +4,21 @@ class BasePolicy
...
@@ -4,7 +4,21 @@ class BasePolicy
end
end
def
self
.
class_for
(
subject
)
def
self
.
class_for
(
subject
)
"
#{
subject
.
class
.
name
}
Policy"
.
constantize
subject
.
class
.
ancestors
.
each
do
|
klass
|
next
unless
klass
.
name
begin
policy_class
=
"
#{
klass
.
name
}
Policy"
.
constantize
# NB: the < operator here tests whether policy_class
# inherits from BasePolicy
return
policy_class
if
policy_class
<
BasePolicy
rescue
NameError
nil
end
end
raise
"no policy for
#{
subject
.
class
.
name
}
"
end
end
attr_reader
:user
,
:subject
attr_reader
:user
,
:subject
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment