<commit_before>class UsersController < ApplicationController

  before_filter :customer_login_required, :except => [:new, :forgotten_password, :accept_invitation, :validate_email, :password_reset]
  before_filter :customer_admin_required, :only => [:index, :create]


  # This is misleadingly placed. Should probably be in the customers (or customer_signup) controller
  # it is step two of the action that started by finding the subdomain, email and invitation code in 
  # customers/new (/signup)
  def new

    if !(request.post? || (current_subdomain && params[:email] && params[:invitation_code]))
      redirect_to :subdomain => false, :controller => "customers", :action => "new"
    end
    
    # Do we have an existing user?
    @user = User.find_by_email((params[:email] || params[:user][:email] || '').downcase)
    if @user
      @new_user = false

      if request.post?
        if !@user.authenticated?(params[:user][:password])
          @user.errors.add :password, "Incorrect password"
        end
      end
    else
      @new_user = true

      @user = User.new(params[:user])
      @user.email ||= params[:email]

      # Can only happen if someone is mucking around
      if !@user.valid? && @user.errors[:email]
        flash[:error] = "Email address " + @user.errors[:email]
        redirect_to :subdomain => false, :controller => "customers", :action => "new"
        return
      end

      # Error messages only on posting the form
      @user.errors.clear if !request.post?
    end

    @customer = Customer.new(params[:customer])
    @customer.subdomain = current_subdomain

    # May happen on double-submission
    if !@customer.valid? && @customer.errors[:subdomain]
      flash[:error] = "Subdomain " + @customer.errors[:subdomain]
      redirect_to :subdomain => false, :controller => "customers", :action => "new"
      return
    end

    if !request.post?
      @customer.errors.clear
      return
    end

    # We need a token to do this for now
    @token = Token::Invitation.find_by_code params[:invitation_code]

    @token.transaction do
      if @token.valid_for?(@customer, @user)
        if ((@new_user && @user.register!) || @user.authenticated?(params[:user][:password])) && @customer.save
          link = CustomerUser.new(:customer => @customer, :user => @user)
          link.activate!
          link.grant_admin!

          Project.new(:customer => @customer, :name => @customer.subdomain.capitalize).save!
          @token.use!

          logout_keeping_session! if current_user
          self.current_user = @user 

          flash[:notice] = "Done!"
          redirect_to :controller => "customers", :action => "welcome"
        end
      else
        flash[:error] = "Token " + @token.errors_on_base
        redirect_to :subdomain => false, :controller => "customers", :action => "new"
      end
    end
  end

  # List of users for the current customer
  def index
    @users = current_customer.users.scoped(:order => 'name, email')
    @user = User.new
  end

  # Invite new user to join customer<commit_msg>Remove reference to Project (not applicable)<commit_after>class UsersController < ApplicationController

  before_filter :customer_login_required, :except => [:new, :forgotten_password, :accept_invitation, :validate_email, :password_reset]
  before_filter :customer_admin_required, :only => [:index, :create]


  # This is misleadingly placed. Should probably be in the customers (or customer_signup) controller
  # it is step two of the action that started by finding the subdomain, email and invitation code in 
  # customers/new (/signup)
  def new

    if !(request.post? || (current_subdomain && params[:email] && params[:invitation_code]))
      redirect_to :subdomain => false, :controller => "customers", :action => "new"
    end
    
    # Do we have an existing user?
    @user = User.find_by_email((params[:email] || params[:user][:email] || '').downcase)
    if @user
      @new_user = false

      if request.post?
        if !@user.authenticated?(params[:user][:password])
          @user.errors.add :password, "<PASSWORD>"
        end
      end
    else
      @new_user = true

      @user = User.new(params[:user])
      @user.email ||= params[:email]

      # Can only happen if someone is mucking around
      if !@user.valid? && @user.errors[:email]
        flash[:error] = "Email address " + @user.errors[:email]
        redirect_to :subdomain => false, :controller => "customers", :action => "new"
        return
      end

      # Error messages only on posting the form
      @user.errors.clear if !request.post?
    end

    @customer = Customer.new(params[:customer])
    @customer.subdomain = current_subdomain

    # May happen on double-submission
    if !@customer.valid? && @customer.errors[:subdomain]
      flash[:error] = "Subdomain " + @customer.errors[:subdomain]
      redirect_to :subdomain => false, :controller => "customers", :action => "new"
      return
    end

    if !request.post?
      @customer.errors.clear
      return
    end

    # We need a token to do this for now
    @token = Token::Invitation.find_by_code params[:invitation_code]

    @token.transaction do
      if @token.valid_for?(@customer, @user)
        if ((@new_user && @user.register!) || @user.authenticated?(params[:user][:password])) && @customer.save
          link = CustomerUser.new(:customer => @customer, :user => @user)
          link.activate!
          link.grant_admin!

          @token.use!

          logout_keeping_session! if current_user
          self.current_user = @user 

          flash[:notice] = "Done!"
          redirect_to :controller => "customers", :action => "welcome"
        end
      else
        flash[:error] = "Token " + @token.errors_on_base
        redirect_to :subdomain => false, :controller => "customers", :action => "new"
      end
    end
  end

  # List of users for the current customer
  def index
    @users = current_customer.users.scoped(:order => 'name, email')
    @user = User.new
  end

  # Invite new user to join customer
{'fraction_non_alphanumeric': 0.12673267326732673, 'fraction_numerical': 0.0, 'mean_word_length': 2.979645436638214, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 44, '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': '955080', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1752, 'n_words': 481}