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'
@@ -1,23 +1,25
1 1 class UserAdminController < ApplicationController
2 2
3 3 before_filter :admin_authorization
4 4
5 5 def index
6 6 list
7 7 render :action => 'list'
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, :create, :update ],
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
15 17 @users = User.find(:all)
16 18 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
17 19 end
18 20
19 21 def active
20 22 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
21 23 @users = []
22 24 sessions.each do |session|
23 25 if session.data[:user_id]
@@ -38,37 +40,61
38 40 @user = User.new(params[:user])
39 41 @user.activated = true
40 42 if @user.save
41 43 flash[:notice] = 'User was successfully created.'
42 44 redirect_to :action => 'list'
43 45 else
44 46 render :action => 'new'
45 47 end
46 48 end
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==4
53 - user = User.new
54 - user.login = items[0]
55 - user.full_name = items[1]
56 - user.alias = items[2]
57 - user.password = items[3]
58 - user.password_confirmation = items[3]
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
66 92 def edit
67 93 @user = User.find(params[:id])
68 94 end
69 95
70 96 def update
71 97 @user = User.find(params[:id])
72 98 if @user.update_attributes(params[:user])
73 99 flash[:notice] = 'User was successfully updated.'
74 100 redirect_to :action => 'show', :id => @user
@@ -100,26 +126,49
100 126 @scorearray << ustat
101 127 end
102 128 end
103 129
104 130 def import
105 131 if params[:file]==''
106 132 flash[:notice] = 'Error importing no file'
107 133 redirect_to :action => 'list' and return
108 134 end
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 = ""
117 166
118 167 country_data = data_hash[:countries]
119 168 site_data = data_hash[:sites]
120 169 user_data = data_hash[:users]
121 170
122 171 # import country
123 172 countries = {}
124 173 country_data.each_pair do |id,country|
125 174 c = Country.find_by_name(country[:name])
@@ -104,24 +104,29
104 104
105 105 def verify_activation_key(key)
106 106 key == activation_key
107 107 end
108 108
109 109 def self.random_password(length=5)
110 110 chars = 'abcdefghjkmnopqrstuvwxyz'
111 111 password = ''
112 112 length.times { password << chars[rand(chars.length - 1)] }
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?
119 124 self.salt = (10+rand(90)).to_s
120 125 self.hashed_password = User.encrypt(self.password,self.salt)
121 126 end
122 127
123 128 def assign_default_site
124 129 # have to catch error when migrating (because self.site is not available).
125 130 begin
126 131 if self.site==nil
127 132 self.site = Site.find_by_name('default')
@@ -20,24 +20,25
20 20 <td><%= submit_tag "Create" %></td>
21 21 </tr></table>
22 22 <% end %>
23 23 <br/>
24 24 <b>Import from site management</b>
25 25 <% form_tag({:action => 'import'}, :multipart => true) do %>
26 26 File: <%= file_field_tag 'file' %> <%= submit_tag 'Import' %>
27 27 <% end %>
28 28 <br/>
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>
35 36
36 37 <table>
37 38 <tr>
38 39 <% for column in User.content_columns %>
39 40 <% if !@hidden_columns.index(column.name) %>
40 41 <th><%= column.human_name %></th>
41 42 <% end %>
42 43 <% end %>
43 44 </tr>
@@ -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,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 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