Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
da1014f0dc60
Commit
d413f8e4
authored
Aug 15, 2017
by
Rubén Dávila
Committed by
Mike Greiling
Aug 26, 2017
Browse files
Add validation for visibility level of sub groups
Sub groups should not have a visibility level higher than its parent.
parent
db0af4484295
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/models/group.rb
View file @
da1014f0
...
...
@@ -26,6 +26,7 @@ class Group < Namespace
validate
:avatar_type
,
if:
->
(
user
)
{
user
.
avatar
.
present?
&&
user
.
avatar_changed?
}
validate
:visibility_level_allowed_by_projects
validate
:visibility_level_allowed_by_parent
validates
:avatar
,
file_size:
{
maximum:
200
.
kilobytes
.
to_i
}
...
...
@@ -102,6 +103,14 @@ def human_name
full_name
end
def
visibility_level_allowed_by_parent
return
if
parent_id
.
blank?
if
parent
&&
(
visibility_level
>
parent
.
visibility_level
)
errors
.
add
(
:visibility_level
,
"
#{
visibility
}
is not allowed since the parent group has a
#{
parent
.
visibility
}
visibility."
)
end
end
def
visibility_level_allowed_by_projects
allowed_by_projects
=
self
.
projects
.
where
(
'visibility_level > ?'
,
self
.
visibility_level
).
none?
...
...
spec/models/group_spec.rb
View file @
da1014f0
...
...
@@ -84,6 +84,39 @@
expect
(
group
).
not_to
be_valid
end
end
describe
'#visibility_level_allowed_by_parent'
do
let
(
:parent
)
{
create
(
:group
,
:internal
)
}
let
(
:sub_group
)
{
build
(
:group
,
parent_id:
parent
.
id
)
}
context
'without a parent'
do
it
'is valid'
do
sub_group
.
parent_id
=
nil
expect
(
sub_group
).
to
be_valid
end
end
context
'with a parent'
do
context
'when visibility of sub group is greater than the parent'
do
it
'is invalid'
do
sub_group
.
visibility_level
=
Gitlab
::
VisibilityLevel
::
PUBLIC
expect
(
sub_group
).
to
be_invalid
end
end
context
'when visibility of sub group is lower or equal to the parent'
do
[
Gitlab
::
VisibilityLevel
::
INTERNAL
,
Gitlab
::
VisibilityLevel
::
PRIVATE
].
each
do
|
level
|
it
'is valid'
do
sub_group
.
visibility_level
=
level
expect
(
sub_group
).
to
be_valid
end
end
end
end
end
end
describe
'.visible_to_user'
do
...
...
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