Allow users to update their OpenPGP pubkey
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -25,7 +25,8 @@ class SettingsController < ApplicationController
|
||||
def update
|
||||
@user.preferences.merge!(user_params[:preferences] || {})
|
||||
@user.display_name = user_params[:display_name]
|
||||
@user.avatar_new = user_params[:avatar]
|
||||
@user.avatar_new = user_params[:avatar]
|
||||
@user.pgp_pubkey = user_params[:pgp_pubkey]
|
||||
|
||||
if @user.save
|
||||
if @user.display_name && (@user.display_name != @user.ldap_entry[:display_name])
|
||||
@@ -36,6 +37,10 @@ class SettingsController < ApplicationController
|
||||
LdapManager::UpdateAvatar.call(dn: @user.dn, file: @user.avatar_new)
|
||||
end
|
||||
|
||||
if @user.pgp_pubkey && (@user.pgp_pubkey != @user.ldap_entry[:pgp_key])
|
||||
UserManager::UpdatePgpKey.call(user: @user)
|
||||
end
|
||||
|
||||
redirect_to setting_path(@settings_section), flash: {
|
||||
success: 'Settings saved.'
|
||||
}
|
||||
@@ -157,7 +162,8 @@ class SettingsController < ApplicationController
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(
|
||||
:display_name, :avatar, preferences: UserPreferences.pref_keys
|
||||
:display_name, :avatar, :pgp_pubkey,
|
||||
preferences: UserPreferences.pref_keys
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class User < ApplicationRecord
|
||||
|
||||
validate :acceptable_avatar
|
||||
|
||||
validate :acceptable_pgp_key_format, if: -> { defined?(@pgp_pubkey) && @pgp_pubkey != "" }
|
||||
validate :acceptable_pgp_key_format, if: -> { defined?(@pgp_pubkey) && @pgp_pubkey.present? }
|
||||
|
||||
#
|
||||
# Scopes
|
||||
|
||||
16
app/services/ldap_manager/update_pgp_key.rb
Normal file
16
app/services/ldap_manager/update_pgp_key.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
module LdapManager
|
||||
class UpdatePgpKey < LdapManagerService
|
||||
def initialize(dn:, pubkey:)
|
||||
@dn = dn
|
||||
@pubkey = pubkey
|
||||
end
|
||||
|
||||
def call
|
||||
if @pubkey.present?
|
||||
replace_attribute @dn, :pgpKey, @pubkey
|
||||
else
|
||||
delete_attribute @dn, :pgpKey
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
24
app/services/user_manager/update_pgp_key.rb
Normal file
24
app/services/user_manager/update_pgp_key.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
module UserManager
|
||||
class UpdatePgpKey < UserManagerService
|
||||
def initialize(user:)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
if @user.pgp_pubkey.blank?
|
||||
@user.update! pgp_fpr: nil
|
||||
else
|
||||
result = GPGME::Key.import(@user.pgp_pubkey)
|
||||
|
||||
if result.imports.present?
|
||||
@user.update! pgp_fpr: result.imports.first.fpr
|
||||
else
|
||||
# TODO notify Sentry, user
|
||||
raise "Failed to import OpenPGP pubkey"
|
||||
end
|
||||
end
|
||||
|
||||
LdapManager::UpdatePgpKey.call(dn: @user.dn, pubkey: @user.pgp_pubkey)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,6 @@
|
||||
<%= tag.section data: {
|
||||
controller: "settings--account--email",
|
||||
"settings--account--email-validation-failed-value": @validation_errors.present?
|
||||
"settings--account--email-validation-failed-value": @validation_errors&.[](:email)&.present?
|
||||
} do %>
|
||||
<h3>E-Mail</h3>
|
||||
<%= form_for(@user, url: update_email_settings_path, method: "post") do |f| %>
|
||||
@@ -23,7 +23,7 @@
|
||||
</span>
|
||||
</button>
|
||||
</p>
|
||||
<% if @validation_errors.present? && @validation_errors[:email].present? %>
|
||||
<% if @validation_errors&.[](:email)&.present? %>
|
||||
<p class="error-msg"><%= @validation_errors[:email].first %></p>
|
||||
<% end %>
|
||||
<div class="initial-hidden">
|
||||
@@ -41,10 +41,33 @@
|
||||
<% end %>
|
||||
<section>
|
||||
<h3>Password</h3>
|
||||
<p class="mb-8">Use the following button to request an email with a password reset link:</p>
|
||||
<p class="mb-6">Use the following button to request an email with a password reset link:</p>
|
||||
<%= form_with(url: reset_password_settings_path, method: :post) do %>
|
||||
<p>
|
||||
<%= submit_tag("Send me a password reset link", class: 'btn-md btn-gray w-full sm:w-auto') %>
|
||||
</p>
|
||||
<% end %>
|
||||
</section>
|
||||
<%= form_for(@user, url: setting_path(:account), html: { :method => :put }) do |f| %>
|
||||
<section class="!pt-8 sm:!pt-12">
|
||||
<h3>OpenPGP</h3>
|
||||
<ul role="list">
|
||||
<%= render FormElements::FieldsetComponent.new(
|
||||
title: "Public key",
|
||||
description: "Your OpenPGP public key in ASCII Armor format ([example])"
|
||||
) do %>
|
||||
<%= f.text_area :pgp_pubkey,
|
||||
value: @user.pgp_pubkey,
|
||||
class: "h-24 w-full" %>
|
||||
<% if @validation_errors&.[](:pgp_pubkey)&.present? %>
|
||||
<p class="error-msg">This <%= @validation_errors[:pgp_pubkey].first %></p>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
</section>
|
||||
<section>
|
||||
<p class="pt-6 border-t border-gray-200 text-right">
|
||||
<%= f.submit 'Save', class: "btn-md btn-blue w-full md:w-auto" %>
|
||||
</p>
|
||||
</section>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user