when an admin creates an account for another user

  - only login / email are able to be set
  - password is autogenerated
  - email is sent to new user instructing them to login / change pass
---
 src/app/controllers/users_controller.rb           |    9 +++++-
 src/app/models/mailer.rb                          |   27 ++++++++++++++++
 src/app/views/mailer/signup_notification.html.erb |    9 +++++
 src/app/views/users/_form.erb                     |   34 +++++++++++----------
 src/config/environment.rb                         |    4 ++
 5 files changed, 66 insertions(+), 17 deletions(-)
 create mode 100644 src/app/models/mailer.rb
 create mode 100644 src/app/views/mailer/signup_notification.html.erb

diff --git a/src/app/controllers/users_controller.rb 
b/src/app/controllers/users_controller.rb
index 3536c0e..09ce6d1 100644
--- a/src/app/controllers/users_controller.rb
+++ b/src/app/controllers/users_controller.rb
@@ -23,14 +23,21 @@ class UsersController < ApplicationController
   before_filter :require_user, :only => [:show, :edit, :update]
 
   def new
+    require_privilege(Privilege::USER_MODIFY) unless current_user.nil?
     @user = User.new
   end
 
   def create
-    require_privilege(Privilege::USER_MODIFY) unless current_user.nil?
+    unless current_user.nil?
+      require_privilege(Privilege::USER_MODIFY)
+      pass = ActiveSupport::SecureRandom.hex(4) # auto create password
+      params[:user][:password] = pass
+      params[:user][:password_confirmation] = pass
+    end
     @user = User.new(params[:user])
     @registration = RegistrationService.new(@user)
     if @registration.save
+      Mailer.deliver_signup_notification(@user, params[:user][:password]) 
unless current_user.nil? # send email to new user
       flash[:notice] = "User registered!"
       redirect_back_or_default url_for(:action => :show, :id => @user.id)
     else
diff --git a/src/app/models/mailer.rb b/src/app/models/mailer.rb
new file mode 100644
index 0000000..ca9cd6e
--- /dev/null
+++ b/src/app/models/mailer.rb
@@ -0,0 +1,27 @@
+# Copyright (C) 2009 Red Hat, Inc.
+# Written by Mohammed Morsi <[email protected]>
+#
+# 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; version 2 of the License.
+#
+# 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, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+class Mailer < ActionMailer::Base
+  def signup_notification(new_user, password)
+    recipients new_user.email
+    from       "[email protected]"
+    subject    "New Deltacloud user created"
+    body       :user => new_user, :password => password
+    content_type "text/html"
+  end
+end
diff --git a/src/app/views/mailer/signup_notification.html.erb 
b/src/app/views/mailer/signup_notification.html.erb
new file mode 100644
index 0000000..5ee6413
--- /dev/null
+++ b/src/app/views/mailer/signup_notification.html.erb
@@ -0,0 +1,9 @@
+Hello <%= @user.email %>,
+A deltacloud account has been created for you. 
+You may <a href="<%= url_for :controller => 'login', :only_path => false 
%>">login</a> using the 
+following password: <%= @password %>. Please
+<a href="<%= url_for :controller => 'users', :action => 'edit', :only_path => 
false %>">change</a>
+your password as soon as possible.
+
+Thank you,
+  -Deltacloud
diff --git a/src/app/views/users/_form.erb b/src/app/views/users/_form.erb
index 4fcd98c..d70ae4d 100644
--- a/src/app/views/users/_form.erb
+++ b/src/app/views/users/_form.erb
@@ -3,22 +3,24 @@
     <%= form.label :login, "Choose a username" %>
     <%= form.text_field :login %>
   </li>
-  <li>
-    <%= form.label :password, form.object.new_record? ? "Choose a password" : 
"Change password" %>
-    <%= form.password_field :password %>
-  </li>
-  <li>
-    <%= form.label :password_confirmation, "Confirm password" %>
-    <%= form.password_field :password_confirmation %>
-  </li>
-  <li>
-    <%= form.label :first_name %>
-    <%= form.text_field :first_name %>
-  </li>
-  <li>
-    <%= form.label :last_name %>
-    <%= form.text_field :last_name %>
-  </li>
+  <% if current_user.nil? || current_user.id == @user.id %>
+    <li>
+      <%= form.label :password, form.object.new_record? ? "Choose a password" 
: "Change password" %>
+      <%= form.password_field :password %>
+    </li>
+    <li>
+      <%= form.label :password_confirmation, "Confirm password" %>
+      <%= form.password_field :password_confirmation %>
+    </li>
+    <li>
+      <%= form.label :first_name %>
+      <%= form.text_field :first_name %>
+    </li>
+    <li>
+      <%= form.label :last_name %>
+      <%= form.text_field :last_name %>
+    </li>
+  <% end %>
   <li>
     <%= form.label :email %>
     <%= form.text_field :email %>
diff --git a/src/config/environment.rb b/src/config/environment.rb
index 4277fe7..2e8d1b4 100644
--- a/src/config/environment.rb
+++ b/src/config/environment.rb
@@ -73,4 +73,8 @@ Rails::Initializer.run do |config|
   # The default locale is :en and all translations from 
config/locales/*.rb,yml are auto loaded.
   # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', 
'*.{rb,yml}')]
   # config.i18n.default_locale = :de
+
+  config.action_mailer.delivery_method = :smtp
+  config.action_mailer.default_url_options = { :host => "deltacloud.org" }
+  #config.action_mailer.smtp_settings = { :address => "localhost" } # set 
:port, :user_name, :password, :authentication, etc
 end
-- 
1.6.2.5

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to