engine/lib/locomotive/hosting/bushido/hooks.rb

58 lines
2.2 KiB
Ruby
Raw Normal View History

require 'bushido'
module Locomotive
module Hosting
module Bushido
module Enabler
module ClassMethods
def subscribe_to_events
::Bushido::Data.listen('app.claimed') do |event|
Locomotive.log "Saving #{Account.first.inspect} with incoming data #{event.inspect}"
Locomotive.log "Devise username column: #{::Devise.cas_username_column}="
Locomotive.log "Setting username to: #{event['data'].try(:[], 'ido_id')}"
account = Account.first
account.email = event['data'].try(:[], 'email')
account.name = account.email.split('@').first
account.send("#{::Devise.cas_username_column}=".to_sym, event['data'].try(:[], 'ido_id'))
account.save
end
::Bushido::Data.listen('user.added') do |event|
Locomotive.log "Adding a new account with incoming data #{event.inspect}"
Locomotive.log "Devise username column: #{::Devise.cas_username_column}="
Locomotive.log "Setting username to: #{event['data'].try(:[], 'ido_id')}"
account = Account.new(:email => event['data'].try(:[], 'email'))
account.name = account.email.split('@').first
account.send("#{::Devise.cas_username_column}=".to_sym, event['data'].try(:[], 'ido_id'))
account.save
Site.all.each do |site|
site.memberships.create(:account => account) if account.errors.empty?
end
end
::Bushido::Data.listen('user.removed') do |event|
Locomotive.log "Removing account based on incoming data #{event.inspect}"
Locomotive.log "Devise username column: #{::Devise.cas_username_column}="
Locomotive.log "Removing username: #{event['data'].try(:[], 'ido_id')}"
ido_id = event['data'].try(:[], 'ido_id')
ido_id and
2011-11-16 20:04:51 +00:00
Account.exists?(:conditions => {::Devise.cas_username_column => ido_id}) and
Account.where(::Devise.cas_username_column => ido_id).destroy
end
end
end
end
end
end
end