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

pulibrary / pdc_describe / 9cd1579d-9684-4b66-ae83-9619a7d64644

pending completion
9cd1579d-9684-4b66-ae83-9619a7d64644

Pull #1094

circleci

GitHub
Merge branch 'main' into sidekiq-prod
Pull Request #1094: Adding redis and sidekiq

1628 of 2126 relevant lines covered (76.58%)

97.58 hits per line

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

85.19
/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
  COLLECTION_MESSAGING_DISABLED = "0"
1✔
5
  COLLECTION_MESSAGING_ENABLED = "1"
1✔
6

7
  before_action :set_user, only: %i[show edit update]
1✔
8

9
  def index
1✔
10
    @users = User.all
×
11
  end
12

13
  # GET /users/1
14
  def show
1✔
15
    @search_terms = params["q"].presence
41✔
16
    @can_edit = can_edit?
41✔
17
    @my_dashboard = current_user.id == @user.id
41✔
18
    @unfinished_works = WorkList.unfinished_works(@user, @search_terms)
41✔
19
    @completed_works = WorkList.completed_works(@user, @search_terms)
41✔
20
    @withdrawn_works = WorkList.withdrawn_works(@user, @search_terms)
41✔
21
    @works_found = @unfinished_works.length + @completed_works.length + @withdrawn_works.length
41✔
22
  end
23

24
  # GET /users/1/edit
25
  def edit
1✔
26
    unless can_edit?
5✔
27
      Rails.logger.warn("Unauthorized to edit user #{@user.id} (current user: #{current_user.id})")
×
28
      redirect_to user_path(@user)
×
29
    end
30
  end
31

32
  # PATCH/PUT /users/1 or /users/1.json
33
  def update
1✔
34
    if can_edit?
3✔
35
      respond_to do |format|
3✔
36
        update_collections_with_messaging if user_params.key?(:collections_with_messaging)
3✔
37

38
        if @user.update(user_params)
3✔
39
          format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
6✔
40
          format.json { render :show, status: :ok, location: @user }
3✔
41
        else
42
          format.html { render :edit, status: :unprocessable_entity }
×
43
          format.json { render json: @user.errors, status: :unprocessable_entity }
×
44
        end
45
      end
46
    else
47
      Rails.logger.warn("Unauthorized to update user #{@user.id} (current user: #{current_user.id})")
×
48
      redirect_to user_path(@user)
×
49
    end
50
  end
51

52
  private
1✔
53

54
    # Use callbacks to share common setup or constraints between actions.
55
    def set_user
1✔
56
      user_id = user_id_from_url
49✔
57
      @user = User.friendly.find(user_id)
49✔
58
      redirect_to action: action_name, id: @user.friendly_id, status: :moved_permanently unless @user.friendly_id == user_id
49✔
59
    end
60

61
    def user_id_from_url
1✔
62
      # For external users UID is in the form `user-name@gmail.com`, however, Rails eats the ".com" from
63
      # the UID and dumps it into the `format` param. Here we make sure the ".com" is preserved when the
64
      # UID looks to be an external user id.
65
      external_uid = params[:id].include?("@")
49✔
66
      if external_uid && params["format"] == "com"
49✔
67
        "#{params[:id]}.#{params['format']}"
×
68
      else
69
        params[:id]
49✔
70
      end
71
    end
72

73
    # Only allow a list of trusted parameters through.
74
    def user_params
1✔
75
      @user_params ||= params.require(:user).permit([:display_name, :full_name, :family_name, :orcid, :email_messages_enabled, collections_with_messaging: {}])
10✔
76
    end
77

78
    def can_edit?
1✔
79
      current_user.id == @user.id or current_user.super_admin?
49✔
80
    end
81

82
    def parameter_enables_messaging?(form_value)
1✔
83
      form_value.to_s == COLLECTION_MESSAGING_ENABLED
5✔
84
    end
85

86
    def update_collections_with_messaging
1✔
87
      if user_params.key?(:collections_with_messaging)
2✔
88
        extracted = user_params.extract!(:collections_with_messaging)
2✔
89
        collections_with_messaging = extracted[:collections_with_messaging]
2✔
90

91
        collections_with_messaging.each_pair do |collection_id, param|
2✔
92
          selected_collection = Collection.find_by(id: collection_id)
5✔
93

94
          if parameter_enables_messaging?(param)
5✔
95
            @user.enable_messages_from(collection: selected_collection)
2✔
96
          else
97
            @user.disable_messages_from(collection: selected_collection)
3✔
98
          end
99
        end
100
      end
101
    end
102
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