• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

07 Apr 2025 06:36PM UTC coverage: 1.283% (-94.6%) from 95.862%
4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

Pull #1994

circleci

hectorcorrea
Switched to use the autocomplete that we aleady use for ROR. Integrated it with the existing logic for creators
Pull Request #1994: Started adding auto complete to contributors

0 of 46 new or added lines in 2 files covered. (0.0%)

4806 existing lines in 74 files now uncovered.

65 of 5065 relevant lines covered (1.28%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/controllers/users_controller.rb
1
# frozen_string_literal: true
UNCOV
2
class UsersController < ApplicationController
×
3
  # Constants set by the <form> <input> parameters transmitted using POST/PATCH/PUT requests
UNCOV
4
  GROUP_MESSAGING_DISABLED = "0"
×
UNCOV
5
  GROUP_MESSAGING_ENABLED = "1"
×
6

7
  # Notice that `set_user` sets the value of the user that we are viewing or editing
8
  # while `authenticate_user` sets the value of the current logged in user.
9
  # These values can be different (e.g. when an admin users is editing the information
10
  # of another user)
UNCOV
11
  before_action :set_user, only: %i[show edit update]
×
UNCOV
12
  before_action :authenticate_user!
×
13

UNCOV
14
  def index
×
UNCOV
15
    @users = User.all.sort_by { |user| user.family_name || "" }
×
UNCOV
16
  end
×
17

18
  # GET /users/1
UNCOV
19
  def show
×
UNCOV
20
    @search_terms = params["q"].presence
×
UNCOV
21
    @can_edit = can_edit?
×
UNCOV
22
    @my_dashboard = current_user.id == @user.id
×
UNCOV
23
    render "forbidden", status: :forbidden if !current_user.super_admin? && !@my_dashboard
×
24

UNCOV
25
    @unfinished_works = WorkList.unfinished_works(@user, @search_terms)
×
UNCOV
26
    @completed_works = WorkList.completed_works(@user, @search_terms)
×
UNCOV
27
    @withdrawn_works = WorkList.withdrawn_works(@user, @search_terms)
×
UNCOV
28
    @works_found = @unfinished_works.length + @completed_works.length + @withdrawn_works.length
×
UNCOV
29
  end
×
30

31
  # GET /users/1/edit
UNCOV
32
  def edit
×
UNCOV
33
    unless can_edit?
×
UNCOV
34
      Rails.logger.warn("Unauthorized to edit user #{@user.id} (current user: #{current_user.id})")
×
UNCOV
35
      redirect_to user_path(@user)
×
UNCOV
36
    end
×
UNCOV
37
  end
×
38

39
  # PATCH/PUT /users/1 or /users/1.json
UNCOV
40
  def update
×
UNCOV
41
    if can_edit?
×
UNCOV
42
      respond_to do |format|
×
UNCOV
43
        update_groups_with_messaging if user_params.key?(:groups_with_messaging)
×
44

UNCOV
45
        if @user.update(user_params)
×
UNCOV
46
          format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
×
UNCOV
47
          format.json { render :show, status: :ok, location: @user }
×
UNCOV
48
        else
×
49
          # return 200 so the loadbalancer doesn't capture the error
UNCOV
50
          format.html { render :edit }
×
UNCOV
51
          format.json { render json: @user.errors }
×
UNCOV
52
        end
×
UNCOV
53
      end
×
UNCOV
54
    else
×
UNCOV
55
      Rails.logger.warn("Unauthorized to update user #{@user.id} (current user: #{current_user.id})")
×
UNCOV
56
      redirect_to user_path(@user)
×
UNCOV
57
    end
×
UNCOV
58
  end
×
59

UNCOV
60
  private
×
61

62
    # Use callbacks to share common setup or constraints between actions.
UNCOV
63
    def set_user
×
UNCOV
64
      user_id = user_id_from_url
×
UNCOV
65
      @user = User.friendly.find(user_id)
×
UNCOV
66
      redirect_to action: action_name, id: @user.friendly_id, status: :moved_permanently unless @user.friendly_id == user_id
×
UNCOV
67
    end
×
68

UNCOV
69
    def user_id_from_url
×
70
      # For external users UID is in the form `user-name@gmail.com`, however, Rails eats the ".com" from
71
      # the UID and dumps it into the `format` param. Here we make sure the ".com" is preserved when the
72
      # UID looks to be an external user id.
UNCOV
73
      external_uid = params[:id].include?("@")
×
UNCOV
74
      if external_uid && params["format"] == "com"
×
75
        "#{params[:id]}.#{params['format']}"
×
UNCOV
76
      else
×
UNCOV
77
        params[:id]
×
UNCOV
78
      end
×
UNCOV
79
    end
×
80

81
    # Only allow a list of trusted parameters through.
UNCOV
82
    def user_params
×
UNCOV
83
      @user_params ||= params.require(:user).permit([
×
UNCOV
84
                                                      :given_name, :full_name, :family_name, :orcid, :email_messages_enabled,
×
UNCOV
85
                                                      :email, :default_group_id, groups_with_messaging: {}
×
UNCOV
86
                                                    ])
×
UNCOV
87
    end
×
88

UNCOV
89
    def can_edit?
×
UNCOV
90
      current_user.id == @user.id or current_user.super_admin?
×
UNCOV
91
    end
×
92

UNCOV
93
    def parameter_enables_messaging?(form_value)
×
UNCOV
94
      form_value.to_s == GROUP_MESSAGING_ENABLED
×
UNCOV
95
    end
×
96

UNCOV
97
    def update_groups_with_messaging
×
UNCOV
98
      if user_params.key?(:groups_with_messaging)
×
UNCOV
99
        extracted = user_params.extract!(:groups_with_messaging)
×
UNCOV
100
        groups_with_messaging = extracted[:groups_with_messaging]
×
101

UNCOV
102
        groups_with_messaging.each_pair do |id, param|
×
UNCOV
103
          group_id, subcommunity = id.split("_")
×
UNCOV
104
          selected_group = Group.find_by(id: group_id)
×
105

UNCOV
106
          if parameter_enables_messaging?(param)
×
UNCOV
107
            selected_group.enable_messages_for(user: @user, subcommunity:)
×
UNCOV
108
          else
×
UNCOV
109
            selected_group.disable_messages_for(user: @user, subcommunity:)
×
UNCOV
110
          end
×
UNCOV
111
        end
×
UNCOV
112
      end
×
UNCOV
113
    end
×
UNCOV
114
end
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc