ece517-p3/expertiza

View on GitHub
app/controllers/users_controller.rb

Summary

Maintainability
C
1 day
Test Coverage

Unprotected mass assignment
Open

    if @user.update_attributes(params[:user])
Severity: Critical
Found in app/controllers/users_controller.rb by brakeman

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Parameters should be whitelisted for mass assignment
Open

    params.permit!
Severity: Critical
Found in app/controllers/users_controller.rb by brakeman

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Unprotected mass assignment
Open

    elsif requested_user.update_attributes(params[:user])
Severity: Critical
Found in app/controllers/users_controller.rb by brakeman

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Unprotected mass assignment
Open

    @user = User.new(user_params)

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Unprotected mass assignment
Open

    requested_user = RequestedUser.new(requested_user_params)

Mass assignment is a feature of Rails which allows an application to create a record from the values of a hash.

Example:

User.new(params[:user])

Unfortunately, if there is a user field called admin which controls administrator access, now any user can make themselves an administrator.

attr_accessible and attr_protected can be used to limit mass assignment. However, Brakeman will warn unless attr_accessible is used, or mass assignment is completely disabled.

There are two different mass assignment warnings which can arise. The first is when mass assignment actually occurs, such as the example above. This results in a warning like

Unprotected mass assignment near line 61: User.new(params[:user])

The other warning is raised whenever a model is found which does not use attr_accessible. This produces generic warnings like

Mass assignment is not restricted using attr_accessible

with a list of affected models.

In Rails 3.1 and newer, mass assignment can easily be disabled:

config.active_record.whitelist_attributes = true

Unfortunately, it can also easily be bypassed:

User.new(params[:user], :without_protection => true)

Brakeman will warn on uses of without_protection.

Assignment Branch Condition size for create_approved_user is too high. [48.48/15]
Open

  def create_approved_user
    requested_user = RequestedUser.find_by(id: params[:id])
    requested_user.status = params[:status]
    if requested_user.status.nil?
      flash[:error] = "Please Approve or Reject before submitting"
Severity: Minor
Found in app/controllers/users_controller.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for create_requested_user_record is too high. [44.67/15]
Open

  def create_requested_user_record
    requested_user = RequestedUser.new(requested_user_params)
    if params[:user][:institution_id].empty?
      institution = Institution.find_or_create_by(name: params[:institution][:name])
      requested_user.institution_id = institution.id
Severity: Minor
Found in app/controllers/users_controller.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for create is too high. [40.22/15]
Open

  def create
    # if the user name already exists, register the user by email address
    check = User.find_by(name: params[:user][:name])
    params[:user][:name] = params[:user][:email] unless check.nil?
    @user = User.new(user_params)
Severity: Minor
Found in app/controllers/users_controller.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for show is too high. [32.7/15]
Open

  def show
    if params[:id].nil? || ((current_user_role? == "Student") && (session[:user].id != params[:id].to_i))
      redirect_to(action: AuthHelper.get_home_action(session[:user]), controller: AuthHelper.get_home_controller(session[:user]))
    else
      @user = User.find(params[:id])
Severity: Minor
Found in app/controllers/users_controller.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for show_selection is too high. [28.44/15]
Open

  def show_selection
    @user = User.find_by(name: params[:user][:name])
    if !@user.nil?
      get_role
      if @role.parent_id.nil? || @role.parent_id < session[:user].role_id || @user.id == session[:user].id
Severity: Minor
Found in app/controllers/users_controller.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

Assignment Branch Condition size for keys is too high. [23.28/15]
Open

  def keys
    if params[:id].nil? || ((current_user_role? == "Student") && (session[:user].id != params[:id].to_i))
      redirect_to(action: AuthHelper.get_home_action(session[:user]), controller: AuthHelper.get_home_controller(session[:user]))
    else
      @user = User.find(params[:id])
Severity: Minor
Found in app/controllers/users_controller.rb by rubocop

This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

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

class UsersController < ApplicationController
  autocomplete :user, :name
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify method: :post, only: %i[destroy create update],
         redirect_to: {action: :list}
