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
Kazuhiko Shiozaki
gitlab-ce
Commits
9321d382
Commit
9321d382
authored
Dec 07, 2015
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add custom NamespaceValidator
parent
ad6a771d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
12 deletions
+46
-12
app/models/namespace.rb
app/models/namespace.rb
+3
-5
app/models/user.rb
app/models/user.rb
+2
-4
app/validators/namespace_validator.rb
app/validators/namespace_validator.rb
+22
-0
spec/models/user_spec.rb
spec/models/user_spec.rb
+17
-1
spec/requests/api/users_spec.rb
spec/requests/api/users_spec.rb
+2
-2
No files found.
app/models/namespace.rb
View file @
9321d382
...
...
@@ -30,12 +30,10 @@ class Namespace < ActiveRecord::Base
validates
:description
,
length:
{
within:
0
..
255
}
validates
:path
,
uniqueness:
{
case_sensitive:
false
},
presence:
true
,
length:
{
within:
1
..
255
},
exclusion:
{
in:
Gitlab
::
Blacklist
.
path
}
,
format:
{
with:
Gitlab
::
Regex
.
namespace_regex
,
message:
Gitlab
::
Regex
.
namespace_regex_messag
e
}
namespace:
true
,
presence:
true
,
uniqueness:
{
case_sensitive:
fals
e
}
delegate
:name
,
to: :owner
,
allow_nil:
true
,
prefix:
true
...
...
app/models/user.rb
View file @
9321d382
...
...
@@ -148,11 +148,9 @@ class User < ActiveRecord::Base
validates
:bio
,
length:
{
maximum:
255
},
allow_blank:
true
validates
:projects_limit
,
presence:
true
,
numericality:
{
greater_than_or_equal_to:
0
}
validates
:username
,
namespace:
true
,
presence:
true
,
uniqueness:
{
case_sensitive:
false
},
exclusion:
{
in:
Gitlab
::
Blacklist
.
path
},
format:
{
with:
Gitlab
::
Regex
.
namespace_regex
,
message:
Gitlab
::
Regex
.
namespace_regex_message
}
uniqueness:
{
case_sensitive:
false
}
validates
:notification_level
,
inclusion:
{
in:
Notification
.
notification_levels
},
presence:
true
validate
:namespace_uniq
,
if:
->
(
user
)
{
user
.
username_changed?
}
...
...
app/validators/namespace_validator.rb
0 → 100644
View file @
9321d382
# NamespaceValidator
#
# Custom validator for GitLab namespace values.
#
# Values are checked for formatting and exclusion from `Gitlab::Blacklist.path`.
class
NamespaceValidator
<
ActiveModel
::
EachValidator
def
validate_each
(
record
,
attribute
,
value
)
unless
value
=~
Gitlab
::
Regex
.
namespace_regex
record
.
errors
.
add
(
attribute
,
Gitlab
::
Regex
.
namespace_regex_message
)
end
if
blacklisted?
(
value
)
record
.
errors
.
add
(
attribute
,
"
#{
value
}
is a reserved name"
)
end
end
private
def
blacklisted?
(
value
)
Gitlab
::
Blacklist
.
path
.
include?
(
value
)
end
end
spec/models/user_spec.rb
View file @
9321d382
...
...
@@ -91,7 +91,23 @@ describe User do
end
describe
'validations'
do
it
{
is_expected
.
to
validate_presence_of
(
:username
)
}
describe
'username'
do
it
'validates presence'
do
expect
(
subject
).
to
validate_presence_of
(
:username
)
end
it
'rejects blacklisted names'
do
user
=
build
(
:user
,
username:
'dashboard'
)
expect
(
user
).
not_to
be_valid
expect
(
user
.
errors
.
values
).
to
eq
[[
'dashboard is a reserved name'
]]
end
it
'validates uniqueness'
do
expect
(
subject
).
to
validate_uniqueness_of
(
:username
)
end
end
it
{
is_expected
.
to
validate_presence_of
(
:projects_limit
)
}
it
{
is_expected
.
to
validate_numericality_of
(
:projects_limit
)
}
it
{
is_expected
.
to
allow_value
(
0
).
for
(
:projects_limit
)
}
...
...
spec/requests/api/users_spec.rb
View file @
9321d382
...
...
@@ -153,7 +153,7 @@ describe API::API, api: true do
expect
(
json_response
[
'message'
][
'projects_limit'
]).
to
eq
([
'must be greater than or equal to 0'
])
expect
(
json_response
[
'message'
][
'username'
]).
to
eq
([
Gitlab
::
Regex
.
send
(
:namespace_regex_message
)
])
to
eq
([
Gitlab
::
Regex
.
namespace_regex_message
])
end
it
"shouldn't available for non admin users"
do
...
...
@@ -296,7 +296,7 @@ describe API::API, api: true do
expect
(
json_response
[
'message'
][
'projects_limit'
]).
to
eq
([
'must be greater than or equal to 0'
])
expect
(
json_response
[
'message'
][
'username'
]).
to
eq
([
Gitlab
::
Regex
.
send
(
:namespace_regex_message
)
])
to
eq
([
Gitlab
::
Regex
.
namespace_regex_message
])
end
context
"with existing 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