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

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

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

1.75
/app/controllers/users_controller.rb
1
# frozen_string_literal: true
2
class UsersController < ApplicationController
1✔
3
  # Constants set by the <form> <input> parameters transmitted using POST/PATCH/PUT requests
4
  GROUP_MESSAGING_DISABLED = "0"
×
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)
11
  before_action :set_user, only: %i[show edit update]
×
12
  before_action :authenticate_user!
×
13

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

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

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

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

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

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

60
  private
×
61

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

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.
73
      external_uid = params[:id].include?("@")
×
74
      if external_uid && params["format"] == "com"
×
75
        "#{params[:id]}.#{params['format']}"
×
76
      else
77
        params[:id]
×
78
      end
79
    end
80

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

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

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

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

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

106
          if parameter_enables_messaging?(param)
×
107
            selected_group.enable_messages_for(user: @user, subcommunity:)
×
108
          else
109
            selected_group.disable_messages_for(user: @user, subcommunity:)
×
110
          end
111
        end
112
      end
113
    end
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