<NME> requested_accounts_controller.rb
<BEF> # frozen_string_literal: true

#= Controller for requesting new wiki accounts and processing those requests
class RequestedAccountsController < ApplicationController
  respond_to :html
  before_action :set_course, except: [:index, :create_all_accounts]
  before_action :check_creation_permissions,
                only: %i[show create_accounts enable_account_requests destroy]

  # This creates (or updates) a RequestedAccount, which is a username and email
  # for a user who wants to create a wiki account (but may not be able to do so
  # because of a shared IP that has hit the new account limit).
  def request_account
    redirect_if_passcode_invalid { return }
    # If there is already a request for a certain username for this course, then
    # it's probably the same user who put in the wrong email the first time.
    # Just overwrite the email with the new one in this case.
    handle_existing_request { return }
    @requested = RequestedAccount.create(course: @course,
                                         username: params[:username],
                                         email: params[:email])
    handle_invalid_request { return }

    unless params[:create_account_now] == 'true'
      render json: { message: I18n.t('courses.new_account_submitted') }
      return
    end
    result = create_account(@requested)
    if result[:success]
      render json: { message: result.values.first }
    else
      render json: { message: result.values.first }, status: :internal_server_error
    end
  end

  # Sets the flag on a course so that clicking 'Sign Up' opens the Request Account
  # modal instead of redirecting to the mediawiki account creation flow.
  def enable_account_requests
    @course.flags[:register_accounts] = true
    @course.save
  end

  # List of requested accounts for a user's courses. @courses set in before action
  def index
    raise_unauthorized_exception unless user_signed_in? && current_user.admin?
    respond_to do |format|
      format.html do
        @courses = all_requested_accounts
        render
      end
      format.json { render json: { requested_accounts: RequestedAccount.any? } }
    end
  end

  # List of requested accounts for a course.
  def show; end

  def destroy
    # raise exception unless requestedaccount belongs to course
    requested_account = RequestedAccount.find_by(id: params[:id])
    raise_unauthorized_exception unless @course.id == requested_account.course_id
    requested_account.delete
    redirect_back fallback_location: '/'
  end

  # Try to create each of the requested accounts for a course, and show the
  # result for each.
  def create_accounts
    @results = []
    @course.requested_accounts.each do |requested_account|
      @results << create_account(requested_account)
    end
  end

  def create_all_accounts
    raise_unauthorized_exception unless user_signed_in? && current_user.admin?
    @courses = all_requested_accounts
    @results = RequestedAccount.all.map { |account| create_account(account) }

    render :index
  end

  private

  def all_requested_accounts
    RequestedAccount.all.includes(:course).group_by { |account| account.course.title }
  end

  def create_account(requested_account)
    creation_attempt = CreateRequestedAccount.new(requested_account, current_user)
    result = creation_attempt.result
    return result unless result[:success]
    # If it was successful, enroll the user in the course
    user = creation_attempt.user
    JoinCourse.new(course: requested_account.course,
                   user:,
                   role: CoursesUsers::Roles::STUDENT_ROLE)
    result
  end

  def check_creation_permissions
    return if user_signed_in? && @course && current_user.can_edit?(@course)
    raise_unauthorized_exception

  def handle_existing_request
    existing_request = RequestedAccount.find_by(course: @course, username: params[:username])
    return if existing_request
    existing_request.update_attribute(:email, params[:email])
    # TODO: sensible error message rendered
    yield
  end
end

  def redirect_if_passcode_invalid
    return if passcode_valid?
    redirect_to '/errors/incorrect_passcode.json'
    yield
  end

  def handle_invalid_request
    return if @requested.valid?
    render json: { message: @requested.invalid_email_message }, status: :unprocessable_entity
    yield
  end

  def passcode_valid?
    return true if @course.passcode.blank?
    params[:passcode] == @course.passcode
  end

  def handle_existing_request
    existing_request = RequestedAccount.find_by(course: @course, username: params[:username])
    if existing_request
      existing_request.update_attribute(:email, params[:email])
      render json: { message: existing_request.updated_email_message }
      yield
    end
  end
end

<MSG> Fixes handle existent requests

<DFF> @@ -104,9 +104,9 @@ class RequestedAccountsController < ApplicationController
 
   def handle_existing_request
     existing_request = RequestedAccount.find_by(course: @course, username: params[:username])
-    return if existing_request
-    existing_request.update_attribute(:email, params[:email])
-    # TODO: sensible error message rendered
-    yield
+    if existing_request
+      existing_request.update_attribute(:email, params[:email])
+      yield
+    end
   end
 end

