Skip to content

Commit

Permalink
[#256] Ability to connect another integration
Browse files Browse the repository at this point in the history
  • Loading branch information
evheny0 committed Dec 24, 2019
1 parent 7fd79f2 commit 31c1223
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ROLLBAR_POST_CLIENT_ITEM_TOKEN=""
GITLAB_APP_ID=
GITLAB_APP_SECRET=
GITLAB_REDIRECT_URI=
GITLAB_API_ENDPOINT=
GITLAB_API_ENDPOINT="https://gitlab.com/api/v4"
GITLAB_WEBHOOK_SECRET=
GITLAB_DEPLOYQA_BOT_ID=
GITLAB_DEPLOYQA_BOT_TOKEN=
20 changes: 19 additions & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SessionsController < ApplicationController
def show; end

def create
result = user_from_omniauth
result = current_user.present? ? connect_user : user_from_omniauth

if result.error?
flash.notice = result.errors.join("/n")
Expand All @@ -29,6 +29,24 @@ def setup_session(session, user)
::Auth::SessionHandler.new(session).set!(user_id: user.id, provider: auth_info_presenter.provider)
end

def connect_user
user_reference = UserReference.find_by(auth_uid: auth_info_presenter.uid, auth_provider: auth_info_presenter.provider)

raise if user_reference && (user_reference.auth_info.present? || user_reference.user_id.present?)
raise if current_user.auth_info.email != auth_info_presenter.email

if user_reference.present?
user_reference.update!(user: current_user.actual_user)
else
user_reference = UserReference.create!(user: current_user.actual_user, auth_uid: auth_info_presenter.uid, auth_provider: auth_info_presenter.provider, full_name: auth_info_presenter.full_name)
end

auth_info_params = Auth::AuthInfoParamsBuilder.new(auth_info_presenter, user_reference).call
AuthInfo.create!(auth_info_params.merge)

ReturnValue.ok(current_user.actual_user)
end

def user_from_omniauth
result = ::Auth::UserAuthenticator.new(auth_info_presenter).call
::Auth::SetupUserProjectsMembership.new(result.object, auth_info_presenter.provider).call if result.ok?
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class UsersController < ApplicationController
IMAGE_PATH = {
::ProjectsConstants::Providers::GITHUB => "media/images/logos/github-logo.svg",
::ProjectsConstants::Providers::GITLAB => "media/images/logos/gitlab-logo.svg"
}.freeze

def show
@user = find_user
@github_auth = @user.user_references.find { |reference| reference.auth_provider == OmniauthConstants::GITHUB }
@gitlab_auth = @user.user_references.find { |reference| reference.auth_provider == OmniauthConstants::GITLAB }
end

private

def find_user
authorize User.includes(user_references: :auth_info).find(params[:id]), :show?, policy_class: UserPolicy
end
end
9 changes: 9 additions & 0 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class UserPolicy < ApplicationPolicy
def show?
return true if user.system_role == UserConstants::SystemRoles::ADMIN

user.actual_user == record
end
end
2 changes: 1 addition & 1 deletion app/services/auth/accessibility_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def call
return if @user_reference.auth_info.blank?
return if @user_reference.auth_info.primary?

raise ::Auth::NotPermittedError, "Please login with another provider"
raise ::Auth::NotPermittedError, "You've already registered in another provider with the same email"
end

def auth_info_email_occupied?
Expand Down
1 change: 1 addition & 0 deletions app/services/auth/auth_info_params_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def call
def load_email(params)
return if params[:email].present?

# TODO: why do we need USER_API_CLIENT? GitLab always have an email and there is no :email method in ProviderAPI::Gitlab::UserClient
api_client = USER_API_CLIENT.fetch(@omniauth_info_presenter.provider).new(@omniauth_info_presenter.token)
email = api_client.emails.find { |email_info| email_info[:primary] }.fetch(:email)
params[:email] = email
Expand Down
4 changes: 4 additions & 0 deletions app/services/auth/user_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ def email
def token
auth_info.token
end

def actual_user
@user
end
end
end
1 change: 1 addition & 0 deletions app/views/layouts/_header.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ nav.navbar.navbar-expand-lg.navbar-light.bg-light
a.nav-link.dropdown-toggle href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
= current_user.full_name
.dropdown-menu aria-labelledby="navbarDropdown"
= link_to "User preferences", user_path(current_user.actual_user), class: "dropdown-item"
= link_to "Log out", sessions_path, method: :delete, class: "dropdown-item"
23 changes: 23 additions & 0 deletions app/views/users/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.container
= render partial: "shared/breadcrumb", locals: {\
text_link_hash: {\
"User" => nil\
},
class: "mt-4" }


.d-flex.justify-content-between.mt-4
h1 = current_user.full_name
.mt-4
p
= image_pack_tag("media/images/logos/github-logo.svg", size: "32x32", class: "mr-2")
- if @github_auth.present?
span.text-success Github integration connected!
- else
= link_to "Click to connect Github", omniauth_path("github"), method: :post
p
= image_pack_tag("media/images/logos/gitlab-logo.svg", size: "32x32", class: "mr-2")
- if @gitlab_auth.present?
span.text-success Gitlab integration connected!
- else
= link_to "Click to connect Gitlab", omniauth_path("gitlab", some_data: "123"), method: :post
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
get "/auth/slack/callback", to: "slack/authentications#create"
get "/auth/failure", to: "slack/authentications#show"
get "/auth/:provider/callback", to: "sessions#create"
get "/auth/:provider", to: "sessions#show", as: "omniauth"
post "/auth/:provider", to: "sessions#show", as: "omniauth"

constraints Routes::LoggedUserConstraint.new(SidekiqPolicy) do
mount Sidekiq::Web => "/sidekiq"
end

resource :sessions, only: %i[show create destroy]
resources :users, only: %i[show]

namespace :webhooks do
resources :github, only: %i[create]
Expand Down

0 comments on commit 31c1223

Please sign in to comment.