theforeman/foreman

View on GitHub
app/models/user.rb

Summary

Maintainability
F
4 days
Test Coverage

Class User has 82 methods (exceeds 20 allowed). Consider refactoring.
Open

class User < ApplicationRecord
  audited :except => [:last_login_on, :password_hash, :password_salt, :password_confirmation],
          :associations => [:roles, :usergroups]
  include Authorizable
  include Foreman::TelemetryHelper
Severity: Major
Found in app/models/user.rb - About 1 day to fix

    File user.rb has 613 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    require 'digest/sha1'
    
    class User < ApplicationRecord
      audited :except => [:last_login_on, :password_hash, :password_salt, :password_confirmation],
              :associations => [:roles, :usergroups]
    Severity: Major
    Found in app/models/user.rb - About 1 day to fix

      Method try_to_login has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring.
      Open

        def self.try_to_login(login, password, api_request = false)
          # Make sure no one can sign in with an empty password
          return nil if password.to_s.empty?
      
          # user is already in local database
      Severity: Minor
      Found in app/models/user.rb - About 4 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 find_or_create_external_user has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
      Open

        def self.find_or_create_external_user(attrs, auth_source_name)
          external_groups = attrs.delete(:groups)
          auth_source = AuthSource.find_by_name(auth_source_name.to_s)
      
          # existing user, we'll update them
      Severity: Minor
      Found in app/models/user.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 try_to_login has 39 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def self.try_to_login(login, password, api_request = false)
          # Make sure no one can sign in with an empty password
          return nil if password.to_s.empty?
      
          # user is already in local database
      Severity: Minor
      Found in app/models/user.rb - About 1 hr to fix

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

          def self.find_or_create_external_user(attrs, auth_source_name)
            external_groups = attrs.delete(:groups)
            auth_source = AuthSource.find_by_name(auth_source_name.to_s)
        
            # existing user, we'll update them
        Severity: Minor
        Found in app/models/user.rb - About 1 hr to fix

          Method try_to_auto_create_user has 27 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def self.try_to_auto_create_user(login, password)
              return if login.blank? || password.blank?
              # User is not yet registered, try to authenticate with available sources
              logger.debug "Attempting to log into an auth source as #{login} for account auto-creation"
              return unless (attrs = AuthSource.authenticate(login, password))
          Severity: Minor
          Found in app/models/user.rb - About 1 hr to fix

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

              def editing_self?(options = {})
                options[:controller].to_s == 'users' &&
                  options[:action] =~ /edit|update/ &&
                  options[:id].to_i == id ||
                options[:controller].to_s =~ /\Aapi\/v\d+\/users\Z/ &&
            Severity: Minor
            Found in app/models/user.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 try_to_auto_create_user has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
            Open

              def self.try_to_auto_create_user(login, password)
                return if login.blank? || password.blank?
                # User is not yet registered, try to authenticate with available sources
                logger.debug "Attempting to log into an auth source as #{login} for account auto-creation"
                return unless (attrs = AuthSource.authenticate(login, password))
            Severity: Minor
            Found in app/models/user.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

            Avoid deeply nested control flow statements.
            Open

                      File.delete(old_avatar) if File.exist?(old_avatar)
            Severity: Major
            Found in app/models/user.rb - About 45 mins to fix

              Avoid deeply nested control flow statements.
              Open

                          unless user.update(valid_attrs)
                            logger.warn "Failed to update #{user.login} attributes: #{user.errors.full_messages.join(', ')}"
                          end
              Severity: Major
              Found in app/models/user.rb - About 45 mins to fix

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

                  def allowed_to?(action)
                    return false if disabled?
                    return true if admin?
                    if action.is_a?(Hash) || action.is_a?(ActionController::Parameters)
                      action = Foreman::AccessControl.normalize_path_hash(action)
                Severity: Minor
                Found in app/models/user.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 check_permissions_for_changing_login has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                  def check_permissions_for_changing_login
                    if login_changed? && !new_record?
                      if !internal?
                        errors.add :login, _("It is not possible to change external users login")
                      elsif !(User.current.can?(:edit_users, self) || (id == User.current.id))
                Severity: Minor
                Found in app/models/user.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 search_by_admin has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                  def self.search_by_admin(key, operator, value)
                    value      = value == 'true'
                    value      = !value if operator == '<>'
                    conditions = [table_name, Usergroup.table_name].map do |base|
                      "(#{base}.admin = ?" + (value ? ')' : " OR #{base}.admin IS NULL)")
                Severity: Minor
                Found in app/models/user.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