From 9132d309adda6d848b2b9cdb4d56dda3010f8b68 Mon Sep 17 00:00:00 2001 From: Jakub Troszok Date: Sun, 24 Jun 2012 22:26:13 +0200 Subject: [PATCH 1/5] Implementing automatic password creation. --- app/models/user.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 69d5ec71..484853bb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -53,6 +53,14 @@ class User < ActiveRecord::Base scope :blocked, where(:blocked => true) scope :active, where(:blocked => false) + before_validation :generate_password, :on1 => :create + + def generate_password + if self.password.blank? && self.password_confirmation.blank? + self.password = self.password_confirmation = Devise.friendly_token.first(8) + end + end + def self.filter filter_name case filter_name when "admins"; self.admins From 56dbfd2af3174a94d0c5ed93a14dd3244370d2f0 Mon Sep 17 00:00:00 2001 From: Jakub Troszok Date: Sun, 24 Jun 2012 22:38:29 +0200 Subject: [PATCH 2/5] Implementing automatic password generation. --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 484853bb..4367c0c1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -53,7 +53,7 @@ class User < ActiveRecord::Base scope :blocked, where(:blocked => true) scope :active, where(:blocked => false) - before_validation :generate_password, :on1 => :create + before_validation :generate_password, :on => :create def generate_password if self.password.blank? && self.password_confirmation.blank? From bea0583951157cc2d1f8923f5b1bc13837aaa24c Mon Sep 17 00:00:00 2001 From: Jakub Troszok Date: Sun, 24 Jun 2012 22:51:58 +0200 Subject: [PATCH 3/5] Added tests for automatic password generation. --- spec/models/user_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6fe96284..5d0b8cd8 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -22,6 +22,12 @@ describe User do user.identifier.should == "test_mail_com" end + it "should generate password when password is empty" do + user = User.create(:email => "test1@mail.com") + user.password.should eql(user.password_confirmation) + user.password.should_not be_empty + end + it "should have authentication token" do user = Factory(:user) user.authentication_token.should_not == "" From 4426bc1844d4f6d1ebc40dcd3babf1d1b167324b Mon Sep 17 00:00:00 2001 From: Jakub Troszok Date: Tue, 26 Jun 2012 23:59:08 +0200 Subject: [PATCH 4/5] Added option to automaticaly generate passwords for new users. --- app/assets/javascripts/admin.js | 11 +++++++++++ app/models/user.rb | 7 +++++-- app/views/admin/users/_form.html.haml | 21 +++++++++++++++------ spec/models/user_spec.rb | 20 ++++++++++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 app/assets/javascripts/admin.js diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js new file mode 100644 index 00000000..bb0a499a --- /dev/null +++ b/app/assets/javascripts/admin.js @@ -0,0 +1,11 @@ +$(document).ready(function(){ + $('input#user_force_random_password').on('change', function(elem) { + var elems = $('#user_password, #user_password_confirmation'); + + if ($(this).attr('checked')) { + elems.val('').attr('disabled', true); + } else { + elems.removeAttr('disabled'); + } + }); +}); diff --git a/app/models/user.rb b/app/models/user.rb index 4367c0c1..69750922 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,7 +5,10 @@ class User < ActiveRecord::Base :recoverable, :rememberable, :trackable, :validatable, :omniauthable attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, - :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, :theme_id + :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, + :theme_id, :force_random_password + + attr_accessor :force_random_password has_many :users_projects, :dependent => :destroy has_many :projects, :through => :users_projects @@ -56,7 +59,7 @@ class User < ActiveRecord::Base before_validation :generate_password, :on => :create def generate_password - if self.password.blank? && self.password_confirmation.blank? + if self.force_random_password == true self.password = self.password_confirmation = Devise.friendly_token.first(8) end end diff --git a/app/views/admin/users/_form.html.haml b/app/views/admin/users/_form.html.haml index 883a7d72..1e8a44e4 100644 --- a/app/views/admin/users/_form.html.haml +++ b/app/views/admin/users/_form.html.haml @@ -18,12 +18,21 @@ .input = f.text_field :email %span.help-inline * required - .clearfix - = f.label :password - .input= f.password_field :password - .clearfix - = f.label :password_confirmation - .input= f.password_field :password_confirmation + %hr + + -if f.object.new_record? + .clearfix + = f.label :admin, :class => "checkbox" do + = f.check_box :force_random_password + %span Generate random password + + %div.password-fields + .clearfix + = f.label :password + .input= f.password_field :password, :disabled => f.object.force_random_password + .clearfix + = f.label :password_confirmation + .input= f.password_field :password_confirmation, :disabled => f.object.force_random_password %hr .clearfix = f.label :skype diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5d0b8cd8..210abcb3 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -22,10 +22,22 @@ describe User do user.identifier.should == "test_mail_com" end - it "should generate password when password is empty" do - user = User.create(:email => "test1@mail.com") - user.password.should eql(user.password_confirmation) - user.password.should_not be_empty + it "should execute callback when force_random_password specified" do + user = User.new(:email => "test@mail.com", :force_random_password => true) + user.should_receive(:generate_password) + user.save + end + + it "should not generate password by default" do + user = Factory(:user, :password => 'abcdefg', :password_confirmation => 'abcdefg') + user.password.should == 'abcdefg' + end + + it "should generate password when forcing random password" do + Devise.stub(:friendly_token).and_return('123456789') + user = User.create(:email => "test1@mail.com", :force_random_password => true) + user.password.should == user.password_confirmation + user.password.should == '12345678' end it "should have authentication token" do From 5ab1856291d845b27f68c41f23ca76c78afcbce1 Mon Sep 17 00:00:00 2001 From: Jakub Troszok Date: Wed, 27 Jun 2012 00:55:54 +0200 Subject: [PATCH 5/5] Fixing requests tests. --- app/models/user.rb | 2 +- app/views/admin/users/_form.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 69750922..02bfabea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -59,7 +59,7 @@ class User < ActiveRecord::Base before_validation :generate_password, :on => :create def generate_password - if self.force_random_password == true + if self.force_random_password self.password = self.password_confirmation = Devise.friendly_token.first(8) end end diff --git a/app/views/admin/users/_form.html.haml b/app/views/admin/users/_form.html.haml index 1e8a44e4..bd2e1362 100644 --- a/app/views/admin/users/_form.html.haml +++ b/app/views/admin/users/_form.html.haml @@ -23,7 +23,7 @@ -if f.object.new_record? .clearfix = f.label :admin, :class => "checkbox" do - = f.check_box :force_random_password + = f.check_box :force_random_password, {}, true, nil %span Generate random password %div.password-fields