Skip to content

Commit b038ec1

Browse files
committed
Merge branch 'tess-1.5'
2 parents a27bd01 + 3224fbf commit b038ec1

13 files changed

Lines changed: 77 additions & 56 deletions

File tree

app/controllers/users_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class UsersController < ApplicationController
66
prepend_before_action :set_user, only: %i[show edit update destroy change_token]
77
prepend_before_action :init_user, only: [:create]
88
before_action :set_breadcrumbs
9+
before_action :check_profile_id, only: [:update]
910

1011
include ActionView::Helpers::TextHelper
1112

@@ -127,7 +128,7 @@ def init_user
127128

128129
def user_params
129130
allowed_parameters = [:email, :username, :password, :image, :image_url, {
130-
profile_attributes: [:firstname, :surname, :email, :website, :public,
131+
profile_attributes: [:id, :firstname, :surname, :email, :website, :public,
131132
:description, :location, :orcid, :experience,
132133
{ expertise_academic: [] }, { expertise_technical: [] },
133134
{ interest: [] }, { activity: [] }, { language: [] },
@@ -141,4 +142,13 @@ def user_params
141142
def invitees_enabled?
142143
feature_enabled?('invitation')
143144
end
145+
146+
# Prevent assigning other profiles
147+
def check_profile_id
148+
profile_id = @user.profile.id
149+
incoming_id = user_params.dig(:profile_attributes, :id)
150+
if profile_id && incoming_id && profile_id.to_i != incoming_id.to_i
151+
handle_error(:forbidden, 'Invalid profile ID.')
152+
end
153+
end
144154
end

app/helpers/search_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def clear_filters_path
1010
end
1111

1212
def facet_title(name, value, html_options = {})
13-
return render_language_name(value) if name == 'language'
13+
lang = render_language_name(value) if name.to_s == 'language'
14+
return lang unless lang.blank?
1415

1516
html_options.delete(:title) || truncate(value.to_s, length: 50)
1617
end

app/models/concerns/searchable.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ def search_and_filter(user, search_params = '', selected_facets = {}, page: 1, s
8686
end
8787
end
8888

89-
if attribute_method?(:public) && !user&.is_admin? # Find a better way of checking this
89+
if name == 'Trainer' || name == 'Profile'
90+
# `public` means "Show in trainer registry" for Trainers/Profiles
91+
any_of do
92+
with(:public, true)
93+
end
94+
elsif attribute_method?(:public) && !user&.is_admin? # Find a better way of checking this
9095
any_of do
9196
with(:public, true)
9297
with(:user_id, user.id) if user

app/models/profile.rb

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,14 @@ class Profile < ApplicationRecord
99
validates :website, url: true, http_url: true, allow_blank: true
1010
validates :orcid, orcid: true, allow_blank: true
1111
after_validation :check_public
12+
after_commit :reindex_trainer
1213
clean_array_fields(:expertise_academic, :expertise_technical, :fields,
1314
:interest, :activity, :language, :social_media)
1415
update_suggestions(:expertise_technical, :interest)
1516

1617
extend FriendlyId
1718
friendly_id :full_name, use: :slugged
1819

19-
after_update_commit :reindex
20-
after_destroy_commit :reindex
21-
22-
if TeSS::Config.solr_enabled
23-
# :nocov:
24-
searchable do
25-
# full text search fields
26-
text :firstname
27-
text :surname
28-
text :description
29-
# sort title
30-
string :sort_title do
31-
full_name.downcase
32-
end
33-
# other fields
34-
integer :user_id
35-
string :full_name
36-
string :location
37-
string :orcid
38-
string :experience do
39-
TrainerExperienceDictionary.instance.lookup_value(self.experience, 'title')
40-
end
41-
string :expertise_academic, multiple: true
42-
string :expertise_technical, multiple: true
43-
string :fields, :multiple => true
44-
string :interest, multiple: true
45-
string :activity, multiple: true
46-
string :language, multiple: true
47-
string :social_media, multiple: true
48-
time :updated_at
49-
boolean :public
50-
end
51-
# :nocov:
52-
end
53-
5420
def self.facet_fields
5521
field_list = %w( full_name )
5622
end
@@ -93,14 +59,14 @@ def check_public
9359
public ? self.type = 'Trainer' : self.type = 'Profile'
9460
end
9561

96-
def reindex
97-
if Rails.env.production?
98-
Trainer.reindex
99-
end
100-
end
101-
10262
def should_generate_new_friendly_id?
10363
firstname_changed? or surname_changed?
10464
end
10565

66+
# This is needed if the `public` status of the profile changes when it is already instantiated - it won't be cast
67+
# to a Trainer object and indexed by solr (only Trainer class is `searchable`).
68+
def reindex_trainer
69+
return unless TeSS::Config.solr_enabled
70+
becomes(Trainer).solr_index
71+
end
10672
end

app/models/trainer.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
# define model for Trainer as subset of Profile
44
class Trainer < Profile
5-
6-
after_update_commit :reindex
7-
after_destroy_commit :reindex
8-
95
extend FriendlyId
106
friendly_id :full_name, use: :slugged
117

app/views/users/_form.html.erb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<% end %>
3838

3939
<% if @user.is_admin?%>
40-
<%= f.input :check_broken_scrapers, label: 'Receive emails with scrapers that have not found anything last period.' %>
40+
<%= user_f.input :check_broken_scrapers %>
4141
<% end %>
4242

4343
<% if TeSS::Config.feature['trainers'] %>
@@ -57,9 +57,8 @@
5757
label: 'Training Experience', prompt: 'Select a level of experience...',
5858
input_html: { title: t('profile.hints.experience') } %>
5959

60-
<%= f.input :language, options: language_options_for_select,
61-
label: 'Languages Spoken', model_name: 'user[profile_attributes]', as: :select,
62-
input_html: { class: 'js-select2' },
60+
<%= f.dropdown :language, options: language_options_for_select,
61+
label: t('simple_form.labels.profile.language'), model_name: 'user[profile_attributes]', as: :select,
6362
errors: @user.profile.errors[:language], title: t('profile.hints.language') %>
6463

6564
<%= f.multi_input :expertise_academic, label: 'Academic expertise',

config/initializers/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
APP_VERSION = '1.5.0'
1+
APP_VERSION = '1.5.1'

config/locales/en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,3 +970,7 @@ en:
970970
visible: Show this event?
971971
material:
972972
visible: Show this material?
973+
user:
974+
check_broken_scrapers: Receive emails with scrapers that have not found anything last period.
975+
profile:
976+
language: Languages Spoken

test/controllers/trainers_controller_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class TrainersControllerTest < ActionController::TestCase
88
assert_response :success
99
trainers = assigns(:trainers)
1010
assert_not_nil trainers
11-
assert_equal 1, trainers.size
11+
assert_equal 2, trainers.size
12+
assert_includes trainers, users(:trainer_user).profile
1213
end
1314

1415
end

test/controllers/users_controller_test.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,19 @@ class UsersControllerTest < ActionController::TestCase
118118
end
119119

120120
test "should update profile" do
121-
sign_in users(:regular_user)
122-
patch :update, params: { id: @user, user: { profile_attributes: { email: 'hot@mail.com' } } }
121+
sign_in @user
122+
profile_id = @user.profile.id
123+
patch :update, params: { id: @user, user: { profile_attributes: { id: profile_id, email: 'hot@mail.com' } } }
123124
assert_redirected_to user_path(assigns(:user))
125+
assert_equal profile_id, assigns(:user).profile.id
126+
end
127+
128+
test "should not update profile ID" do
129+
sign_in @user
130+
another_profile_id = users(:another_regular_user).profile.id
131+
patch :update, params: { id: @user, user: { profile_attributes: { id: another_profile_id, email: 'hot@mail.com' } } }
132+
assert_response :forbidden
133+
assert_not_equal another_profile_id, @user.reload.profile.id
124134
end
125135

126136
test "should reset token" do
@@ -458,4 +468,16 @@ class UsersControllerTest < ActionController::TestCase
458468
assert_response :success
459469
refute assigns(:users).include?(users(:basic_user))
460470
end
471+
472+
test "should get edit for trainers feature enabled" do
473+
user = users(:trainer_user)
474+
sign_in(user)
475+
get :edit, params: { id: user }
476+
assert_response :success
477+
478+
user = users(:admin_trainer)
479+
sign_in(user)
480+
get :edit, params: { id: user }
481+
assert_response :success
482+
end
461483
end

0 commit comments

Comments
 (0)