Skip to content
Extraits de code Groupes Projets
invitations_controller.rb 3,06 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]
    @sent_invitations = current_user.invitations_from_me.includes(:recipient)
ilya's avatar
ilya a validé
  def create
    if !AppConfig[:open_invitations] && current_user.invites == 0
        flash[:error] = I18n.t 'invitations.create.no_more'
        redirect_to :back
        return
      end
maxwell's avatar
maxwell a validé
      aspect = params[:user].delete(:aspects)
      message = params[:user].delete(:invite_messages)
      emails = params[:user][:email].to_s.gsub(/\s/, '').split(/, */)
maxwell's avatar
maxwell a validé
      good_emails, bad_emails = emails.partition{|e| e.try(:match, Devise.email_regexp)}

MrZYX's avatar
MrZYX a validé
      if good_emails.include?(current_user.email)
        if good_emails.length == 1
          flash[:error] = I18n.t 'invitations.create.own_address'
          redirect_to :back
          return
        else
          bad_emails.push(current_user.email)
          good_emails.delete(current_user.email)
        end
      end

Raphael Sofaer's avatar
Raphael Sofaer a validé
      good_emails.each{|e| Resque.enqueue(Job::InviteUserByEmail, current_user.id, e, aspect, message)}
maxwell's avatar
maxwell a validé

      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
      invitation_token = params[:user][:invitation_token]
      if invitation_token.nil? || invitation_token.blank?
        raise I18n.t('invitations.check_token.not_found')
      user = User.find_by_invitation_token(params[:user][:invitation_token])
      user.accept_invitation!(params[:user])
      user.seed_aspects
Raphael Sofaer's avatar
Raphael Sofaer a validé
    rescue Exception => e #What exception is this trying to rescue?  If it is ActiveRecord::NotFound, we should say so.
ilya's avatar
ilya a validé
      user = nil
      record = e.record
      record.errors.delete(:person)

      flash[:error] = record.errors.full_messages.join(", ")
ilya's avatar
ilya a validé
    end
maxwell's avatar
maxwell a validé

ilya's avatar
ilya a validé
    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
zhitomirskiyi's avatar
zhitomirskiyi a validé
  def resend
    invitation = current_user.invitations_from_me.where(:id => params[:id]).first
    if invitation
      Resque.enqueue(Job::ResendInvitation, invitation.id)
Raphael Sofaer's avatar
Raphael Sofaer a validé
      flash[:notice] = I18n.t('invitations.create.sent') + invitation.recipient.email
zhitomirskiyi's avatar
zhitomirskiyi a validé
    redirect_to :back
  end

Maxwell Salzberg's avatar
Maxwell Salzberg a validé
  def email
    @invs = []
Maxwell Salzberg's avatar
Maxwell Salzberg a validé
    @resource = User.find_by_invitation_token(params[:invitation_token]) 
Maxwell Salzberg's avatar
Maxwell Salzberg a validé
    render 'devise/mailer/invitation_instructions', :layout => false
  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