Sha256: 3a2f4be6ee8b4b81b1ceb6705cad7095ecb93f3f7bd55b648ce0ecf0943e3594

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Decidim
  module Devise
    # This controller customizes the behaviour of Devise::Invitable.
    class InvitationsController < ::Devise::InvitationsController
      include Decidim::DeviseControllers
      include NeedsTosAccepted

      helper Decidim::PasswordsHelper

      before_action :configure_permitted_parameters

      # We do not users to create invitations, so we just redirect them to the
      # homepage.
      def authenticate_inviter!
        redirect_to root_path
      end

      # Overwrite the method that returns the path after a user accepts an
      # invitation. Using the param `invite_redirect` we can redirect the user
      # to a custom path after it has accepted the invitation.
      def after_accept_path_for(resource)
        invite_redirect_path || after_sign_in_path_for(resource)
      end

      # When a managed user accepts the invitation is promoted to non-managed user.
      def accept_resource
        resource = resource_class.accept_invitation!(update_resource_params)

        if resource.valid? && resource.invitation_accepted?
          resource.update!(newsletter_notifications_at: Time.current) if update_resource_params[:newsletter_notifications]
          resource.update!(managed: false) if resource.managed?
          resource.update!(accepted_tos_version: resource.organization.tos_version)
        end

        resource
      end

      protected

      def invite_redirect_path
        path = params[:invite_redirect]
        return unless path
        return unless path.starts_with?(%r{^/[a-z0-9]+})

        path
      end

      def configure_permitted_parameters
        devise_parameter_sanitizer.permit(:accept_invitation, keys: [:nickname, :tos_agreement, :newsletter_notifications])
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/controllers/decidim/devise/invitations_controller.rb
decidim-core-0.29.0 app/controllers/decidim/devise/invitations_controller.rb
decidim-core-0.29.0.rc4 app/controllers/decidim/devise/invitations_controller.rb
decidim-core-0.29.0.rc3 app/controllers/decidim/devise/invitations_controller.rb
decidim-core-0.29.0.rc2 app/controllers/decidim/devise/invitations_controller.rb
decidim-core-0.29.0.rc1 app/controllers/decidim/devise/invitations_controller.rb