diff options
Diffstat (limited to 'app/services/keys')
-rw-r--r-- | app/services/keys/base_service.rb | 3 | ||||
-rw-r--r-- | app/services/keys/create_service.rb | 3 | ||||
-rw-r--r-- | app/services/keys/expiry_notification_service.rb | 42 |
3 files changed, 46 insertions, 2 deletions
diff --git a/app/services/keys/base_service.rb b/app/services/keys/base_service.rb index 113e22b01ce..9b238e2f176 100644 --- a/app/services/keys/base_service.rb +++ b/app/services/keys/base_service.rb @@ -5,7 +5,8 @@ module Keys attr_accessor :user, :params def initialize(user, params = {}) - @user, @params = user, params + @user = user + @params = params @ip_address = @params.delete(:ip_address) end diff --git a/app/services/keys/create_service.rb b/app/services/keys/create_service.rb index c256de7b35d..c1c3ef8792f 100644 --- a/app/services/keys/create_service.rb +++ b/app/services/keys/create_service.rb @@ -5,7 +5,8 @@ module Keys attr_accessor :current_user def initialize(current_user, params = {}) - @current_user, @params = current_user, params + @current_user = current_user + @params = params @ip_address = @params.delete(:ip_address) @user = params.delete(:user) || current_user end diff --git a/app/services/keys/expiry_notification_service.rb b/app/services/keys/expiry_notification_service.rb new file mode 100644 index 00000000000..b486f77ced2 --- /dev/null +++ b/app/services/keys/expiry_notification_service.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Keys + class ExpiryNotificationService < ::Keys::BaseService + attr_accessor :keys, :expiring_soon + + def initialize(user, params) + @keys = params[:keys] + @expiring_soon = params[:expiring_soon] + + super + end + + def execute + return unless allowed? + + if expiring_soon + trigger_expiring_soon_notification + else + trigger_expired_notification + end + end + + private + + def allowed? + user.can?(:receive_notifications) + end + + def trigger_expiring_soon_notification + notification_service.ssh_key_expiring_soon(user, keys.map(&:fingerprint)) + + keys.update_all(before_expiry_notification_delivered_at: Time.current.utc) + end + + def trigger_expired_notification + notification_service.ssh_key_expired(user, keys.map(&:fingerprint)) + + keys.update_all(expiry_notification_delivered_at: Time.current.utc) + end + end +end |