Commit 4426bc1844d4f6d1ebc40dcd3babf1d1b167324b
1 parent
bea05839
Exists in
master
and in
4 other branches
Added option to automaticaly generate passwords for new users.
Showing
4 changed files
with
47 additions
and
12 deletions
Show diff stats
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +$(document).ready(function(){ | ||
2 | + $('input#user_force_random_password').on('change', function(elem) { | ||
3 | + var elems = $('#user_password, #user_password_confirmation'); | ||
4 | + | ||
5 | + if ($(this).attr('checked')) { | ||
6 | + elems.val('').attr('disabled', true); | ||
7 | + } else { | ||
8 | + elems.removeAttr('disabled'); | ||
9 | + } | ||
10 | + }); | ||
11 | +}); |
app/models/user.rb
@@ -5,7 +5,10 @@ class User < ActiveRecord::Base | @@ -5,7 +5,10 @@ class User < ActiveRecord::Base | ||
5 | :recoverable, :rememberable, :trackable, :validatable, :omniauthable | 5 | :recoverable, :rememberable, :trackable, :validatable, :omniauthable |
6 | 6 | ||
7 | attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, | 7 | attr_accessible :email, :password, :password_confirmation, :remember_me, :bio, |
8 | - :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, :theme_id | 8 | + :name, :projects_limit, :skype, :linkedin, :twitter, :dark_scheme, |
9 | + :theme_id, :force_random_password | ||
10 | + | ||
11 | + attr_accessor :force_random_password | ||
9 | 12 | ||
10 | has_many :users_projects, :dependent => :destroy | 13 | has_many :users_projects, :dependent => :destroy |
11 | has_many :projects, :through => :users_projects | 14 | has_many :projects, :through => :users_projects |
@@ -56,7 +59,7 @@ class User < ActiveRecord::Base | @@ -56,7 +59,7 @@ class User < ActiveRecord::Base | ||
56 | before_validation :generate_password, :on => :create | 59 | before_validation :generate_password, :on => :create |
57 | 60 | ||
58 | def generate_password | 61 | def generate_password |
59 | - if self.password.blank? && self.password_confirmation.blank? | 62 | + if self.force_random_password == true |
60 | self.password = self.password_confirmation = Devise.friendly_token.first(8) | 63 | self.password = self.password_confirmation = Devise.friendly_token.first(8) |
61 | end | 64 | end |
62 | end | 65 | end |
app/views/admin/users/_form.html.haml
@@ -18,12 +18,21 @@ | @@ -18,12 +18,21 @@ | ||
18 | .input | 18 | .input |
19 | = f.text_field :email | 19 | = f.text_field :email |
20 | %span.help-inline * required | 20 | %span.help-inline * required |
21 | - .clearfix | ||
22 | - = f.label :password | ||
23 | - .input= f.password_field :password | ||
24 | - .clearfix | ||
25 | - = f.label :password_confirmation | ||
26 | - .input= f.password_field :password_confirmation | 21 | + %hr |
22 | + | ||
23 | + -if f.object.new_record? | ||
24 | + .clearfix | ||
25 | + = f.label :admin, :class => "checkbox" do | ||
26 | + = f.check_box :force_random_password | ||
27 | + %span Generate random password | ||
28 | + | ||
29 | + %div.password-fields | ||
30 | + .clearfix | ||
31 | + = f.label :password | ||
32 | + .input= f.password_field :password, :disabled => f.object.force_random_password | ||
33 | + .clearfix | ||
34 | + = f.label :password_confirmation | ||
35 | + .input= f.password_field :password_confirmation, :disabled => f.object.force_random_password | ||
27 | %hr | 36 | %hr |
28 | .clearfix | 37 | .clearfix |
29 | = f.label :skype | 38 | = f.label :skype |
spec/models/user_spec.rb
@@ -22,10 +22,22 @@ describe User do | @@ -22,10 +22,22 @@ describe User do | ||
22 | user.identifier.should == "test_mail_com" | 22 | user.identifier.should == "test_mail_com" |
23 | end | 23 | end |
24 | 24 | ||
25 | - it "should generate password when password is empty" do | ||
26 | - user = User.create(:email => "test1@mail.com") | ||
27 | - user.password.should eql(user.password_confirmation) | ||
28 | - user.password.should_not be_empty | 25 | + it "should execute callback when force_random_password specified" do |
26 | + user = User.new(:email => "test@mail.com", :force_random_password => true) | ||
27 | + user.should_receive(:generate_password) | ||
28 | + user.save | ||
29 | + end | ||
30 | + | ||
31 | + it "should not generate password by default" do | ||
32 | + user = Factory(:user, :password => 'abcdefg', :password_confirmation => 'abcdefg') | ||
33 | + user.password.should == 'abcdefg' | ||
34 | + end | ||
35 | + | ||
36 | + it "should generate password when forcing random password" do | ||
37 | + Devise.stub(:friendly_token).and_return('123456789') | ||
38 | + user = User.create(:email => "test1@mail.com", :force_random_password => true) | ||
39 | + user.password.should == user.password_confirmation | ||
40 | + user.password.should == '12345678' | ||
29 | end | 41 | end |
30 | 42 | ||
31 | it "should have authentication token" do | 43 | it "should have authentication token" do |