# HG changeset patch # User jittat # Date 2008-12-05 08:52:15 # Node ID f1e975d4e9fc766f2a67eb372bcdb59c8d095992 # Parent 3580f3e3f91b3d08c0d5c6cae4621e10183f57cd start working on e-mail registration git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@294 6386c4cd-e34a-4fa8-8920-d93eb39b512e diff --git a/app/controllers/user_admin_controller.rb b/app/controllers/user_admin_controller.rb --- a/app/controllers/user_admin_controller.rb +++ b/app/controllers/user_admin_controller.rb @@ -25,6 +25,7 @@ def create @user = User.new(params[:user]) + @user.activated = true if @user.save flash[:notice] = 'User was successfully created.' redirect_to :action => 'list' @@ -44,6 +45,7 @@ user.alias = items[2] user.password = items[3] user.password_confirmation = items[3] + user.activated = true user.save end end diff --git a/app/models/configuration.rb b/app/models/configuration.rb --- a/app/models/configuration.rb +++ b/app/models/configuration.rb @@ -103,7 +103,11 @@ def self.read_one_key(key) conf = Configuration.find_by_key(key) - return Configuration.convert_type(conf.value,conf.value_type) + if conf + return Configuration.convert_type(conf.value,conf.value_type) + else + return nil + end end def self.read_grading_info diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -37,7 +37,11 @@ end def authenticated?(password) - hashed_password == User.encrypt(password,self.salt) + if self.activated + hashed_password == User.encrypt(password,self.salt) + else + false + end end def admin? @@ -72,6 +76,14 @@ self.alias=e end + def activation_key + Digest::SHA1.hexdigest(self.hashed_password)[0..7] + end + + def verify_activation_key(key) + key == activation_key + end + protected def encrypt_new_password return if password.blank? diff --git a/app/views/main/login.html.haml b/app/views/main/login.html.haml --- a/app/views/main/login.html.haml +++ b/app/views/main/login.html.haml @@ -30,7 +30,14 @@ %br/ -- if Configuration['system.mode']=='contest' +-# if Configuration['system.online_registration'] + Want to participate? + %b + Please + = link_to 'register.', :controller => :users, :action => :new + %br/ + +- if (Configuration['system.mode']=='contest') and (Configuration['contest.multisites']) %script{:type => 'text/javascript'} var siteList = new Array(); - @countries.each do |country| diff --git a/config/boot.rb b/config/boot.rb --- a/config/boot.rb +++ b/config/boot.rb @@ -82,14 +82,14 @@ def load_rubygems require 'rubygems' - - unless rubygems_version >= '0.9.4' - $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.) + min_version = '1.1.1' + unless rubygems_version >= min_version + $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.) exit 1 end rescue LoadError - $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org) + $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org) exit 1 end diff --git a/config/environment.rb.SAMPLE b/config/environment.rb.SAMPLE --- a/config/environment.rb.SAMPLE +++ b/config/environment.rb.SAMPLE @@ -43,8 +43,15 @@ # See Rails::Configuration for more options + # ------------- + # Required gems + # ------------- + # This is for rspec config.gem "rspec-rails", :lib => "spec" + config.gem "haml" + config.gem "pony" + #config.gem "BlueCloth", :lig => "bluecloth" end # Add new inflection rules using the following format diff --git a/db/migrate/20081204081043_add_more_options_to_configurations.rb b/db/migrate/20081204081043_add_more_options_to_configurations.rb new file mode 100644 --- /dev/null +++ b/db/migrate/20081204081043_add_more_options_to_configurations.rb @@ -0,0 +1,23 @@ +class AddMoreOptionsToConfigurations < ActiveRecord::Migration + def self.up + # If the server is in contest mode and + # Configuration['contest.multisites'] is true + # the menu for site administrator is shown. + + Configuration.create(:key => 'contest.multisites', + :value_type => 'boolean', + :value => 'false') + + # If Configuration['system.online_registration'] is true, + # the registration menu would appear + + Configuration.create(:key => 'system.online_registration', + :value_type => 'boolean', + :value => 'false') + end + + def self.down + Configuration.find_by_key('contest.multisites').destroy + Configuration.find_by_key('system.online_registration').destroy + end +end diff --git a/db/migrate/20081204122651_add_activated_to_users.rb b/db/migrate/20081204122651_add_activated_to_users.rb new file mode 100644 --- /dev/null +++ b/db/migrate/20081204122651_add_activated_to_users.rb @@ -0,0 +1,15 @@ +class AddActivatedToUsers < ActiveRecord::Migration + def self.up + add_column :users, :activated, :boolean, :default => 0 + + User.find(:all).each do |user| + user.activated = true + user.save + end + end + + + def self.down + remove_column :users, :activated + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20081107145815) do +ActiveRecord::Schema.define(:version => 20081204122651) do create_table "announcements", :force => true do |t| t.string "author" @@ -182,6 +182,7 @@ t.string "email" t.integer "site_id" t.integer "country_id" + t.boolean "activated", :default => false end add_index "users", ["login"], :name => "index_users_on_login", :unique => true diff --git a/script/dbconsole b/script/dbconsole new file mode 100755 --- /dev/null +++ b/script/dbconsole @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__) + '/../config/boot' +require 'commands/dbconsole' diff --git a/script/performance/request b/script/performance/request new file mode 100755 --- /dev/null +++ b/script/performance/request @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__) + '/../../config/boot' +require 'commands/performance/request' diff --git a/spec/models/configuration_spec.rb b/spec/models/configuration_spec.rb --- a/spec/models/configuration_spec.rb +++ b/spec/models/configuration_spec.rb @@ -3,6 +3,11 @@ describe Configuration do + # only work with cached configuration + class Configuration + @@cache = true + end + before(:each) do @int_config = mock(Configuration, :id => 1, diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,53 @@ + +require File.dirname(__FILE__) + '/../spec_helper' + +describe User do + + before(:each) do + @password = "hello" + @salt = "123" + @john = stub_model(User, :salt => @salt, + :hashed_password => User.encrypt(@password,@salt)) + end + + it "should authenticate activated user" do + @john.should_receive(:activated).and_return(true) + @john.authenticated?(@password).should == true + end + + it "should not authenticate inactivated user" do + @john.should_receive(:activated).and_return(false) + @john.authenticated?(@password).should == false + end + + it "should not authenticate user with incorrect password" do + @john.should_receive(:activated).and_return(true) + @john.should_receive(:hashed_password).and_return("byebye") + @john.authenticated?(@password).should == false + end + +end + +describe User, "during registration" do + + class User + public :encrypt_new_password + end + + before(:each) do + @john = User.new(:login => 'john', :password => 'hello') + @john.encrypt_new_password + end + + it "should produce and accept activation key" do + activation_key = @john.activation_key + + @john.verify_activation_key(activation_key).should == true + end + + it "should not accept invalid activation key" do + @john.verify_activation_key("12345").should == false + end + + +end diff --git a/vendor/plugins/haml/init.rb b/vendor/plugins/haml/init.rb --- a/vendor/plugins/haml/init.rb +++ b/vendor/plugins/haml/init.rb @@ -4,4 +4,5 @@ require 'haml' # From gem end +# Load Haml and Sass Haml.init_rails(binding)