lessonly / scim_rails

SCIM Adapter for Rails.
MIT License
68 stars 76 forks source link

Support callbacks for SCIM-based user operations #51

Closed stevegrossi closed 2 years ago

stevegrossi commented 2 years ago

The Problem

At Lessonly, we have a need to run other code after a user is created or updated via scim_rails. For instance, when a user is created via SCIM we might want to send them a welcome email. Currently, this does not appear possible without an after_create callback on the User model, which is inadvisable because there are other situations in which we don't want to send the welcome email after a user is created.

Potential Solutions

Option A: an "after" callback in scim_rails

For example, we might add a configuration setting like so:

# config/scim_rails.rb
config.after_user_create = ->(user) { UserMailer.welcome_email(user).deliver_later }

This would be run (if configured) after the ScimUsersController#create controller action. It would likely make sense to add similar callbacks for after_user_update, etc.

Option B: monkey-patching (with no changes to scim_rails)

Though I suppose monkey-patching that controller would also be an option for end-users if there are reasons not to add this functionality to scim_rails itself, e.g.

# config/scim_rails_addons.rb
module ScimRails
  class ScimUsersController
    def create
      super
      UserMailer.welcome_email(user).deliver_later
    end
  end
end

however that would be more brittle than supporting this functionality natively in scim_rails.

Option C: Use a custom scim_users_model with callbacks

A conversation with @emmoistner prompted what I think is an even better solution than the above which would require no changes to scim_rails. Since config.scim_users_model is already configurable, a solution for adding behavior on create/update that we want to keep local to SCIM would be a custom superclass, e.g.

# config/initializers/scim_rails_config.rb
config.scim_users_model = "ScimUser"

# app/models/scim_user.rb
class ScimUser < User
  after_create :send_welcome_email

  def send_welcome_email
    UserMailer.welcome_email(self).deliver_later
  end
end
stevegrossi commented 2 years ago

I've added a third option above that I think would meet this need simply and flexibly with no additional changes to scim_rails 🙏🏻

stevegrossi commented 2 years ago

Closing this as the third option (which requires no changes to this library) has worked nicely for us.