Class: UsersController

Inherits:
ApplicationController show all
Includes:
Foreman::Controller::AutoCompleteSearch
Defined in:
app/controllers/users_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foreman::Controller::AutoCompleteSearch

#auto_complete_search, #invalid_search_query

Methods inherited from ApplicationController

#api_request?, #welcome

Methods included from Foreman::ThreadSession::Cleaner

#clear_thread, included

Instance Attribute Details

#editing_selfObject

Returns the value of attribute editing_self


9
10
11
# File 'app/controllers/users_controller.rb', line 9

def editing_self
  @editing_self
end

Instance Method Details

#createObject


33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/users_controller.rb', line 33

def create
  admin = params[:user].delete :admin
  @user = User.new(params[:user]){|u| u.admin = admin }
  if @user.save
    @user.roles << Role.find_by_name("Anonymous") unless @user.roles.map(&:name).include? "Anonymous"
    process_success
  else
    process_error
  end
end

#destroyObject


79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/users_controller.rb', line 79

def destroy
  if @user == User.current
    notice _("You are currently logged in, suicidal?")
    redirect_to :back and return
  end
  if @user.destroy
    process_success
  else
    process_error
  end
end

#editObject


44
45
46
47
48
49
# File 'app/controllers/users_controller.rb', line 44

def edit
  if @user.user_facts.count == 0
    user_fact = @user.user_facts.build :operator => "==", :andor => "or"
    user_fact.fact_name_id = FactName.first.id if FactName.first
  end
end

#indexObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/users_controller.rb', line 11

def index
  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

  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
end

#loginObject

Called from the login form. Stores the user id in the session and redirects required URL or default homepage


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/users_controller.rb', line 93

def 
  session[:user] = User.current = nil
  session[:locale] = nil
  if request.post?
    user = User.(params[:login]['login'].downcase, params[:login]['password'])
    if user.nil?
      #failed to authenticate, and/or to generate the account on the fly
      error _("Incorrect username or password")
      redirect_to 
    else
      #valid user
      (user)
    end
  end
end

#logoutObject

Called from the logout link Clears the rails session and redirects to the login action


110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/users_controller.rb', line 110

def logout
  TopbarSweeper.expire_cache(self)
  sso_logout_path = get_sso_method.try(:logout_url)
  session[:user] = @user = User.current = nil
  if flash[:notice] or flash[:error]
    flash.keep
  else
    session.clear
    notice _("Logged out - See you soon")
  end
  redirect_to sso_logout_path || 
end

#newObject


29
30
31
# File 'app/controllers/users_controller.rb', line 29

def new
  @user = User.new
end

#updateObject


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
# File 'app/controllers/users_controller.rb', line 51

def update
  # Remove keys for restricted variables when the user is editing their own account
  if editing_self
    for key in params[:user].keys
      params[:user].delete key unless %w{password_confirmation password mail firstname lastname locale}.include? key
    end
    User.current.editing_self = true
  end

  # Only an admin can update admin attribute of another user
  # this is required, as the admin field is blacklisted above
  admin = params[:user].delete :admin
  @user.admin = admin if User.current.admin

  if @user.update_attributes(params[:user])
    @user.roles << Role.find_by_name("Anonymous") unless @user.roles.map(&:name).include? "Anonymous"
    hostgroup_ids = params[:user]["hostgroup_ids"].reject(&:empty?).map(&:to_i) unless params[:user]["hostgroup_ids"].empty?
    update_hostgroups_owners(hostgroup_ids) unless hostgroup_ids.empty?
    process_success editing_self ? { :success_redirect => hosts_path } : {}
  else
    process_error
  end
  User.current.editing_self = false if editing_self

  # Remove locale from the session when set to "Browser Locale" and editing self
  session.delete(:locale) if params[:user][:locale].try(:empty?) and params[:id].to_i == User.current.id
end