rosa-build/app/controllers/admin/users_controller.rb

93 lines
2.7 KiB
Ruby
Raw Normal View History

class Admin::UsersController < Admin::BaseController
include AvatarHelper
prepend_before_filter :find_user
2014-01-21 04:51:49 +00:00
load_and_authorize_resource collection: [:system, :list]
2012-03-20 18:11:28 +00:00
def index
@filter = params[:filter] || 'all'
end
2012-12-28 19:24:41 +00:00
def system
2013-01-11 10:03:24 +00:00
@users, @filter = @users.system, 'system'
2013-01-10 15:50:49 +00:00
render :index
2012-12-28 19:24:41 +00:00
end
2012-03-20 18:11:28 +00:00
def new
2012-12-28 19:24:41 +00:00
@user.role = 'system' if params[:system] == 'true'
2012-03-20 18:11:28 +00:00
end
def create
2012-03-22 14:50:15 +00:00
@user.role = params[:role]
2012-12-28 19:24:41 +00:00
@user.email, @user.password = "#{@user.uname}@rosalinux.ru", SecureRandom.base64 if @user.system?
@user.confirmed_at = Time.now.utc
if (@user.save rescue false)
2012-03-20 18:11:28 +00:00
flash[:notice] = t('flash.user.saved')
2012-12-29 12:38:14 +00:00
flash[:warning] = @user.authentication_token
2012-12-28 19:24:41 +00:00
redirect_to(@user.system? ? system_admin_users_path : admin_users_path)
2012-03-20 18:11:28 +00:00
else
flash[:error] = t('flash.user.save_error')
flash[:warning] = @user.errors.full_messages.join('. ')
2012-12-28 19:24:41 +00:00
@system = @user.system?
2014-01-21 04:51:49 +00:00
render action: :new
2012-03-20 18:11:28 +00:00
end
end
def edit
2012-03-20 18:11:28 +00:00
end
def update
2012-03-22 14:50:15 +00:00
@user.role = params[:role]
2012-03-20 18:11:28 +00:00
if @user.update_without_password(params[:user])
update_avatar(@user, params)
2012-03-20 18:11:28 +00:00
flash[:notice] = t('flash.user.saved')
redirect_to admin_users_path
2012-03-20 18:11:28 +00:00
else
flash[:error] = t('flash.user.save_error')
flash[:warning] = @user.errors.full_messages.join('. ')
2014-01-21 04:51:49 +00:00
render action: :edit
2012-03-20 18:11:28 +00:00
end
end
def destroy
@user.destroy
flash[:notice] = t("flash.user.destroyed")
2012-12-29 12:38:14 +00:00
redirect_to(@user.system? ? system_admin_users_path : admin_users_path)
2012-03-20 18:11:28 +00:00
end
def list
2013-01-11 10:03:24 +00:00
if params[:system] != 'true'
colName, @users = %w(name uname email created_at), @users.opened
2013-01-11 10:03:24 +00:00
else
colName, @users, @system_list = ['uname'], @users.system, true
2013-01-10 14:50:12 +00:00
end
2012-03-20 18:11:28 +00:00
sort_col = params[:iSortCol_0] || 0
sort_dir = params[:sSortDir_0]=="asc" ? 'asc' : 'desc'
order = "users.#{colName[sort_col.to_i]} #{sort_dir}"
2012-03-20 18:11:28 +00:00
2014-01-21 04:51:49 +00:00
@users = @users.paginate(page: (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i).to_i + 1, per_page: params[:iDisplayLength])
2012-03-20 18:11:28 +00:00
@total_users = @users.count
if !params[:sSearch].blank? && search = "%#{params[:sSearch]}%"
@users = @users.where('users.name ILIKE ? or users.uname ILIKE ? or users.email ILIKE ?', search, search, search)
end
2012-03-21 18:02:58 +00:00
@filter = params[:filter] || 'all'
2012-12-06 18:04:39 +00:00
@users = @users.send(@filter) if ['real', 'admin', 'banned', 'tester'].include? @filter
2012-03-20 18:11:28 +00:00
@users = @users.order(order)
2014-01-21 04:51:49 +00:00
render partial: 'users_ajax', layout: false
end
2012-12-28 19:24:41 +00:00
def reset_auth_token
@user.reset_authentication_token!
flash[:notice] = t("flash.user.reset_auth_token")
2012-12-29 12:38:14 +00:00
flash[:warning] = @user.authentication_token
redirect_to system_admin_users_path
2012-12-28 19:24:41 +00:00
end
protected
def find_user
@user = User.find_by_uname!(params[:id]) if params[:id]
2012-03-20 18:11:28 +00:00
end
end