NAK -
In 006_create_user_privileges.rb, for the first user you should set :admin_site => true.
In the app/view/emails/edit.html.erb, a little “s” are show.
After update mail’s configuration, we should tell to the user that the server must reboot.
For security reason, we must not show the password in app/view/emails/show.html.erb.
App/view/emails/update.html.erb seems to be useless.
In app/models/mail_config.rb, at line 1 some quotes are here.
All schedules are killed because you try to call load_mail_config. I think you should just call load_mail_config in mailer.rb itself, and put a require in schedules.rb.

I will tell you if I found another bugs. ;)
 
--
Benjamin LAN-SUN-LUK



Le 21/01/09 03:54, « Darryl L. Pierce » <mcpierce@gmail.com> a écrit :

From: Darryl L. Pierce <dpierce@redhat.com>

This patch requires a migration.

This patch removes the need for config/mailer.yml. It also introduces a
new base class, ConfigGroup, for aggregating different properties to
representing a single logical group.

Signed-off-by: Darryl L. Pierce <mcpierce@gmail.com>
---
 app/controllers/admin_controller.rb                |   30 ++++++
 app/controllers/application.rb                     |    1 +
 app/controllers/emails_controller.rb               |   71 ++++++++++++++
 app/controllers/users_controller.rb                |   29 +++---
 app/helpers/emails_helper.rb                       |    2 +
 app/models/config_group.rb                         |   33 +++++++
 app/models/mail_config.rb                          |   28 ++++++
 app/models/user_mailer.rb                          |   27 ++++--
 app/views/admin/index.html.erb                     |    3 +
 app/views/emails/edit.html.erb                     |   55 +++++++++++
 app/views/emails/show.html.erb                     |   51 ++++++++++
 app/views/emails/update.html.erb                   |    2 +
 app/views/layouts/default.html.erb                 |    3 +
 app/views/report/index.html.erb                    |    1 -
 config/initializers/mailer.rb                      |   39 ++++----
 config/initializers/schedules.rb                   |    4 +
 config/mailer.yml.example                          |   12 ---
 config/routes.rb                                   |    6 +
 db/migrate/001_create_users.rb                     |   10 +-
 .../025_add_admin_site_to_user_privileges.rb       |   31 ++++++
 test/fixtures/user_privileges.yml                  |    1 +
 test/functional/emails_controller_test.rb          |  100 ++++++++++++++++++++
 22 files changed, 479 insertions(+), 60 deletions(-)
 create mode 100644 app/controllers/admin_controller.rb
 create mode 100644 app/controllers/emails_controller.rb
 create mode 100644 app/helpers/emails_helper.rb
 create mode 100644 app/models/config_group.rb
 create mode 100644 app/models/mail_config.rb
 create mode 100644 app/views/admin/index.html.erb
 create mode 100644 app/views/emails/edit.html.erb
 create mode 100644 app/views/emails/show.html.erb
 create mode 100644 app/views/emails/update.html.erb
 delete mode 100644 config/mailer.yml.example
 create mode 100644 db/migrate/025_add_admin_site_to_user_privileges.rb
 create mode 100644 test/functional/emails_controller_test.rb

diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb
new file mode 100644
index 0000000..e6b4938
--- /dev/null
+++ b/app/controllers/admin_controller.rb
@@ -0,0 +1,30 @@
+# admin_controller.rb
+# Copyright (C) 2008, Darryl L. Pierce <mcpierce@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+class AdminController < ApplicationController
+  before_filter :authenticated
+  before_filter :can_admin_site
+
+  def can_admin_site
+    unless @user && @user.privileges.admin_site
+      respond_to do |format|
+        flash[:error] = 'You do not have administrative privileges.'
+        format.html { redirect_to error_path }
+      end
+    end
+  end
+end
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index 22c7661..ae9cc2a 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -58,6 +58,7 @@ class ApplicationController < ActionController::Base
   def handle_exceptions
     yield
   rescue Exception => error
