Fix nickname format check. Apply readonly nickname field. Show flash notice during registration through OpenID. Fix email read from OpenID response. Refs #1954

This commit is contained in:
Pavel Chipiga 2011-10-13 13:19:42 +03:00
parent 77e03df70b
commit 0efa8c83a2
7 changed files with 16 additions and 35 deletions

View File

@ -1,5 +1,6 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def open_id
# raise env['omniauth.auth'].inspect
generic
end
@ -15,31 +16,14 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
if user_signed_in? # New authentication method for current_user
authentication.user = current_user
authentication.save
else # Register new user
else # Register new user from session
session["devise.omniauth_data"] = env["omniauth.auth"]
flash[:notice] = I18n.t "devise.omniauth_callbacks.register"
redirect_to new_user_registration_url
end
else
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => action_name.classify
sign_in_and_redirect authentication.user, :event => :authentication
end
# @authentication = Authentication.find_or_initialize_by_provider_and_uid(env['omniauth.auth']['provider'], env['omniauth.auth']['uid'])
# if @authentication.new_record?
# if user_signed_in? # New authentication method for current_user
# user = current_user
# else # Register new user
# user = User.new(:password => Devise.friendly_token[0,20]) # Stub password
# user.init_from env['omniauth.auth'], action_name
# user.
# end
# extra = env['omniauth.auth']['extra']['user_hash'] rescue {}
# @authentication.user_info = env['omniauth.auth']['user_info'].merge extra
# # Assign authentication to user
# @authentication.user = user
# @authentication.save
# end
# flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => action_name.classify
# sign_in_and_redirect @authentication.user, :event => :authentication
end
end

View File

@ -4,14 +4,13 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
:recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable
validates :nickname, :presence => true, :uniqueness => {:case_sensitive => false},
:format => {:with => /[\w]+/i} #, :exclusion => {:in => %w(superuser moderator')}
validates :nickname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => /^[a-zA-Z0-9_]+$/i
attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :nickname, :name, :ssh_key
attr_readonly :nickname
attr_accessor :login
before_validation(:on => :update) { raise "Can't change nickname" if nickname_changed? } # disable edit username
# after_create() { UserMailer.new_user_notification(self).deliver }
class << self
@ -25,7 +24,7 @@ class User < ActiveRecord::Base
super.tap do |user|
if data = session["devise.omniauth_data"]
if info = data['user_info'] and info.present?
user.email ||= info['email'].presence
user.email = info['email'].presence if user.email.blank?
user.nickname ||= info['nickname'].presence || info['username'].presence
user.name ||= info['name'].presence || [info['first_name'], info['last_name']].join(' ').strip
end

View File

@ -4,15 +4,13 @@
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :class => "form" }) do |f|
- if resource.errors.present?
.flash
.message.error
- messages = resource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join.html_safe
= messages
.message.errorresource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join.html_safe
/ .group.wat-cf
/ .left
/ = f.label :nickname, :class => "label"
/ .right
/ = f.text_field :nickname, :class => "text_field"
.group.wat-cf
.left
= f.label :nickname, :class => "label"
.right
= f.text_field :nickname, :class => "text_field", :disabled => true
.group.wat-cf
.left

View File

@ -4,9 +4,7 @@
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => "form" }) do |f|
- if resource.errors.present?
.flash
.message.error
- messages = resource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join.html_safe
= messages
.message.error= resource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join.html_safe
.group.wat-cf
.left

View File

@ -1,4 +1,4 @@
.flash
- flash.each do |type, message|
%div{:class => "message #{type}"}
.message{:class => type}
%p= message

View File

@ -10,4 +10,5 @@
%body
#container
#box
= render :partial => "layouts/flashes"
= yield

View File

@ -58,6 +58,7 @@ ru:
send_paranoid_instructions: "Если ваша учётная запись существует, то в течение нескольких минут вы получите письмо с инструкциями по её разблокировке."
unlocked: 'Ваша учетная запись разблокирована. Вы вошли.'
omniauth_callbacks:
register: "Аутентификация прошла успешно, но необходимо уточнить дополнительные данные"
success: "Вход в систему выполнен с учётной записью из %{kind}."
failure: "Вы не можете войти в систему с учётной записью из %{kind}, т.к. \"%{reason}\"."
mailer: