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
Jérome Perrin
gitlab-ce
Commits
62fc8064
Commit
62fc8064
authored
Sep 03, 2014
by
Jan-Willem van der Meer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor Oauth::User class to use instance methods
parent
e8f1331f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
72 deletions
+80
-72
app/models/user.rb
app/models/user.rb
+0
-4
lib/gitlab/ldap/user.rb
lib/gitlab/ldap/user.rb
+4
-0
lib/gitlab/oauth/user.rb
lib/gitlab/oauth/user.rb
+76
-68
No files found.
app/models/user.rb
View file @
62fc8064
...
@@ -474,10 +474,6 @@ class User < ActiveRecord::Base
...
@@ -474,10 +474,6 @@ class User < ActiveRecord::Base
email
=~
/\Atemp-email-for-oauth/
email
=~
/\Atemp-email-for-oauth/
end
end
def
generate_tmp_oauth_email
self
.
email
=
"temp-email-for-oauth-
#{
username
}
@gitlab.localhost"
end
def
public_profile?
def
public_profile?
authorized_projects
.
public_only
.
any?
authorized_projects
.
public_only
.
any?
end
end
...
...
lib/gitlab/ldap/user.rb
View file @
62fc8064
...
@@ -68,6 +68,10 @@ module Gitlab
...
@@ -68,6 +68,10 @@ module Gitlab
private
private
def
needs_blocking?
false
end
def
find_by_uid_and_provider
def
find_by_uid_and_provider
find_by_uid
(
uid
)
find_by_uid
(
uid
)
end
end
...
...
lib/gitlab/oauth/user.rb
View file @
62fc8064
...
@@ -16,97 +16,105 @@ module Gitlab
...
@@ -16,97 +16,105 @@ module Gitlab
def
create
(
auth
)
def
create
(
auth
)
@auth
=
auth
@auth
=
auth
password
=
Devise
.
friendly_token
[
0
,
8
].
downcase
user
=
new
(
auth
).
user
opts
=
{
extern_uid:
uid
,
provider:
provider
,
name:
name
,
username:
username
,
email:
email
,
password:
password
,
password_confirmation:
password
,
}
user
=
model
.
build_user
(
opts
)
user
.
skip_confirmation!
# Services like twitter and github does not return email via oauth
# In this case we generate temporary email and force user to fill it later
if
user
.
email
.
blank?
user
.
generate_tmp_oauth_email
elsif
provider
!=
"ldap"
# Google oauth returns email but dont return nickname
# So we use part of email as username for new user
# For LDAP, username is already set to the user's
# uid/userid/sAMAccountName.
email_username
=
email
.
match
(
/^[^@]*/
)[
0
]
# Strip apostrophes since they are disallowed as part of username
user
.
username
=
email_username
.
gsub
(
"'"
,
""
)
end
begin
user
.
save!
user
.
save!
log
.
info
"(OAuth) Creating user
#{
email
}
from login with extern_uid =>
#{
uid
}
"
user
.
block
if
needs_blocking?
user
rescue
ActiveRecord
::
RecordInvalid
=>
e
rescue
ActiveRecord
::
RecordInvalid
=>
e
log
.
info
"(OAuth) Email
#{
e
.
record
.
errors
[
:email
]
}
. Username
#{
e
.
record
.
errors
[
:username
]
}
"
log
.
info
"(OAuth) Email
#{
e
.
record
.
errors
[
:email
]
}
. Username
#{
e
.
record
.
errors
[
:username
]
}
"
return
nil
,
e
.
record
.
errors
return
nil
,
e
.
record
.
errors
end
end
log
.
info
"(OAuth) Creating user
#{
email
}
from login with extern_uid =>
#{
uid
}
"
private
if
Gitlab
.
config
.
omniauth
[
'block_auto_created_users'
]
&&
!
ldap?
def
find_by_uid_and_provider
user
.
block
::
User
.
where
(
provider:
provider
,
extern_uid:
uid
).
last
end
end
user
def
provider
auth
.
provider
end
end
private
def
uid
auth
.
uid
.
to_s
end
def
find_by_uid_and_provider
def
needs_blocking?
model
.
where
(
provider:
provider
,
extern_uid:
uid
).
last
Gitlab
.
config
.
omniauth
[
'block_auto_created_users'
]
end
end
attr_accessor
:auth
,
:user
def
initialize
(
auth
)
self
.
auth
=
auth
self
.
user
=
::
User
.
new
(
user_attributes
)
user
.
skip_confirmation!
end
def
user_attributes
{
extern_uid:
uid
,
provider:
provider
,
name:
name
,
username:
username
,
email:
email
,
password:
password
,
password_confirmation:
password
,
}
end
end
def
uid
def
uid
auth
.
uid
.
to_s
auth
.
uid
.
to_s
end
end
def
email
def
provider
return
unless
auth
.
info
.
respond_to?
(
:email
)
auth
.
provider
auth
.
info
.
email
.
downcase
unless
auth
.
info
.
email
.
nil?
end
def
info
auth
.
info
end
end
def
name
def
name
if
auth
.
info
.
name
.
nil?
(
info
.
name
||
full_name
).
to_s
.
force_encoding
(
'utf-8'
)
"
#{
auth
.
info
.
first_name
}
#{
auth
.
info
.
last_name
}
"
.
force_encoding
(
'utf-8'
)
else
auth
.
info
.
name
.
to_s
.
force_encoding
(
'utf-8'
)
end
end
def
full_name
"
#{
info
.
first_name
}
#{
info
.
last_name
}
"
end
end
def
username
def
username
return
unless
auth
.
info
.
respond_to?
(
:nickname
)
(
info
.
try
(
:nickname
)
||
generate_username
).
to_s
.
force_encoding
(
'utf-8'
)
auth
.
info
.
nickname
.
to_s
.
force_encoding
(
"utf-8"
)
end
end
def
provider
def
email
auth
.
provider
(
info
.
try
(
:email
)
||
generate_temporarily_email
).
downcase
end
end
def
log
def
password
Gitlab
::
AppLogger
@password
||=
Devise
.
friendly_token
[
0
,
8
].
downcase
end
end
def
model
def
log
::
Us
er
Gitlab
::
AppLogg
er
end
end
def
raise_error
(
message
)
def
raise_error
(
message
)
raise
OmniAuth
::
Error
,
"(OAuth) "
+
message
raise
OmniAuth
::
Error
,
"(OAuth) "
+
message
end
end
def
ldap?
# Get the first part of the email address (before @)
provider
==
'ldap'
# In addtion in removes illegal characters
def
generate_username
email
.
match
(
/^[^@]*/
)[
0
].
parameterize
end
end
def
generate_temporarily_email
"temp-email-for-oauth-
#{
username
}
@gitlab.localhost"
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