Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'develop' of github.com:kaize/nastachku into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
PanfilovDenis committed Feb 19, 2013
2 parents 97eec27 + a39db9f commit 0c40d99
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 26 deletions.
14 changes: 5 additions & 9 deletions app/controllers/web/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
class Web::SessionsController < Web::ApplicationController

def new
#FIXME Frank change this type to UserSignInType
@user = UserRegistrationType.new
@type = UserSignInType.new
end

def create
#FIXME Frank change this type to UserSignInType
user = UserRegistrationType.find_by_email(params[:user][:email])
@type = UserSignInType.new(params[:user_sign_in_type])

if user.try(:authenticate, params[:user][:password])
if @type.valid?
user = @type.user
flash_success

sign_in(user)
redirect_to root_path
else
flash_error

render action: 'new'
render :new
end
end

Expand Down
19 changes: 16 additions & 3 deletions app/controllers/web/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ def new
@user = UserRegistrationType.new
end

def activate
token = User::AuthToken.find_by_authentication_token!(params[:auth_token])
user = token.user
if token && user
user.activate!
flash_success
else
flash_error
end
redirect_to root_path
end

def create
@user = UserRegistrationType.new(params[:user])

if @user.save
UserMailer.welcome(@user).deliver
sign_in @user
token = @user.create_auth_token
UserMailer.confirm_registration(@user, token).deliver
#sign_in @user
flash_success
redirect_to root_path
redirect_to new_session_path
else
flash_error
render action: "new"
Expand Down
3 changes: 2 additions & 1 deletion app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class UserMailer < ActionMailer::Base
default_url_options[:host] = configus.mailer.default_host
default from: configus.mailer.default_from

def welcome(user)
def confirm_registration(user, token)
@user = user
@token = token
mail :to => @user.email
end

Expand Down
7 changes: 4 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ class User < ActiveRecord::Base

has_many :auth_tokens

state_machine :state, initial: :active do
state_machine :state, initial: :new do
state :new
state :active
state :inactive

event :activate do
transition any - :active => :active
transition [:inactive, :new] => :active
end

event :deactivate do
transition :active => :inactive
transition [:active, :new] => :inactive
end
end

Expand Down
34 changes: 34 additions & 0 deletions app/types/user_sign_in_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class UserSignInType
include ActiveModel::Validations
include ActiveModel::Conversion
include Virtus

attribute :email, String
attribute :password, String

validates :email, presence: true, email: true
validates :password, presence: true

validates_each :email do |record, attr, value|
user = record.user
if user.try :new?
record.errors.add(attr, :user_new)
end

if user.try :inactive?
record.errors.add(attr, :user_inactive)
end

if !user.try(:authenticate, record.password)
record.errors.add(attr, :user_or_password_invalid)
end
end

def persisted?
false
end

def user
@user ||= User.find_by_email(email)
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/web/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%link{:href => "/favicon.ico", :rel => "icon", :type => "image/x-icon"}/
%link{:href => "/favicon.ico", :rel => "shortcut icon", :type => "image/x-icon"}/
%meta{:content => "http://nastachku.ru/assets/fb_logo.png", :property => "og:image"}
%meta{:content => "Digital-коммуникации, Программирование, IT-стартапы, Электронная коммерция, Менеджмент в IT, IT на службе общества", :property => "og:summary"}
%meta{:content => "Digital-коммуникации, Программирование, IT-стартапы, Электронная коммерция, Менеджмент в IT, IT на службе общества", :property => "og:description"}
%title= content_for?(:title) ? yield(:title) : "Стачка! Конференция"
= analytics_init if Rails.env.production?
= csrf_meta_tags
Expand Down
6 changes: 6 additions & 0 deletions app/views/user_mailer/confirm_registration.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Добрый день, <%= @user.full_name %>.</p>
<p>Ваш почтовый ящик был указан при регистрации на сайте <%= link_to 'nastachku.ru', root_url %>.</p>
<p>Пожалуйста, ПОДТВЕРДИТЕ ВАШУ РЕГИСТРАЦИЮ, пройдя по
<%= link_to 'ссылке', activate_user_url(auth_token: @token.authentication_token) %>
</p>
<p>Не забудьте записать Ваш персональный номер для участия в конференции: <strong><%= @user.id %></strong></p>
2 changes: 1 addition & 1 deletion app/views/web/sessions/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.formy
%h5= t '.login'
.form
= simple_form_for :user, url: session_path, html: { class: 'form-type-login' } do |f|
= simple_form_for @type, url: session_path, html: { class: 'form-type-login' } do |f|

= f.input :email, :tabindex => 1
= f.input :password, :tabindex => 2
Expand Down
9 changes: 8 additions & 1 deletion config/locales/activemodel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ ru:
attributes:
email:
email: 'Введите корректный email!'
user_should_not_exists: 'Пользователь с таким email не существует!'
user_should_not_exists: 'Пользователь с таким email не существует!'
user_sign_in_type:
attributes:
email:
email: 'Введен некорректный email'
user_new: 'Пожалуйста, активируйте свой аккаунт, перейдя по ссылке из письма.'
user_inactive: "Ваш аккаунт заблокирован. Для возобновления работы аккаунта обратитесь в оргкомитет сайта info@nastachku.ru"
user_or_password_invalid: 'Email и/или пароль неверны'
1 change: 1 addition & 0 deletions config/locales/activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ ru:
user:
attributes:
email:
email: 'Введен некорректный email'
taken: 'Данный Email уже зарегистрирован'

4 changes: 3 additions & 1 deletion config/locales/flash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ ru:
web:
users:
create:
success: 'Регистрация прошла успешно. На указанный электронный адрес отправлено письмо с персональным номером для участия в конференции.'
success: 'На указанный Email отправлено письмо со ссылкой на активацию аккаунта и персональным номером для участия в конференции.'
error: 'Пожалуйста, заполните все обязательные поля.'
activate:
success: 'Ваш аккаунт успешно активирован!'

account:
passwords:
Expand Down
4 changes: 2 additions & 2 deletions config/locales/subject.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ru:
user_mailer:
welcome:
subject: "Персональный номер для участия в конференции Стачка"
confirm_registration:
subject: "Подтверждение регистрации"
remind_password:
subject: "Восстановление пароля"
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

scope :module => :web do
resources :users, only: [:new, :create, :index]
resource :user do
get :activate
end

resources :pages, only: [:show]
resource :session, only: [:new, :create, :destroy]
resources :news, only: [:index]
Expand Down
4 changes: 2 additions & 2 deletions lib/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def sign_out
end

def signed_in?
session[:user_id] && User.find_by_id(session[:user_id])
current_user
end

def signed_as_admin?
Expand All @@ -25,7 +25,7 @@ def authenticate_admin!
end

def current_user
@current_user ||= signed_in?
@current_user ||= User.find_by_id(session[:user_id])
end

def basic_auth
Expand Down
3 changes: 1 addition & 2 deletions test/functional/web/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Web::SessionsControllerTest < ActionController::TestCase
test "should authenticate" do
attrs = { email: @user.email, password: @user.password }

post :create, user: attrs

post :create, user_sign_in_type: attrs
assert_response :redirect
assert signed_in?
end
Expand Down

0 comments on commit 0c40d99

Please sign in to comment.