+    puts error.message
     puts error.backtrace
     erase_results
     @title = "An Error Has Occurred."
diff --git a/app/controllers/emails_controller.rb b/app/controllers/emails_controller.rb
new file mode 100644
index 0000000..13afd87
--- /dev/null
+++ b/app/controllers/emails_controller.rb
@@ -0,0 +1,71 @@
+# emails_controller.rb
+# Copyright (C) 2008, Darryl L. Pierce <mcpierce@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# Allows an admin to modify the email system.
+class EmailsController < AdminController
+  before_filter :load_config, :only => [:edit, :show, :update]
+
+  # DISABLED
+  def new
+    respond_to do |format|
+      flash[:error] = 'The NEW function is not supported for the email system.'
+      format.html { redirect_to error_path }
+    end
+  end
+
+  # GET /admin/emails/edit
+  def edit
+  end
+
+  # GET /admin/emails
+  def show
+  end
+
+  # PUT /admin/emails
+  def update
+    ConfigProperty.transaction do
+      puts "params[:hostname] == #{params[:hostname]}"
+      @config.hostname = params[:hostname]
+      @config.hostport = params[:hostport]
+      @config.from_address = params[:from_address]
+      @config.username = params[:server_username]
+      @config.password = params[:server_password]
+      @config.auth_type = params[:auth_type]
+      @config.use_tls = params[:use_tls]
+    end
+
+    # restart the email system
+    load_mailer_config
+
+    respond_to do |format|
+      format.html { redirect_to admin_email_path }
+    end
+  end
+
+  # DISABLED
+  def destroy
+    respond_to do |format|
+      format.html { redirect_to error_path }
+    end
+  end
+
+  private
+
+  def load_config
+    @config = MailConfig.new
+  end
+end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 87ca3e2..3706f03 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -63,22 +63,23 @@ class UsersController < ApplicationController
   def create
     respond_to do |format|
       if (@user == nil) || (@user.create_users?)
-        @this_user = User.new(params[:user])
-        @this_user.privileges = UserPrivilege.new
-        @this_user.notifications = Notifications.new
-        @this_user.verification = UserVerification.new(
-          :sent  => Date.today,
-          :token => UserVerification.create_token)
-
-        if @this_user.save
-          UserMailer.deliver_email_verification(@this_user,@this_user.verification.token)
+        User.transaction do
+          @this_user = User.new(params[:user])
+          @this_user.privileges = UserPrivilege.new
+          @this_user.notifications = Notifications.new
+          @this_user.verification = UserVerification.new(:sent  => Date.today,
+                                                         :token => UserVerification.create_token)
+
+          if @this_user.save
+            UserMailer.deliver_email_verification(@this_user,@this_user.verification.token)

-          flash[:message] = "An email verification has been sent to #{@this_user.email}."
-          format.html { redirect_to user_path(@this_user) }
-        else
-          @title = "User Account (New)"
-          @this_user.valid?
-          format.html { render :action => :edit }
+            flash[:message] = "An email verification has been sent to #{@this_user.email}."
+            format.html { redirect_to user_path(@this_user) }
+          else
+            @title = "User Account (New)"
+            @this_user.valid?
+            format.html { render :action => :edit }
+          end
         end
       else
         flash[:error] = "You cannot create a new user account."
