google oauth added and also preferences

This commit is contained in:
Harry Bomrah 2015-03-04 18:31:36 +08:00
parent 5773d49218
commit ed3308abbb
22 changed files with 4123 additions and 40 deletions

View File

@ -1,5 +1,8 @@
source 'https://rubygems.org'
#social gems
gem "omniauth-google-oauth2", "~> 0.2.1"
#rails gem
gem 'rails', '~> 4.1.0'
gem 'sanitize'

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -346,12 +346,14 @@
position: relative;
border-bottom: 5px solid #b1b1b1;
height: 8px;
text-align: center;
}
#profile #member-roles .roles h4 span {
position: absolute;
/*position: absolute;*/
background-color: #F3F3F3;
padding: 0 5px;
left: 50%;
/*left: 50%;*/
margin: auto !important;
}
#profile #member-roles .roles dl {
background-color: #ffffff;
@ -365,12 +367,22 @@
font-size: 1.2em;
margin-top: 20px;
}
#profile #member-roles .roles dt:first-child {
#profile #member-roles .roles #google_connection {
margin-top: 0px;
position: relative;
}
#profile #member-roles .wait_text {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
#profile #member-roles .roles dd {
color: #999999;
}
#profile #module-navbar {
margin-left: 10px;
}

View File

@ -28,6 +28,70 @@ class SessionsController < ApplicationController
end
end
def google_result
@code = params[:code]
if @code.nil?
redirect_to root_url
end
end
def google_callback
auth = env["omniauth.auth"]
user = Google.find_by("google_uid" => auth.uid).user rescue nil
if user.nil? && current_user.nil?
user_connected = false
else
user_connected = true
if user.nil? && !current_user.nil?
connection_successful = connect_account(auth)
else
if login_user(user,auth)
if params[:referer_url]
redirect_to URI.parse(params[:referer_url]).path and return
else
redirect_to admin_dashboards_path and return
end
end
end
end
if user_connected && connection_successful
code = 1
elsif user_connected && !connection_successful
code = 2
else !user_connected && !connection_successful
code = 3
end
redirect_to auth_google_result_path(:code => code)
end
def google_remove
current_user.google.destroy
redirect_to admin_member_path(current_user.member_profile.to_param) and return
end
def connect_account(auth)
if !current_user.nil?
google = Google.new
google.google_uid = auth.uid
google.token = auth.credentials.token
google.connected = true
google.save
current_user.google = google
current_user.save
return true
else
return false
end
end
def login_user(user,auth)
if user.google.token != auth.credentials.token
user.google.token = auth.credentials.token
user.google.save
end
session[:user_id] = user.id
end
def destroy
log_user_action
session[:user_id] = nil

10
app/models/google.rb Normal file
View File

@ -0,0 +1,10 @@
class Google
include Mongoid::Document
include Mongoid::Timestamps
field :token
field :google_uid
field :connected, type: Boolean
belongs_to :user
end

View File

@ -34,6 +34,10 @@ class Site
field :site_settings
field :template, type: String
field :store_token
field :google_oauth_enabled, :type => Boolean, :default => false
field :google_client_id
field :google_client_secret
field :month_traffic_cache

View File

@ -21,6 +21,7 @@ class User
has_many :authorizations
belongs_to :member_profile
has_one :facebook, :autosave => true, :dependent => :destroy
has_one :google, :autosave => true, :dependent => :destroy
has_one :desktop, :dependent => :destroy

View File

@ -1,12 +1,11 @@
<div class="roles">
<h4><span><%= show_roles.title %><%= t(:role_info)%></span></h4>
<dl>
<dt><%= t('status') %></dt>
<dd><%= @member.role_statuses.where(role_id: show_roles.id).map{|t|t.title.to_s}.join(',') rescue '' %></dd>
<% show_roles.attribute_fields.where(:disabled=>false).asc(:_id).each do |rf| %>
<dt><%= rf.title %></dt>
<dd><%= nl2br(show_attribute_value(@member.get_attribute_value(rf,"role").get_value_by_locale(I18n.locale))) rescue '' %></dd>
<% end -%>
</dl>
</div>
<div class="roles">
<h4><span><%= show_roles.title %><%= t(:role_info)%></span></h4>
<dl>
<dt><%= t('status') %></dt>
<dd><%= @member.role_statuses.where(role_id: show_roles.id).map{|t|t.title.to_s}.join(',') rescue '' %></dd>
<% show_roles.attribute_fields.where(:disabled=>false).asc(:_id).each do |rf| %>
<dt><%= rf.title %></dt>
<dd><%= nl2br(show_attribute_value(@member.get_attribute_value(rf,"role").get_value_by_locale(I18n.locale))) rescue '' %></dd>
<% end -%>
</dl>
</div>

