redmine/redmine

View on GitHub
app/models/user.rb

Summary

Maintainability
F
5 days
Test Coverage

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

class User < Principal
  include Redmine::Ciphering
  include Redmine::SafeAttributes

  # Different ways of displaying/sorting users
Severity: Major
Found in app/models/user.rb - About 1 day to fix

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

    require "digest/sha1"
    
    class User < Principal
      include Redmine::Ciphering
      include Redmine::SafeAttributes
    Severity: Major
    Found in app/models/user.rb - About 1 day to fix

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

        def allowed_to?(action, context, options={}, &block)
          if context && context.is_a?(Project)
            return false unless context.allows_to?(action)
            # Admin users are authorized for anything else
            return true if admin?
      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 try_to_login! has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
      Open

        def self.try_to_login!(login, password, active_only=true)
          login = login.to_s.strip
          password = password.to_s
      
          # Make sure no one can sign in with an empty login or password
      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 project_ids_by_role has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
      Open

        def project_ids_by_role
          # Clear project condition for when called from chained scopes
          # eg. project.children.visible(user)
          Project.unscoped do
            return @project_ids_by_role if @project_ids_by_role
      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

      Consider simplifying this complex logical expression.
      Open

          elsif (admin? && destroyed? && active?) ||      # active admin user was deleted
                (!admin? && saved_change_to_admin? && active?) || # admin is no longer admin
                (admin? && saved_change_to_status? && !active?)   # admin was locked
            deliver = true
            options[:message] = :mail_body_security_notification_remove
      Severity: Critical
      Found in app/models/user.rb - About 1 hr to fix

        Consider simplifying this complex logical expression.
        Open

            if (admin? && saved_change_to_id? && active?) ||    # newly created admin
               (admin? && saved_change_to_admin? && active?) || # regular user became admin
               (admin? && saved_change_to_status? && active?)   # locked admin became active again
              deliver = true
              options[:message] = :mail_body_security_notification_add
        Severity: Critical
        Found in app/models/user.rb - About 1 hr to fix

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

            def deliver_security_notification
              options = {
                field: :field_admin,
                value: login,
                title: :label_user_plural,
          Severity: Minor
          Found in app/models/user.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 allowed_to? has 28 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def allowed_to?(action, context, options={}, &block)
              if context && context.is_a?(Project)
                return false unless context.allows_to?(action)
                # Admin users are authorized for anything else
                return true if admin?
          Severity: Minor
          Found in app/models/user.rb - About 1 hr to fix

            Method remove_references_before_destroy has 26 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

              def remove_references_before_destroy
                return if self.id.nil?
            
                substitute = User.anonymous
                Attachment.where(['author_id = ?', id]).update_all(['author_id = ?', substitute.id])
            Severity: Minor
            Found in app/models/user.rb - About 1 hr to fix

              Method project_ids_by_role has 26 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                def project_ids_by_role
                  # Clear project condition for when called from chained scopes
                  # eg. project.children.visible(user)
                  Project.unscoped do
                    return @project_ids_by_role if @project_ids_by_role
              Severity: Minor
              Found in app/models/user.rb - About 1 hr to fix

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

                  def notify_about?(object)
                    if mail_notification == 'all'
                      true
                    elsif mail_notification.blank? || mail_notification == 'none'
                      false
                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 verify_session_token has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                  def self.verify_session_token(user_id, token)
                    return false if user_id.blank? || token.blank?
                
                    scope = Token.where(:user_id => user_id, :value => token.to_s, :action => 'session')
                    if Setting.session_lifetime?
                Severity: Minor
                Found in app/models/user.rb - About 35 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_notified_project_ids has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                  def update_notified_project_ids
                    if @notified_projects_ids_changed
                      ids = (mail_notification == 'selected' ? Array.wrap(notified_projects_ids).reject(&:blank?) : [])
                      members.update_all(:mail_notification => false)
                      members.where(:project_id => ids).update_all(:mail_notification => true) if ids.any?
                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

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                  scope :not_in_group, (lambda do |group|
                    group_id = group.is_a?(Group) ? group.id : group.to_i
                    where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id)
                Severity: Minor
                Found in app/models/user.rb and 1 other location - About 20 mins to fix
                app/models/user.rb on lines 139..141

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 28.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                  scope :in_group, (lambda do |group|
                    group_id = group.is_a?(Group) ? group.id : group.to_i
                    where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id)
                Severity: Minor
                Found in app/models/user.rb and 1 other location - About 20 mins to fix
                app/models/user.rb on lines 143..145

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 28.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                There are no issues that match your filters.

                Category
                Status