diff --git a/app/helpers/emails_helper.rb b/app/helpers/emails_helper.rb
new file mode 100644
index 0000000..b4dc6ec
--- /dev/null
+++ b/app/helpers/emails_helper.rb
@@ -0,0 +1,2 @@
+module EmailsHelper
+end
diff --git a/app/models/config_group.rb b/app/models/config_group.rb
new file mode 100644
index 0000000..5e93d78
--- /dev/null
+++ b/app/models/config_group.rb
@@ -0,0 +1,33 @@
+# Copyright (C) 2008, Darryl L. Pierce <mcpierce@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# A +ConfigGroup+ pulls together several +ConfigProperty+ instances to represent a
+# single, logical group.
+class ConfigGroup
+  class << self
+    def property(field, key, *args)
+      module_eval <<-"end;"
+        def #{field}()
+           ConfigProperty.fetch("#{key}")
+        end
+
+        def #{field}=(val)
+          ConfigProperty.store("#{key}", val)
+        end
+      end;
+    end
+  end
+end
diff --git a/app/models/mail_config.rb b/app/models/mail_config.rb
new file mode 100644
index 0000000..5a3dbf0
--- /dev/null
+++ b/app/models/mail_config.rb
@@ -0,0 +1,28 @@
+''# mail_config.rb
+# Copyright (C) 2008, Darryl L. Pierce <mcpierce@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+class MailConfig < ConfigGroup
+  AUTH_TYPES = {"Login" => "login", "Plain" => "plain"}
+
+  property :hostname,     "email.server.name"
+  property :hostport,     "email.server.port"
+  property :from_address, "email.server.from-address"
+  property :username,     "email.server.username"
+  property :password,     "email.server.password"
+  property :auth_type,    "email.server.auth-type"
+  property :use_tls,      "email.server.use-tls"
+end
diff --git a/app/models/user_mailer.rb b/app/models/user_mailer.rb
index 5609dc8..a50235f 100644
--- a/app/models/user_mailer.rb
+++ b/app/models/user_mailer.rb
@@ -18,32 +18,36 @@
 class UserMailer < ActionMailer::Base
   # Sends an email verification to the specified user.
   def email_verification(user, token)
+    load_mailer_config
     recipients user.email
-    from       MAIL_CONFIG[:from]
+    from       ConfigProperty.fetch("email.from-address")
     subject    "Email verification"
     body       :user => user, :token => token
   end

   # Sends an email to the old address when the user changes his email address.
   def email_change_notification(user, old_email)
+    load_mailer_config
     recipients old_email
-    from       MAIL_CONFIG[:from]
+    from       ConfigProperty.fetch("email.from-address")
     subject    "Email change notification"
     body       :user => user, :old_email => old_email
   end

   # Send an e-mail to an user and notify him his new password.
   def new_generated_password(user, new_password)
+    load_mailer_config
     recipients  user.email
-    from        MAIL_CONFIG[:from]
+    from        ConfigProperty.fetch("email.from-address")
     subject     "Your password"
     body        :user => user, :new_password => new_password
   end

   # Sends an email to a product owner when a user requests a product role.
   def product_role_request(requested_role)
+    load_mailer_config
     recipients requested_role.product.owner.email
-    from       MAIL_CONFIG[:from]
+    from       ConfigProperty.fetch("email.from-address")
     subject    "Role request for #{requested_role.product.name}..."
     body       :requested_role => requested_role
   end
@@ -51,17 +55,19 @@ class UserMailer < ActionMailer::Base
   # Sends an email to the user letting them know the disposition for their
   # product role request.
   def product_role_disposition(role)
+    load_mailer_config
     recipients role.user.email
-    from       MAIL_CONFIG[:from]
+    from       ConfigProperty.fetch("email.from-address")
     subject    "Role request status..."
     body       :role => role
   end

   # Sends an email to a user letting him know his daily activities.
   def daily_updates(user, cc_list, open_items, completed_items, task_performed)
+    load_mailer_config
     recipients user.email
     cc         cc_list
-    from       MAIL_CONFIG[:from]
+    from       ConfigProperty.fetch("email.from-address")
     subject    "Daily updates for #{user.display_name}"
     body       :user => user, :open_items => open_items,
                :completed_items => completed_items, :tasks_performed => task_performed
@@ -70,8 +76,9 @@ class UserMailer < ActionMailer::Base
   # Send an email to a user to notify him that no activity has been detected in
   # his backlog.
   def no_activity_recorded(user, backlog_items)
+    load_mailer_config
     recipients  user.email
