Skip to content

Commit

Permalink
Fix: 修正开放注册后,三方帐号为注册用户登陆错无的问题;
Browse files Browse the repository at this point in the history
Mod: Timeline 一些小事件把字体改小;
Add: 加入 Github帐号 绑定功能;
  • Loading branch information
huacnlee committed Apr 3, 2011
1 parent 5007a8f commit 83a077d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
14 changes: 10 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ def unfollow
def auth_callback
auth = request.env["omniauth.auth"]
redirect_to root_path if auth.blank?
provider_name = auth['provider'].gsub(/^t/,"").titleize

if current_user
Authorization.create_from_hash(auth, current_user)
flash[:notice] = "成功绑定了 #{auth['provider']} 帐号。"
flash[:notice] = "成功绑定了 #{provider_name} 帐号。"
redirect_to edit_user_registration_path
elsif @user = Authorization.find_from_hash(auth)
sign_in @user
Expand All @@ -80,9 +81,14 @@ def auth_callback
else
if Setting.allow_register
@new_user = Authorization.create_from_hash(auth, current_user) #Create a new user
sign_in @new_user
flash[:notice] = "欢迎来自 #{auth['provider']} 的用户,你的帐号已经创建成功。"
redirect_to "/"
if @new_user.errors.blank?
sign_in @new_user
flash[:notice] = "欢迎来自 #{provider_name} 的用户,你的帐号已经创建成功。"
redirect_to "/"
else
flash[:notice] = "#{provider_name}的帐号提供信息不全,无法直接登陆,请先注册。"
redirect_to "/register"
end
else
flash[:alert] = "你还没有注册用户。"
redirect_back_or_default "/login"
Expand Down
13 changes: 10 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ def password_required?
mount_uploader :avatar, AvatarUploader

def self.create_from_hash(auth)
Rails.logger.debug { "---------#{auth}"}
user = User.new
user.name = auth["user_info"]["name"]
user.email = auth['user_info']['email']
user.save(false)
if user.email.blank?
user.errors.add("Email","三方网站没有提供你的Email信息,无法直接注册。")
return user
end
user.save
user.reset_persistence_token! #set persistence_token else sessions will not be created
user
end
Expand All @@ -67,8 +72,10 @@ def self.create_from_hash(auth)
# 此方法用于处理开始注册是自动生成 slug, 因为没表单,只能自动
def auto_slug
if self.slug.blank?
self.slug = self.email.split("@")[0]
self.slug = self.slug.safe_slug
if !self.email.blank?
self.slug = self.email.split("@")[0]
self.slug = self.slug.safe_slug
end
# 如果 slug 被 safe_slug 后是空的,就用 id 代替
if self.slug.blank?
self.slug = self.id.to_s
Expand Down
6 changes: 3 additions & 3 deletions app/views/logs/_log.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ when "UserLog"
%>
<div class="ask" ask-id="<%= log.id %>">
<% if ["FOLLOW_TOPIC", "UNFOLLOW_TOPIC"].include?(action) %>
<div class="title"><%= user_name_tag(log.user) %> <%= action == "FOLLOW_TOPIC" ? "关注" : "取消关注" %>了话题 <a href="/topics/<%= item.name %>">#<%= item.name %></a></div>
<div class="title small_title"><%= user_name_tag(log.user) %> <%= action == "FOLLOW_TOPIC" ? "关注" : "取消关注" %>了话题 <a href="/topics/<%= item.name %>">#<%= item.name %></a></div>
<% elsif ["FOLLOW_USER", "UNFOLLOW_USER"].include?(action) %>
<div class="title"><%= user_name_tag(log.user) %> <%= action == "FOLLOW_USER" ? "关注" : "取消关注" %><%= user_name_tag(item) %></div>
<div class="title small_title"><%= user_name_tag(log.user) %> <%= action == "FOLLOW_USER" ? "关注" : "取消关注" %><%= user_name_tag(item) %></div>
<% else %>
<div class="title"><a href="/asks/<%= item.id %>"><%= item.title %></a></div>
<div class="info">
Expand Down Expand Up @@ -146,7 +146,7 @@ when "TopicLog"
if !item.nil?
%>
<div class="ask" ask-id="<%= log.id %>">
<div class="title"><%= user_name_tag(log.user) %> 创建了话题 <a href="/topics/<%= item.name %>">#<%= item.name %></a></div>
<div class="title small_title"><%= user_name_tag(log.user) %> 创建了话题 <a href="/topics/<%= item.name %>">#<%= item.name %></a></div>
</div>
<%
end
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_omniauth_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<% if not current_user %>
<ul>
<li class="google"><a href="/auth/google">Google帐号</a></li>
<li class="github"><a href="/auth/github">Github</a></li>
<li class="douban"><a href="/auth/douban">豆瓣</a></li>
<li class="tqq"><a href="/auth/tqq">QQ帐号(腾讯微博)</a></li>
<li class="tsina"><a href="/auth/tsina">新浪微博</a></li>
Expand All @@ -16,6 +17,11 @@
<% else %>
<li class="google"><a href="#" class="disabled">Google帐号</a><span>[已绑定]</span></li>
<% end %>
<% if not current_user.auths.index("github") %>
<li class="github"><a href="/auth/github">Github</a></li>
<% else %>
<li class="github"><a href="#" class="disabled">Github</a><span>[已绑定]</span></li>
<% end %>
<% if not current_user.auths.index("douban") %>
<li class="douban"><a href="/auth/douban">豆瓣</a></li>
<% else %>
Expand Down
1 change: 1 addition & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
provider :tqq, 'd474e02630a849a5bbbf13bca0f9795f', '279bad9e954cc2b3f9988df534b4be49'
provider :t163, 'BMO70rvqyCs0Pby5', 'AwRwtzF1eRPVrZa5rzusedS8JdM79pNj'
provider :tsohu, 'XtA4sAn6dpVnqIMNucge', 'URzg4g)Vj9M(9VdMarXIZddgQ8!(5m8cehkH^WB0'
provider :github, '11f1ad7b2ef9263b2ead', '4f07422d1e499126e2fb247dd547b670e01a47d1'
end
Binary file modified public/images/favicons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions public/stylesheets/front.css
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ address, blockquote {
.ask a.topic:hover { background:#19558D; color:#FFF; }

.ask .title { font-size: 1.3em; line-height: 1.2em; margin: 2px 0 5px; }
.ask .small_title { font-size:12px; margin:0px; color:#999; }
.ask .title h1 { font-size: 1.2em; line-height: 1.2em; display:inline; }
.ask .info { color: #999; margin:3px 0; }
.ask .last_answer { margin-top:5px; }
Expand Down Expand Up @@ -299,6 +300,7 @@ a.user img.normal { width:100px; height:100px; }
#omniauth_links li.t163 a { background-position:0 -80px; }
#omniauth_links li.tsohu a { background-position:0 -96px; }
#omniauth_links li.google a { background-position:0 -48px; }
#omniauth_links li.github a { background-position:0 -113px; }
#omniauth_links li span { color:#999; margin-left:3px; }

.search_result_dropdown {}
Expand Down

0 comments on commit 83a077d

Please sign in to comment.