2012-03-20 18:11:28 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-05-02 10:18:07 +01:00
|
|
|
class Admin::UsersController < Admin::BaseController
|
2012-10-03 17:38:42 +01:00
|
|
|
include AvatarHelper
|
2012-05-02 10:18:07 +01:00
|
|
|
prepend_before_filter :find_user
|
2012-12-28 19:24:41 +00:00
|
|
|
load_and_authorize_resource :read => :system
|
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
|
|
|
|
@users = @users.system
|
|
|
|
@filter = 'system'
|
|
|
|
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?
|
2012-03-31 00:37:54 +01:00
|
|
|
@user.confirmed_at = Time.now.utc
|
2012-07-25 22:49:55 +01:00
|
|
|
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')
|
2012-03-26 15:49:00 +01:00
|
|
|
flash[:warning] = @user.errors.full_messages.join('. ')
|
2012-12-28 19:24:41 +00:00
|
|
|
@system = @user.system?
|
|
|
|
|
2012-03-20 18:11:28 +00:00
|
|
|
render :action => :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
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])
|
2012-10-03 17:38:42 +01:00
|
|
|
update_avatar(@user, params)
|
2012-03-20 18:11:28 +00:00
|
|
|
flash[:notice] = t('flash.user.saved')
|
2012-05-02 10:18:07 +01:00
|
|
|
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('. ')
|
2012-05-02 10:18:07 +01: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-10 14:50:12 +00:00
|
|
|
if action_name == 'list'
|
|
|
|
colName, @users = ['users.name', 'users.uname', 'users.email'], @users.opened
|
|
|
|
else # system_list
|
|
|
|
colName, @users = ['users.uname'], @users.system
|
|
|
|
end
|
2012-03-20 18:11:28 +00:00
|
|
|
sort_col = params[:iSortCol_0] || 0
|
|
|
|
sort_dir = params[:sSortDir_0]=="asc" ? 'asc' : 'desc'
|
|
|
|
order = "#{colName[sort_col.to_i]} #{sort_dir}"
|
|
|
|
|
2013-01-10 14:50:12 +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)
|
|
|
|
|
2013-01-10 14:50:12 +00:00
|
|
|
render :partial => "#{action_name == 'system_list' ? 'system_' : ''}users_ajax", :layout => false
|
2012-05-02 10:18:07 +01:00
|
|
|
end
|
|
|
|
|
2012-12-28 19:24:41 +00:00
|
|
|
def system_list
|
2013-01-10 14:50:12 +00:00
|
|
|
list
|
2012-12-28 19:24:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
def find_user
|
|
|
|
@user = User.find_by_uname!(params[:id]) if params[:id]
|
2012-03-20 18:11:28 +00:00
|
|
|
end
|
2012-03-22 17:11:12 +00:00
|
|
|
end
|