Description:
use uuid cookie
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r849:2290fefd2c38 - - 3 files changed: 20 inserted, 2 deleted

@@ -1,243 +1,251
1 1 require 'ipaddr'
2 + require "securerandom"
2 3
3 4 class ApplicationController < ActionController::Base
4 5 protect_from_forgery
5 6
6 7 before_action :current_user
7 8 before_action :nav_announcement
9 + before_action :unique_visitor_id
8 10
9 11 SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode'
10 12 MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login'
11 13 WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore'
12 14 WHITELIST_IP_CONF_KEY = 'right.whitelist_ip'
13 15
14 16 #report and redirect for unauthorized activities
15 17 def unauthorized_redirect(notice = 'You are not authorized to view the page you requested')
16 18 flash[:notice] = notice
17 19 redirect_to login_main_path
18 20 end
19 21
20 22 # Returns the current logged-in user (if any).
21 23 def current_user
22 24 return nil unless session[:user_id]
23 25 @current_user ||= User.find(session[:user_id])
24 26 end
25 27
26 28 def nav_announcement
27 29 @nav_announcement = Announcement.where(on_nav_bar: true)
28 30 end
29 31
30 32 def admin_authorization
31 33 return false unless check_valid_login
32 34 user = User.includes(:roles).find(session[:user_id])
33 35 unless user.admin?
34 36 unauthorized_redirect
35 37 return false
36 38 end
37 39 return true
38 40 end
39 41
40 42 def authorization_by_roles(allowed_roles)
41 43 return false unless check_valid_login
42 44 unless @current_user.roles.detect { |role| allowed_roles.member?(role.name) }
43 45 unauthorized_redirect
44 46 return false
45 47 end
46 48 end
47 49
48 50 def testcase_authorization
49 51 #admin always has privileged
50 52 if @current_user.admin?
51 53 return true
52 54 end
53 55
54 56 unauthorized_redirect unless GraderConfiguration["right.view_testcase"]
55 57 end
56 58
59 + def unique_visitor_id
60 + unless cookies[:uuid]
61 + value = SecureRandom.uuid
62 + cookies[:uuid] = { value: value, expires: 20.year }
63 + end
64 + end
57 65
58 66 protected
59 67
60 68 #redirect to root (and also force logout)
61 69 #if the user is not logged_in or the system is in "ADMIN ONLY" mode
62 70 def check_valid_login
63 71 #check if logged in
64 72 unless session[:user_id]
65 73 if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY]
66 74 unauthorized_redirect('You need to login but you cannot log in at this time')
67 75 else
68 76 unauthorized_redirect('You need to login')
69 77 end
70 78 return false
71 79 end
72 80
73 81 # check if run in single user mode
74 82 if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY]
75 83 if @current_user==nil || (!@current_user.admin?)
76 84 unauthorized_redirect('You cannot log in at this time')
77 85 return false
78 86 end
79 87 end
80 88
81 89 # check if the user is enabled
82 90 unless @current_user.enabled? || @current_user.admin?
83 91 unauthorized_redirect 'Your account is disabled'
84 92 return false
85 93 end
86 94
87 95 # check if user ip is allowed
88 96 unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY]
89 97 unless is_request_ip_allowed?
90 98 unauthorized_redirect 'Your IP is not allowed to login at this time.'
91 99 return false
92 100 end
93 101 end
94 102
95 103 if GraderConfiguration.multicontests?
96 104 return true if @current_user.admin?
97 105 begin
98 106 if @current_user.contest_stat(true).forced_logout
99 107 flash[:notice] = 'You have been automatically logged out.'
100 108 redirect_to :controller => 'main', :action => 'index'
101 109 end
102 110 rescue
103 111 end
104 112 end
105 113 return true
106 114 end
107 115
108 116 #redirect to root (and also force logout)
109 117 #if the user use different ip from the previous connection
110 118 # only applicable when MULTIPLE_IP_LOGIN options is false only
111 119 def authenticate_by_ip_address
112 120 #this assume that we have already authenticate normally
113 121 unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY]
114 122 user = User.find(session[:user_id])
115 123 if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip)
116 124 flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}"
117 125 redirect_to :controller => 'main', :action => 'login'
118 126 return false
119 127 end
120 128 unless user.last_ip
121 129 user.last_ip = request.remote_ip
122 130 user.save
123 131 end
124 132 end
125 133 return true
126 134 end
127 135
128 136 def authorization
129 137 return false unless check_valid_login
130 138 user = User.find(session[:user_id])
131 139 unless user.roles.detect { |role|
132 140 role.rights.detect{ |right|
133 141 right.controller == self.class.controller_name and
134 142 (right.action == 'all' || right.action == action_name)
135 143 }
136 144 }
137 145 flash[:notice] = 'You are not authorized to view the page you requested'
138 146 #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login')
139 147 redirect_to :controller => 'main', :action => 'login'
140 148 return false
141 149 end
142 150 end
143 151
144 152 def verify_time_limit
145 153 return true if session[:user_id]==nil
146 154 user = User.find(session[:user_id], :include => :site)
147 155 return true if user==nil || user.site == nil
148 156 if user.contest_finished?
149 157 flash[:notice] = 'Error: the contest you are participating is over.'
150 158 redirect_to :back
151 159 return false
152 160 end
153 161 return true
154 162 end
155 163
156 164 def is_request_ip_allowed?
157 165 unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY]
158 166 user_ip = IPAddr.new(request.remote_ip)
159 167 allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || ''
160 168
161 169 allowed.delete(' ').split(',').each do |ips|
162 170 allow_ips = IPAddr.new(ips)
163 171 if allow_ips.include?(user_ip)
164 172 return true
165 173 end
166 174 end
167 175 return false
168 176 end
169 177 return true
170 178 end
171 179
172 180 #function for datatable ajax query
173 181 #return record,total_count,filter_count
174 182 def process_query_record(record,
175 183 total_count: nil,
176 184 select: '',
177 185 global_search: [],
178 186 no_search: false,
179 187 force_order: '',
180 188 date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until',
181 189 hard_limit: nil)
182 190 arel_table = record.model.arel_table
183 191
184 192 if !no_search && params['search']
185 193 global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase)
186 194 if !global_value.blank?
187 195 global_value.split.each do |value|
188 196 global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ')
189 197 record = record.where(global_where)
190 198 end
191 199 end
192 200
193 201 params['columns'].each do |i, col|
194 202 if !col['search']['value'].blank?
195 203 record = record.where(arel_table[col['name']].lower.matches("%#{col['search']['value'].strip.downcase}%"))
196 204 end
197 205 end
198 206 end
199 207
200 208 if !date_filter.blank?
201 209 param_since = params[date_param_since]
202 210 param_until = params[date_param_until]
203 211 date_since = Time.zone.parse( param_since ) || Time.new(1,1,1) rescue Time.new(1,1,1)
204 212 date_until = Time.zone.parse( param_until ) || Time.zone.now() rescue Time.zone.now()
205 213 date_range = date_since..(date_until.end_of_day)
206 214 record = record.where(date_filter.to_sym => date_range)
207 215 end
208 216
209 217 if force_order.blank?
210 218 if params['order']
211 219 params['order'].each do |i, o|
212 220 colName = params['columns'][o['column']]['name']
213 221 colName = "#{record.model.table_name}.#{colName}" if colName.upcase == 'ID'
214 222 record = record.order("#{colName} #{o['dir'].casecmp('desc') != 0 ? 'ASC' : 'DESC'}") unless colName.blank?
215 223 end
216 224 end
217 225 else
218 226 record = record.order(force_order)
219 227 end
220 228
221 229 filterCount = record.count(record.model.primary_key)
222 230 # if .group() is used, filterCount might be like {id_1: count_1, id_2: count_2, ...}
223 231 # so we should count the result again..
224 232 if filterCount.is_a? Hash
225 233 filterCount = filterCount.count
226 234 end
227 235
228 236
229 237 record = record.offset(params['start'] || 0)
230 238 record = record.limit(hard_limit)
231 239 if (params['length'])
232 240 limit = params['length'].to_i
233 241 limit == hard_limit if (hard_limit && hard_limit < limit)
234 242 record = record.limit(limit)
235 243 end
236 244 if (!select.blank?)
237 245 record = record.select(select)
238 246 end
239 247
240 248 return record, total_count || record.model.count, filterCount
241 249 end
242 250
243 251 end
@@ -1,89 +1,99
1 1 class LoginController < ApplicationController
2 2
3 3 @@authenticators = []
4 4
5 5 def index
6 6 # show login screen
7 7 reset_session
8 8 redirect_to :controller => 'main', :action => 'login'
9 9 end
10 10
11 11 def login
12 12 user = get_authenticated_user(params[:login], params[:password])
13 13 unless user
14 14 flash[:notice] = 'Wrong password'
15 15 redirect_to :controller => 'main', :action => 'login'
16 16 return
17 17 end
18 18
19 19 if (!GraderConfiguration['right.bypass_agreement']) and (!params[:accept_agree]) and !user.admin?
20 20 flash[:notice] = 'You must accept the agreement before logging in'
21 21 redirect_to :controller => 'main', :action => 'login'
22 22 return
23 23 end
24 24
25 + #store uuid when login
26 + if user.last_ip.nil?
27 + user.last_ip = cookies[:uuid]
28 + else
29 + if user.last_ip != cookies[:uuid]
30 + user.last_ip =cookies[:uuid]
31 + #log different login
32 + end
33 + end
34 +
25 35 #process logging in
26 36 session[:user_id] = user.id
27 37 session[:admin] = user.admin?
28 38
29 39 # clear forced logout flag for multicontests contest change
30 40 if GraderConfiguration.multicontests?
31 41 contest_stat = user.contest_stat
32 42 if contest_stat.respond_to? :forced_logout
33 43 if contest_stat.forced_logout
34 44 contest_stat.forced_logout = false
35 45 contest_stat.save
36 46 end
37 47 end
38 48 end
39 49
40 50 #save login information
41 - Login.create(user_id: user.id, ip_address: request.remote_ip)
51 + Login.create(user_id: user.id, ip_address: cookies[:uuid])
42 52
43 53 redirect_to :controller => 'main', :action => 'list'
44 54 end
45 55
46 56 def site_login
47 57 begin
48 58 site = Site.find(params[:login][:site_id])
49 59 rescue ActiveRecord::RecordNotFound
50 60 site = nil
51 61 end
52 62 if site==nil
53 63 flash[:notice] = 'Wrong site'
54 64 redirect_to :controller => 'main', :action => 'login' and return
55 65 end
56 66 if (site.password) and (site.password == params[:login][:password])
57 67 session[:site_id] = site.id
58 68 redirect_to :controller => 'site', :action => 'index'
59 69 else
60 70 flash[:notice] = 'Wrong site password'
61 71 redirect_to :controller => 'site', :action => 'login'
62 72 end
63 73 end
64 74
65 75 def logout
66 76 redirect_to root_path
67 77 end
68 78
69 79 def self.add_authenticator(authenticator)
70 80 @@authenticators << authenticator
71 81 end
72 82
73 83 protected
74 84
75 85 def get_authenticated_user(login, password)
76 86 if @@authenticators.empty?
77 87 return User.authenticate(login, password)
78 88 else
79 89 user = User.authenticate(login, password)
80 90 @@authenticators.each do |authenticator|
81 91 if not user
82 92 user = authenticator.authenticate(login, password)
83 93 end
84 94 end
85 95 return user
86 96 end
87 97 end
88 98
89 99 end
@@ -1,427 +1,427
1 1 require 'digest/sha1'
2 2 require 'net/pop'
3 3 require 'net/https'
4 4 require 'net/http'
5 5 require 'json'
6 6
7 7 class User < ActiveRecord::Base
8 8
9 9 has_and_belongs_to_many :roles
10 10
11 11 #has_and_belongs_to_many :groups
12 12 has_many :groups_users, class_name: 'GroupUser'
13 13 has_many :groups, :through => :groups_users
14 14
15 15 has_many :test_requests, -> {order(submitted_at: :desc)}
16 16
17 17 has_many :messages, -> { order(created_at: :desc) },
18 18 :class_name => "Message",
19 19 :foreign_key => "sender_id"
20 20
21 21 has_many :replied_messages, -> { order(created_at: :desc) },
22 22 :class_name => "Message",
23 23 :foreign_key => "receiver_id"
24 24
25 25 has_many :logins
26 26
27 27 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
28 28
29 29 belongs_to :site
30 30 belongs_to :country
31 31
32 32 has_and_belongs_to_many :contests, -> { order(:name)}
33 33
34 34 scope :activated_users, -> {where activated: true}
35 35
36 36 validates_presence_of :login
37 37 validates_uniqueness_of :login
38 38 validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/
39 39 validates_length_of :login, :within => 3..30
40 40
41 41 validates_presence_of :full_name
42 42 validates_length_of :full_name, :minimum => 1
43 -
43 +
44 44 validates_presence_of :password, :if => :password_required?
45 45 validates_length_of :password, :within => 4..50, :if => :password_required?
46 46 validates_confirmation_of :password, :if => :password_required?
47 47
48 48 validates_format_of :email,
49 49 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
50 50 :if => :email_validation?
51 51 validate :uniqueness_of_email_from_activated_users,
52 52 :if => :email_validation?
53 53 validate :enough_time_interval_between_same_email_registrations,
54 54 :if => :email_validation?
55 55
56 56 # these are for ytopc
57 57 # disable for now
58 58 #validates_presence_of :province
59 59
60 60 attr_accessor :password
61 61
62 62 before_save :encrypt_new_password
63 63 before_save :assign_default_site
64 64 before_save :assign_default_contest
65 65
66 66 # this is for will_paginate
67 67 cattr_reader :per_page
68 68 @@per_page = 50
69 69
70 70 def self.authenticate(login, password)
71 71 user = find_by_login(login)
72 72 if user
73 73 return user if user.authenticated?(password)
74 74 if user.authenticated_by_cucas?(password)
75 75 user.password = password
76 76 user.save
77 77 return user
78 78 end
79 79 end
80 80 end
81 81
82 82
83 83 def authenticated?(password)
84 84 if self.activated
85 85 hashed_password == User.encrypt(password,self.salt)
86 86 else
87 87 false
88 88 end
89 89 end
90 90
91 91 def login_with_name
92 92 "[#{login}] #{full_name}"
93 93 end
94 94
95 95 def authenticated_by_cucas?(password)
96 96 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
97 97 appid = '41508763e340d5858c00f8c1a0f5a2bb'
98 98 appsecret ='d9cbb5863091dbe186fded85722a1e31'
99 99 post_args = {
100 100 'appid' => appid,
101 101 'appsecret' => appsecret,
102 102 'username' => login,
103 103 'password' => password
104 104 }
105 105
106 106 #simple call
107 107 begin
108 108 http = Net::HTTP.new('www.cas.chula.ac.th', 443)
109 109 http.use_ssl = true
110 110 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
111 111 result = [ ]
112 112 http.start do |http|
113 113 req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
114 114 #req = Net::HTTP::Post.new('/appX/prod/?q=studentAuthenticate')
115 115 #req = Net::HTTP::Post.new('/app2/prod/api/?q=studentAuthenticate')
116 116 param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
117 117 resp = http.request(req,param)
118 118 result = JSON.parse resp.body
119 119 puts result
120 120 end
121 121 return true if result["type"] == "beanStudent"
122 122 rescue => e
123 123 puts e
124 124 puts e.message
125 125 return false
126 126 end
127 127 return false
128 128 end
129 129
130 130 def admin?
131 131 has_role?('admin')
132 132 end
133 133
134 134 def has_role?(role)
135 135 self.roles.where(name: role).count > 0
136 136 end
137 137
138 138 def email_for_editing
139 139 if self.email==nil
140 140 "(unknown)"
141 141 elsif self.email==''
142 142 "(blank)"
143 143 else
144 144 self.email
145 145 end
146 146 end
147 147
148 148 def email_for_editing=(e)
149 149 self.email=e
150 150 end
151 151
152 152 def alias_for_editing
153 153 if self.alias==nil
154 154 "(unknown)"
155 155 elsif self.alias==''
156 156 "(blank)"
157 157 else
158 158 self.alias
159 159 end
160 160 end
161 161
162 162 def alias_for_editing=(e)
163 163 self.alias=e
164 164 end
165 165
166 166 def activation_key
167 167 if self.hashed_password==nil
168 168 encrypt_new_password
169 169 end
170 170 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
171 171 end
172 172
173 173 def verify_activation_key(key)
174 174 key == activation_key
175 175 end
176 176
177 177 def self.random_password(length=5)
178 178 chars = 'abcdefghjkmnopqrstuvwxyz'
179 179 password = ''
180 180 length.times { password << chars[rand(chars.length - 1)] }
181 181 password
182 182 end
183 183
184 184 # Contest information
185 185
186 186 def self.find_users_with_no_contest()
187 187 users = User.all
188 188 return users.find_all { |u| u.contests.length == 0 }
189 189 end
190 190
191 191
192 192 def contest_time_left
193 193 if GraderConfiguration.contest_mode?
194 194 return nil if site==nil
195 195 return site.time_left
196 196 elsif GraderConfiguration.indv_contest_mode?
197 197 time_limit = GraderConfiguration.contest_time_limit
198 198 if time_limit == nil
199 199 return nil
200 200 end
201 201 if contest_stat==nil or contest_stat.started_at==nil
202 202 return (Time.now.gmtime + time_limit) - Time.now.gmtime
203 203 else
204 204 finish_time = contest_stat.started_at + time_limit
205 205 current_time = Time.now.gmtime
206 206 if current_time > finish_time
207 207 return 0
208 208 else
209 209 return finish_time - current_time
210 210 end
211 211 end
212 212 else
213 213 return nil
214 214 end
215 215 end
216 216
217 217 def contest_finished?
218 218 if GraderConfiguration.contest_mode?
219 219 return false if site==nil
220 220 return site.finished?
221 221 elsif GraderConfiguration.indv_contest_mode?
222 222 return false if self.contest_stat==nil
223 223 return contest_time_left == 0
224 224 else
225 225 return false
226 226 end
227 227 end
228 228
229 229 def contest_started?
230 230 if GraderConfiguration.indv_contest_mode?
231 231 stat = self.contest_stat
232 232 return ((stat != nil) and (stat.started_at != nil))
233 233 elsif GraderConfiguration.contest_mode?
234 234 return true if site==nil
235 235 return site.started
236 236 else
237 237 return true
238 238 end
239 239 end
240 240
241 241 def update_start_time
242 242 stat = self.contest_stat
243 243 if stat.nil? or stat.started_at.nil?
244 244 stat ||= UserContestStat.new(:user => self)
245 245 stat.started_at = Time.now.gmtime
246 246 stat.save
247 247 end
248 248 end
249 249
250 250 def problem_in_user_contests?(problem)
251 251 problem_contests = problem.contests.all
252 252
253 253 if problem_contests.length == 0 # this is public contest
254 254 return true
255 255 end
256 256
257 257 contests.each do |contest|
258 258 if problem_contests.find {|c| c.id == contest.id }
259 259 return true
260 260 end
261 261 end
262 262 return false
263 263 end
264 264
265 265 def available_problems_group_by_contests
266 266 contest_problems = []
267 267 pin = {}
268 268 contests.enabled.each do |contest|
269 269 available_problems = contest.problems.available
270 270 contest_problems << {
271 271 :contest => contest,
272 272 :problems => available_problems
273 273 }
274 274 available_problems.each {|p| pin[p.id] = true}
275 275 end
276 276 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
277 277 contest_problems << {
278 278 :contest => nil,
279 279 :problems => other_avaiable_problems
280 280 }
281 281 return contest_problems
282 282 end
283 283
284 284 def solve_all_available_problems?
285 285 available_problems.each do |p|
286 286 u = self
287 287 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
288 288 return false if !p or !sub or sub.points < p.full_score
289 289 end
290 290 return true
291 291 end
292 292
293 293 #get a list of available problem
294 294 def available_problems
295 295 # first, we check if this is normal mode
296 296 if not GraderConfiguration.multicontests?
297 297
298 298 #if this is a normal mode
299 299 #we show problem based on problem_group, if the config said so
300 300 if GraderConfiguration.use_problem_group?
301 301 return available_problems_in_group
302 302 else
303 303 return Problem.available_problems
304 304 end
305 305 else
306 306 #this is multi contest mode
307 307 contest_problems = []
308 308 pin = {}
309 309 contests.enabled.each do |contest|
310 310 contest.problems.available.each do |problem|
311 311 if not pin.has_key? problem.id
312 312 contest_problems << problem
313 313 end
314 314 pin[problem.id] = true
315 315 end
316 316 end
317 317 other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
318 318 return contest_problems + other_avaiable_problems
319 319 end
320 320 end
321 321
322 322 # new feature, get list of available problem in all enabled group that the user belongs to
323 323 def available_problems_in_group
324 324 problem = []
325 325 self.groups.where(enabled: true).each do |group|
326 326 group.problems.where(available: true).each { |p| problem << p }
327 327 end
328 328 problem.uniq!
329 329 if problem
330 330 problem.sort! do |a,b|
331 331 case
332 332 when a.date_added < b.date_added
333 333 1
334 334 when a.date_added > b.date_added
335 335 -1
336 336 else
337 337 a.name <=> b.name
338 338 end
339 339 end
340 340 return problem
341 341 else
342 342 return []
343 343 end
344 344 end
345 345
346 346 #check if the user has the right to view that problem
347 347 #this also consider group based problem policy
348 348 def can_view_problem?(problem)
349 349 return true if admin?
350 350 return available_problems.include? problem
351 351 end
352 352
353 353 def self.clear_last_login
354 354 User.update_all(:last_ip => nil)
355 355 end
356 356
357 357 #create multiple user, one per lines of input
358 358 def self.create_from_list(lines)
359 359 error_logins = []
360 360 first_error = nil
361 361 created_users = []
362 362
363 363 lines.split("\n").each do |line|
364 364 #split with large limit, this will cause consecutive ',' to be result in a blank
365 365 items = line.chomp.split(',',1000)
366 366 if items.length>=2
367 367 login = items[0]
368 368 full_name = items[1]
369 369 remark =''
370 370 user_alias = ''
371 371
372 372 added_random_password = false
373 373 added_password = false
374 374
375 375 #given password?
376 376 if items.length >= 3
377 377 if items[2].chomp(" ").length > 0
378 378 password = items[2].chomp(" ")
379 379 added_password = true
380 380 end
381 381 else
382 382 password = random_password
383 383 added_random_password=true;
384 384 end
385 385
386 386 #given alias?
387 387 if items.length>= 4 and items[3].chomp(" ").length > 0;
388 388 user_alias = items[3].chomp(" ")
389 389 else
390 390 user_alias = login
391 391 end
392 392
393 393 #given remark?
394 394 has_remark = false
395 395 if items.length>=5
396 396 remark = items[4].strip;
397 397 has_remark = true
398 398 end
399 399
400 400 user = User.find_by_login(login)
401 401 if (user)
402 402 user.full_name = full_name
403 403 user.remark = remark if has_remark
404 404 user.password = password if added_password || added_random_password
405 405 else
406 406 #create a random password if none are given
407 407 password = random_password unless password
408 408 user = User.new({:login => login,
409 409 :full_name => full_name,
410 410 :password => password,
411 411 :password_confirmation => password,
412 412 :alias => user_alias,
413 413 :remark => remark})
414 414 end
415 415 user.activated = true
416 416
417 417 if user.save
418 418 created_users << user
419 419 else
420 420 error_logins << "'#{login}'"
421 421 first_error = user.errors.full_messages.to_sentence unless first_error
422 422 end
423 423 end
424 424 end
425 425
426 426 return {error_logins: error_logins, first_error: first_error, created_users: created_users}
427 427
You need to be logged in to leave comments. Login now