Severity: Minor
Found in app/controllers/users_controller.rb - About 2 hrs to fix

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

    require 'will_paginate/array'
    
    class UsersController < ApplicationController
      autocomplete :user, :name
      # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
    Severity: Minor
    Found in app/controllers/users_controller.rb - About 2 hrs to fix

      Perceived complexity for create_approved_user is too high. [11/7]
      Open

        def create_approved_user
          requested_user = RequestedUser.find_by(id: params[:id])
          requested_user.status = params[:status]
          if requested_user.status.nil?
            flash[:error] = "Please Approve or Reject before submitting"
      Severity: Minor
      Found in app/controllers/users_controller.rb by rubocop

      This cop tries to produce a complexity score that's a measure of the complexity the reader experiences when looking at a method. For that reason it considers when nodes as something that doesn't add as much complexity as an if or a &&. Except if it's one of those special case/when constructs where there's no expression after case. Then the cop treats it as an if/elsif/elsif... and lets all the when nodes count. In contrast to the CyclomaticComplexity cop, this cop considers else nodes as adding complexity.

      Example:

      def my_method                   # 1
        if cond                       # 1
          case var                    # 2 (0.8 + 4 * 0.2, rounded)
          when 1 then func_one
          when 2 then func_two
          when 3 then func_three
          when 4..10 then func_other
          end
        else                          # 1
          do_something until a && b   # 2
        end                           # ===
      end                             # 7 complexity points

      Assignment Branch Condition size for update is too high. [18.36/15]
      Open

        def update
          params.permit!
          @user = User.find params[:id]
          # update username, when the user cannot be deleted
          # rename occurs in 'show' page, not in 'edit' page
      Severity: Minor
      Found in app/controllers/users_controller.rb by rubocop

      This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

      Assignment Branch Condition size for destroy is too high. [17.29/15]
      Open

        def destroy
          begin
            @user = User.find(params[:id])
            AssignmentParticipant.where(user_id: @user.id).each(&:delete)
            TeamsUser.where(user_id: @user.id).each(&:delete)
      Severity: Minor
      Found in app/controllers/users_controller.rb by rubocop

      This cop checks that the ABC size of methods is not higher than the configured maximum. The ABC size is based on assignments, branches (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric

      Cyclomatic complexity for create_approved_user is too high. [7/6]
      Open

        def create_approved_user
          requested_user = RequestedUser.find_by(id: params[:id])
          requested_user.status = params[:status]
          if requested_user.status.nil?
            flash[:error] = "Please Approve or Reject before submitting"
      Severity: Minor
      Found in app/controllers/users_controller.rb by rubocop

      This cop checks that the cyclomatic complexity of methods is not higher than the configured maximum. The cyclomatic complexity is the number of linearly independent paths through a method. The algorithm counts decision points and adds one.

      An if statement (or unless or ?:) increases the complexity by one. An else branch does not, since it doesn't add a decision point. The && operator (or keyword and) can be converted to a nested if statement, and ||/or is shorthand for a sequence of ifs, so they also add one. Loops can be said to have an exit condition, so they add one.

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

        def create_approved_user
          requested_user = RequestedUser.find_by(id: params[:id])
          requested_user.status = params[:status]
          if requested_user.status.nil?
            flash[:error] = "Please Approve or Reject before submitting"
      Severity: Minor
      Found in app/controllers/users_controller.rb - About 1 hr to fix

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

          def create_approved_user
            requested_user = RequestedUser.find_by(id: params[:id])
            requested_user.status = params[:status]
            if requested_user.status.nil?
              flash[:error] = "Please Approve or Reject before submitting"
        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 create has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
        Open

          def create
            # if the user name already exists, register the user by email address
            check = User.find_by(name: params[:user][:name])
            params[:user][:name] = params[:user][:email] unless check.nil?
            @user = User.new(user_params)
        Severity: Minor
        Found in app/controllers/users_controller.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 show_selection has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
        Open

          def show_selection
            @user = User.find_by(name: params[:user][:name])
            if !@user.nil?
              get_role
              if @role.parent_id.nil? || @role.parent_id < session[:user].role_id || @user.id == session[: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

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

          def create_requested_user_record
            requested_user = RequestedUser.new(requested_user_params)
            if params[:user][:institution_id].empty?
              institution = Institution.find_or_create_by(name: params[:institution][:name])
              requested_user.institution_id = institution.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

        Avoid using update_columns because it skips validations.
        Open

              if requested_user.update_columns(status: params[:status])
        Severity: Minor
        Found in app/controllers/users_controller.rb by rubocop

        This cop checks for the use of methods which skip validations which are listed in http://guides.rubyonrails.org/active_record_validations.html#skipping-validations

        Example:

        # bad
        Article.first.decrement!(:view_count)
        DiscussionBoard.decrement_counter(:post_count, 5)
        Article.first.increment!(:view_count)
        DiscussionBoard.increment_counter(:post_count, 5)
        person.toggle :active
        product.touch
        Billing.update_all("category = 'authorized', author = 'David'")
        user.update_attribute(website: 'example.com')
        user.update_columns(last_request_at: Time.current)
        Post.update_counters 5, comment_count: -1, action_count: 1
        
        # good
        user.update_attributes(website: 'example.com')
        FileUtils.touch('file')

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

          def user_params
            params.require(:user).permit(:name,
                                         :crypted_password,
                                         :role_id,
                                         :password_salt,
        Severity: Minor
        Found in app/controllers/users_controller.rb and 1 other location - About 20 mins to fix
        app/controllers/profile_controller.rb on lines 32..53

        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 27.

        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

        Do not introduce global variables.
        Open

            anonymized_view_starter_ips = $redis.get('anonymized_view_starter_ips') || ''
        Severity: Minor
        Found in app/controllers/users_controller.rb by rubocop

        This cops looks for uses of global variables. It does not report offenses for built-in global variables. Built-in global variables are allowed by default. Additionally users can allow additional variables via the AllowedVariables option.

        Note that backreferences like $1, $2, etc are not global variables.

        Example:

        # bad
        $foo = 2
        bar = $foo + 5
        
        # good
        FOO = 2
        foo = 2
        $stdin.read

        Do not prefix reader method names with get_.
        Open

          def get_role
        Severity: Minor
        Found in app/controllers/users_controller.rb by rubocop

        This cop makes sure that accessor methods are named properly.

        Example:

        # bad
        def set_attribute(value)
        end
        
        # good
        def attribute=(value)
        end
        
        # bad
        def get_attribute
        end
        
        # good
        def attribute
        end

        Do not introduce global variables.
        Open

            $redis.set('anonymized_view_starter_ips', anonymized_view_starter_ips)
        Severity: Minor
        Found in app/controllers/users_controller.rb by rubocop

        This cops looks for uses of global variables. It does not report offenses for built-in global variables. Built-in global variables are allowed by default. Additionally users can allow additional variables via the AllowedVariables option.

        Note that backreferences like $1, $2, etc are not global variables.

        Example:

        # bad
        $foo = 2
        bar = $foo + 5
        
        # good
        FOO = 2
        foo = 2
        $stdin.read

        There are no issues that match your filters.

        Category
        Status