Commit 181ebaa8 authored by Shubham Kumar's avatar Shubham Kumar

Extracted Admin E-mail notification code from EE to core

Changelog: changed
parent e66d4614
# frozen_string_literal: true
module Users
class UnsubscribesController < ApplicationController
skip_before_action :authenticate_user!
feature_category :users
def show
@user = get_user
end
def create
@user = get_user
if @user
@user.admin_unsubscribe!
Notify.send_unsubscribed_notification(@user.id).deliver_later
end
redirect_to new_user_session_path, notice: 'You have been unsubscribed'
end
protected
# rubocop: disable CodeReuse/ActiveRecord
def get_user
@email = Base64.urlsafe_decode64(params[:email])
User.find_by(email: @email)
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
...@@ -22,6 +22,7 @@ class Notify < ApplicationMailer ...@@ -22,6 +22,7 @@ class Notify < ApplicationMailer
include Emails::Reviews include Emails::Reviews
include Emails::ServiceDesk include Emails::ServiceDesk
include Emails::InProductMarketing include Emails::InProductMarketing
include Emails::AdminNotification
helper TimeboxesHelper helper TimeboxesHelper
helper MergeRequestsHelper helper MergeRequestsHelper
......
...@@ -1305,6 +1305,10 @@ class User < ApplicationRecord ...@@ -1305,6 +1305,10 @@ class User < ApplicationRecord
save if notification_email_changed? || public_email_changed? || commit_email_changed? save if notification_email_changed? || public_email_changed? || commit_email_changed?
end end
def admin_unsubscribe!
update_column :admin_email_unsubscribed_at, Time.current
end
def set_projects_limit def set_projects_limit
# `User.select(:id)` raises # `User.select(:id)` raises
# `ActiveModel::MissingAttributeError: missing attribute: projects_limit` # `ActiveModel::MissingAttributeError: missing attribute: projects_limit`
......
# frozen_string_literal: true # frozen_string_literal: true
get 'unsubscribes/:email', to: 'users/unsubscribes#show', as: :unsubscribe
post 'unsubscribes/:email', to: 'users/unsubscribes#create'
# Allows individual providers to be directed to a chosen controller # Allows individual providers to be directed to a chosen controller
# Call from inside devise_scope # Call from inside devise_scope
def override_omniauth(provider, controller, path_prefix = '/users/auth') def override_omniauth(provider, controller, path_prefix = '/users/auth')
......
# frozen_string_literal: true
class UnsubscribesController < ApplicationController
skip_before_action :authenticate_user!
feature_category :users
def show
@user = get_user
end
def create
@user = get_user
if @user
@user.admin_unsubscribe!
Notify.send_unsubscribed_notification(@user.id).deliver_later
end
redirect_to new_user_session_path, notice: 'You have been unsubscribed'
end
protected
# rubocop: disable CodeReuse/ActiveRecord
def get_user
@email = Base64.urlsafe_decode64(params[:email])
User.find_by(email: @email)
end
# rubocop: enable CodeReuse/ActiveRecord
end
...@@ -9,7 +9,6 @@ module EE ...@@ -9,7 +9,6 @@ module EE
# interaction between ActiveSupport::Concern and ActionMailer::Base # interaction between ActiveSupport::Concern and ActionMailer::Base
# See https://gitlab.com/gitlab-org/gitlab/issues/7846 # See https://gitlab.com/gitlab-org/gitlab/issues/7846
prepended do prepended do
include ::Emails::AdminNotification
include ::Emails::Epics include ::Emails::Epics
include ::Emails::Requirements include ::Emails::Requirements
include ::Emails::UserCap include ::Emails::UserCap
......
...@@ -308,10 +308,6 @@ module EE ...@@ -308,10 +308,6 @@ module EE
::Gitlab.config.ldap['sync_time'] ::Gitlab.config.ldap['sync_time']
end end
def admin_unsubscribe!
update_column :admin_email_unsubscribed_at, Time.current
end
override :allow_password_authentication_for_web? override :allow_password_authentication_for_web?
def allow_password_authentication_for_web?(*) def allow_password_authentication_for_web?(*)
return false if group_managed_account? return false if group_managed_account?
......
# frozen_string_literal: true # frozen_string_literal: true
get 'unsubscribes/:email', to: 'unsubscribes#show', as: :unsubscribe
post 'unsubscribes/:email', to: 'unsubscribes#create'
devise_scope :user do devise_scope :user do
get '/users/auth/kerberos_spnego/negotiate' => 'omniauth_kerberos_spnego#negotiate' get '/users/auth/kerberos_spnego/negotiate' => 'omniauth_kerberos_spnego#negotiate'
end end
......
...@@ -347,32 +347,6 @@ RSpec.describe Notify do ...@@ -347,32 +347,6 @@ RSpec.describe Notify do
end end
end end
describe 'admin notification' do
let(:example_site_path) { root_path }
let(:user) { create(:user) }
subject { @email = described_class.send_admin_notification(user.id, 'Admin announcement', 'Text') }
it 'is sent as the author' do
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq("GitLab")
expect(sender.address).to eq(gitlab_sender)
end
it 'is sent to recipient' do
is_expected.to deliver_to user.email
end
it 'has the correct subject' do
is_expected.to have_subject 'Admin announcement'
end
it 'includes unsubscribe link' do
unsubscribe_link = "http://localhost/unsubscribes/#{Base64.urlsafe_encode64(user.email)}"
is_expected.to have_body_text(unsubscribe_link)
end
end
describe 'new user was created via saml' do describe 'new user was created via saml' do
let(:group_member) { create(:group_member, user: create(:user, :unconfirmed)) } let(:group_member) { create(:group_member, user: create(:user, :unconfirmed)) }
let(:group) { group_member.source } let(:group) { group_member.source }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe UnsubscribesController do RSpec.describe Users::UnsubscribesController do
let!(:user) { create :user, email: 'me@example.com' } let!(:user) { create :user, email: 'me@example.com' }
describe "show" do describe "show" do
......
...@@ -1609,6 +1609,32 @@ RSpec.describe Notify do ...@@ -1609,6 +1609,32 @@ RSpec.describe Notify do
end end
end end
end end
describe 'admin notification' do
let(:example_site_path) { root_path }
let(:user) { create(:user) }
subject { @email = described_class.send_admin_notification(user.id, 'Admin announcement', 'Text') }
it 'is sent as the author' do
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq("GitLab")
expect(sender.address).to eq(gitlab_sender)
end
it 'is sent to recipient' do
is_expected.to deliver_to user.email
end
it 'has the correct subject' do
is_expected.to have_subject 'Admin announcement'
end
it 'includes unsubscribe link' do
unsubscribe_link = "http://localhost/unsubscribes/#{Base64.urlsafe_encode64(user.email)}"
is_expected.to have_body_text(unsubscribe_link)
end
end
end end
describe 'confirmation if email changed' do describe 'confirmation if email changed' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment