-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathusers_controller.rb
More file actions
150 lines (132 loc) · 4.56 KB
/
Copy pathusers_controller.rb
File metadata and controls
150 lines (132 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
# The controller for actions related to the Users model
class UsersController < ApplicationController
before_action -> { ensure_feature_enabled('invitation') }, only: [:invitees]
prepend_before_action :set_user, only: %i[show edit update destroy change_token]
prepend_before_action :init_user, only: [:create]
before_action :set_breadcrumbs
before_action :check_profile_id, only: [:update]
include ActionView::Helpers::TextHelper
# GET /users
# GET /users.json
def index
@users = User.visible
@users = @users.with_query(params[:q].chomp('*')) if params[:q].present?
@users = @users.paginate(page: params[:page], per_page: 50)
respond_to do |format|
format.html
format.json
format.json_api { render(json: @users, links: { self: users_path }) }
end
end
# GET/invitees
def invitees
if current_user.is_admin? || current_user.is_curator?
@users = User.invited
respond_to do |format|
format.html
end
else
redirect_to users_path
end
end
# GET /users/1
# GET /users/1.json
def show
respond_to do |format|
format.html
format.json
format.json_api { render json: @user }
end
end
# GET /users/1/edit
def edit
authorize @user
end
# POST /users
# POST /users.json
def create
authorize User
@user.assign_attributes(user_params)
logger.info "PARAMS: #{user_params}"
logger.info "USER: #{@user.inspect}"
respond_to do |format|
if @user.save
@user.create_activity :create, owner: current_user
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
# THIS IS FOR UPDATING PROFILES
def update
authorize @user
respond_to do |format|
if @user.update(user_params)
@user.create_activity :update, owner: current_user
format.html { redirect_to @user, notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
format.js { head :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
format.js { head :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
authorize @user
@user.create_activity :destroy, owner: current_user
@user.destroy
respond_to do |format|
format.html { redirect_to users_path, notice: 'User was successfully destroyed.' } # Devise is also doing redirection here
format.json { head :no_content }
end
end
def change_token
authorize @user
handle_error(:unprocessable_entity, 'Authentication token cannot be set to nil - action not allowed (status code: 422 Unprocessable Entity).') and return if @user.authentication_token.nil?
@user.authentication_token = Devise.friendly_token
if @user.save
flash[:notice] = 'Authentication token successfully regenerated.'
redirect_to @user
else
handle_error(:unprocessable_entity, 'Failed to regenerate Authentication token (status code: 422 Unprocessable Entity).')
end
end
private
def set_user
@user = User.friendly.find(params[:id])
end
# Need to do this before `user_params` is called, to ensure policy(@user).change_role? works
def init_user
@user = User.new
end
def user_params
allowed_parameters = [:email, :username, :password, :image, :image_url, {
profile_attributes: [:id, :firstname, :surname, :email, :website, :public,
:description, :location, :orcid, :experience,
{ expertise_academic: [] }, { expertise_technical: [] },
{ interest: [] }, { activity: [] }, { language: [] },
{ fields: [] }, { social_media: [] }]
}]
allowed_parameters << :role_id if policy(@user).change_role?
allowed_parameters << :check_broken_scrapers if @user.is_admin?
params.require(:user).permit(allowed_parameters)
end
# Prevent assigning other profiles
def check_profile_id
profile_id = @user.profile.id
incoming_id = user_params.dig(:profile_attributes, :id)
if profile_id && incoming_id && profile_id.to_i != incoming_id.to_i
handle_error(:forbidden, 'Invalid profile ID.')
end
end
end