View File

@ -1,23 +1,23 @@
<div class="roles">
<h4><span><%= t(i18n)%></span></h4>
<dl>
<dt><%= t('users.first_name')%></dt>
<dd><%= @member.first_name %></dd>
<dt><%= t('users.last_name')%></dt>
<dd><%= @member.last_name %></dd>
<dt><%= t('users.email')%></dt>
<dd><%= @member.email %></dd>
<% if !@member.sid.blank? %>
<dt><%= t('users.sid')%></dt>
<dd><%= @member.sid %></dd>
<dt><%= t('users.office_tel')%></dt>
<dd><%= @member.office_tel %></dd>
<% end %>
<dt><%= t('users.sex')%></dt>
<dd><%= t("users.#{(@member.sex.nil? ? 'unknown' : @member.sex)}") %></dd>
<% items.each do |item| %>
<dt><%= item.member_profile_field.title %></dt>
<dd><%= nl2br(show_attribute_value(@member.get_attribute_value(item.member_profile_field).get_value_by_locale(I18n.locale))) rescue '' %></dd>
<% end -%>
</dl>
</div>
<h4><span><%= t(i18n)%></span></h4>
<dl>
<dt><%= t('users.first_name')%></dt>
<dd><%= @member.first_name %></dd>
<dt><%= t('users.last_name')%></dt>
<dd><%= @member.last_name %></dd>
<dt><%= t('users.email')%></dt>
<dd><%= @member.email %></dd>
<% if !@member.sid.blank? %>
<dt><%= t('users.sid')%></dt>
<dd><%= @member.sid %></dd>
<dt><%= t('users.office_tel')%></dt>
<dd><%= @member.office_tel %></dd>
<% end %>
<dt><%= t('users.sex')%></dt>
<dd><%= t("users.#{(@member.sex.nil? ? 'unknown' : @member.sex)}") %></dd>
<% items.each do |item| %>
<dt><%= item.member_profile_field.title %></dt>
<dd><%= nl2br(show_attribute_value(@member.get_attribute_value(item.member_profile_field).get_value_by_locale(I18n.locale))) rescue '' %></dd>
<% end -%>
</dl>
</div>

View File

@ -0,0 +1,8 @@
<div class="roles">
<h4><span><%= t("social_profile_connections")%></span></h4>
<dl>
<% if current_site.google_oauth_enabled %>
<dt id="google_connection"><div class="main_text">Google <span class="pull-right"><input <%= (!@member.user.google.nil? ? "checked=checked" : "") %> type="checkbox" id="google_connect_box" class="toggle-check set-sidebar-state" data-disabled="true"></span></div><div class="wait_text" style="text-align:center; display:none;">Please wait...</div></dt>
<% end %>
</dl>
</div>

View File

@ -1,10 +1,9 @@
<%= render :partial => "js_and_css"%>
<%= stylesheet_link_tag "lib/togglebox"%>
<% content_for :page_specific_javascript do -%>
<%= javascript_include_tag "lib/member/member.js" %>
<%= javascript_include_tag "lib/footable-0.1.js" %>
<% end -%>
<div id="profile" class="clearfix">
<div id="basic-info" class="clearfix">
@ -24,6 +23,7 @@
<div id="member-roles" class="nano">
<div class="content">
<%= render :partial=> "user_profile",:locals=>{:role_class=>"basic",:i18n=>"profile", :items=>@custom_fields} %>
<%= render :partial=> "user_social" %>
<%= render :partial=> "show_roles",collection: @member.roles %>
</div>
</div>
@ -68,4 +68,20 @@
<% end %>
</div>
<script type="text/javascript">
$("#google_connect_box").on("click",function(){
$("#google_connection div.main_text").css("opacity","0.5");
$("#google_connection div.wait_text").show();
if($(this).parent().hasClass("disable")){
window.location.href = "/auth/google_oauth2"
}else{
if(confirm("Are you sure?")){
window.location.href = "/auth/google_oauth2/remove";
}else{
$("#google_connection div.main_text").css("opacity","1");
$("#google_connection div.wait_text").hide();
return false;
}
}
})
</script>

