Skip to content
Snippets Groups Projects
Commit 2d36aedd authored by Дмитрий Коваленок's avatar Дмитрий Коваленок
Browse files

Updated controller redmine_omniauth_controller. Now possible login through google

parent 82b5ec8f
Branches
No related tags found
No related merge requests found
gem 'omniauth-google' gem 'oauth2'
\ No newline at end of file gem 'json'
group :development, :test do
gem 'pry'
end
\ No newline at end of file
require 'account_controller' require 'account_controller'
require 'json'
class RedmineOmniauthController < ApplicationController class RedmineOmniauthController < AccountController
def omniauth_google def omniauth_google
AccountController.new.send(:open_id_authenticate, params[:openid_url]) redirect_to oauth_client.auth_code.authorize_url(redirect_uri: oauth_google_callback_url, scope: scopes)
end
def oauth_google_callback
token = oauth_client.auth_code.get_token(params[:code], redirect_uri: oauth_google_callback_url)
result = token.get('https://www.googleapis.com/oauth2/v1/userinfo')
info = JSON.parse(result.body)
if info["verified_email"]
user = User.find_or_initialize_by_mail(info["email"])
if user.new_record?
# Self-registration off
redirect_to(home_url) && return unless Setting.self_registration?
# Create on the fly
user.login = info["email"].match(/(.+)@/)[1] unless info["email"].nil?
user.mail = info["email"] unless info["email"].nil?
user.firstname, user.lastname = info["name"].split(' ') unless info['name'].nil?
user.random_password
user.register
case Setting.self_registration
when '1'
register_by_email_activation(user) do
onthefly_creation_failed(user)
end
when '3'
register_automatically(user) do
onthefly_creation_failed(user)
end
else
register_manually_by_administrator(user) do
onthefly_creation_failed(user)
end
end
else
# Existing record
if user.active?
successful_authentication(user)
else
account_pending
end
end
end
end
def oauth_client
@client ||= OAuth2::Client.new(settings[:client_id], settings[:client_secret],
site: 'https://accounts.google.com',
authorize_url: '/o/oauth2/auth',
token_url: '/o/oauth2/token')
end
def settings
@settings ||= Setting.plugin_redmine_omniauth_google
end
def scopes
'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
end end
end end
\ No newline at end of file
<p>
<label>Client ID:</label>
<%= text_field_tag 'settings[client_id]', @settings[:client_id] %>
</p>
<p>
<label>Client Secret:</label>
<%= text_field_tag 'settings[client_secret]', @settings[:client_secret] %>
</p>
\ No newline at end of file
get 'omniauth_google', to: 'redmine_omniauth#omniauth_google', as: :omniauth_google get 'omniauth_google', to: 'redmine_omniauth#omniauth_google', as: :omniauth_google
get 'oauth_google_callback', to: 'redmine_omniauth#oauth_google_callback'
\ No newline at end of file
...@@ -8,4 +8,7 @@ Redmine::Plugin.register :redmine_omniauth_google do ...@@ -8,4 +8,7 @@ Redmine::Plugin.register :redmine_omniauth_google do
version '0.0.1' version '0.0.1'
url 'http://gitlab.tsdv.net/redmine_omniauth_google' url 'http://gitlab.tsdv.net/redmine_omniauth_google'
author_url 'https://tsdv.net/redmine/users/105' author_url 'https://tsdv.net/redmine/users/105'
settings default: {
client_id: '214698823792.apps.googleusercontent.com',
client_secret: 'M0HJPMypEgrDAKKHGiP6Y2R-' }, partial: 'settings/google_settings'
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment