<commit_before>class InviteController < PublicController

  needs_profile
  before_filter :login_required
  before_filter :check_permissions_to_invite

  def invite_friends
    @import_from = params[:import_from] || "manual"
    @mail_template = params[:mail_template] || environment.invitation_mail_template(profile)
    if request.post?
      contact_list = ContactList.create
      Delayed::Job.enqueue GetEmailContactsJob.new(@import_from, params[:login], params[:password], contact_list.id) if @import_from != 'manual'
      redirect_to :action => 'select_friends', :contact_list => contact_list.id, :import_from => @import_from
    end
  end

  def select_friends
    @contact_list = ContactList.find(params[:contact_list])
    @mail_template = params[:mail_template] || environment.invitation_mail_template(profile)
    @import_from = params[:import_from] || "manual"
    if request.post?
      manual_import_addresses = params[:manual_import_addresses]
      webmail_import_addresses = params[:webmail_import_addresses]
      contacts_to_invite = Invitation.join_contacts(manual_import_addresses, webmail_import_addresses)
      if !contacts_to_invite.empty?
        Delayed::Job.enqueue InvitationJob.new(current_user.person.id, contacts_to_invite, params[:mail_template], profile.id, @contact_list.id, locale)
        session[:notice] = _('Your invitations are being sent.')
        if profile.person?
          redirect_to :controller => 'profile', :action => 'friends'
        else
          redirect_to :controller => 'profile', :action => 'members'
        end
        return
      else
        session[:notice] = _('Please enter a valid email address.')
      end
      @manual_import_addresses = manual_import_addresses || ""
      @webmail_import_addresses = webmail_import_addresses || []
    end
  end

  def invitation_data
    contact_list = ContactList.find(params[:contact_list])
    render :text => contact_list.data.to_json, :layout => false, :content_type => "application/javascript"
  end

  def add_contact_list
    contact_list = ContactList.find(params[:contact_list])
    contacts = contact_list.list
    render :partial => 'invite/contact_list', :locals => {:contacts => contacts}
  end

  def cancel_fetching_emails
    contact_list = ContactList.find(params[:contact_list])
    contact_list.destroy
    redirect_to :action => 'invite_friends'
  end

  def invite_registered_friend
    contacts_to_invite = params['q'].split(',')
    if !contacts_to_invite.empty?
      Delayed::Job.enqueue InvitationJob.new(current_user.person.id, contacts_to_invite, '', profile.id, nil, locale)
      session[:notice] = _('Your invitations are being sent.')
      if profile.person?
        redirect_to :controller => 'profile', :action => 'friends'
      else
        redirect_to :controller => 'profile', :action => 'members'
      end
      return
    else
      redirect_to :action => 'invite_friends'
      session[:notice] = _('Please enter a valid profile.')
    end
  end

  include InviteHelper
  helper :invite

  def search_friend
    fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s }
    values = ["%#{params['q']}%"] * fields.count
    render :text => environment.people.find(:all, :joins => ['INNER JOIN users ON profiles.user_id=users.id'], :conditions => [fields.map {|field| "LOWER(#{field}) LIKE ?"}.join(' OR '), *values]).
      select {|person| !profile.members.include?(person) }.
      map {|person| {:id => person.id, :name => person.name} }.to_json
  end

  protected

  def check_permissions_to_invite
    if profile.person? and !current_user.person.has_permission?(:manage_friends, profile) or
      profile.community? and !current_user.person.has_permission?(:invite_members, profile)
      render_access_denied
    end
  end

end<commit_msg>replaces current_user.person for a more clean alternative<commit_after>class InviteController < PublicController

  needs_profile
  before_filter :login_required
  before_filter :check_permissions_to_invite

  def invite_friends
    @import_from = params[:import_from] || "manual"
    @mail_template = params[:mail_template] || environment.invitation_mail_template(profile)
    if request.post?
      contact_list = ContactList.create
      Delayed::Job.enqueue GetEmailContactsJob.new(@import_from, params[:login], params[:password], contact_list.id) if @import_from != 'manual'
      redirect_to :action => 'select_friends', :contact_list => contact_list.id, :import_from => @import_from
    end
  end

  def select_friends
    @contact_list = ContactList.find(params[:contact_list])
    @mail_template = params[:mail_template] || environment.invitation_mail_template(profile)
    @import_from = params[:import_from] || "manual"
    if request.post?
      manual_import_addresses = params[:manual_import_addresses]
      webmail_import_addresses = params[:webmail_import_addresses]
      contacts_to_invite = Invitation.join_contacts(manual_import_addresses, webmail_import_addresses)
      if !contacts_to_invite.empty?
        Delayed::Job.enqueue InvitationJob.new(user.id, contacts_to_invite, params[:mail_template], profile.id, @contact_list.id, locale)
        session[:notice] = _('Your invitations are being sent.')
        if profile.person?
          redirect_to :controller => 'profile', :action => 'friends'
        else
          redirect_to :controller => 'profile', :action => 'members'
        end
        return
      else
        session[:notice] = _('Please enter a valid email address.')
      end
      @manual_import_addresses = manual_import_addresses || ""
      @webmail_import_addresses = webmail_import_addresses || []
    end
  end

  def invitation_data
    contact_list = ContactList.find(params[:contact_list])
    render :text => contact_list.data.to_json, :layout => false, :content_type => "application/javascript"
  end

  def add_contact_list
    contact_list = ContactList.find(params[:contact_list])
    contacts = contact_list.list
    render :partial => 'invite/contact_list', :locals => {:contacts => contacts}
  end

  def cancel_fetching_emails
    contact_list = ContactList.find(params[:contact_list])
    contact_list.destroy
    redirect_to :action => 'invite_friends'
  end

  def invite_registered_friend
    contacts_to_invite = params['q'].split(',')
    if !contacts_to_invite.empty?
      Delayed::Job.enqueue InvitationJob.new(user.id, contacts_to_invite, '', profile.id, nil, locale)
      session[:notice] = _('Your invitations are being sent.')
      if profile.person?
        redirect_to :controller => 'profile', :action => 'friends'
      else
        redirect_to :controller => 'profile', :action => 'members'
      end
      return
    else
      redirect_to :action => 'invite_friends'
      session[:notice] = _('Please enter a valid profile.')
    end
  end

  include InviteHelper
  helper :invite

  def search_friend
    fields = %w[name identifier email] + plugins_options.map {|field| field[:field].to_s }
    values = ["%#{params['q']}%"] * fields.count
    render :text => environment.people.find(:all, :joins => ['INNER JOIN users ON profiles.user_id=users.id'], :conditions => [fields.map {|field| "LOWER(#{field}) LIKE ?"}.join(' OR '), *values]).
      select {|person| !profile.members.include?(person) }.
      map {|person| {:id => person.id, :name => person.name} }.to_json
  end

  protected

  def check_permissions_to_invite
    if profile.person? and !user.has_permission?(:manage_friends, profile) or
      profile.community? and !user.has_permission?(:invite_members, profile)
      render_access_denied
    end
  end

end
{'fraction_non_alphanumeric': 0.1161298769311338, 'fraction_numerical': 0.0, 'mean_word_length': 4.052248677248677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4218520', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2293, 'n_words': 487}