diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 926b4d5..0f1be4b 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -13,17 +13,17 @@ class AccountController < PublicController def login @user = User.new return unless request.post? - self.current_user = User.authenticate(params[:user][:login], params[:user][:password]) + self.current_user = User.authenticate(params[:user][:login], params[:user][:password]) if params[:user] if logged_in? if params[:remember_me] == "1" self.current_user.remember_me cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at } end - go_to_user_initial_page + go_to_user_initial_page if redirect? flash[:notice] = _("Logged in successfully") else flash[:notice] = _('Incorrect username or password') - redirect_to :back + redirect_to :back if redirect? end end @@ -32,6 +32,7 @@ class AccountController < PublicController end def login_popup + @user = User.new render :action => 'login', :layout => false end @@ -41,25 +42,18 @@ class AccountController < PublicController @user = User.new(params[:user]) @user.terms_of_use = environment.terms_of_use @terms_of_use = environment.terms_of_use - if request.post? && params[self.icaptcha_field].blank? && answer_correct + if request.post? && params[self.icaptcha_field].blank? @user.save! @user.person.environment = environment @user.person.save! self.current_user = @user owner_role = Role.find_by_name('owner') @user.person.affiliate(@user.person, [owner_role]) if owner_role - post_activate_enterprise if params[:enterprise_code] - go_to_user_initial_page + go_to_user_initial_page if redirect? flash[:notice] = _("Thanks for signing up!") - else - activate_enterprise if params[:enterprise_code] end rescue ActiveRecord::RecordInvalid - if params[:enterprise_code] - render :action => 'activate_enterprise' - else - render :action => 'signup' - end + render :action => 'signup' end end @@ -128,47 +122,87 @@ class AccountController < PublicController end end - protected - - def activate_enterprise - enterprise = load_enterprise - @enterprise = enterprise - - unless enterprise + def activation_question + @enterprise = load_enterprise + unless @enterprise render :action => 'invalid_enterprise_code' return end - - if enterprise.enabled + if @enterprise.enabled render :action => 'already_activated' return end - - # Reaches here only if answer is not correct - if request.post? && !answer_correct - enterprise.block - end - @question = enterprise.question - - if !@question || enterprise.blocked? + @question = @enterprise.question + if !@question || @enterprise.blocked? render :action => 'blocked' return end + end - render :action => 'activate_enterprise' + def accept_terms + @enterprise = load_enterprise + @question = @enterprise.question + check_answer + @terms_of_enterprise_use = environment.terms_of_enterprise_use end - def post_activate_enterprise + def activate_enterprise + @enterprise = load_enterprise + @question = @enterprise.question + return unless check_answer + return unless check_acceptance_of_terms + load_user + activation = load_enterprise_activation - if activation + if activation && user activation.requestor = user activation.finish + redirect_to :controller => 'profile_editor', :action => 'index', :profile => @enterprise.identifier + end + end + + protected + + def redirect? + !@cannot_redirect + end + + def no_redirect + @cannot_redirect = true + end + + def load_user + unless logged_in? + no_redirect + if params[:new_user] + signup + else + login + end + end + true + end + + def check_answer + unless answer_correct + @enterprise.block + render :action => 'blocked' + return + end + true + end + + def check_acceptance_of_terms + unless params[:terms_accepted] + redirect_to :action => 'index' + return end + true end def load_enterprise_activation - EnterpriseActivation.find_by_code(params[:enterprise_code]) + @enterprise_activation ||= EnterpriseActivation.find_by_code(params[:enterprise_code]) end def load_enterprise diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 095378c..3618886 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -195,7 +195,7 @@ class SearchController < ApplicationController [ [ :people, _('People'), @finder.recent('people', limit) ], [ :enterprises, __('Enterprises'), @finder.recent('enterprises', limit) ], - [ :products, ('Products'), @finder.recent('products', limit) ], + [ :products, _('Products'), @finder.recent('products', limit) ], [ :events, _('Upcoming events'), @finder.upcoming_events({:per_page => limit}) ], [ :communities, __('Communities'), @finder.recent('communities', limit) ], [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles(limit) ], diff --git a/app/models/environment.rb b/app/models/environment.rb index ddde374..327ed9c 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -136,6 +136,23 @@ class Environment < ActiveRecord::Base ! self.settings['terms_of_use'].nil? end + # the environment's terms of enterprise use: every enterprise member must accept them before + # registering or activating enterprises. + def terms_of_enterprise_use + self.settings['terms_of_enterprise_use'] + end + + # sets the environment's terms of enterprise use. + def terms_of_enterprise_use=(value) + self.settings['terms_of_enterprise_use'] = value + end + + # returns true if this Environment has terms of enterprise use to be + # accepted by users before registration or activation of enterprises. + def has_terms_of_enterprise_use? + ! self.settings['terms_of_enterprise_use'].blank? + end + def message_for_disabled_enterprise self.settings['message_for_disabled_enterprise'] end diff --git a/app/models/environment_statistics_block.rb b/app/models/environment_statistics_block.rb index b895e38..033c1ab 100644 --- a/app/models/environment_statistics_block.rb +++ b/app/models/environment_statistics_block.rb @@ -15,8 +15,8 @@ class EnvironmentStatisticsBlock < Block info = [ n_('One user', '%{num} users', users) % { :num => users }, - n__('One enterprise', '%{num} enterprises', enterprises) % { :num => enterprises }, - n__('One community', '%{num} communities', communities) % { :num => communities }, + n_('One enterprise', '%{num} enterprises', enterprises) % { :num => enterprises }, + n_('One community', '%{num} communities', communities) % { :num => communities }, ] block_title(title) + content_tag('ul', info.map {|item| content_tag('li', item) }.join("\n")) diff --git a/app/views/account/_login_form.rhtml b/app/views/account/_login_form.rhtml new file mode 100644 index 0000000..c02853a --- /dev/null +++ b/app/views/account/_login_form.rhtml @@ -0,0 +1,19 @@ +<% labelled_form_for :user, @user, + :url => { :controller => 'account', :action => 'login' }, + :html => { :help => _('If you are a registered user, enter your username and password to be authenticated.')+'
'+_('To join on this environment, click on "I want to be an user!".')+''+_('If you forgot your password, click on "I forgot my password!" link.') } do |f| %> + +<%= f.text_field :login, + :id => ( lightbox? ? 'lightbox_' : '' ) + 'user_login', + :help => _('Here goes the nickname that you give on the registration.'), + :onchange => 'this.value = convToValidLogin( this.value )' %> + +<%= f.password_field :password, + :id => ( lightbox? ? 'lightbox_' : '' ) + 'user_password', + :help => _('your password is personal, protect it.') %> + +<% button_bar do %> + <%= submit_button( 'login', _('Log in') )%> + <%= lightbox_close_button(_('Cancel')) if lightbox? %> +<% end %> + +<% end %> diff --git a/app/views/account/_signup_form.rhtml b/app/views/account/_signup_form.rhtml new file mode 100644 index 0000000..32aaef7 --- /dev/null +++ b/app/views/account/_signup_form.rhtml @@ -0,0 +1,42 @@ ++ <%= @terms_of_use %> +
++ <%= check_box 'user', 'terms_accepted' %> + <%= _('I accept the terms of use') %> +
+<% end %> + +<% button_bar do %> + <%= submit_button('save', _('Sign up'), :cancel => {:action => 'index'}, :class => 'icon-menu-login') %> +<% end %> +<% end -%> diff --git a/app/views/account/accept_terms.rhtml b/app/views/account/accept_terms.rhtml new file mode 100644 index 0000000..1c8cec3 --- /dev/null +++ b/app/views/account/accept_terms.rhtml @@ -0,0 +1,6 @@ +- <%= @terms_of_use %> -
-- <%= check_box 'user', 'terms_accepted' %> - <%= _('I accept the terms of use') %> -
-<% end %> - -<% button_bar do %> - <%= submit_button('save', _('Sign up'), :cancel => {:action => 'index'}, :class => 'icon-menu-login') %> -<% end %> -<% end -%> +- <%= @terms_of_use %> -
-- <%= check_box 'user', 'terms_accepted' %> - <%= _('I accept the terms of use') %> -
-<% end %> - -<% button_bar do %> - <%= submit_button('save', _('Sign up'), :cancel => {:action => 'index'}, :class => 'icon-menu-login') %> -<% end %> -<% end -%> +<%= render :partial => 'signup_form' %> diff --git a/db/schema.rb b/db/schema.rb index 98ff7c2..a7d7daa 100644 --- 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 => 49) do +ActiveRecord::Schema.define(:version => 50) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -219,6 +219,7 @@ ActiveRecord::Schema.define(:version => 49) do t.text "custom_header" t.text "custom_footer" t.string "theme" + t.boolean "public_profile", :default => true end add_index "profiles", ["environment_id"], :name => "index_profiles_on_environment_id" diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index ac6c075..6600789 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -277,10 +277,16 @@ class AccountControllerTest < Test::Unit::TestCase assert_redirected_to :controller => 'profile_editor' end +################################ +# # +# Enterprise activation tests # +# # +################################ + should 'report invalid enterprise code on signup' do EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once - get :signup, :enterprise_code => 'some_invalid_code' + get :activation_question, :enterprise_code => 'some_invalid_code' assert_template 'invalid_enterprise_code' end @@ -291,19 +297,19 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_template 'already_activated' end - should 'load enterprise from code on signup' do + should 'load enterprise from code on for validation question' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') task = mock task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_equal ent, assigns(:enterprise) end @@ -315,7 +321,7 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_template 'blocked' end @@ -327,34 +333,32 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' - assert_template 'activate_enterprise' + assert_template 'activation_question' end should 'show form to those enterprises that have cnpj' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :cnpj => '0'*14, :enabled => false) - task = mock task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' - assert_template 'activate_enterprise' + assert_template 'activation_question' end should 'block those who are blocked' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => '1998', :enabled => false) ent.block - task = mock task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_template 'blocked' end @@ -366,7 +370,8 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - create_user({}, :enterprise_code => '0123456789', :answer => '1997') + post :accept_terms, :enterprise_code => '0123456789', :answer => '1997' + ent.reload assert_nil User.find_by_login('test_user') @@ -374,17 +379,97 @@ class AccountControllerTest < Test::Unit::TestCase assert_template 'blocked' end - should 'activate enterprise for those who answer the question right and make them admin of the enterprise' do + should 'show terms of use for enterprise owners' do + env = Environment.default + env.terms_of_enterprise_use = 'Some terms' + env.save! + + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' + + assert_template 'accept_terms' + assert_tag :tag => 'div', :content => 'Some terms' + end + + should 'not activate if user does not accept terms' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + login_as(p.identifier) + + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false + ent.reload + + assert !ent.enabled + assert_not_includes ent.members, p + end + + should 'ask for login or singup if not logged in' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true + + assert_template 'activate_enterprise' + end + + should 'activate enterprise and make logged user admin' do ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + login_as(p.identifier) task = EnterpriseActivation.create!(:enterprise => ent) EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - create_user({}, :enterprise_code => '0123456789', :answer => '1998') + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true ent.reload assert ent.enabled - assert_includes ent.members, assigns(:user).person + assert_includes ent.members, p + end + + should 'not activate enterprise for inexistent user' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true, :user => { :login => 'inexistent_user', :password => 'inexistent_password' } + ent.reload + + assert !ent.enabled + end + + should 'activate enterprise and make unlogged user admin' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + p = User.create!(:login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com').person + + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true, :user => { :login => 'test_user', :password => 'blih' } + ent.reload + + assert ent.enabled + assert_includes ent.members, p + end + + should 'activate enterprise, create user and make admin' do + ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :foundation_year => 1998, :enabled => false) + + task = EnterpriseActivation.create!(:enterprise => ent) + EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + + post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true, :new_user => true, :user => { :login => 'test_user', :password => 'blih', :password_confirmation => 'blih', :email => 'test@noosfero.com' } + ent.reload + + assert ent.enabled + assert_includes ent.members.map(&:identifier), 'test_user' end should 'put hidden field with enterprise code for answering question' do @@ -394,12 +479,13 @@ class AccountControllerTest < Test::Unit::TestCase task.expects(:enterprise).returns(ent).at_least_once EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once - get :signup, :enterprise_code => '0123456789' + get :activation_question, :enterprise_code => '0123456789' assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'enterprise_code', :value => '0123456789'} - end +# end of enterprise activation tests + should 'not be able to signup while inverse captcha field filled' do assert_no_difference User, :count do create_user({}, @controller.icaptcha_field => 'bli@bla.email.foo') diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 01ebed2..341b072 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -307,15 +307,16 @@ class SearchControllerTest < Test::Unit::TestCase get :index, :query => 'display' names = { - :articles => 'Articles', - :enterprises => 'Enterprises', - :communities => 'Communities', - :products => 'Products', - :events => 'Events', + :articles => ['Articles', article], + :enterprises => ['Enterprises', ent], + :communities => ['Communities', community], + :products => ['Products', product], + :events => ['Events', event], } names.each do |thing, description| + description, object = description assert_tag :tag => 'div', :attributes => { :class => /search-results-#{thing}/ }, :descendant => { :tag => 'h3', :content => Regexp.new(description) } - assert_tag :tag => 'a', :content => "display #{thing.to_s.singularize}" + assert_tag :tag => 'a', :content => object.respond_to?(:short_name) ? object.short_name : object.name end # display only first name on people listing diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 0e14e66..548b782 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -78,6 +78,22 @@ class EnvironmentTest < Test::Unit::TestCase assert v.has_terms_of_use? end + def test_terms_of_enterprise_use + v = Environment.new(:name => 'My test environment') + assert_nil v.terms_of_enterprise_use + v.terms_of_enterprise_use = 'To be owner of an enterprise in this environment, you must accept the following terms: ...' + assert v.save + id = v.id + assert_equal 'To be owner of an enterprise in this environment, you must accept the following terms: ...', Environment.find(id).terms_of_enterprise_use + end + + def test_has_terms_of_enterprise_use + v = Environment.new + assert !v.has_terms_of_enterprise_use? + v.terms_of_enterprise_use = 'some terms of enterprise use' + assert v.has_terms_of_enterprise_use? + end + def test_should_list_top_level_categories env = Environment.create!(:name => 'a test environment') cat1 = Category.create!(:name => 'first category', :environment_id => env.id) -- libgit2 0.21.2