-    from        MAIL_CONFIG[:from]
+    from        ConfigProperty.fetch("email.from-address")
     subject     "No activity has been detected"
     body        :user => user, :backlog_items => backlog_items
   end
@@ -79,8 +86,9 @@ class UserMailer < ActionMailer::Base
   # Send an email to a user to notify him that no task has been detected in his
   # backlog.
   def no_task_recorded(user, backlog_items)
+    load_mailer_config
     recipients  user.email
-    from        MAIL_CONFIG[:from]
+    from        ConfigProperty.fetch("email.from-address")
     subject     "No task has been detected"
     body        :user => user, :backlog_items => backlog_items
   end
@@ -88,8 +96,9 @@ class UserMailer < ActionMailer::Base
   # Send an email to a user to notify him the status of each sprint of his
   # products.
   def sprints_products_status(user, products)
+    load_mailer_config
     recipients  user.email
-    from        MAIL_CONFIG[:from]
+    from        ConfigProperty.fetch("email.from-address")
     subject     "Status of sprints"
     body        :user => user, :products => products
   end
diff --git a/app/views/admin/index.html.erb b/app/views/admin/index.html.erb
new file mode 100644
index 0000000..ab027b7
--- /dev/null
+++ b/app/views/admin/index.html.erb
@@ -0,0 +1,3 @@
+<ul>
+  <li><%= link_to "Configuration Email", admin_email_url %></li>
+</ul>
diff --git a/app/views/emails/edit.html.erb b/app/views/emails/edit.html.erb
new file mode 100644
index 0000000..231127a
--- /dev/null
+++ b/app/views/emails/edit.html.erb
@@ -0,0 +1,55 @@
+s<fieldset id="email-config">
+  <legend>Email Configuration</legend>
+  <% form_tag admin_email_path, :method => :put do %>
+  <table class="edit">
+    <tbody>
+      <tr>
+       <td class="label">Server hostname</td>
+       <td class="value"><%= text_field_tag :hostname, @config.hostname %></td>
+      </tr>
+
+      <tr>
+       <td class="label">Server port</td>
+       <td class="value"><%= text_field_tag :hostport, @config.hostport, :maxlength => 5, :size => 5 %></td>
+      </tr>
+
+      <tr>
+       <td class="label">From address</td>
+       <td class="value"<%= text_field_tag :from_address, @config.from_address %></td>
+      </tr>
+
+      <tr>
+       <td class="label">Authentication type</td>
+       <td class="value">
+         <%= select_tag :auth_type,
+             options_for_select(MailConfig::AUTH_TYPES, @config.auth_type) %>
+       </td>
+      </tr>
+
+      <tr>
+       <td class="label">Username</td>
+       <td class="value"><%= text_field_tag :server_username, @config.username %></td>
+      </tr>
+
+      <tr>
+       <td class="label">Password</td>
+       <td class="value"><%= password_field_tag :server_password, @config.password %></td>
+      </tr>
+
+      <tr>
+       <td />
+       <td class="value">
+         <%= check_box_tag :use_tls, true, @config.use_tls %>
+         <%= label_tag :require_tls, "Requires TLS for connections." %>
+       </td>
+      </tr>
+
+      <tr>
+       <td class="buttons" colspan="2">
+         <%= submit_tag "Save" %>
+       </td>
+      </tr>
+    </tbody>
+  </table>
+  <% end %>
+</fieldset>
diff --git a/app/views/emails/show.html.erb b/app/views/emails/show.html.erb
new file mode 100644
index 0000000..24fac52
--- /dev/null
+++ b/app/views/emails/show.html.erb
@@ -0,0 +1,51 @@
+<table class="detail">
+  <thead>
+    <tr>
+      <th class="title" colspan="2">Email Configuration</th>
+    </tr>
+    <tr>
+      <th class="toolbar" colspan="2">
+       <%= link_to(image_tag("icons/edit.png", :title => "Edit this configuration..."),
+       edit_admin_email_url) %>
+      </th>
+    </tr>
+  </thead>
+
+  <tbody>
+    <tr>
+      <td class="label">Hostname:</td>
+      <td class="value"><%= @config.hostname %></td>
+    </tr>
+
+    <tr>
+      <td class="label">Port:</td>
+      <td class="value"><%= @config.hostport %></td>
+    </tr>
+
+    <tr>
+      <td class="label">From address:</td>
+      <td class="value"><%= @config.from_address %></td>
+    </tr>
+
+    <tr>
+      <td class="label">Username:</td>
+      <td class="value"><%= @config.username %></td>
+    </tr>
+
+    <tr>
+      <td class="label">Password:</td>
+      <td class="value"><%= @config.password %></td>
+    </tr>
+
+    <tr>
+      <td class="label">Authentication type:</td>
+      <td class="value"><%= @config.auth_type %></td>
+    </tr>
+
+    <tr>
+      <td class="label">Use TLS:</td>
+      <td class="value"><%= @config.use_tls %></td>
+    </tr>
+
+  </tbody>
+</table>
diff --git a/app/views/emails/update.html.erb b/app/views/emails/update.html.erb
new file mode 100644
index 0000000..4e496a6
--- /dev/null
+++ b/app/views/emails/update.html.erb
@@ -0,0 +1,2 @@
+<h1>Emails#update</h1>
+<p>Find me in app/views/emails/update.html.erb</p>
diff --git a/app/views/layouts/default.html.erb b/app/views/layouts/default.html.erb
index d22ef97..9e27d4d 100644
--- a/app/views/layouts/default.html.erb
+++ b/app/views/layouts/default.html.erb
@@ -32,6 +32,9 @@
       <li><%= link_to "Products (#{@product_count})", products_path %></li>
       <li><%= link_to "Users (#{@user_count})", users_path %></li>
       <li><%= link_to "Reports", :controller => :report, :action => :index  %></li>
+      <% if @user && @user.privileges.admin_site %>
+      <li><%= link_to "Admin", admin_url %></li>
+      <% end %>
     </ul>

     <% if flash[:message] %>
diff --git a/app/views/report/index.html.erb b/app/views/report/index.html.erb
index 4436714..840753b 100644
--- a/app/views/report/index.html.erb
+++ b/app/views/report/index.html.erb
@@ -1,4 +1,3 @@
-
 <table>

   <% form_tag(:action => :effort) do %>
diff --git a/config/initializers/mailer.rb b/config/initializers/mailer.rb
index d660238..f62783e 100644
--- a/config/initializers/mailer.rb
+++ b/config/initializers/mailer.rb
@@ -15,25 +15,26 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.

 # Load the config or error
-mailer_file_path = "#{RAILS_ROOT}/config/mailer.yml"
-if not File.exist?(mailer_file_path)
-    raise "Mailer configuration file not found at #{mailer_file_path}"
-end
-MAIL_CONFIG = YAML.load(File.open(mailer_file_path))

-require 'tlsmail'
+# Initializes the email configuration.
+def load_mailer_config
+  require 'tlsmail'
+
+  Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
+  ActionMailer::Base.delivery_method = :smtp
+  # ActionMailer::Base.perform_deliveries = true
+  # ActionMailer::Base.raise_delivery_errors = truey

-Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
-ActionMailer::Base.delivery_method = MAIL_CONFIG[:delivery_method]
-ActionMailer::Base.perform_deliveries = MAIL_CONFIG[:perform_deliveries]
-ActionMailer::Base.default_charset = MAIL_CONFIG[:default_charset]
-ActionMailer::Base.raise_delivery_errors = MAIL_CONFIG[:raise_delivery_errors]
+  config = MailConfig.new
+
+  ActionMailer::Base.smtp_settings = {
+    :address         => config.hostname,
+    :port            => config.hostport.to_i,
+    :tls             => config.use_tls,
+    :authentication  => config.auth_type,
+    :user_name       => config.username,
+    :password        => config.password
+  }
+end

