Skip to content

Commit

Permalink
FIX: allow encrypt_pms_default to be null (#231)
Browse files Browse the repository at this point in the history
We should allow `encrypt_pms_default` to be null in case plugin is disabled or deleted.

In addition, fallback is added to use value from SiteSetting.
  • Loading branch information
lis2 committed Nov 24, 2022
1 parent 4e8a0a2 commit ee42134
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class AllowEncryptPmsDefaultToBeNull < ActiveRecord::Migration[7.0]
def up
change_column_null(:user_options, :encrypt_pms_default, true)
end

def down
change_column_null(:user_options, :encrypt_pms_default, false)
end
end
4 changes: 2 additions & 2 deletions plugin.rb
Expand Up @@ -314,7 +314,7 @@ class DiscourseEncrypt::Engine < Rails::Engine
end

add_to_serializer(:current_user, :encrypt_pms_default, false) do
object.user_option.encrypt_pms_default
object.user_option.encrypt_pms_default || SiteSetting.encrypt_pms_default
end

add_to_serializer(:current_user, :include_encrypt_pms_default?) do
Expand All @@ -330,7 +330,7 @@ class DiscourseEncrypt::Engine < Rails::Engine
end

add_to_serializer(:user_option, :encrypt_pms_default) do
object.encrypt_pms_default
object.encrypt_pms_default || SiteSetting.encrypt_pms_default
end

add_model_callback(:user_option, :before_create) do
Expand Down
11 changes: 10 additions & 1 deletion spec/serializers/current_user_serialier_spec.rb
Expand Up @@ -5,10 +5,19 @@
describe CurrentUserSerializer do
let(:user) { Fabricate(:user) }

it 'contains public and private key' do
it 'contains public, private key and encrypt_pms_default' do
UserEncryptionKey.create!(user_id: user.id, encrypt_public: "public key", encrypt_private: "private key")
SiteSetting.encrypt_pms_default = true
serialized = described_class.new(user, scope: Guardian.new(user), root: false).as_json
expect(serialized[:encrypt_public]).to eq("public key")
expect(serialized[:encrypt_private]).to eq("private key")
expect(serialized[:encrypt_pms_default]).to be true
end

it 'use SiteSetting as default when encrypt_pms_default is not set' do
user.user_option.update!(encrypt_pms_default: nil)
UserEncryptionKey.create!(user_id: user.id, encrypt_public: "public key", encrypt_private: "private key")
serialized = described_class.new(user, scope: Guardian.new(user), root: false).as_json
expect(serialized[:encrypt_pms_default]).to be false
end
end

0 comments on commit ee42134

Please sign in to comment.