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
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
Show 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
...
@@ -10,23 +10,27 @@ module Gitlab
module
LDAP
module
LDAP
class
User
<
Gitlab
::
OAuth
::
User
class
User
<
Gitlab
::
OAuth
::
User
class
<<
self
class
<<
self
def
find_or_create
(
auth
)
def
find_or_create
(
auth
_hash
)
self
.
auth
=
aut
h
self
.
auth
_hash
=
auth_has
h
find
(
auth
)
||
create
(
aut
h
)
find
(
auth
_hash
)
||
find_and_connect_by_email
(
auth_hash
)
||
create
(
auth_has
h
)
end
end
# overloaded from Gitlab::Oauth::User
def
find_and_connect_by_email
(
auth_hash
)
# TODO: it's messy, needs cleanup, less complexity
self
.
auth_hash
=
auth_hash
def
create
(
auth
)
user
=
model
.
find_by
(
email:
self
.
auth_hash
.
email
)
ldap_user
=
new
(
auth
)
# first try to find the user based on the returned email address
user
=
ldap_user
.
find_gitlab_user_by_email
if
user
if
user
user
.
update_attributes
(
extern_uid:
ldap_user
.
uid
,
provider:
ldap_user
.
provider
)
user
.
update_attributes
(
extern_uid:
auth_hash
.
uid
,
provider:
auth_hash
.
provider
)
Gitlab
::
AppLogger
.
info
(
"(LDAP) Updating legacy LDAP user
#{
ldap_user
.
email
}
with extern_uid =>
#{
ldap_user
.
uid
}
"
)
Gitlab
::
AppLogger
.
info
(
"(LDAP) Updating legacy LDAP user
#{
self
.
auth_hash
.
email
}
with extern_uid =>
#{
auth_hash
.
uid
}
"
)
return
user
return
user
end
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
# if the user isn't found by an exact email match, use oauth methods
ldap_user
.
save_and_trigger_callbacks
ldap_user
.
save_and_trigger_callbacks
...
@@ -58,7 +62,7 @@ module Gitlab
...
@@ -58,7 +62,7 @@ module Gitlab
protected
protected
def
find_by_uid_and_provider
def
find_by_uid_and_provider
find_by_uid
(
uid
)
find_by_uid
(
auth_hash
.
uid
)
end
end
def
find_by_uid
(
uid
)
def
find_by_uid
(
uid
)
...
@@ -79,10 +83,6 @@ module Gitlab
...
@@ -79,10 +83,6 @@ module Gitlab
end
end
end
end
def
find_gitlab_user_by_email
self
.
class
.
model
.
find_by
(
email:
email
)
end
def
needs_blocking?
def
needs_blocking?
false
false
end
end
...
...
lib/gitlab/oauth/user.rb
View file @
5b86dab0
...
@@ -7,15 +7,15 @@ module Gitlab
...
@@ -7,15 +7,15 @@ module Gitlab
module
OAuth
module
OAuth
class
User
class
User
class
<<
self
class
<<
self
attr_
accessor
:aut
h
attr_
reader
:auth_has
h
def
find
(
auth
)
def
find
(
auth
_hash
)
self
.
auth
=
aut
h
self
.
auth
_hash
=
auth_has
h
find_by_uid_and_provider
find_by_uid_and_provider
end
end
def
create
(
auth
)
def
create
(
auth
_hash
)
user
=
new
(
auth
)
user
=
new
(
auth
_hash
)
user
.
save_and_trigger_callbacks
user
.
save_and_trigger_callbacks
end
end
...
@@ -23,31 +23,32 @@ module Gitlab
...
@@ -23,31 +23,32 @@ module Gitlab
::
User
::
User
end
end
protected
def
auth_hash
=
(
auth_hash
)
def
find_by_uid_and_provider
@auth_hash
=
AuthHash
.
new
(
auth_hash
)
model
.
where
(
provider:
provider
,
extern_uid:
uid
).
last
end
def
provider
auth
.
provider
end
end
def
uid
protected
auth
.
uid
.
to_s
def
find_by_uid_and_provider
model
.
where
(
provider:
auth_hash
.
provider
,
extern_uid:
auth_hash
.
uid
).
last
end
end
end
end
attr_accessor
:auth
,
:user
# Instance methods
attr_accessor
:auth_hash
,
:user
def
initialize
(
auth
)
def
initialize
(
auth
_hash
)
self
.
auth
=
aut
h
self
.
auth
_hash
=
auth_has
h
self
.
user
=
self
.
class
.
model
.
new
(
user_attributes
)
self
.
user
=
self
.
class
.
model
.
new
(
user_attributes
)
user
.
skip_confirmation!
user
.
skip_confirmation!
end
end
def
auth_hash
=
(
auth_hash
)
@auth_hash
=
AuthHash
.
new
(
auth_hash
)
end
def
save_and_trigger_callbacks
def
save_and_trigger_callbacks
user
.
save!
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
.
block
if
needs_blocking?
user
user
...
@@ -58,48 +59,16 @@ module Gitlab
...
@@ -58,48 +59,16 @@ module Gitlab
def
user_attributes
def
user_attributes
{
{
extern_uid:
uid
,
extern_uid:
auth_hash
.
uid
,
provider:
provider
,
provider:
auth_hash
.
provider
,
name:
name
,
name:
auth_hash
.
name
,
username:
username
,
username:
auth_hash
.
username
,
email:
email
,
email:
auth_hash
.
email
,
password:
password
,
password:
auth_hash
.
password
,
password_confirmation:
password
,
password_confirmation:
auth_hash
.
password
,
}
}
end
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
def
log
Gitlab
::
AppLogger
Gitlab
::
AppLogger
end
end
...
@@ -108,16 +77,6 @@ module Gitlab
...
@@ -108,16 +77,6 @@ module Gitlab
raise
OmniAuth
::
Error
,
"(OAuth) "
+
message
raise
OmniAuth
::
Error
,
"(OAuth) "
+
message
end
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?
def
needs_blocking?
Gitlab
.
config
.
omniauth
[
'block_auto_created_users'
]
Gitlab
.
config
.
omniauth
[
'block_auto_created_users'
]
end
end
...
...
spec/lib/gitlab/ldap/user_spec.rb
View file @
5b86dab0
require
'spec_helper'
require
'spec_helper'
describe
Gitlab
::
LDAP
::
User
do
describe
Gitlab
::
LDAP
::
User
do
let
(
:gl_
auth
)
{
Gitlab
::
LDAP
::
User
}
let
(
:gl_
user
)
{
Gitlab
::
LDAP
::
User
}
let
(
:info
)
do
let
(
:info
)
do
double
(
double
(
name:
'John'
,
name:
'John'
,
...
@@ -19,12 +19,12 @@ describe Gitlab::LDAP::User do
...
@@ -19,12 +19,12 @@ describe Gitlab::LDAP::User do
it
"finds the user if already existing"
do
it
"finds the user if already existing"
do
existing_user
=
create
(
:user
,
extern_uid:
'my-uid'
,
provider:
'ldap'
)
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
end
it
"connects to existing non-ldap user if the email matches"
do
it
"connects to existing non-ldap user if the email matches"
do
existing_user
=
create
(
:user
,
email:
'john@example.com'
)
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
existing_user
.
reload
expect
(
existing_user
.
extern_uid
).
to
eql
'my-uid'
expect
(
existing_user
.
extern_uid
).
to
eql
'my-uid'
...
@@ -32,7 +32,7 @@ describe Gitlab::LDAP::User do
...
@@ -32,7 +32,7 @@ describe Gitlab::LDAP::User do
end
end
it
"creates a new user if not found"
do
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
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