Project

General

Profile

0001-my-mods-suggestions.patch

Ohad Levy, 09/29/2010 12:24 PM

View differences:

app/controllers/application_controller.rb
13 13
  helper 'layout'
14 14

  
15 15
  before_filter :require_ssl, :require_login
16
  before_filter :welcome, :detect_notices, :only => :index
16 17
  before_filter :load_tabs, :manage_tabs
17
  before_filter :welcome, :only => :index
18
  # We only really need to see these when listing, (with a GET)
19
  before_filter :detect_notices, :only => :index
20 18

  
21 19
  # host list AJAX methods
22 20
  # its located here, as it might be requested from the dashboard controller or via the hosts controller
......
79 77
  end
80 78

  
81 79

  
82
  # Force a user to login if ldap authentication is enabled
80
  # Force a user to login if authentication is enabled
83 81
  # Sets @user and User.current to the logged in user, or to admin if logins are not used
84 82
  def require_login
85
    if SETTINGS[:login]
86
      unless (session[:user] and (@user = User.find(session[:user])))
87
        session[:original_uri] = request.request_uri
88
        redirect_to login_users_path
83
    unless session[:user] and @user = User.find(session[:user])
84
      # User is not found or first login
85
      if SETTINGS[:login]
86
        # authentication is enabled
87
        session[:original_uri] = request.request_uri # keep the old request uri that we can redirect later on
88
        redirect_to login_users_path and return
89
      else
90
        # We assume we always have a user logged in, if authentication is disabled, the user is the build-in admin account.
91
        if @user = User.find_by_login("admin")
92
          session[:user] = @user.id
93
        else
94
          # TODO: Test this!
95
          flash[:foreman_error] = "Unable to find internal system admin account - do you want to fix this?..."
96
        end
89 97
      end
90
    else
91
      session[:user] ||= User.find_by_login "admin"
92
      @user = session[:user]
93 98
    end
94 99
    User.current = @user
95 100
  end
96 101

  
97 102
  # returns current user
98 103
  def current_user
99
    @username
104
    @user
100 105
  end
101 106

  
102 107
  def invalid_request
......
127 132

  
128 133
  private
129 134
  def detect_notices
130
    @notices = User.current.try :notices
135
    @notices = current_user.notices
131 136
  end
132 137

  
133 138
  def active_tab=(value); @active_tab = session[:controller_active_tabs][controller_name] = value; end
app/controllers/notices_controller.rb
1 1
class NoticesController < ApplicationController
2
  def acknowledge
2
  def destroy
3 3
    @notice = Notice.find(params[:id])
4 4
    if @notice.global
5 5
      @notice.destroy
6 6
    else
7
      @notice.users.delete(User.current)
7
      @notice.users.delete current_user
8 8
      @notice.destroy unless @notice.users.any?
9 9
    end
10 10
    redirect_to :back
app/models/notice.rb
1 1
class Notice < ActiveRecord::Base
2 2
  has_and_belongs_to_many :users, :join_table =>'user_notices'
3 3

  
4
  validates_inclusion_of :level, :in => %w{warning message}
4
  TYPES = %w{message warning}
5
  before_validation :set_default_notice_level
6
  validates_inclusion_of :level, :in => TYPES
5 7

  
8
  validates_presence_of :content
6 9
  before_save :add_to_all_users
7 10

  
11
  def to_s
12
    "#{global? ? "global" : "individual"} #{content}"
13
  end
14

  
15
  private
16

  
8 17
  def add_to_all_users
9 18
    self.users = User.all
10 19
  end
11 20

  
12
  def to_s
13
    "#{global? ? "global" : "individual"} #{content[0,20]}"
21
  def set_default_notice_level
22
    self.level ||= TYPES.first
14 23
  end
24

  
15 25
end
app/models/user.rb
21 21
  validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
22 22
  validates_length_of :mail, :maximum => 60, :allow_nil => true
23 23

  
24
  before_destroy Ensure_not_used_by.new(:hosts)
24
  before_destroy Ensure_not_used_by.new(:hosts), :ensure_admin_is_not_deleted
25

  
25 26
  validate :name_used_in_a_usergroup
26 27
  before_validation :prepare_password
27 28

  
......
94 95
    auth_source and auth_source.can_set_password?
95 96
  end
96 97

  
97
  private
98
 private
98 99

  
99 100
  def prepare_password
100 101
    unless password.blank?
......
113 114
    end
114 115
  end
115 116

  
117
  # The internal Admin Account is always available
118
  # this is required as when not using external authentication, the systems logs you in with the
119
  # admin account automatically
120
  def ensure_admin_is_not_deleted
121
    if login == "admin"
122
      errors.add_to_base "Can't Delete Internal Admin account"
123
      logger.warn "Unable to delete Internal Admin Account"
