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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
5b86dab0
Commit
5b86dab0
authored
Sep 04, 2014
by
Jan-Willem van der Meer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move auth hash to a seperate class
parent
1bd15fa7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
87 deletions
+46
-87
lib/gitlab/ldap/user.rb
lib/gitlab/ldap/user.rb
+16
-16
lib/gitlab/oauth/user.rb
lib/gitlab/oauth/user.rb
+26
-67
spec/lib/gitlab/ldap/user_spec.rb
spec/lib/gitlab/ldap/user_spec.rb
+4
-4
No files found.
lib/gitlab/ldap/user.rb
View file @
5b86dab0
...
...
@@ -10,23 +10,27 @@ module Gitlab
module
LDAP
class
User
<
Gitlab
::
OAuth
::
User
class
<<
self
def
find_or_create
(
auth
)
self
.
auth
=
aut
h
find
(
auth
)
||
create
(
aut
h
)
def
find_or_create
(
auth
_hash
)
self
.
auth
_hash
=
auth_has
h
find
(
auth
_hash
)
||
find_and_connect_by_email
(
auth_hash
)
||
create
(
auth_has
h
)
end
# overloaded from Gitlab::Oauth::User
# TODO: it's messy, needs cleanup, less complexity
def
create
(
auth
)
ldap_user
=
new
(
auth
)
# first try to find the user based on the returned email address
user
=
ldap_user
.
find_gitlab_user_by_email
def
find_and_connect_by_email
(
auth_hash
)
self
.
auth_hash
=
auth_hash
user
=
model
.
find_by
(
email:
self
.
auth_hash
.
email
)
if
user
user
.
update_attributes
(
extern_uid:
ldap_user
.
uid
,
provider:
ldap_user
.
provider
)
Gitlab
::
AppLogger
.
info
(
"(LDAP) Updating legacy LDAP user
#{
ldap_user
.
email
}
with extern_uid =>
#{
ldap_user
.
uid
}
"
)
user
.
update_attributes
(
extern_uid:
auth_hash
.
uid
,
provider:
auth_hash
.
provider
)
Gitlab
::
AppLogger
.
info
(
"(LDAP) Updating legacy LDAP user
#{
self
.
auth_hash
.
email
}
with extern_uid =>
#{
auth_hash
.
uid
}
"
)
return
user
end
end
# overloaded from Gitlab::Oauth::User
# TODO: it's messy, needs cleanup, less complexity
def
create
(
auth_hash
)
ldap_user
=
new
(
auth_hash
)
# first try to find the user based on the returned email address
# if the user isn't found by an exact email match, use oauth methods
ldap_user
.
save_and_trigger_callbacks
...
...
@@ -58,7 +62,7 @@ module Gitlab
protected
def
find_by_uid_and_provider
find_by_uid
(
uid
)
find_by_uid
(
auth_hash
.
uid
)
end
def
find_by_uid
(
uid
)
...
...
@@ -79,10 +83,6 @@ module Gitlab
end
end
def
find_gitlab_user_by_email
self
.
class
.
model
.
find_by
(
email:
email
)
end
def
needs_blocking?
false
end
...
...
lib/gitlab/oauth/user.rb
View file @
5b86dab0
...
...
@@ -7,15 +7,15 @@ module Gitlab
module
OAuth
class
User
class
<<
self
attr_
accessor
:aut
h
attr_
reader
:auth_has
h
def
find
(
auth
)
self
.
auth
=
aut
h
def
find
(
auth
_hash
)
self
.
auth
_hash
=
auth_has
h
find_by_uid_and_provider
end
def
create
(
auth
)
user
=
new
(
auth
)
def
create
(
auth
_hash
)
user
=
new
(
auth
_hash
)
user
.
save_and_trigger_callbacks
end
...
...
@@ -23,31 +23,32 @@ module Gitlab
::
User
end
protected
def
find_by_uid_and_provider
model
.
where
(
provider:
provider
,
extern_uid:
uid
).
last
end
def
provider
auth
.
provider
def
auth_hash
=
(
auth_hash
)
@auth_hash
=
AuthHash
.
new
(
auth_hash
)
end
def
uid
auth
.
uid
.
to_s
protected
def
find_by_uid_and_provider
model
.
where
(
provider:
auth_hash
.
provider
,
extern_uid:
auth_hash
.
uid
).
last
end
end
attr_accessor
:auth
,
:user
# Instance methods
attr_accessor
:auth_hash
,
:user
def
initialize
(
auth
)
self
.
auth
=
aut
h
def
initialize
(
auth
_hash
)
self
.
auth
_hash
=
auth_has
h
self
.
user
=
self
.
class
.
model
.
new
(
user_attributes
)
user
.
skip_confirmation!
end
def
auth_hash
=
(
auth_hash
)
@auth_hash
=
AuthHash
.
new
(
auth_hash
)
end
def
save_and_trigger_callbacks
user
.
save!
log
.
info
"(OAuth) Creating user
#{
email
}
from login with extern_uid =>
#{
uid
}
"
log
.
info
"(OAuth) Creating user
#{
auth_hash
.
email
}
from login with extern_uid =>
#{
auth_hash
.
uid
}
"
user
.
block
if
needs_blocking?
user
...
...
@@ -58,48 +59,16 @@ module Gitlab
def
user_attributes
{
extern_uid:
uid
,
provider:
provider
,
name:
name
,
username:
username
,
email:
email
,
password:
password
,
password_confirmation:
password
,
extern_uid:
auth_hash
.
uid
,
provider:
auth_hash
.
provider
,
name:
auth_hash
.
name
,
username:
auth_hash
.
username
,
email:
auth_hash
.
email
,
password:
auth_hash
.
password
,
password_confirmation:
auth_hash
.
password
,
}
end
def
uid
auth
.
uid
.
to_s
end
def
provider
auth
.
provider
end
def
info
auth
.
info
end
def
name
(
info
.
name
||
full_name
).
to_s
.
force_encoding
(
'utf-8'
)
end
def
full_name
"
#{
info
.
first_name
}
#{
info
.
last_name
}
"
end
def
username
(
info
.
try
(
:nickname
)
||
generate_username
).
to_s
.
force_encoding
(
'utf-8'
)
end
def
email
(
info
.
try
(
:email
)
||
generate_temporarily_email
).
downcase
end
def
password
@password
||=
Devise
.
friendly_token
[
0
,
8
].
downcase
end
def
log
Gitlab
::
AppLogger
end
...
...
@@ -108,16 +77,6 @@ module Gitlab
raise
OmniAuth
::
Error
,
"(OAuth) "
+
message
end
# Get the first part of the email address (before @)
# In addtion in removes illegal characters
def
generate_username
email
.
match
(
/^[^@]*/
)[
0
].
parameterize
end
def
generate_temporarily_email
"temp-email-for-oauth-
#{
username
}
@gitlab.localhost"
end
def
needs_blocking?
Gitlab
.
config
.
omniauth
[
'block_auto_created_users'
]
end
...
...
spec/lib/gitlab/ldap/user_spec.rb
View file @
5b86dab0
require
'spec_helper'
describe
Gitlab
::
LDAP
::
User
do
let
(
:gl_
auth
)
{
Gitlab
::
LDAP
::
User
}
let
(
:gl_
user
)
{
Gitlab
::
LDAP
::
User
}
let
(
:info
)
do
double
(
name:
'John'
,
...
...
@@ -19,12 +19,12 @@ describe Gitlab::LDAP::User do
it
"finds the user if already existing"
do
existing_user
=
create
(
:user
,
extern_uid:
'my-uid'
,
provider:
'ldap'
)
expect
{
gl_
auth
.
find_or_create
(
auth
)
}.
to_not
change
{
User
.
count
}
expect
{
gl_
user
.
find_or_create
(
auth
)
}.
to_not
change
{
User
.
count
}
end
it
"connects to existing non-ldap user if the email matches"
do
existing_user
=
create
(
:user
,
email:
'john@example.com'
)
expect
{
gl_
auth
.
find_or_create
(
auth
)
}.
to_not
change
{
User
.
count
}
expect
{
gl_
user
.
find_or_create
(
auth
)
}.
to_not
change
{
User
.
count
}
existing_user
.
reload
expect
(
existing_user
.
extern_uid
).
to
eql
'my-uid'
...
...
@@ -32,7 +32,7 @@ describe Gitlab::LDAP::User do
end
it
"creates a new user if not found"
do
expect
{
gl_
auth
.
find_or_create
(
auth
)
}.
to
change
{
User
.
count
}.
by
(
1
)
expect
{
gl_
user
.
find_or_create
(
auth
)
}.
to
change
{
User
.
count
}.
by
(
1
)
end
end
end
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