<commit_before>require 'securerandom'

class UserDelegatesController < ApplicationController
  def index
    user = User.find(params[:user_id], :include => :created_users)
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if user != current_user || (!user.role?(:admin) && !user.role?(:respondent))
    @user_delegates = user.user_delegates
    @user_delegators = user.user_delegators
  end

  def show
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if !current_user || (!current_user.role?(:admin) && !current_user.role?(:respondent))
    @user_delegate = UserDelegate.find(params[:id], :include => :delegations)
  end

  def new
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if !current_user || (!current_user.role?(:admin) && !current_user.role?(:respondent))
    user = User.find(params[:user_id])
    @delegates = User.delegates
    @user_delegate = user.user_delegates.new
  end

  def create
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if !current_user || (!current_user.role?(:admin) && !current_user.role?(:respondent))
    success = false
    @user_delegate = UserDelegate.new(params[:user_delegate])
    @user_delegate.user = current_user
    if @user_delegate.delegate_id
      if UserDelegate.find_by_user_id_and_delegate_id(@user_delegate.user_id, @user_delegate.delegate_id)
        flash[:error] = t('user_delegates.already_a_delegate')
      elsif @user_delegate.save
        UserMailer.delegate_request(@user_delegate).deliver
      end
      success = true
    else
      delegate = User.find_by_email(params[:delegate_email])
      role = Role.find_by_name("delegate")
      if delegate
        @user_delegate.delegate_id = delegate.id
        delegate.roles << role unless delegate.roles.include?(role)
        delegate.save
        if @user_delegate.save
          UserMailer.delegate_request(@user_delegate).deliver
          success = true
        elsif UserDelegate.find_by_user_id_and_delegate_id(@user_delegate.user_id, @user_delegate.delegate_id)
          flash[:error] = t('user_delegates.email_already_a_delegate')
          success = true
        end
      else
        password = <PASSWORD>(8)
        delegate = User.new(:email => params[:delegate_email], :password => password, :password_confirmation => password,
        :language => (params[:delegate][:language] || "en"), :first_name => params[:delegate_first_name], :last_name => params[:delegate_last_name])
        delegate.roles << role
        if delegate.save
          @user_delegate.delegate_id = delegate.id
          if @user_delegate.save
            UserMailer.delegate_registration(delegate, password, "http://#{request.host}/").deliver
            success = true
          end
        end
      end
    end
    if success
      redirect_to user_user_delegates_path(current_user)
    else
      flash[:error] = delegate.errors.messages.map{ |key, value| "#{key} #{value}" }.join("<br/>")
      render :action => "new"
    end
  end

  def delete
  end

  def destroy
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if !current_user || (!current_user.role?(:admin) && !current_user.role?(:respondent))
    current_user.user_delegates.destroy UserDelegate.find params[:id]
    respond_to do |format|
      flash[:notice] = "Delegate has been successfully deleted."
      format.html { redirect_to(user_user_delegates_path(current_user)) }
      format.xml { head :ok }
    end
  end

  def dashboard
    @delegate = User.find(params[:id], :include => [:delegated_tasks])
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if @delegate != current_user
    @delegated_tasks = @delegate.delegated_tasks.reject{ |d| d.questionnaire.nil? }
  end
end<commit_msg>Reuse same code for permissions in controller<commit_after>require 'securerandom'

class UserDelegatesController < ApplicationController

  before_filter :check_permissions, except: [:index, :delete, :dashboard]

  def index
    user = User.find(params[:user_id], :include => :created_users)
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if user != current_user || (!user.role?(:admin) && !user.role?(:respondent))
    @user_delegates = user.user_delegates
    @user_delegators = user.user_delegators
  end

  def show
    @user_delegate = UserDelegate.find(params[:id], :include => :delegations)
  end

  def new
    user = User.find(params[:user_id])
    @delegates = User.delegates
    @user_delegate = user.user_delegates.new
  end

  def create
    success = false
    @user_delegate = UserDelegate.new(params[:user_delegate])
    @user_delegate.user = current_user
    if @user_delegate.delegate_id
      if UserDelegate.find_by_user_id_and_delegate_id(@user_delegate.user_id, @user_delegate.delegate_id)
        flash[:error] = t('user_delegates.already_a_delegate')
      elsif @user_delegate.save
        UserMailer.delegate_request(@user_delegate).deliver
      end
      success = true
    else
      delegate = User.find_by_email(params[:delegate_email])
      role = Role.find_by_name("delegate")
      if delegate
        @user_delegate.delegate_id = delegate.id
        delegate.roles << role unless delegate.roles.include?(role)
        delegate.save
        if @user_delegate.save
          UserMailer.delegate_request(@user_delegate).deliver
          success = true
        elsif UserDelegate.find_by_user_id_and_delegate_id(@user_delegate.user_id, @user_delegate.delegate_id)
          flash[:error] = t('user_delegates.email_already_a_delegate')
          success = true
        end
      else
        password = <PASSWORD>(8)
        delegate = User.new(:email => params[:delegate_email], :password => password, :password_confirmation => password,
        :language => (params[:delegate][:language] || "en"), :first_name => params[:delegate_first_name], :last_name => params[:delegate_last_name])
        delegate.roles << role
        if delegate.save
          @user_delegate.delegate_id = delegate.id
          if @user_delegate.save
            UserMailer.delegate_registration(delegate, password, "http://#{request.host}/").deliver
            success = true
          end
        end
      end
    end
    if success
      redirect_to user_user_delegates_path(current_user)
    else
      flash[:error] = delegate.errors.messages.map{ |key, value| "#{key} #{value}" }.join("<br/>")
      render :action => "new"
    end
  end

  def delete
  end

  def destroy
    current_user.user_delegates.destroy UserDelegate.find params[:id]
    respond_to do |format|
      flash[:notice] = "Delegate has been successfully deleted."
      format.html { redirect_to(user_user_delegates_path(current_user)) }
      format.xml { head :ok }
    end
  end

  def dashboard
    @delegate = User.find(params[:id], :include => [:delegated_tasks])
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if @delegate != current_user
    @delegated_tasks = @delegate.delegated_tasks.reject{ |d| d.questionnaire.nil? }
  end

  private

  def check_permissions
    raise CanCan::AccessDenied.new(t('flash_messages.not_authorized')) if !current_user || (!current_user.role?(:admin) && !current_user.role?(:respondent))
  end
end
{'fraction_non_alphanumeric': 0.11844820470491127, 'fraction_numerical': 0.00027514100976750583, 'mean_word_length': 3.711600777705768, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2756490', 'n_tokens_mistral': 2204, 'n_tokens_neox': 2214, 'n_words': 403}