diff --git a/plugins/stoa/README b/plugins/stoa/README new file mode 100644 index 0000000..a3485e4 --- /dev/null +++ b/plugins/stoa/README @@ -0,0 +1,12 @@ +Banco de Dados +============== + +É preciso adicionar uma seção definindo a conexão com o banco de usuários da USP, no arquivo config/database.yml com identificador stoa. +`` +stoa: + adapter: mysql + host: db2.stoa.usp.br + database: usp + username: + password: +`` diff --git a/plugins/stoa/controllers/stoa_plugin_controller.rb b/plugins/stoa/controllers/stoa_plugin_controller.rb new file mode 100644 index 0000000..ad68c07 --- /dev/null +++ b/plugins/stoa/controllers/stoa_plugin_controller.rb @@ -0,0 +1,31 @@ +class StoaPluginController < PublicController + append_view_path File.join(File.dirname(__FILE__) + '/../views') + + def authenticate + if request.ssl? && request.post? + user = User.authenticate(params[:login], params[:password], environment) + if user + result = { + :username => user.login, + :email => user.email, + :name => user.name, + :nusp => user.person.usp_id, + :first_name => user.name.split(' ').first, + :surname => user.name.split(' ',2).last, + :address => user.person.address, + :homepage => user.person.url, + } + else + result = { :error => _('Incorrect user/password pair.') } + end + render :text => result.to_json + else + render :text => { :error => _('Conection requires SSL certificate and post method.') }.to_json + end + end + + def check_usp_id + render :text => { :exists => StoaPlugin::UspUser.exists?(params[:usp_id]) }.to_json + end + +end diff --git a/plugins/stoa/db/migrate/20120301212702_add_usp_id_to_profile.rb b/plugins/stoa/db/migrate/20120301212702_add_usp_id_to_profile.rb new file mode 100644 index 0000000..74d262e --- /dev/null +++ b/plugins/stoa/db/migrate/20120301212702_add_usp_id_to_profile.rb @@ -0,0 +1,9 @@ +class AddUspIdToProfile < ActiveRecord::Migration + def self.up + add_column :profiles, :usp_id, :string + end + + def self.down + remove_column :profiles, :usp_id + end +end diff --git a/plugins/stoa/lib/stoa_plugin.rb b/plugins/stoa/lib/stoa_plugin.rb new file mode 100644 index 0000000..d75239a --- /dev/null +++ b/plugins/stoa/lib/stoa_plugin.rb @@ -0,0 +1,59 @@ +require_dependency 'person' + +class StoaPlugin < Noosfero::Plugin + + Person.human_names[:usp_id] = _('USP number') + + def self.plugin_name + "Stoa" + end + + def self.plugin_description + _("Add Stoa features") + end + + def stylesheet? + true + end + + def signup_extra_contents + lambda { + required(labelled_form_field(_('USP number'), text_field_tag('profile_data[usp_id]', '', :id => 'usp_id_field'))) + + labelled_form_field(_('Select a confirmation data'), select_tag('confirmation_field', + options_for_select([['CPF','cpf'], [_('Mother\'s name'), 'mother'], [_('Birth date (yyyy-mm-dd)'), 'birth']]) + )) + + required(labelled_form_field(_('Confirmation value'), text_field_tag('confirmation_value', '', :placeholder=>_('Confirmation value')))) + + javascript_tag(<<-EOF + jQuery("#usp_id_field").change(function(){ + var me=this; + jQuery(this).addClass('checking').removeClass('validated'); + jQuery.getJSON('#{url_for(:controller => 'stoa_plugin', :action => 'check_usp_id')}?usp_id='+this.value, + function(data){ + if(data.exists) jQuery(me).removeClass('checking').addClass('validated'); + else jQuery(me).removeClass('checking').addClass('invalid'); + } + ); + }); + EOF + ) + } + end + + def account_controller_filters + block = lambda do + if request.post? + if !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[:confirmation_value]) + @person = Person.new + @person.errors.add(:usp_id, _(' validation failed')) + render :action => :signup + end + end + end + + [{ :type => 'before_filter', + :method_name => 'validate_usp_id', + :options => {:only => 'signup'}, + :block => block }] + end + +end diff --git a/plugins/stoa/lib/stoa_plugin/usp_user.rb b/plugins/stoa/lib/stoa_plugin/usp_user.rb new file mode 100644 index 0000000..be48df7 --- /dev/null +++ b/plugins/stoa/lib/stoa_plugin/usp_user.rb @@ -0,0 +1,21 @@ +class StoaPlugin::UspUser < ActiveRecord::Base + + establish_connection(:stoa) + set_table_name('pessoa') + + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] + + alias_attribute :cpf, :numcpf + alias_attribute :rg, :numdocidf + + def self.exists?(usp_id) + !StoaPlugin::UspUser.find(:first, :conditions => {:codpes => usp_id}).nil? + end + + def self.matches?(usp_id, field, value) + user = StoaPlugin::UspUser.find(:first, :conditions => {:codpes => usp_id}) + return false if user.nil? || !user.respond_to?(field) || value.blank? + user.send(field) == Digest::MD5.hexdigest(SALT+value.to_s) + end + +end diff --git a/plugins/stoa/public/style.css b/plugins/stoa/public/style.css new file mode 100644 index 0000000..18346f8 --- /dev/null +++ b/plugins/stoa/public/style.css @@ -0,0 +1,5 @@ +#signup-form label[for="usp_id_field"], +#signup-form label[for="confirmation_field"] { + display: block; +} + diff --git a/plugins/stoa/test/functional/account_controller_test.rb b/plugins/stoa/test/functional/account_controller_test.rb new file mode 100644 index 0000000..75a113c --- /dev/null +++ b/plugins/stoa/test/functional/account_controller_test.rb @@ -0,0 +1,32 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../../../app/controllers/public/account_controller' + +# Re-raise errors caught by the controller. +class AccountController; def rescue_action(e) raise e end; end + +class AccountControllerTest < ActionController::TestCase + + def setup + @controller = AccountController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + environment = Environment.default + environment.enabled_plugins = ['StoaPlugin'] + environment.save! + @db = Tempfile.new('stoa-test') + configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} + end + + should 'fail if confirmation value doesn\'t match' do + StoaPlugin::UspUser.stubs(:matches?).returns(false) + post :signup, :profile_data => {:usp_id => '87654321'}, :confirmation_field => 'cpf', :confirmation_value => '00000000' + assert_not_nil assigns(:person).errors[:usp_id] + end + + should 'pass if confirmation value matches' do + StoaPlugin::UspUser.stubs(:matches?).returns(true) + post :signup, :profile_data => {:usp_id => '87654321'}, :confirmation_field => 'cpf', :confirmation_value => '12345678' + assert_nil assigns(:person).errors[:usp_id] + end + +end diff --git a/plugins/stoa/test/functional/stoa_plugin_controller_test.rb b/plugins/stoa/test/functional/stoa_plugin_controller_test.rb new file mode 100644 index 0000000..68a3091 --- /dev/null +++ b/plugins/stoa/test/functional/stoa_plugin_controller_test.rb @@ -0,0 +1,74 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/stoa_plugin_controller' + +# Re-raise errors caught by the controller. +class StoaPluginController; def rescue_action(e) raise e end; end + +class StoaPluginControllerTest < ActionController::TestCase + + def setup + @controller = StoaPluginController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @user = create_user('real_user', :password => '123456', :password_confirmation => '123456') + environment = Environment.default + environment.enabled_plugins = ['StoaPlugin'] + environment.save! + @db = Tempfile.new('stoa-test') + configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} + end + + attr_accessor :user + + should 'not authenticate if method not post' do + @request.stubs(:ssl?).returns(true) + get :authenticate, :login => user.login, :password => '123456' + + assert_not_nil json_response['error'] + assert_match /post method/,json_response['error'] + end + + should 'not authenticate if request is not using ssl' do + @request.stubs(:ssl?).returns(false) + post :authenticate, :login => user.login, :password => '123456' + + assert_not_nil json_response['error'] + assert_match /SSL/,json_response['error'] + end + + should 'not authenticate if method password is wrong' do + @request.stubs(:ssl?).returns(true) + post :authenticate, :login => user.login, :password => 'wrong_password' + + assert_not_nil json_response['error'] + assert_match /password/,json_response['error'] + end + + should 'authenticate if everything is right' do + @request.stubs(:ssl?).returns(true) + post :authenticate, :login => user.login, :password => '123456' + + assert_nil json_response['error'] + assert_equal user.login, json_response['username'] + end + + should 'check invalid usp id' do + StoaPlugin::UspUser.stubs(:exists?).returns(false) + get :check_usp_id, :usp_id => '987654321' + assert !json_response['exists'] + end + + should 'check valid usp id' do + StoaPlugin::UspUser.stubs(:exists?).returns(true) + get :check_usp_id, :usp_id => '987654321' + assert json_response['exists'] + end + + private + + def json_response + ActiveSupport::JSON.decode @response.body + end + +end + diff --git a/plugins/stoa/test/unit/usp_user.rb b/plugins/stoa/test/unit/usp_user.rb new file mode 100644 index 0000000..9575bad --- /dev/null +++ b/plugins/stoa/test/unit/usp_user.rb @@ -0,0 +1,35 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class StoaPlugin::UspUserTest < ActiveSupport::TestCase + + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] + + def setup + @db = Tempfile.new('stoa-test') + configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} + ActiveRecord::Base.establish_connection(:stoa) + ActiveRecord::Schema.create_table "pessoa" do |t| + t.integer "codpes" + t.text "numcpf" + t.text "numdocidf" + end + ActiveRecord::Base.establish_connection(:test) + StoaPlugin::UspUser.create!(:codpes => 123456, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :rg => Digest::MD5.hexdigest(SALT+'87654321')) + end + + def teardown + @db.unlink + end + + should 'check existence of usp_id' do + assert StoaPlugin::UspUser.exists?(123456) + assert !StoaPlugin::UspUser.exists?(654321) + end + + should 'check if usp_id matches with a field' do + assert StoaPlugin::UspUser.matches?(123456, :cpf, 12345678) + assert !StoaPlugin::UspUser.matches?(123456, :cpf, 87654321) + assert !StoaPlugin::UspUser.matches?(654321, :cpf, 12345678) + end +end + -- libgit2 0.21.2