Project

General

Profile

Download (4.23 KB) Statistics
| Branch: | Tag: | Revision:
1ba05a93 Ohad Levy
class UsersController < ApplicationController
4a8190ef Ohad Levy
include Foreman::Controller::AutoCompleteSearch
1ba05a93 Ohad Levy
d9a2ebac Ohad Levy
skip_before_filter :require_login, :authorize, :session_expiry, :update_activity_time, :only => [:login, :logout]
after_filter :update_activity_time, :only => :login
1ba05a93 Ohad Levy
572a19c3 Paul Kelly
attr_accessor :editing_self

6874bbd9 Paul Kelly
def index
4a8190ef Ohad Levy
begin
users = User.search_for(params[:search], :order => params[:order])
rescue => e
error e.to_s
users = User.search_for('', :order => params[:order]).paginate :page => params[:page]
end
a0c0e14d Ohad Levy
4a8190ef Ohad Levy
respond_to do |format|
format.html do
@users = users.paginate :page => params[:page], :include => [:auth_source]
end
format.json do
render :json => users.all
end
end
6874bbd9 Paul Kelly
end

def new
@user = User.new
end

def create
3013f5e2 Paul Kelly
@user = User.new(params[:user]){|u| u.admin = params[:user][:admin] }
6874bbd9 Paul Kelly
if @user.save
3013f5e2 Paul Kelly
@user.roles << Role.find_by_name("Anonymous") unless @user.roles.map(&:name).include? "Anonymous"
b28fdce4 Ohad Levy
process_success
6874bbd9 Paul Kelly
else
b28fdce4 Ohad Levy
process_error
6874bbd9 Paul Kelly
end
end

def edit
@user = User.find(params[:id])
9fd7478e Paul Kelly
if @user.user_facts.count == 0
user_fact = @user.user_facts.build :operator => "==", :andor => "or"
5d264a2d Ohad Levy
user_fact.fact_name_id = FactName.first.id if FactName.first
9fd7478e Paul Kelly
end
6874bbd9 Paul Kelly
end

def update
@user = User.find(params[:id])
7a4ec5cf Paul Kelly
admin = params[:user].delete :admin
572a19c3 Paul Kelly
# Remove keys for restricted variables when the user is editing their own account
if editing_self
9fd7478e Paul Kelly
for key in params[:user].keys
params[:user].delete key unless %w{password_confirmation password mail firstname lastname}.include? key
end
572a19c3 Paul Kelly
User.current.editing_self = true
9fd7478e Paul Kelly
end
6874bbd9 Paul Kelly
if @user.update_attributes(params[:user])
9fd7478e Paul Kelly
# Only an admin can update admin attribute of another use
# this is required, as the admin field is blacklisted above
@user.update_attribute(:admin, admin) if User.current.admin
74a4cd7e Paul Kelly
@user.roles << Role.find_by_name("Anonymous") unless @user.roles.map(&:name).include? "Anonymous"
ae9fafcc Ohad Levy
process_success editing_self ? { :success_redirect => hosts_path } : {}
6874bbd9 Paul Kelly
else
b28fdce4 Ohad Levy
process_error
6874bbd9 Paul Kelly
end
572a19c3 Paul Kelly
User.current.editing_self = false if editing_self
6874bbd9 Paul Kelly
end

def destroy
8ba2e00a Ohad Levy
@user = User.find(params[:id])
if @user == User.current
754b1a01 Ohad Levy
notice "You are currently logged in, suicidal?"
7a4ec5cf Paul Kelly
redirect_to :back and return
end
8ba2e00a Ohad Levy
if @user.destroy
b28fdce4 Ohad Levy
process_success
6874bbd9 Paul Kelly
else
b28fdce4 Ohad Levy
process_error
6874bbd9 Paul Kelly
end
end
1ba05a93 Ohad Levy
7a4ec5cf Paul Kelly
# Called from the login form.
f5df7d44 Paul Kelly
# Stores the user id in the session and redirects required URL or default homepage
1ba05a93 Ohad Levy
def login
7e64f911 Ohad Levy
session[:user] = User.current = nil
1ba05a93 Ohad Levy
if request.post?
user = User.try_to_login(params[:login]['login'].downcase, params[:login]['password'])
if user.nil?
#failed to authenticate, and/or to generate the account on the fly
754b1a01 Ohad Levy
error "Incorrect username or password"
6874bbd9 Paul Kelly
redirect_to login_users_path
1ba05a93 Ohad Levy
else
#valid user
session[:user] = user.id
uri = session[:original_uri]
session[:original_uri] = nil
4db578f3 Ohad Levy
redirect_to (uri || hosts_path)
1ba05a93 Ohad Levy
end
end
end
# Called from the logout link
# Clears the rails session and redirects to the login action
def logout
9fd7478e Paul Kelly
session[:user] = @user = User.current = nil
754b1a01 Ohad Levy
if flash[:notice] or flash[:error]
1ba05a93 Ohad Levy
flash.keep
else
9fd7478e Paul Kelly
session.clear
754b1a01 Ohad Levy
notice "Logged out - See you soon"
1ba05a93 Ohad Levy
end
6874bbd9 Paul Kelly
redirect_to login_users_path
1ba05a93 Ohad Levy
end

7a4ec5cf Paul Kelly
def auth_source_selected
render :update do |page|
if params[:auth_source_id] and AuthSource.find(params[:auth_source_id]).can_set_password?
017e1049 Ohad Levy
page['#password'].show
7a4ec5cf Paul Kelly
else
017e1049 Ohad Levy
page['#password'].hide
7a4ec5cf Paul Kelly
end
end
end

9fd7478e Paul Kelly
private

def authorize(ctrl = params[:controller], action = params[:action])
572a19c3 Paul Kelly
# Editing self is true when the user is granted access to just their own account details

a8134ab0 Ohad Levy
if action == 'auto_complete_search' and User.current.allowed_to?({:controller => ctrl, :action => 'index'})
return true
end

572a19c3 Paul Kelly
self.editing_self = false
9fd7478e Paul Kelly
return true if User.current.allowed_to?({:controller => ctrl, :action => action})
ae9fafcc Ohad Levy
if (action =~ /edit|update/ and params[:id].to_i == User.current.id)
572a19c3 Paul Kelly
return self.editing_self = true
9fd7478e Paul Kelly
else
deny_access and return
end
end

1ba05a93 Ohad Levy
end