-ActionMailer::Base.smtp_settings = {
-  :address         => MAIL_CONFIG[:server_name],
-  :port            => MAIL_CONFIG[:server_port],
-  :tls             => MAIL_CONFIG[:require_smtp_tls],
-  :authentication  => MAIL_CONFIG[:server_auth_mode],
-  :user_name       => MAIL_CONFIG[:server_username],
-  :password        => MAIL_CONFIG[:server_password]
-}
+# load_mailer_config
diff --git a/config/initializers/schedules.rb b/config/initializers/schedules.rb
index 16996c2..c95ba3f 100644
--- a/config/initializers/schedules.rb
+++ b/config/initializers/schedules.rb
@@ -29,6 +29,7 @@ SCHEDULES_CONFIG = YAML.load(File.open(schedules_file_path))
 threads = {}

 threads["daily updates"] = Thread.new do
+  load_mail_config
   scheduler = Scheduler.new
   scheduler.start

@@ -87,6 +88,7 @@ threads["daily updates"] = Thread.new do
 end

 threads["daily reminders"] = Thread.new do
+  load_mail_config
   scheduler = Scheduler.new
   scheduler.start

@@ -134,6 +136,7 @@ threads["daily reminders"] = Thread.new do
 end

 threads["product status"] = Thread.new do
+  load_mail_config
   scheduler = Scheduler.new
   scheduler.start

@@ -158,6 +161,7 @@ threads["product status"] = Thread.new do
 end

 threads["user verification expiration"] =  Thread.new do
+  load_mail_config
   scheduler = Scheduler.new
   scheduler.start

diff --git a/config/mailer.yml.example b/config/mailer.yml.example
deleted file mode 100644
index 1dd4aa2..0000000
--- a/config/mailer.yml.example
+++ /dev/null
@@ -1,12 +0,0 @@
----
-:delivery_method: :smtp
-:require_smtp_tls: true
-:perform_deliveries: true
-:default_charset: utf-8
-:raise_delivery_errors: true
-:server_name: smtp.gmail.com
-:server_port: 587
-:server_auth_mode: :login
-:server_username: your_username
-:server_password: your_password
-:from: noreply@projxp.org
diff --git a/config/routes.rb b/config/routes.rb
index 6ae69bc..6bf6212 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -16,6 +16,12 @@
 #

 ActionController::Routing::Routes.draw do |map|
+  map.admin "/admin", :controller => 'admin', :action => 'index'
+
+  map.resource :admin do |admin|
+    admin.resource :email
+  end
+
   map.resources :projects
   map.resources :products do |product|
     product.resources :roles
diff --git a/db/migrate/001_create_users.rb b/db/migrate/001_create_users.rb
index 3349083..49b9e6e 100644
--- a/db/migrate/001_create_users.rb
+++ b/db/migrate/001_create_users.rb
@@ -20,16 +20,16 @@ class CreateUsers < ActiveRecord::Migration
       t.string :display_name,    :null => false, :limit => 128
       t.string :hashed_password, :null => false
       t.string :salt,            :null => false
-      t.text   :introduction,    :null => true
+      t.text   :introduction,    :null => true

       t.timestamps
     end
-   
+
     add_index :users, :email, :unique => true

-    user = User.new(:email        => 'admin@localhost.localdomain',
-      :display_name => 'Admin')
-    user.password = 'chiroxp'
+    user = User.new(:email        => 'admin@projxp.org',
+                    :display_name => 'Admin')
+    user.password = 'projxp'
     user.save!
   end

