Revision 6874bbd9
Added by Paul Kelly about 14 years ago
- ID 6874bbd96a2988d324bee75a00c288211556ba40
app/controllers/application_controller.rb | ||
---|---|---|
return true unless SETTINGS[:ldap]
|
||
unless (session[:user] and (@user = User.find(session[:user])))
|
||
session[:original_uri] = request.request_uri
|
||
redirect_to login_path
|
||
redirect_to login_users_path
|
||
end
|
||
end
|
||
|
app/controllers/hosts_controller.rb | ||
---|---|---|
def setBuild
|
||
host = Host.find params[:id]
|
||
if host.setBuild != false
|
||
flash[:foreman_notice] = "Enabled #{host.name} for installation boot away"
|
||
flash[:foreman_notice] = "Enabled #{host.name} for rebuild on next boot"
|
||
else
|
||
flash[:foreman_error] = "Failed to enable #{host.name} for installation"
|
||
end
|
app/controllers/users_controller.rb | ||
---|---|---|
filter_parameter_logging :password
|
||
before_filter :require_login, :except => [:login, :logout]
|
||
|
||
def index
|
||
@search = User.search(params[:search])
|
||
@users = @search.paginate(:page => params[:page], :include => [:auth_source], :per_page => 10, :order => "firstname")
|
||
end
|
||
|
||
def new
|
||
@user = User.new
|
||
end
|
||
|
||
def create
|
||
@user = User.new(params[:user])
|
||
if @user.save
|
||
flash[:foreman_notice] = "Successfully created user."
|
||
redirect_to users_url
|
||
else
|
||
render :action => 'new'
|
||
end
|
||
end
|
||
|
||
def edit
|
||
@user = User.find(params[:id])
|
||
end
|
||
|
||
def update
|
||
@user = User.find(params[:id])
|
||
if @user.update_attributes(params[:user])
|
||
flash[:foreman_notice] = "Successfully updated user."
|
||
redirect_to users_url
|
||
else
|
||
render :action => 'edit'
|
||
end
|
||
end
|
||
|
||
def destroy
|
||
@user = User.find(params[:id])
|
||
if @user.destroy
|
||
flash[:foreman_notice] = "Successfully destroyed user."
|
||
else
|
||
flash[:foreman_error] = @user.errors.full_messages.join("<br>")
|
||
end
|
||
redirect_to users_url
|
||
end
|
||
|
||
active_scaffold :users do |config|
|
||
config.label = "Users"
|
||
config.actions.exclude :create
|
||
columns[:firstname].label = "First name"
|
||
columns[:lastname].label = "Surname"
|
||
columns[:admin].label = "Admin"
|
||
config.columns = [:firstname, :lastname, :login, :mail, :admin, :auth_source, :usergroups, :last_login_on]
|
||
config.update.columns = [:firstname, :lastname, :login, :mail, :admin, :auth_source, :last_login_on]
|
||
config.columns[:auth_source].form_ui = :select
|
||
config.columns[:admin].form_ui = :checkbox
|
||
config.columns[:usergroups].clear_link
|
||
list.sorting = {:last_login_on => 'DESC' }
|
||
config.update.columns.exclude :last_login_on
|
||
end
|
||
|
||
# Called from the login form.
|
||
# Stores the username in the session and redirects required URL or default homepage
|
||
def login
|
||
session[:user] = nil
|
||
if request.post?
|
||
... | ... | |
if user.nil?
|
||
#failed to authenticate, and/or to generate the account on the fly
|
||
flash[:foreman_error] = "Incorrect username or password"
|
||
redirect_to login_path
|
||
redirect_to login_users_path
|
||
else
|
||
#valid user
|
||
session[:user] = user.id
|
||
... | ... | |
end
|
||
end
|
||
end
|
||
|
||
# Called from the logout link
|
||
# Clears the rails session and redirects to the login action
|
||
def logout
|
||
... | ... | |
else
|
||
flash[:foreman_notice] = "Logged out - See you soon"
|
||
end
|
||
redirect_to login_path
|
||
redirect_to login_users_path
|
||
end
|
||
|
||
end
|
app/helpers/users_helper.rb | ||
---|---|---|
module UsersHelper
|
||
def last_login_on_column record
|
||
time_ago_in_words(record.last_login_on.getlocal) if record.last_login_on
|
||
time_ago_in_words(record.last_login_on.getlocal) + " ago" if record.last_login_on
|
||
end
|
||
|
||
def admin_column record
|
||
image_tag("true.png", :size => "18x18") if record.admin
|
||
end
|
||
|
||
def auth_source_column record
|
||
record.auth_source.to_label if record.auth_source
|
||
end
|
||
end
|
app/models/auth_source.rb | ||
---|---|---|
"Abstract"
|
||
end
|
||
|
||
def to_label
|
||
if type_before_type_cast.empty?
|
||
logger.warn "Corrupt AuthSource! Record id:#{id} name:#{name} does not have an associated type. This may be due to importing a production database."
|
||
return nil
|
||
end
|
||
|
||
kind = type_before_type_cast.sub("AuthSource","")
|
||
"#{kind.upcase}-#{name}" if name
|
||
end
|
||
|
||
# By default a user may not set their password via Foreman
|
||
# An internal AuthSource should override this and provide a password mechanism
|
||
def can_set_password?
|
||
false
|
||
end
|
||
|
||
# Try to authenticate a user not yet registered against available sources
|
||
def self.authenticate(login, password)
|
||
AuthSource.find(:all).each do |source|
|
app/models/host_mailer.rb | ||
---|---|---|
|
||
def error_state(report)
|
||
host = report.host
|
||
email = host.owner.recipients if SETTINGS[:ldap] and not host.owner.empty?
|
||
email = host.owner.recipients if SETTINGS[:ldap] and not host.owner.nil?
|
||
email = SETTINGS[:administrator] if email.empty?
|
||
raise "unable to find recipients" if email.empty?
|
||
recipients email
|
app/models/user.rb | ||
---|---|---|
has_many :changes, :class_name => 'Audit', :as => :user
|
||
has_many :usergroups, :through => :usergroup_member
|
||
has_many :direct_hosts, :as => :owner, :class_name => "Host"
|
||
has_many :hosts
|
||
|
||
default_scope :order => "firstname"
|
||
|
||
validates_uniqueness_of :login, :message => "already exists"
|
||
validates_presence_of :login, :mail
|
||
... | ... | |
|
||
# The text item to see in a select dropdown menu
|
||
def select_title
|
||
name + " (#{login})"
|
||
to_label + " (#{login})"
|
||
end
|
||
|
||
def self.try_to_login(login, password)
|
app/views/home/_topbar.rhtml | ||
---|---|---|
<% if @user -%>
|
||
<%= link_to "Sign Out #{@user.login}", logout_path %>
|
||
<%= link_to "Sign Out #{@user.login}", logout_users_path %>
|
||
<% end -%>
|
||
<%= link_to 'Wiki', "http://theforeman.org/wiki/foreman" %>
|
||
<%= link_to 'Support', "http://theforeman.org/projects/foreman/boards" %>
|
app/views/operatingsystems/_form.html.erb | ||
---|---|---|
</span>
|
||
</p>
|
||
<p>
|
||
<% field_set_tag("Partition tables") do %>
|
||
<% field_set_tag("Partition tables") do %>
|
||
<%= edit_habtm @operatingsystem, Ptable %>
|
||
<% end -%>
|
||
</p>
|
||
<p>
|
||
<% field_set_tag("Installation Medias") do %>
|
||
<% field_set_tag("Installation Medias") do %>
|
||
<%= edit_habtm @operatingsystem, Media %>
|
||
<% end -%>
|
||
</p>
|
app/views/reports/show.rhtml | ||
---|---|---|
Reported at <%= @report.reported_at.getlocal %>, which is <b><%= time_ago_in_words(@report.reported_at) %> ago</b>
|
||
</div>
|
||
|
||
<% if @report.log.logs.size > 0 -%>
|
||
<% if @report.log.logs.size > 0 -%>
|
||
<div>
|
||
<%= render :partial => 'output', :locals => { :logs => @report.log.logs} %>
|
||
</div>
|
app/views/users/_form.html.erb | ||
---|---|---|
<% form_for @user do |f| %>
|
||
<%= f.error_messages %>
|
||
<table>
|
||
<tr>
|
||
<td> Login<br /> <%= f.text_field :login %> </td>
|
||
</tr>
|
||
<tr>
|
||
<td> First name<br /> <%= f.text_field :firstname %> </td>
|
||
</tr>
|
||
<tr>
|
||
<td> Last name<br /> <%= f.text_field :lastname %> </td>
|
||
</tr>
|
||
<tr>
|
||
<td> Mail<br /> <%= f.text_field :mail %> </td>
|
||
</tr>
|
||
<% if @user.auth_source and @user.auth_source.can_set_password? -%>
|
||
<tr>
|
||
<td> Password<br /> <%= f.password_field :password %> </td>
|
||
</tr>
|
||
<tr>
|
||
<td> Verification<br /> <%= password_field_tag :verification, nil, :size => 30 %> </td>
|
||
</tr>
|
||
<% end -%>
|
||
<tr>
|
||
<td> Authorized by<br />
|
||
<%= f.collection_select :auth_source_id, AuthSource.all.delete_if{|a| a.to_label.nil?}, :id, :to_label %>
|
||
</td>
|
||
</tr>
|
||
<tr>
|
||
<td> Administrator <%= f.check_box :admin %> </td>
|
||
</tr>
|
||
<tr>
|
||
<td> <%= f.submit "Submit" %> </td>
|
||
</tr>
|
||
</table>
|
||
<% end %>
|
app/views/users/edit.html.erb | ||
---|---|---|
<% title "Edit User" %>
|
||
|
||
<%= render :partial => 'form' %>
|
||
|
||
<p>
|
||
<%= link_to "View All", users_path %>
|
||
</p>
|
app/views/users/index.html.erb | ||
---|---|---|
<% title "Users" %>
|
||
|
||
<table class="list">
|
||
<tr>
|
||
<th>Login</th>
|
||
<th>First name</th>
|
||
<th>Last name</th>
|
||
<th>Mail</th>
|
||
<th>Administrator</th>
|
||
<th>Last Logged in</th>
|
||
<th>Authorized by</th>
|
||
<th></th>
|
||
</tr>
|
||
<% for user in @users %>
|
||
<tr class="<%= cycle("even", "odd")-%>" >
|
||
<td><%=link_to h(user.login), edit_user_path(user) %></td>
|
||
<td><%=h user.firstname %></td>
|
||
<td><%=h user.lastname %></td>
|
||
<td><%=h user.mail %></td>
|
||
<td><%=admin_column user %></td>
|
||
<td><%=h last_login_on_column user %></td>
|
||
<td><%=h auth_source_column user %></td>
|
||
<td><%= link_to "Destroy", user, :confirm => 'Are you sure?', :method => :delete %></td>
|
||
</tr>
|
||
<% end %>
|
||
</table>
|
||
|
||
<%= page_entries_info @users %>
|
||
<%= will_paginate @users %>
|
||
|
||
<p><%= link_to "New User", new_user_path %></p>
|
app/views/users/new.html.erb | ||
---|---|---|
<% title "New User" %>
|
||
|
||
<%= render :partial => 'form' %>
|
||
|
||
<p><%= link_to "Back to List", users_path %></p>
|
config/routes.rb | ||
---|---|---|
map.resources :fact_values, :active_scaffold => true
|
||
map.resources :ptables
|
||
map.resources :auth_source_ldaps
|
||
map.login '/login', :controller => 'users', :action => 'login'
|
||
map.logout '/logout', :controller => 'users', :action => 'logout'
|
||
map.resources :users
|
||
map.resources :users, :collection => {:login => [:get, :post], :logout => :get}
|
||
#default
|
||
map.connect ':controller/:action/:id'
|
||
map.connect ':controller/:action/:id.:format'
|
test/functional/hosts_controller_test.rb | ||
---|---|---|
assert_difference 'Host.count' do
|
||
post :create, { :commit => "Create",
|
||
:host => {:name => "myotherfullhost",
|
||
:mac => "aabbecddee00",
|
||
:ip => "123.05.02.25",
|
||
:mac => "aabbecddee06",
|
||
:ip => "123.05.04.25",
|
||
:domain => Domain.find_or_create_by_name("othercompany.com"),
|
||
:operatingsystem => Operatingsystem.first,
|
||
:operatingsystem => Operatingsystem.first,
|
||
:architecture => Architecture.first,
|
||
:environment => Environment.first,
|
||
:disk => "empty partition"
|
||
}
|
||
}
|
||
}, set_session_user
|
||
end
|
||
assert_redirected_to host_url(assigns['host'])
|
||
end
|
||
|
||
test "should get edit" do
|
||
... | ... | |
assert_redirected_to host_url(assigns(:host))
|
||
end
|
||
|
||
def test_destroy
|
||
host = Host.first
|
||
delete :destroy, {:id => host}, set_session_user
|
||
test "should destroy host" do
|
||
assert_difference('Host.count', -1) do
|
||
delete :destroy, {:id => @host.id}, set_session_user
|
||
end
|
||
assert_redirected_to hosts_url
|
||
assert !Host.exists?(host.id)
|
||
end
|
||
|
||
test "externalNodes should render 404 when no params are given" do
|
||
... | ... | |
assert_template :text => @host.info.to_yaml
|
||
end
|
||
|
||
test "when host is saved after setBuild, the flash should informe it" do
|
||
test "when host is saved after setBuild, the flash should inform it" do
|
||
mock(@host).setBuild {true}
|
||
mock(Host).find(@host.id.to_s) {@host}
|
||
@request.env['HTTP_REFERER'] = hosts_path
|
||
... | ... | |
assert_response :found
|
||
assert_redirected_to hosts_path
|
||
assert_not_nil flash[:foreman_notice]
|
||
assert flash[:foreman_notice] == "Enabled myfullhost.company.com for installation boot away"
|
||
assert flash[:foreman_notice] == "Enabled myfullhost.company.com for rebuild on next boot"
|
||
end
|
||
|
||
test "when host is not saved after setBuild, the flash should informe it" do
|
||
test "when host is not saved after setBuild, the flash should inform it" do
|
||
mock(@host).setBuild {false}
|
||
mock(Host).find(@host.id.to_s) {@host}
|
||
@request.env['HTTP_REFERER'] = hosts_path
|
test/functional/lookup_keys_controller_test.rb | ||
---|---|---|
test "should create lookup_keys" do
|
||
assert_difference('LookupKey.count') do
|
||
post :create, {:lookup_key=>{"lookup_values_attributes"=>{"0"=>{"priority"=>"1", "value"=>"x", "_destroy"=>""},
|
||
"1"=>{"priority"=>"2", "value"=>"y", "_destroy"=>""}},
|
||
"key"=>"tests"}
|
||
},
|
||
set_session_user
|
||
"1"=>{"priority"=>"2", "value"=>"y", "_destroy"=>""} }, "key" =>"tests" } }, set_session_user
|
||
end
|
||
|
||
assert_redirected_to lookup_keys_path(assigns(:lookup_keys))
|
test/functional/users_controller_test.rb | ||
---|---|---|
assert u.save!
|
||
logger.info "************ ID = #{u.id}"
|
||
get :edit, {:id => u.id}, set_session_user
|
||
#assert_response :success
|
||
assert_response :success
|
||
end
|
||
|
||
test "should update user" do
|
||
user = User.create :login => "foo", :mail => "foo@bar.com"
|
||
|
||
put :update, { :commit => "Update", :id => user.id, :record => {:login => "johnsmith"} }, set_session_user
|
||
put :update, { :commit => "Submit", :id => user.id, :user => {:login => "johnsmith"} }, set_session_user
|
||
mod_user = User.find_by_id(user.id)
|
||
|
||
assert mod_user.login == "johnsmith"
|
||
assert_redirected_to users_path
|
||
end
|
||
|
||
test "should get show" do
|
||
u = User.create :login => "foo", :mail => "foo@bar.com"
|
||
get :show, {:id => u.id}, set_session_user
|
||
assert_not_nil assigns("record")
|
||
assert_response :success
|
||
end
|
||
|
||
test "should delete" do
|
||
user = User.last
|
||
delete :destroy, {:id => user}, set_session_user
|
test/unit/user_test.rb | ||
---|---|---|
def setup
|
||
@user = User.create :login => "foo", :mail => "foo@bar.com"
|
||
end
|
||
|
||
|
||
test "should have login" do
|
||
u = User.new :mail => "foo@bar.com"
|
||
assert !u.save
|
Also available in: Unified diff
Fixes #232 - Removes AS from Users page
The password verification test has not yet been implemented but will get done in AuthSourceInternal fix
All tests now pass These tests now work whether settings[:ldap] is enabled or not
Plus whitespace fix