Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
5b86dab0
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5b86dab0
编写于
9月 04, 2014
作者:
J
Jan-Willem van der Meer
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move auth hash to a seperate class
上级
1bd15fa7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
46 addition
and
87 deletion
+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
未找到文件。
lib/gitlab/ldap/user.rb
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录