Commit 4c1f435ab78e5d4da3d6aaaf20579be13b662d02
Exists in
master
and in
4 other branches
Merge pull request #971 from gingko/master
Automatic password creation..
Showing
4 changed files
with
56 additions
and
7 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 |
@@ -53,6 +56,14 @@ class User < ActiveRecord::Base | @@ -53,6 +56,14 @@ class User < ActiveRecord::Base | ||
53 | scope :blocked, where(:blocked => true) | 56 | scope :blocked, where(:blocked => true) |
54 | scope :active, where(:blocked => false) | 57 | scope :active, where(:blocked => false) |
55 | 58 | ||
59 | + before_validation :generate_password, :on => :create | ||
60 | + | ||
61 | + def generate_password | ||
62 | + if self.force_random_password | ||
63 | + self.password = self.password_confirmation = Devise.friendly_token.first(8) | ||
64 | + end | ||
65 | + end | ||
66 | + | ||
56 | def self.filter filter_name | 67 | def self.filter filter_name |
57 | case filter_name | 68 | case filter_name |
58 | when "admins"; self.admins | 69 | when "admins"; self.admins |
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, {}, true, nil | ||
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,6 +22,24 @@ describe User do | @@ -22,6 +22,24 @@ 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 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' | ||
41 | + end | ||
42 | + | ||
25 | it "should have authentication token" do | 43 | it "should have authentication token" do |
26 | user = Factory(:user) | 44 | user = Factory(:user) |
27 | user.authentication_token.should_not == "" | 45 | user.authentication_token.should_not == "" |