Skip to content
Extraits de code Groupes Projets
invitations_controller.rb 1,87 ko
Newer Older
ilya's avatar
ilya a validé
#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

class InvitationsController < Devise::InvitationsController
  before_filter :check_token, :only => [:edit] 

ilya's avatar
ilya a validé
  def create
      if current_user.invites == 0
        flash[:error] = I18n.t 'invitations.create.no_more'
        redirect_to :back
        return
      end
      params[:user][:aspect_id] = params[:user].delete(:aspects)
      message = params[:user].delete(:invite_messages)
      params[:user][:invite_message] = message unless message == ""
      emails = params[:user][:email].split(/, */)

maxwell's avatar
maxwell a validé
      good_emails, bad_emails = emails.partition{|e| e.try(:match, Devise.email_regexp)}

      good_emails.each{|e| Resque.enqueue(Jobs::InviteUser, current_user.id, params[:user].merge({:email => e}))}

      if bad_emails.any?
        flash[:error] = I18n.t('invitations.create.sent') + good_emails.join(', ') + " "+ I18n.t('invitations.create.rejected') + bad_emails.join(', ')
maxwell's avatar
maxwell a validé
        flash[:notice] = I18n.t('invitations.create.sent') + good_emails.join(', ')
maxwell's avatar
maxwell a validé

ilya's avatar
ilya a validé
  def update
    begin
      user = User.find_by_invitation_token(params[:user][:invitation_token])
maxwell's avatar
maxwell a validé
      user.seed_aspects
      user.accept_invitation!(params[:user])
ilya's avatar
ilya a validé
    rescue MongoMapper::DocumentNotValid => e
      user = nil
      flash[:error] = e.message
    end
    if user
      flash[:notice] = I18n.t 'registrations.create.success'
      sign_in_and_redirect(:user, user)
    else
      redirect_to accept_user_invitation_path(
        :invitation_token => params[:user][:invitation_token])
ilya's avatar
ilya a validé
    end
  end

  protected

  def check_token
    if User.find_by_invitation_token(params[:invitation_token]).nil?
MrZYX's avatar
MrZYX a validé
      flash[:error] = I18n.t 'invitations.check_token.not_found'
      redirect_to root_url
    end
  end
ilya's avatar
ilya a validé
end