tuxwebror/app/controllers/users_controller.rb
2018-12-21 19:06:39 +01:00

63 lines
1.5 KiB
Ruby

class UsersController < ApplicationController
def index
@total_users = User.all().length
if params[:key_search]
@users = User.where('id LIKE :key_search OR username LIKE :key_search', key_search: "%#{params[:key_search]}%").page(params[:page])
else
@users = User.order(:id).page(params[:page])
end
end
def show
@user = User.find(params[:id])
@app_title = @user.username
end
def validate_key
row = Session.find_by_token(params[:key])
if row \
and (time_delta(Time.now.utc, row.updated_at) < 10.minutes) \
and (row.is_admin or row.user_id == params[:id])
@user = User.find(params[:id])
session[:identified] = true
session[:identified_at] = Time.now.utc
redirect_to edit_user_path(id: @user.id) and return
else
redirect_to user_path(id: params[:id]), flash: {error: true} and return
end
end
def edit
@user = User.find(params[:id])
if session[:identified] and (time_delta(Time.now.utc, session[:identified_at]) < 10.minutes)
@app_title = @user.username
render "users/edit"
else
redirect_to user_path(id: params[:id]), flash: {error: true} and return
end
end
def update
@user = User.find(params[:id])
@user.update(update_post_params)
redirect_to user_path(id: @user.id)
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def update_post_params
params.require(:user).permit(:os, :config, :pays)
end
def time_delta(new, old)
Time.parse(new.to_s) - Time.parse(old.to_s)
end
end