diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,3 +1,5 @@ +require 'pony' + class UsersController < ApplicationController before_filter :authenticate, :except => [:new, :register] @@ -28,4 +30,27 @@ redirect_to :action => 'index' end + def new + @user = User.new + render :action => 'new', :layout => 'empty' + end + + def register + @user = User.new(params[:user]) + @user.password_confirmation = @user.password = User.random_password + @user.activated = false + if (@user.valid?) and (@user.save) + send_confirmation_email(@user) + render :action => 'new_splash', :layout => 'empty' + else + @user.errors.add_to_base("Email cannot be blank") if @user.email=='' + render :action => 'new', :layout => 'empty' + end + end + + protected + + def send_confirmation_email(user) + end + end diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,7 +19,13 @@ belongs_to :site belongs_to :country + named_scope :activated, :conditions => {:activated => true} + validates_presence_of :login + validates_uniqueness_of :login + validates_format_of :login, :with => /^[\_a-z0-9]+$/ + validates_length_of :login, :within => 3..10 + validates_presence_of :full_name validates_length_of :full_name, :minimum => 1 @@ -27,6 +33,10 @@ validates_length_of :password, :within => 4..20, :if => :password_required? validates_confirmation_of :password, :if => :password_required? + validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true + + validate :uniqueness_of_email_from_activated_users + attr_accessor :password before_save :encrypt_new_password @@ -84,6 +94,13 @@ key == activation_key end + def self.random_password(length=5) + chars = 'abcdefghjkmnopqrstuvwxyz' + password = '' + length.times { password << chars[rand(chars.length - 1)] } + password + end + protected def encrypt_new_password return if password.blank? @@ -98,4 +115,10 @@ def self.encrypt(string,salt) Digest::SHA1.hexdigest(salt + string) end + + def uniqueness_of_email_from_activated_users + if User.activated.find_by_email(self.email)!=nil + self.errors.add_to_base("Email has already been taken") + end + end end 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,7 @@ %br/ --# if Configuration['system.online_registration'] +- if Configuration['system.online_registration'] Want to participate? %b Please diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml new file mode 100644 --- /dev/null +++ b/app/views/users/new.html.haml @@ -0,0 +1,29 @@ +%h1 New user registration + += error_messages_for :user, :header_message => 'Errors occured during registration' + +%table + - form_for :user, @user, :url => { :action => 'register' } do |f| + %tr + %td Login: + %td= f.text_field :login + %tr + %td + %td + %small Only a-z, A-Z, 0-9 and _ + %tr + %td Full name: + %td= f.text_field :full_name + %tr + %td E-mail: + %td= f.text_field :email + %tr + %td + %td + %small + Please make sure that your e-mail is correct. + %br/ + You'll need to verify your account by email. + %tr + %td{:colspan => 2}= submit_tag "Register" + diff --git a/app/views/users/new_splash.html.haml b/app/views/users/new_splash.html.haml new file mode 100644 --- /dev/null +++ b/app/views/users/new_splash.html.haml @@ -0,0 +1,11 @@ +%h1 Registration successful + +We have sent a confimation message to your e-mail. +%br/ +Please check at += "#{@user.email}." +%br/ +%br/ + +Go back to += link_to 'login page.', :controller => 'main', :action => 'login' diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,26 +10,26 @@ :hashed_password => User.encrypt(@password,@salt)) end - it "should authenticate activated user" do + it "should be authenticated if activated" do @john.should_receive(:activated).and_return(true) @john.authenticated?(@password).should == true end - it "should not authenticate inactivated user" do + it "should not be authenticated if inactivated" do @john.should_receive(:activated).and_return(false) @john.authenticated?(@password).should == false end - it "should not authenticate user with incorrect password" do + it "should not be authenticated if incorrect password is provided" 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 @@ -38,16 +38,26 @@ @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 +describe User, "as a class" do + it "should be able to generate random password" do + password1 = User.random_password + password2 = User.random_password + + password1.should_not == password2 + end + end