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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
6895e1ad
Commit
6895e1ad
authored
Jan 11, 2022
by
Lee Tickett
Committed by
Kerri Miller
Jan 11, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enforce unique contact email for group hierarchy
Changelog: added
parent
93ccfa72
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
0 deletions
+46
-0
app/models/customer_relations/contact.rb
app/models/customer_relations/contact.rb
+11
-0
locale/gitlab.pot
locale/gitlab.pot
+3
-0
spec/models/customer_relations/contact_spec.rb
spec/models/customer_relations/contact_spec.rb
+32
-0
No files found.
app/models/customer_relations/contact.rb
View file @
6895e1ad
...
@@ -24,6 +24,7 @@ class CustomerRelations::Contact < ApplicationRecord
...
@@ -24,6 +24,7 @@ class CustomerRelations::Contact < ApplicationRecord
validates
:email
,
length:
{
maximum:
255
}
validates
:email
,
length:
{
maximum:
255
}
validates
:description
,
length:
{
maximum:
1024
}
validates
:description
,
length:
{
maximum:
1024
}
validate
:validate_email_format
validate
:validate_email_format
validate
:unique_email_for_group_hierarchy
def
self
.
find_ids_by_emails
(
group_id
,
emails
)
def
self
.
find_ids_by_emails
(
group_id
,
emails
)
raise
ArgumentError
,
"Cannot lookup more than
#{
MAX_PLUCK
}
emails"
if
emails
.
length
>
MAX_PLUCK
raise
ArgumentError
,
"Cannot lookup more than
#{
MAX_PLUCK
}
emails"
if
emails
.
length
>
MAX_PLUCK
...
@@ -39,4 +40,14 @@ class CustomerRelations::Contact < ApplicationRecord
...
@@ -39,4 +40,14 @@ class CustomerRelations::Contact < ApplicationRecord
self
.
errors
.
add
(
:email
,
I18n
.
t
(
:invalid
,
scope:
'valid_email.validations.email'
))
unless
ValidateEmail
.
valid?
(
self
.
email
)
self
.
errors
.
add
(
:email
,
I18n
.
t
(
:invalid
,
scope:
'valid_email.validations.email'
))
unless
ValidateEmail
.
valid?
(
self
.
email
)
end
end
def
unique_email_for_group_hierarchy
return
unless
group
return
unless
email
duplicate_email_exists
=
CustomerRelations
::
Contact
.
where
(
group_id:
group
.
self_and_hierarchy
.
pluck
(
:id
),
email:
email
)
.
where
.
not
(
id:
id
).
exists?
self
.
errors
.
add
(
:email
,
_
(
'contact with same email already exists in group hierarchy'
))
if
duplicate_email_exists
end
end
end
locale/gitlab.pot
View file @
6895e1ad
...
@@ -41910,6 +41910,9 @@ msgstr ""
...
@@ -41910,6 +41910,9 @@ msgstr ""
msgid "compliance violation has already been recorded"
msgid "compliance violation has already been recorded"
msgstr ""
msgstr ""
msgid "contact with same email already exists in group hierarchy"
msgstr ""
msgid "container_name can contain only lowercase letters, digits, '-', and '.' and must start and end with an alphanumeric character"
msgid "container_name can contain only lowercase letters, digits, '-', and '.' and must start and end with an alphanumeric character"
msgstr ""
msgstr ""
...
...
spec/models/customer_relations/contact_spec.rb
View file @
6895e1ad
...
@@ -26,6 +26,38 @@ RSpec.describe CustomerRelations::Contact, type: :model do
...
@@ -26,6 +26,38 @@ RSpec.describe CustomerRelations::Contact, type: :model do
it_behaves_like
'an object with RFC3696 compliant email-formatted attributes'
,
:email
it_behaves_like
'an object with RFC3696 compliant email-formatted attributes'
,
:email
end
end
describe
'#unique_email_for_group_hierarchy'
do
let_it_be
(
:parent
)
{
create
(
:group
)
}
let_it_be
(
:group
)
{
create
(
:group
,
parent:
parent
)
}
let_it_be
(
:subgroup
)
{
create
(
:group
,
parent:
group
)
}
let_it_be
(
:existing_contact
)
{
create
(
:contact
,
group:
group
)
}
context
'with unique email for group hierarchy'
do
subject
{
build
(
:contact
,
group:
group
)
}
it
{
is_expected
.
to
be_valid
}
end
context
'with duplicate email in group'
do
subject
{
build
(
:contact
,
email:
existing_contact
.
email
,
group:
group
)
}
it
{
is_expected
.
to
be_invalid
}
end
context
'with duplicate email in parent group'
do
subject
{
build
(
:contact
,
email:
existing_contact
.
email
,
group:
subgroup
)
}
it
{
is_expected
.
to
be_invalid
}
end
context
'with duplicate email in subgroup'
do
subject
{
build
(
:contact
,
email:
existing_contact
.
email
,
group:
parent
)
}
it
{
is_expected
.
to
be_invalid
}
end
end
describe
'#before_validation'
do
describe
'#before_validation'
do
it
'strips leading and trailing whitespace'
do
it
'strips leading and trailing whitespace'
do
contact
=
described_class
.
new
(
first_name:
' First '
,
last_name:
' Last '
,
phone:
' 123456 '
)
contact
=
described_class
.
new
(
first_name:
' First '
,
last_name:
' Last '
,
phone:
' 123456 '
)
...
...
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