View File

@ -32,6 +32,9 @@
<li>
<a href="#orbitBar-them" data-toggle="tab"><%= t('preferences.orbitbar_theme') %></a>
</li>
<li>
<a href="#google-oauth" data-toggle="tab">Google OAuth</a>
</li>
</ul>
<div class="tab-content">
@ -281,6 +284,28 @@
</div>
</div>
<div id="google-oauth" class="tab-pane fade">
<div class="control-group">
<label class="control-label muted">Enable Google OAuth</label>
<div class="controls">
<%= f.check_box :google_oauth_enabled %>
</div>
</div>
<div class="control-group">
<label class="control-label muted">Google client id</label>
<div class="controls">
<%= f.text_field :google_client_id, :value => (@site['google_client_id'] rescue nil), :class=>"input-large google-oauth-fields", :placeholder=>"Google client id", :disabled => !@site[:google_oauth_enabled] %>
</div>
</div>
<div class="control-group">
<label class="control-label muted">Google client secret</label>
<div class="controls">
<%= f.text_field :google_client_secret, :value => (@site['google_client_secret'] rescue nil), :class=>"input-large google-oauth-fields", :placeholder=>"Google client secret", :disabled => !@site[:google_oauth_enabled] %>
</div>
</div>
<div class="alert alert-info">Please restart the website after modifying these settings. Click on the restart button and refresh the page in sometime. <a class="btn btn-small btn-primary" id="restart_server">Restart server</a></div>
</div>
</div>
<!-- Form Actions -->
@ -355,4 +380,19 @@
$('a[href='+lastTab+']').click();
}
});
$("#site_google_oauth_enabled").on("click",function(){
if($(this).is(":checked")){
$(".google-oauth-fields").removeAttr("disabled");
}else{
$(".google-oauth-fields").attr("disabled","disabled");
}
})
$("#restart_server").on("click",function(){
$.ajax({
url : "<%= admin_site_restart_server_path(@site) %>",
type : "get"
})
return false;
})
</script>

View File

@ -29,6 +29,11 @@
<a href="/users/new">Sign up</a>
<% end %>
</div>
<% if current_site.google_oauth_enabled %>
<div class="pull-right">
<a href="/auth/google_oauth2"><img src="/assets/sign-in-with-google.png" width="185px" /></a>
</div>
<% end %>
</div>
<% end %>
<input type="checkbox" id="open-orbit-nav">

View File

@ -0,0 +1 @@
Google account successfully connected.

View File

@ -0,0 +1 @@
Google account could not be connected.

View File

@ -0,0 +1 @@
Google account is not connected. Please login using your username and password and then connect google account in your member profile.

View File

@ -0,0 +1,3 @@
<section id="main-wrap">
<%= render :partial => "google_code_#{@code}" %>
</section>

3894
cacert.pem Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
OmniAuth.config.logger = Rails.logger
site = Site.first
client_id = site.google_client_id
client_secret = site.google_client_secret
if !client_id.nil? && !client_secret.nil?
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, client_id, client_secret,{ access_type: "offline", approval_prompt: "" }
end
else
site.google_oauth_enabled = false
site.save
end

View File

@ -65,6 +65,7 @@ en:
widget_info_for_ad_image_size: "Best size with: %{best_size}"
add_attribute_field: Add attribute field
social_profile_connections: Social profile connections
add_image: Add image
add_item: Add item
add_member: Create New Member

View File

@ -114,6 +114,7 @@ zh_tw:
by_sub_role: 次用戶狀態
author: 作者
authorization: 權限
social_profile_connections: Social profile connections
back: 回上一步
basic: 基本
browse: 瀏覽

View File

@ -18,6 +18,10 @@ Orbit::Application.routes.draw do
get "/page_parts/edit_sub_part" => "page_parts#edit_sub_part"
get "/admin/license_denied" => "store_api#render_license_denied"
#routes for google to callback after omniauth
get 'auth/:provider/callback', to: 'sessions#google_callback'
get 'auth/:provider/remove', to: 'sessions#google_remove'
get 'auth/google/result', to: 'sessions#google_result'
resources :pages
resources :page_parts do
member do