Description:
added random user passwords, better user creation by list.
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@429 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r200:3383214a2837 - - 5 files changed: 104 inserted, 14 deleted
@@ -0,0 +1,39 | |||
|
1 | + %h1 Random user passwords | |
|
2 | + | |
|
3 | + -if @changed | |
|
4 | + %p | |
|
5 | + %b Done! | |
|
6 | + Here's a new password list. | |
|
7 | + Go back to | |
|
8 | + = (link_to '[user list]', :action => 'index') + '.' | |
|
9 | + %br/ | |
|
10 | + %table | |
|
11 | + %tr | |
|
12 | + %th Login | |
|
13 | + %th Fullname | |
|
14 | + %th Password | |
|
15 | + -for u in @non_admin_users | |
|
16 | + %tr | |
|
17 | + %td= u.login | |
|
18 | + %td= u.full_name | |
|
19 | + %td | |
|
20 | + %tt= u.password | |
|
21 | + | |
|
22 | + -else | |
|
23 | + -if @prefix!='' | |
|
24 | + Current prefix: | |
|
25 | + = @prefix | |
|
26 | + -form_tag((url_for :action => 'random_all_passwords'), :method => 'get') do | |
|
27 | + Change prefix | |
|
28 | + =text_field_tag 'prefix' | |
|
29 | + =submit_tag 'Change' | |
|
30 | + | |
|
31 | + This will change passwords of the following users. | |
|
32 | + %ul | |
|
33 | + -for u in @non_admin_users | |
|
34 | + %li= u.login | |
|
35 | + | |
|
36 | + -form_tag((url_for :action => 'random_all_passwords'), :method => 'post') do | |
|
37 | + =hidden_field_tag 'prefix', @prefix | |
|
38 | + Are you sure? | |
|
39 | + =submit_tag 'Go ahead' |
@@ -8,7 +8,9 | |||
|
8 | 8 | end |
|
9 | 9 | |
|
10 | 10 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
11 |
- verify :method => :post, :only => [ :destroy, |
|
|
11 | + verify :method => :post, :only => [ :destroy, | |
|
12 | + :create, :create_from_list, | |
|
13 | + :update ], | |
|
12 | 14 | :redirect_to => { :action => :list } |
|
13 | 15 | |
|
14 | 16 | def list |
@@ -47,19 +49,43 | |||
|
47 | 49 | |
|
48 | 50 | def create_from_list |
|
49 | 51 | lines = params[:user_list] |
|
52 | + | |
|
53 | + note = [] | |
|
54 | + | |
|
50 | 55 | lines.split("\n").each do |line| |
|
51 | 56 | items = line.chomp.split(',') |
|
52 |
- if items.length |
|
|
53 | - user = User.new | |
|
54 |
- |
|
|
55 | - user.full_name = items[1] | |
|
56 | - user.alias = items[2] | |
|
57 | - user.password = items[3] | |
|
58 |
- |
|
|
57 | + if items.length>=2 | |
|
58 | + login = items[0] | |
|
59 | + full_name = items[1] | |
|
60 | + | |
|
61 | + added_random_password = false | |
|
62 | + if items.length>=3 | |
|
63 | + password = items[2] | |
|
64 | + user_alias = (items.length>=4) ? items[3] : login | |
|
65 | + else | |
|
66 | + password = random_password | |
|
67 | + user_alias = (items.length>=4) ? items[3] : login | |
|
68 | + added_random_password = true | |
|
69 | + end | |
|
70 | + | |
|
71 | + user = User.new({:login => login, | |
|
72 | + :full_name => full_name, | |
|
73 | + :password => password, | |
|
74 | + :password_confirmation => password, | |
|
75 | + :alias => user_alias}) | |
|
59 | 76 | user.activated = true |
|
60 | 77 | user.save |
|
78 | + | |
|
79 | + if added_random_password | |
|
80 | + note << "'#{login}' (+)" | |
|
81 | + else | |
|
82 | + note << login | |
|
83 | + end | |
|
61 | 84 | end |
|
62 | 85 | end |
|
86 | + flash[:notice] = 'User(s) ' + note.join(', ') + | |
|
87 | + ' were successfully created. ' + | |
|
88 | + '( (+) - created with random passwords.)' | |
|
63 | 89 | redirect_to :action => 'list' |
|
64 | 90 | end |
|
65 | 91 | |
@@ -109,8 +135,31 | |||
|
109 | 135 | import_from_file(params[:file]) |
|
110 | 136 | end |
|
111 | 137 | |
|
138 | + def random_all_passwords | |
|
139 | + users = User.find(:all) | |
|
140 | + @prefix = params[:prefix] || '' | |
|
141 | + @non_admin_users = User.find_non_admin_with_prefix(@prefix) | |
|
142 | + @changed = false | |
|
143 | + if request.request_method == :post | |
|
144 | + @non_admin_users.each do |user| | |
|
145 | + password = random_password | |
|
146 | + user.password = password | |
|
147 | + user.password_confirmation = password | |
|
148 | + user.save | |
|
149 | + end | |
|
150 | + @changed = true | |
|
151 | + end | |
|
152 | + end | |
|
153 | + | |
|
112 | 154 | protected |
|
113 | 155 | |
|
156 | + def random_password(length=5) | |
|
157 | + chars = 'abcdefghijkmnopqrstuvwxyz23456789' | |
|
158 | + newpass = "" | |
|
159 | + length.times { newpass << chars[rand(chars.size-1)] } | |
|
160 | + return newpass | |
|
161 | + end | |
|
162 | + | |
|
114 | 163 | def import_from_file(f) |
|
115 | 164 | data_hash = YAML.load(f) |
|
116 | 165 | @import_log = "" |
@@ -113,6 +113,11 | |||
|
113 | 113 | password |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | + def self.find_non_admin_with_prefix(prefix='') | |
|
117 | + users = User.find(:all) | |
|
118 | + return users.find_all { |u| !(u.admin?) and u.login.index(prefix)==0 } | |
|
119 | + end | |
|
120 | + | |
|
116 | 121 | protected |
|
117 | 122 | def encrypt_new_password |
|
118 | 123 | return if password.blank? |
@@ -29,6 +29,7 | |||
|
29 | 29 | <b>What else: </b> |
|
30 | 30 | <%= link_to '[New user]', :action => 'new' %> |
|
31 | 31 | <%= link_to '[New list of users]', :action => 'new_list' %> |
|
32 | + <%= link_to '[Random passwords]', :action => 'random_all_passwords' %> | |
|
32 | 33 | <%= link_to '[View active users]', :action => 'active' %> |
|
33 | 34 | |
|
34 | 35 | </div> |
@@ -1,12 +1,8 | |||
|
1 | 1 | <h1>Adding list of users</h1> |
|
2 | 2 | |
|
3 | - <div class="usermenu"> | |
|
4 | - <%= link_to 'User admin', :action => 'list' %> | |
|
5 | - <%= link_to 'Main', :controller => 'main', :action => 'list' %> | |
|
6 | - </div> | |
|
7 | - | |
|
8 | 3 | <% form_tag :action => 'create_from_list' do %> |
|
9 | 4 | <%= submit_tag 'create users' %><br/> |
|
10 |
- List of user information: user_id,name |
|
|
5 | + List of user information in this format: <tt>user_id,name(,passwd(,alias))</tt><br/> | |
|
6 | + Note that <tt>passwd</tt> and <tt>alias</tt> is optional.<br/> | |
|
11 | 7 | <%= text_area_tag 'user_list', nil, :rows => 50, :cols => 80 %> |
|
12 | 8 | <% end %> |
You need to be logged in to leave comments.
Login now