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
402fb043
Commit
402fb043
authored
Mar 17, 2020
by
Pavel Shutsin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract unique_internal to a module
Cleanup to make user.rb smaller. No behavior changes intended.
parent
cfab5a81
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
42 deletions
+52
-42
app/models/concerns/has_unique_internal_users.rb
app/models/concerns/has_unique_internal_users.rb
+51
-0
app/models/user.rb
app/models/user.rb
+1
-42
No files found.
app/models/concerns/has_unique_internal_users.rb
0 → 100644
View file @
402fb043
# frozen_string_literal: true
module
HasUniqueInternalUsers
extend
ActiveSupport
::
Concern
class_methods
do
private
def
unique_internal
(
scope
,
username
,
email_pattern
,
&
block
)
scope
.
first
||
create_unique_internal
(
scope
,
username
,
email_pattern
,
&
block
)
end
def
create_unique_internal
(
scope
,
username
,
email_pattern
,
&
creation_block
)
# Since we only want a single one of these in an instance, we use an
# exclusive lease to ensure than this block is never run concurrently.
lease_key
=
"user:unique_internal:
#{
username
}
"
lease
=
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
1
.
minute
.
to_i
)
until
uuid
=
lease
.
try_obtain
# Keep trying until we obtain the lease. To prevent hammering Redis too
# much we'll wait for a bit between retries.
sleep
(
1
)
end
# Recheck if the user is already present. One might have been
# added between the time we last checked (first line of this method)
# and the time we acquired the lock.
existing_user
=
uncached
{
scope
.
first
}
return
existing_user
if
existing_user
.
present?
uniquify
=
Uniquify
.
new
username
=
uniquify
.
string
(
username
)
{
|
s
|
User
.
find_by_username
(
s
)
}
email
=
uniquify
.
string
(
->
(
n
)
{
Kernel
.
sprintf
(
email_pattern
,
n
)
})
do
|
s
|
User
.
find_by_email
(
s
)
end
user
=
scope
.
build
(
username:
username
,
email:
email
,
&
creation_block
)
Users
::
UpdateService
.
new
(
user
,
user:
user
).
execute
(
validate:
false
)
user
ensure
Gitlab
::
ExclusiveLease
.
cancel
(
lease_key
,
uuid
)
end
end
end
app/models/user.rb
View file @
402fb043
...
...
@@ -21,6 +21,7 @@ class User < ApplicationRecord
include
OptionallySearch
include
FromUnion
include
BatchDestroyDependentAssociations
include
HasUniqueInternalUsers
include
IgnorableColumns
DEFAULT_NOTIFICATION_LEVEL
=
:participating
...
...
@@ -1793,48 +1794,6 @@ class User < ApplicationRecord
end
end
def
self
.
unique_internal
(
scope
,
username
,
email_pattern
,
&
block
)
scope
.
first
||
create_unique_internal
(
scope
,
username
,
email_pattern
,
&
block
)
end
def
self
.
create_unique_internal
(
scope
,
username
,
email_pattern
,
&
creation_block
)
# Since we only want a single one of these in an instance, we use an
# exclusive lease to ensure than this block is never run concurrently.
lease_key
=
"user:unique_internal:
#{
username
}
"
lease
=
Gitlab
::
ExclusiveLease
.
new
(
lease_key
,
timeout:
1
.
minute
.
to_i
)
until
uuid
=
lease
.
try_obtain
# Keep trying until we obtain the lease. To prevent hammering Redis too
# much we'll wait for a bit between retries.
sleep
(
1
)
end
# Recheck if the user is already present. One might have been
# added between the time we last checked (first line of this method)
# and the time we acquired the lock.
existing_user
=
uncached
{
scope
.
first
}
return
existing_user
if
existing_user
.
present?
uniquify
=
Uniquify
.
new
username
=
uniquify
.
string
(
username
)
{
|
s
|
User
.
find_by_username
(
s
)
}
email
=
uniquify
.
string
(
->
(
n
)
{
Kernel
.
sprintf
(
email_pattern
,
n
)
})
do
|
s
|
User
.
find_by_email
(
s
)
end
user
=
scope
.
build
(
username:
username
,
email:
email
,
&
creation_block
)
Users
::
UpdateService
.
new
(
user
,
user:
user
).
execute
(
validate:
false
)
# rubocop: disable CodeReuse/ServiceClass
user
ensure
Gitlab
::
ExclusiveLease
.
cancel
(
lease_key
,
uuid
)
end
def
groups_with_developer_maintainer_project_access
project_creation_levels
=
[
::
Gitlab
::
Access
::
DEVELOPER_MAINTAINER_PROJECT_ACCESS
]
...
...
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