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:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

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 end
8 end
9
9
10 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
10 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
11 - verify :method => :post, :only => [ :destroy, :create, :update ],
11 + verify :method => :post, :only => [ :destroy,
12 + :create, :create_from_list,
13 + :update ],
12 :redirect_to => { :action => :list }
14 :redirect_to => { :action => :list }
13
15
14 def list
16 def list
@@ -47,19 +49,43
47
49
48 def create_from_list
50 def create_from_list
49 lines = params[:user_list]
51 lines = params[:user_list]
52 +
53 + note = []
54 +
50 lines.split("\n").each do |line|
55 lines.split("\n").each do |line|
51 items = line.chomp.split(',')
56 items = line.chomp.split(',')
52 - if items.length==4
57 + if items.length>=2
53 - user = User.new
58 + login = items[0]
54 - user.login = items[0]
59 + full_name = items[1]
55 - user.full_name = items[1]
60 +
56 - user.alias = items[2]
61 + added_random_password = false
57 - user.password = items[3]
62 + if items.length>=3
58 - user.password_confirmation = items[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 user.activated = true
76 user.activated = true
60 user.save
77 user.save
78 +
79 + if added_random_password
80 + note << "'#{login}' (+)"
81 + else
82 + note << login
83 + end
61 end
84 end
62 end
85 end
86 + flash[:notice] = 'User(s) ' + note.join(', ') +
87 + ' were successfully created. ' +
88 + '( (+) - created with random passwords.)'
63 redirect_to :action => 'list'
89 redirect_to :action => 'list'
64 end
90 end
65
91
@@ -109,8 +135,31
109 import_from_file(params[:file])
135 import_from_file(params[:file])
110 end
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 protected
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 def import_from_file(f)
163 def import_from_file(f)
115 data_hash = YAML.load(f)
164 data_hash = YAML.load(f)
116 @import_log = ""
165 @import_log = ""
@@ -113,6 +113,11
113 password
113 password
114 end
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 protected
121 protected
117 def encrypt_new_password
122 def encrypt_new_password
118 return if password.blank?
123 return if password.blank?
@@ -29,6 +29,7
29 <b>What else: </b>
29 <b>What else: </b>
30 <%= link_to '[New user]', :action => 'new' %>
30 <%= link_to '[New user]', :action => 'new' %>
31 <%= link_to '[New list of users]', :action => 'new_list' %>
31 <%= link_to '[New list of users]', :action => 'new_list' %>
32 + <%= link_to '[Random passwords]', :action => 'random_all_passwords' %>
32 <%= link_to '[View active users]', :action => 'active' %>
33 <%= link_to '[View active users]', :action => 'active' %>
33
34
34 </div>
35 </div>
@@ -1,12 +1,8
1 <h1>Adding list of users</h1>
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 <% form_tag :action => 'create_from_list' do %>
3 <% form_tag :action => 'create_from_list' do %>
9 <%= submit_tag 'create users' %><br/>
4 <%= submit_tag 'create users' %><br/>
10 - List of user information: user_id,name,alias,passwd<br/>
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 <%= text_area_tag 'user_list', nil, :rows => 50, :cols => 80 %>
7 <%= text_area_tag 'user_list', nil, :rows => 50, :cols => 80 %>
12 <% end %>
8 <% end %>
You need to be logged in to leave comments. Login now