discourse/discourse

View on GitHub
app/controllers/users_controller.rb

Summary

Maintainability
F
1 wk
Test Coverage

File users_controller.rb has 1025 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require_dependency 'discourse_hub'
require_dependency 'user_name_suggester'
require_dependency 'rate_limiter'
require_dependency 'wizard'
require_dependency 'wizard/builder'
Severity: Major
Found in app/controllers/users_controller.rb - About 2 days to fix

    Method password_reset has a Cognitive Complexity of 63 (exceeds 5 allowed). Consider refactoring.
    Open

      def password_reset
        expires_now
    
        token = params[:token]
    
    
    Severity: Minor
    Found in app/controllers/users_controller.rb - About 1 day to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Class UsersController has 58 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class UsersController < ApplicationController
    
      skip_before_action :authorize_mini_profiler, only: [:avatar]
    
      requires_login only: [
    Severity: Major
    Found in app/controllers/users_controller.rb - About 1 day to fix

      Method create has a Cognitive Complexity of 48 (exceeds 5 allowed). Consider refactoring.
      Open

        def create
          params.require(:email)
          params.require(:username)
          params.permit(:user_fields)
      
      
      Severity: Minor
      Found in app/controllers/users_controller.rb - About 7 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method admin_login has a Cognitive Complexity of 46 (exceeds 5 allowed). Consider refactoring.
      Open

        def admin_login
          return redirect_to(path("/")) if current_user
      
          if request.put? && params[:email].present?
            RateLimiter.new(nil, "admin-login-hr-#{request.remote_ip}", 6, 1.hour).performed!
      Severity: Minor
      Found in app/controllers/users_controller.rb - About 7 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method password_reset has 104 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def password_reset
          expires_now
      
          token = params[:token]
      
      
      Severity: Major
      Found in app/controllers/users_controller.rb - About 4 hrs to fix

        Method create has 90 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def create
            params.require(:email)
            params.require(:username)
            params.permit(:user_fields)
        
        
        Severity: Major
        Found in app/controllers/users_controller.rb - About 3 hrs to fix

          Method update has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
          Open

            def update
              user = fetch_user_from_params
              guardian.ensure_can_edit!(user)
              attributes = user_params
          
          
          Severity: Minor
          Found in app/controllers/users_controller.rb - About 2 hrs to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Method admin_login has 55 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def admin_login
              return redirect_to(path("/")) if current_user
          
              if request.put? && params[:email].present?
                RateLimiter.new(nil, "admin-login-hr-#{request.remote_ip}", 6, 1.hour).performed!
          Severity: Major
          Found in app/controllers/users_controller.rb - About 2 hrs to fix

            Method perform_account_activation has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
            Open

              def perform_account_activation
                raise Discourse::InvalidAccess.new if honeypot_or_challenge_fails?(params)
            
                if @user = EmailToken.confirm(params[:token])
                  # Log in the user unless they need to be approved
            Severity: Minor
            Found in app/controllers/users_controller.rb - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Method email_login has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
            Open

              def email_login
                raise Discourse::NotFound if !SiteSetting.enable_local_logins_via_email
                return redirect_to path("/") if current_user
            
                expires_now
            Severity: Minor
            Found in app/controllers/users_controller.rb - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Method pick_avatar has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

              def pick_avatar
                user = fetch_user_from_params
                guardian.ensure_can_edit!(user)
            
                type = params[:type]
            Severity: Minor
            Found in app/controllers/users_controller.rb - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Method update_activation_email has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
            Open

              def update_activation_email
                RateLimiter.new(nil, "activate-edit-email-hr-#{request.remote_ip}", 5, 1.hour).performed!
            
                if params[:username].present?
                  @user = User.find_by_username_or_email(params[:username])
            Severity: Minor
            Found in app/controllers/users_controller.rb - About 1 hr to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Method user_params has 36 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def user_params
                permitted = [
                  :name,
                  :email,
                  :password,
            Severity: Minor
            Found in app/controllers/users_controller.rb - About 1 hr to fix

              Method search_users has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
              Open

                def search_users
                  term = params[:term].to_s.strip
                  topic_id = params[:topic_id]
                  topic_id = topic_id.to_i if topic_id
                  topic_allowed_users = params[:topic_allowed_users] || false
              Severity: Minor
              Found in app/controllers/users_controller.rb - About 1 hr to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Method search_users has 35 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def search_users
                  term = params[:term].to_s.strip
                  topic_id = params[:topic_id]
                  topic_id = topic_id.to_i if topic_id
                  topic_allowed_users = params[:topic_allowed_users] || false
              Severity: Minor
              Found in app/controllers/users_controller.rb - About 1 hr to fix

                Method update_second_factor has 35 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                  def update_second_factor
                    params.require(:second_factor_token)
                    params.require(:second_factor_method)
                    params.require(:second_factor_target)
                
                
                Severity: Minor
                Found in app/controllers/users_controller.rb - About 1 hr to fix

                  Method is_local_username has 33 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                    def is_local_username
                      usernames = params[:usernames]
                      usernames = [params[:username]] if usernames.blank?
                  
                      groups = Group.where(name: usernames).pluck(:name)
                  Severity: Minor
                  Found in app/controllers/users_controller.rb - About 1 hr to fix

                    Method show has 31 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                      def show
                        return redirect_to path('/login') if SiteSetting.hide_user_profiles_from_public && !current_user
                    
                        @user = fetch_user_from_params(
                          include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts)
                    Severity: Minor
                    Found in app/controllers/users_controller.rb - About 1 hr to fix

                      Method pick_avatar has 29 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                        def pick_avatar
                          user = fetch_user_from_params
                          guardian.ensure_can_edit!(user)
                      
                          type = params[:type]
                      Severity: Minor
                      Found in app/controllers/users_controller.rb - About 1 hr to fix

                        Method select_avatar has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                          def select_avatar
                            user = fetch_user_from_params
                            guardian.ensure_can_edit!(user)
                        
                            url = params[:url]
                        Severity: Minor
                        Found in app/controllers/users_controller.rb - About 1 hr to fix

                          Method account_created has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                          Open

                            def account_created
                              if current_user.present?
                                if SiteSetting.enable_sso_provider && payload = cookies.delete(:sso_payload)
                                  return redirect_to(session_sso_provider_url + "?" + payload)
                                elsif destination_url = cookies.delete(:destination_url)
                          Severity: Minor
                          Found in app/controllers/users_controller.rb - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Method send_activation_email has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                          Open

                            def send_activation_email
                              if current_user.blank? || !current_user.staff?
                                RateLimiter.new(nil, "activate-hr-#{request.remote_ip}", 30, 1.hour).performed!
                                RateLimiter.new(nil, "activate-min-#{request.remote_ip}", 6, 1.minute).performed!
                              end
                          Severity: Minor
                          Found in app/controllers/users_controller.rb - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Method show has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                          Open

                            def show
                              return redirect_to path('/login') if SiteSetting.hide_user_profiles_from_public && !current_user
                          
                              @user = fetch_user_from_params(
                                include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts)
                          Severity: Minor
                          Found in app/controllers/users_controller.rb - About 1 hr to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Method select_avatar has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                          Open

                            def select_avatar
                              user = fetch_user_from_params
                              guardian.ensure_can_edit!(user)
                          
                              url = params[:url]
                          Severity: Minor
                          Found in app/controllers/users_controller.rb - About 55 mins to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Method update_second_factor has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                          Open

                            def update_second_factor
                              params.require(:second_factor_token)
                              params.require(:second_factor_method)
                              params.require(:second_factor_target)
                          
                          
                          Severity: Minor
                          Found in app/controllers/users_controller.rb - About 55 mins to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Method is_local_username has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                          Open

                            def is_local_username
                              usernames = params[:usernames]
                              usernames = [params[:username]] if usernames.blank?
                          
                              groups = Group.where(name: usernames).pluck(:name)
                          Severity: Minor
                          Found in app/controllers/users_controller.rb - About 45 mins to fix

                          Cognitive Complexity

                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                          A method's cognitive complexity is based on a few simple rules:

                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                          • Code is considered more complex for each "break in the linear flow of the code"
                          • Code is considered more complex when "flow breaking structures are nested"

                          Further reading

                          Avoid deeply nested control flow statements.
                          Open

                                    if @user && @user.admin?
                                      log_on_user(@user)
                                      return redirect_to path("/")
                                    else
                                      @message = I18n.t("admin_login.errors.unknown_email_address")
                          Severity: Major
                          Found in app/controllers/users_controller.rb - About 45 mins to fix

                            Avoid deeply nested control flow statements.
                            Open

                                        if second_factor_token.present?
                                          if email_token_user.authenticate_second_factor(second_factor_token, second_factor_method)
                                            true
                                          else
                                            @error = I18n.t("login.invalid_second_factor_code")
                            Severity: Major
                            Found in app/controllers/users_controller.rb - About 45 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                        return fail_with("login.missing_user_field") if f.required?
                              Severity: Major
                              Found in app/controllers/users_controller.rb - About 30 mins to fix

                                Avoid too many return statements within this method.
                                Open

                                      return render json: failed_json, status: 422
                                Severity: Major
                                Found in app/controllers/users_controller.rb - About 30 mins to fix

                                  Avoid too many return statements within this method.
                                  Open

                                        return fail_with('login.incorrect_username_email_or_password')
                                  Severity: Major
                                  Found in app/controllers/users_controller.rb - About 30 mins to fix

                                    Avoid too many return statements within this method.
                                    Open

                                          return render body: nil, status: :forbidden
                                    Severity: Major
                                    Found in app/controllers/users_controller.rb - About 30 mins to fix

                                      Method confirm_admin has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                      Open

                                        def confirm_admin
                                          @confirmation = AdminConfirmation.find_by_code(params[:token])
                                      
                                          raise Discourse::NotFound unless @confirmation
                                          raise Discourse::InvalidAccess.new unless
                                      Severity: Minor
                                      Found in app/controllers/users_controller.rb - About 25 mins to fix

                                      Cognitive Complexity

                                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                      A method's cognitive complexity is based on a few simple rules:

                                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                      • Code is considered more complex for each "break in the linear flow of the code"
                                      • Code is considered more complex when "flow breaking structures are nested"

                                      Further reading

                                      Method invited has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                      Open

                                        def invited
                                          inviter = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
                                          offset = params[:offset].to_i || 0
                                          filter_by = params[:filter]
                                      
                                      
                                      Severity: Minor
                                      Found in app/controllers/users_controller.rb - About 25 mins to fix

                                      Cognitive Complexity

                                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                      A method's cognitive complexity is based on a few simple rules:

                                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                      • Code is considered more complex for each "break in the linear flow of the code"
                                      • Code is considered more complex when "flow breaking structures are nested"

                                      Further reading

                                      Method notification_level has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                      Open

                                        def notification_level
                                          user = fetch_user_from_params
                                      
                                          if params[:notification_level] == "ignore"
                                            guardian.ensure_can_ignore_user!(user.id)
                                      Severity: Minor
                                      Found in app/controllers/users_controller.rb - About 25 mins to fix

                                      Cognitive Complexity

                                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                      A method's cognitive complexity is based on a few simple rules:

                                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                      • Code is considered more complex for each "break in the linear flow of the code"
                                      • Code is considered more complex when "flow breaking structures are nested"

                                      Further reading

                                      There are no issues that match your filters.

                                      Category
                                      Status