{'commit': 'diff --git a/app/controllers/requested_accounts_controller.rb b/app/controllers/requested_accounts_controller.rb\nindex 6a5c358f2..136eb24ee 100644\n--- a/app/controllers/requested_accounts_controller.rb\n+++ b/app/controllers/requested_accounts_controller.rb\n@@ -104,9 +104,9 @@ class RequestedAccountsController < ApplicationController\n \n   def handle_existing_request\n     existing_request = RequestedAccount.find_by(course: @course, username: params[:username])\n-    return if existing_request\n-    existing_request.update_attribute(:email, params[:email])\n-    # TODO: sensible error message rendered\n-    yield\n+    if existing_request\n+      existing_request.update_attribute(:email, params[:email])\n+      yield\n+    end\n   end\n end\n', 'commit_subject': 'Fixes handle existent requests', 'repo_name': 'WikiEducationFoundation/WikiEduDashboard', 'committer': {'name': 'mauditecandela', 'email': 'f7ccc0395f9e3c3303a5a2c62733e11156b22442@gmail.com', 'time_sec': '1517418234', 'tz_offset': '60', 'date': {'seconds': '1517418234'}}, 'commit_tree': 'cc6fbd8e43719a253dafef5781df3ba6a86ce494', 'commit_message': 'Fixes handle existent requests\n', 'language_name': 'JavaScript', 'license': 'mit', 'hunks': '@@ -104,9 +104,9 @@ class RequestedAccountsController < ApplicationController\n \n   def handle_existing_request\n     existing_request = RequestedAccount.find_by(course: @course, username: params[:username])\n-    return if existing_request\n-    existing_request.update_attribute(:email, params[:email])\n-    # TODO: sensible error message rendered\n-    yield\n+    if existing_request\n+      existing_request.update_attribute(:email, params[:email])\n+      yield\n+    end\n   end\n end\n', 'addition_count': 4, 'deletion_count': 4, 'src_file': 'a/app/controllers/requested_accounts_controller.rb', 'tgt_file': 'b/app/controllers/requested_accounts_controller.rb', 'file_extension': '.rb', 'before_file': "# frozen_string_literal: true\n\n#= Controller for requesting new wiki accounts and processing those requests\nclass RequestedAccountsController < ApplicationController\n  respond_to :html\n  before_action :set_course, except: [:index, :create_all_accounts]\n  before_action :check_creation_permissions,\n                only: %i[show create_accounts enable_account_requests destroy]\n\n  # This creates (or updates) a RequestedAccount, which is a username and email\n  # for a user who wants to create a wiki account (but may not be able to do so\n  # because of a shared IP that has hit the new account limit).\n  def request_account\n    redirect_if_passcode_invalid { return }\n    # If there is already a request for a certain username for this course, then\n    # it's probably the same user who put in the wrong email the first time.\n    # Just overwrite the email with the new one in this case.\n    handle_existing_request { return }\n    @requested = RequestedAccount.create(course: @course,\n                                         username: params[:username],\n                                         email: params[:email])\n    handle_invalid_request { return }\n\n    unless params[:create_account_now] == 'true'\n      render json: { message: I18n.t('courses.new_account_submitted') }\n      return\n    end\n    result = create_account(@requested)\n    if result[:success]\n      render json: { message: result.values.first }\n    else\n      render json: { message: result.values.first }, status: :internal_server_error\n    end\n  end\n\n  # Sets the flag on a course so that clicking 'Sign Up' opens the Request Account\n  # modal instead of redirecting to the mediawiki account creation flow.\n  def enable_account_requests\n    @course.flags[:register_accounts] = true\n    @course.save\n  end\n\n  # List of requested accounts for a user's courses. @courses set in before action\n  def index\n    raise_unauthorized_exception unless user_signed_in? && current_user.admin?\n    respond_to do |format|\n      format.html do\n        @courses = all_requested_accounts\n        render\n      end\n      format.json { render json: { requested_accounts: RequestedAccount.any? } }\n    end\n  end\n\n  # List of requested accounts for a course.\n  def show; end\n\n  def destroy\n    # raise exception unless requestedaccount belongs to course\n    requested_account = RequestedAccount.find_by(id: params[:id])\n    raise_unauthorized_exception unless @course.id == requested_account.course_id\n    requested_account.delete\n    redirect_back fallback_location: '/'\n  end\n\n  # Try to create each of the requested accounts for a course, and show the\n  # result for each.\n  def create_accounts\n    @results = []\n    @course.requested_accounts.each do |requested_account|\n      @results << create_account(requested_account)\n    end\n  end\n\n  def create_all_accounts\n    raise_unauthorized_exception unless user_signed_in? && current_user.admin?\n    @courses = all_requested_accounts\n    @results = RequestedAccount.all.map { |account| create_account(account) }\n\n    render :index\n  end\n\n  private\n\n  def all_requested_accounts\n    RequestedAccount.all.includes(:course).group_by { |account| account.course.title }\n  end\n\n  def create_account(requested_account)\n    creation_attempt = CreateRequestedAccount.new(requested_account, current_user)\n    result = creation_attempt.result\n    return result unless result[:success]\n    # If it was successful, enroll the user in the course\n    user = creation_attempt.user\n    JoinCourse.new(course: requested_account.course,\n                   user:,\n                   role: CoursesUsers::Roles::STUDENT_ROLE)\n    result\n  end\n\n  def check_creation_permissions\n    return if user_signed_in? && @course && current_user.can_edit?(@course)\n    raise_unauthorized_exception\n\n  def handle_existing_request\n    existing_request = RequestedAccount.find_by(course: @course, username: params[:username])\n    return if existing_request\n    existing_request.update_attribute(:email, params[:email])\n    # TODO: sensible error message rendered\n    yield\n  end\nend\n\n  def redirect_if_passcode_invalid\n    return if passcode_valid?\n    redirect_to '/errors/incorrect_passcode.json'\n    yield\n  end\n\n  def handle_invalid_request\n    return if @requested.valid?\n    render json: { message: @requested.invalid_email_message }, status: :unprocessable_entity\n    yield\n  end\n\n  def passcode_valid?\n    return true if @course.passcode.blank?\n    params[:passcode] == @course.passcode\n  end\n\n  def handle_existing_request\n    existing_request = RequestedAccount.find_by(course: @course, username: params[:username])\n    if existing_request\n      existing_request.update_attribute(:email, params[:email])\n      render json: { message: existing_request.updated_email_message }\n      yield\n    end\n  end\nend\n", 'message': 'Fixes handle existent requests\n', 'author_name': 'mauditecandela', 'lang': 'rb'}