/
user_controller.rb
283 lines (239 loc) · 8.12 KB
/
user_controller.rb
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require 'base64'
require 'event'
class Webui::UserController < Webui::WebuiController
include Webui::WebuiHelper
include Webui::NotificationSettings
before_filter :check_display_user, :only => [:show, :edit, :requests, :list_my, :delete, :save, :confirm, :admin, :lock]
before_filter :require_login, :only => [:edit, :save, :notifications, :update_notifications, :index]
before_filter :require_admin, :only => [:edit, :delete, :lock, :confirm, :admin, :index]
skip_before_action :check_anonymous, only: [:do_login]
def index
@users = User.all_without_nobody
end
def logout
logger.info "Logging out: #{session[:login]}"
reset_session
User.current = nil
if CONFIG['proxy_auth_mode'] == :on
redirect_to CONFIG['proxy_auth_logout_page']
else
redirect_to root_path
end
end
def login
end
def do_login
mode = CONFIG['proxy_auth_mode'] || CONFIG['ichain_mode'] || :basic
logger.debug "do_login: with #{mode}"
case mode
when :on
user = User.authenticate(request.env['HTTP_X_USERNAME'])
when :basic, :off
user = User.authenticate(params[:username], params[:password])
end
if user.nil? || (user.state == User::STATES['ichainrequest'] || user.state == User::STATES['unconfirmed'])
redirect_to(user_login_path, error: 'Authentication failed')
return
end
logger.debug "USER found: #{user.login}"
session[:login] = User.current.login
session[:password] = params[:password]
redirect_back_or_to root_path
end
def show
@iprojects = @displayed_user.involved_projects.pluck(:name, :title)
@ipackages = @displayed_user.involved_packages.joins(:project).pluck(:name, 'projects.name as pname')
@owned = @displayed_user.owned_packages
if User.current == @displayed_user
@reviews = @displayed_user.involved_reviews
@patchinfos = @displayed_user.involved_patchinfos
@requests_in = @displayed_user.incoming_requests
@requests_out = @displayed_user.outgouing_requests
@declined_requests = @displayed_user.declined_requests
@user_have_requests = @displayed_user.requests?
end
end
def home
if params[:user].present?
redirect_to :action => :show, user: params[:user]
else
redirect_to :action => :show, user: User.current
end
end
# Request from the user
def requests
sortable_fields = {
0 => :created_at,
3 => :creator,
5 => :priority
}
sorting_field = sortable_fields[params[:iSortCol_0].to_i]
sorting_field ||= :created_at
sorting_dir = params[:sSortDir_0].try(:to_sym)
sorting_dir = :asc unless ["asc", "desc"].include?(params[:sSortDir_0])
@requests = @displayed_user.requests(params[:sSearch])
@requests_count = @requests.count
@requests = @requests.offset(params[:iDisplayStart].to_i).limit(params[:iDisplayLength].to_i).reorder(sorting_field => sorting_dir)
respond_to do |format|
# For jquery dataTable
format.json {
render_json_response_for_dataTable(
echo_next_count: params[:sEcho].to_i + 1,
total_records_count: @displayed_user.requests.count,
total_filtered_records_count: @requests_count,
records: @requests
) do |request|
render_to_string(:partial => "shared/single_request.json", locals: { req: request, no_target: true, hide_state: true }).to_s.split(',')
end
}
end
end
def save
unless User.current.is_admin?
if User.current != @displayed_user
flash[:error] = "Can't edit #{@displayed_user.login}"
redirect_to(:back) and return
end
end
@displayed_user.realname = params[:realname]
@displayed_user.email = params[:email]
if User.current.is_admin?
@displayed_user.state = User::STATES[params[:state]] if params[:state]
@displayed_user.update_globalroles([params[:globalrole]]) if params[:globalrole]
end
@displayed_user.save!
flash[:success] = "User data for user '#{@displayed_user.login}' successfully updated."
redirect_back_or_to :action => 'show', user: @displayed_user
end
def edit
@roles = Role.global_roles
@states = %w(confirmed unconfirmed deleted locked)
end
def delete
@displayed_user.state = User::STATES['deleted']
@displayed_user.save
redirect_back_or_to :action => 'show', user: @displayed_user
end
def confirm
@displayed_user.state = User::STATES['confirmed']
@displayed_user.save
redirect_back_or_to :action => 'show', user: @displayed_user
end
def lock
@displayed_user.state = User::STATES['locked']
@displayed_user.save
redirect_back_or_to :action => 'show', user: @displayed_user
end
def admin
@displayed_user.update_globalroles(%w(Admin))
@displayed_user.save
redirect_back_or_to :action => 'show', user: @displayed_user
end
def save_dialog
@roles = Role.global_roles
render_dialog
end
def user_icon
required_parameters :icon
params[:user] = params[:icon].gsub(/.png$/, '')
icon
end
def icon
required_parameters :user
size = params[:size].to_i || '20'
user = User.find_by_login(params[:user])
if user.nil? or (content = user.gravatar_image(size)) == :none
redirect_to ActionController::Base.helpers.asset_path('default_face.png')
return
end
expires_in 5.hours, public: true
if stale?(etag: Digest::MD5.hexdigest(content))
render text: content, layout: false, content_type: 'image/png'
end
end
def register
opts = { login: params[:login],
email: params[:email],
realname: params[:realname],
password: params[:password],
state: params[:state] }
begin
UnregisteredUser.register(opts)
rescue APIException => e
flash[:error] = e.message
redirect_back_or_to :controller => 'main', :action => 'index' and return
end
flash[:success] = "The account '#{params[:login]}' is now active."
if User.current.is_admin?
redirect_to :controller => :user, :action => :index
else
session[:login] = opts[:login]
User.current = User.find_by_login(session[:login])
if Project.where(name: User.current.home_project_name).exists?
redirect_to project_show_path(User.current.home_project_name)
else
redirect_to :controller => :main, :action => :index
end
end
end
def register_user
end
def password_dialog
render_dialog
end
def change_password
# check the valid of the params
unless User.current.password_equals?(params[:password])
errmsg = 'The value of current password does not match your current password. Please enter the password and try again.'
end
if not params[:new_password] == params[:repeat_password]
errmsg = 'The passwords do not match, please try again.'
end
if params[:password] == params[:new_password]
errmsg = 'The new password is the same as your current password. Please enter a new password.'
end
if errmsg
flash[:error] = errmsg
redirect_to :action => :show, user: User.current
return
end
user = User.current
user.update_password params[:new_password]
user.save!
flash[:success] = 'Your password has been changed successfully.'
redirect_to :action => :show, user: User.current
end
def autocomplete
required_parameters :term
render json: list_users(params[:term])
end
def tokens
required_parameters :q
render json: list_users(params[:q], true)
end
def notifications
@notifications = notifications_for_user(User.current)
end
def update_notifications
User.current.groups_users.each do |gu|
gu.email = params[gu.group.title] == '1'
gu.save
end
update_notifications_for_user(params, User.current)
flash[:notice] = 'Notifications settings updated'
redirect_to action: :notifications
end
protected
def list_users(prefix = nil, hash = nil)
names = []
users = User.arel_table
User.where(users[:login].matches("#{prefix}%")).pluck(:login).each do |user|
if hash
names << { 'name' => user }
else
names << user
end
end
names
end
end