generated from OpenSourcePolitics/decidim-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backport: Export authorization metadata to user extended data
- Loading branch information
Showing
7 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class AuthorizationDataToUserDataJob < ApplicationJob | ||
queue_as :exports | ||
|
||
def perform(*args) | ||
Decidim::AuthorizationDataToUserDataService.run(*args) | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
app/services/decidim/authorization_data_to_user_data_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
class AuthorizationDataToUserDataService | ||
def self.run(**args) | ||
new(**args).execute | ||
end | ||
|
||
def initialize(**args) | ||
@name = args[:name] | ||
@user = args[:user] | ||
end | ||
|
||
def execute | ||
Decidim::Authorization.find_each(filter) do |authorization| | ||
authorization.user.update(extended_data: authorization.user.extended_data.merge({ @name.to_s => authorization.metadata })) | ||
end | ||
end | ||
|
||
def filter | ||
@filter ||= { | ||
name: @name, | ||
user: @user | ||
}.compact | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
module AuthorizationExtends | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
after_commit :export_to_user_extended_data, if: proc { |authorization| | ||
Rails.application.secrets.dig(:decidim, :export_data_to_userdata_enabled_for)&.split(",")&.include?(authorization.name) | ||
} | ||
|
||
def export_to_user_extended_data | ||
Decidim::AuthorizationDataToUserDataService.run(name: name, user: user) | ||
end | ||
end | ||
end | ||
|
||
Decidim::Authorization.include(AuthorizationExtends) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :authorizations do | ||
task export_to_user_extended_data: :environment do | ||
name = ENV["AUTHORIZATION_HANDLE_NAME"].presence | ||
raise "AUTHORIZATION_HANDLE_NAME is blank." if name.blank? | ||
|
||
raise "No data found for authorization handler name '#{name}'" unless Decidim::Authorization.exists?(name: name) | ||
|
||
AuthorizationDataToUserDataJob.perform_later(name: name) | ||
end | ||
end |