124
      return false
125
    end
126
  end
127

  
116 128
end
app/views/common/_notice.erb
1
<% unless (@notices.empty?) -%>
2
  <div id="notice" style="clear:both;">
3
    <% field_set_tag "Notifications" do %>
4
      <table width="100%">
5
        <% for notice in  @notices-%>
6
          <tr>
7
            <td style="width:1%"><img src="images/<%= notice.level -%>.png"></td>
8
            <td>
9
              <%= notice.content %>
10
            </td>
11
            <td align="right" style="width:1%">
12
              <% link_to notice_ack_path(notice) do -%>
13
                <img src="images/close_hl.png">
14
              <% end -%>
15
            </td>
16
          </tr>
17
        <% end %>
18
      </table>
19
    <% end %>
20
  </div>
21
  <%= link_to_function "Toggle notifications section", toggle_div(:notice), :style => "float:right;color:red"%>
22
<% end %>
1
<div id="notice" style="clear:both;">
2
  <% field_set_tag "Notifications" do %>
3
    <table width="100%">
4
      <% for notice in  @notices-%>
5
        <tr>
6
          <td style="width:1%"><img src="images/<%= notice.level -%>.png"></td>
7
          <td>
8
            <%= truncate notice.content, 100 %>
9
          </td>
10
          <td align="right" style="width:1%">
11
            <% link_to notice, :method => :delete do -%>
12
              <img src="images/close_hl.png">
13
            <% end -%>
14
          </td>
15
        </tr>
16
      <% end %>
17
    </table>
18
  <% end %>
19
</div>
20
<%= link_to_function "Toggle notifications section", toggle_div(:notice), :style => "float:right;color:red"%>
app/views/layouts/standard.rhtml
17 17
    <%= render "common/searchbar" rescue "<!-- The search facility is not available in this controllers -->" unless params[:action] == 'show' %>
18 18
    <%= content_tag('div', flash[:foreman_error],  :class => 'flash error')  if flash[:foreman_error] -%>
19 19
    <%= content_tag('div', flash[:foreman_notice], :class => 'flash notice') if flash[:foreman_notice] -%>
20
    <%= render 'common/notice' -%>
20
    <%= render 'common/notice' unless @notices.empty? -%>
21 21
    <div id="content">
22 22
      <%- if show_title? -%>
23 23
        <h1><%=h yield(:title) %></h1>
config/routes.rb
18 18
      :multiple_enable => :get, :submit_multiple_enable => :post}
19 19
  map.dashboard '/dashboard', :controller => 'dashboard'
20 20
  map.statistics '/statistics', :controller => 'statistics'
21
  map.notice_ack '/acknowlege/:id', :controller => 'notices', :action => 'acknowledge'
21
  map.resources :notices, :only => :destroy
22 22
  map.resources :audits
23 23
  map.resources :usergroups
24 24
  map.resources :lookup_keys
test/functional/notices_controller_test.rb
2 2

  
3 3
class NoticesControllerTest < ActionController::TestCase
4 4
  def setup
5
    #User.current = User.first
6 5
    @notice = Notice.create :global => false, :content => "hello", :level => "message"
7 6
    @request.env['HTTP_REFERER'] = hosts_path
8 7
  end
9 8

  
10 9
  def test_acknowledge_for_global
11
    @notice = Notice.create :global => true, :content => "hello", :level => "message"
10
    @new_notice = Notice.create :global => true, :content => "hello", :level => "warning"
12 11
    original = Notice.count
13
    get :acknowledge, {:id => @notice.id}, set_session_user
12
    delete :destroy, {:id => @new_notice}, set_session_user
14 13
    final = Notice.count
15 14
    assert original == final + 1
16 15
  end
17 16

  
18 17
  def test_acknowledge_for_individual
19 18
    original = User.current.notices.count
20
    get :acknowledge, {:id => @notice.id}, set_session_user
19
    delete :destroy, {:id => @notice}, set_session_user
21 20
    final = User.current.notices.count
22 21
    assert (original == final + 1)
23 22
  end
......
25 24
  def test_notice_is_finally_deleted
26 25
    for user in User.all do
27 26
      User.current = user
28
      get :acknowledge, {:id => @notice.id}, {:user => user}
27
      delete :destroy, {:id => @notice}, set_session_user
29 28
    end
30 29
    assert Notice.count == 0
31 30
  end
test/unit/user_test.rb
82 82
    assert_equal nil, User.try_to_login("anything", "")
83 83
  end
84 84
  # couldn't continue testing the rest of login method cause use auth_source.authenticate, which is not implemented yet
85

  
86
  test  "should not be able to delete the admin account" do
87
    assert !User.find_by_login("admin").delete
88
  end
85 89
end
86 90

  
87
-