diff --git a/db/migrate/025_add_admin_site_to_user_privileges.rb b/db/migrate/025_add_admin_site_to_user_privileges.rb
new file mode 100644
index 0000000..cb89cb8
--- /dev/null
+++ b/db/migrate/025_add_admin_site_to_user_privileges.rb
@@ -0,0 +1,31 @@
+# add_admin_site_to_user_privileges.rb
+# Copyright (C) 2009, Darryl L. Pierce <mcpierce@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+class AddAdminSiteToUserPrivileges < ActiveRecord::Migration
+  def self.up
+    add_column :user_privileges, :admin_site, :boolean, :nil => true, :default => false
+    user = User.find(:all).each do |user|
+      user.privileges.admin_site = user.privileges.admin_users && user.privileges.admin_projects
+      puts "Granting admin rights to #{user.display_name}." if user.privileges.admin_site
+      user.privileges.save!
+    end
+  end
+
+  def self.down
+    remove_column :user_privileges, :admin_site
+  end
+end
diff --git a/test/fixtures/user_privileges.yml b/test/fixtures/user_privileges.yml
index 38ffbf7..ba57357 100644
--- a/test/fixtures/user_privileges.yml
+++ b/test/fixtures/user_privileges.yml
@@ -4,6 +4,7 @@ admin_privileges:
     user_id: <%= Fixtures.identify(:admin) %>
     admin_projects: true
     admin_users: true
+    admin_site: true

 projxp_owner_privileges:
     user_id: <%= Fixtures.identify(:projxp_owner) %>
diff --git a/test/functional/emails_controller_test.rb b/test/functional/emails_controller_test.rb
new file mode 100644
index 0000000..c4782be
--- /dev/null
+++ b/test/functional/emails_controller_test.rb
@@ -0,0 +1,100 @@
+# emails_controller.rb
+# Copyright (C) 2009, Darryl L. Pierce <mcpierce@gmail.com>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+require File.dirname(__FILE__) + '/../test_helper'
+
+class EmailsControllerTest < ActionController::TestCase
+  fixtures :users
+
+  def setup
+    @admin = users(:admin)
+    raise "user must be a site admin!" unless @admin.privileges.admin_site
+
+    @nonadmin = users(:mcpierce)
+    raise "User must not be an admin!" if @nonadmin.privileges.admin_site
+
+    @config = {
+    :hostname => "email.server.projxp.org",
+    :hostport => "999",
+    :from_addres => "dude@lebowski.com",
+    :username => "username",
+    :password => "password",
+    :auth_type => "login",
+    :use_tls => "true"}
+  end
+
+  # Ensures that the new action is disabled.
+  def test_new
+    get :new, {}, {:user_id => @admin.id}
+
+    assert_redirected_to error_path
+  end
+
+  # Ensures that anonymous users cannot access the edit page.
+  def test_edit_as_anonymous
+    get :edit
+
+    assert_redirected_to login_path
+  end
+
+  # Ensures that only users with admin privileges can edit the
+  # email configuration.
+  def test_edit_as_nonadmin
+    get :edit, {}, {:user_id => @nonadmin.id}
+
+    assert_redirected_to error_path
+  end
+
+  # Ensures that admins can edit the email configuration.
+  def test_edit
+    get :edit, {}, {:user_id => @admin.id}
+
+    assert_response :success
+  end
+
+  # Ensures anonymous users cannot update the email config.
+  def test_update_as_anonymous
+    put :update
+
+    assert_redirected_to login_url
+  end
+
+  # Ensures that non-admins cannot update the email config.
+  def test_update_as_nonadmin
+    put :update, {}, {:user_id => @nonadmin.id}
+
+    assert_redirected_to error_path
+  end
+
+  # Ensures that admins can update the email config.
+  def test_update
+    put :update,
+    @config,
+    {:user_id => @admin.id}
+
+    assert_redirected_to admin_email_url
+    assert_equal @config[:hostname], ConfigProperty.find_by_name('email.server.name').value,
+    "Values were not properly saved."
+  end
+
+  # Ensures that the destroy method is disabled.
+  def test_destroy
+    delete :destroy, {}, {:user_id => @admin.id }
+
+    assert_redirected_to error_path
+  end
+end
--
1.6.0.2

_______________________________________________
projxp-devel mailing list
projxp-devel@lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/projxp-devel