diff --git a/app/api/app.rb b/app/api/app.rb index e6cb3f1..378db97 100644 --- a/app/api/app.rb +++ b/app/api/app.rb @@ -11,7 +11,30 @@ module Api mount Federation::Webfinger end - class BaseApi < Grape::API + class App < Grape::API + use Rack::JSONP + + logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) + logger.formatter = GrapeLogging::Formatters::Default.new + #use GrapeLogging::Middleware::RequestLogger, { logger: logger } + + rescue_from :all do |e| + logger.error e + error! e.message, 500 + end unless Rails.env.test? + + @@NOOSFERO_CONF = nil + def self.NOOSFERO_CONF + if @@NOOSFERO_CONF + @@NOOSFERO_CONF + else + file = Rails.root.join('config', 'noosfero.yml') + @@NOOSFERO_CONF = File.exists?(file) ? YAML.load_file(file)[Rails.env] || {} : {} + end + end + + mount NoosferoFederation + before { set_locale } before { setup_multitenancy } before { detect_stuff_by_domain } @@ -43,31 +66,7 @@ module Api mount V1::Blocks mount V1::Profiles mount V1::Activities - end - - class App < Grape::API - use Rack::JSONP - - logger = Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV'] || 'production'}_api.log")) - logger.formatter = GrapeLogging::Formatters::Default.new - #use GrapeLogging::Middleware::RequestLogger, { logger: logger } - rescue_from :all do |e| - logger.error e - error! e.message, 500 - end unless Rails.env.test? - - @@NOOSFERO_CONF = nil - def self.NOOSFERO_CONF - if @@NOOSFERO_CONF - @@NOOSFERO_CONF - else - file = Rails.root.join('config', 'noosfero.yml') - @@NOOSFERO_CONF = File.exists?(file) ? YAML.load_file(file)[Rails.env] || {} : {} - end - end - mount BaseApi - mount NoosferoFederation # hook point which allow plugins to add Grape::API extensions to Api::App #finds for plugins which has api mount points classes defined (the class should extends Grape::API) @plugins = Noosfero::Plugin.all.map { |p| p.constantize } diff --git a/app/api/federation/webfinger.rb b/app/api/federation/webfinger.rb index 9e9aa6a..821e653 100644 --- a/app/api/federation/webfinger.rb +++ b/app/api/federation/webfinger.rb @@ -44,7 +44,7 @@ def acct_hash acct = Hash.new{|hash, key| hash[key] = Hash.new{|hash, key| hash[key] = Array.new}} url = rails.options[:Host] + ':' + rails.options[:Port].to_s + '/' person = Person.find_by_identifier(extract_person_identifier) - + if person.nil? Rails.logger.error 'Person not found' not_found! diff --git a/app/api/v1/activities.rb b/app/api/v1/activities.rb index 494c9b1..e29245b 100644 --- a/app/api/v1/activities.rb +++ b/app/api/v1/activities.rb @@ -1,7 +1,7 @@ module Api module V1 class Activities < Grape::API - + resource :profiles do get ':id/activities' do @@ -9,7 +9,7 @@ module Api not_found! if profile.blank? || profile.secret || !profile.visible forbidden! if !profile.display_private_info_to?(current_person) - + activities = profile.activities.map(&:activity) present activities, :with => Entities::Activity, :current_person => current_person end diff --git a/app/api/v1/articles.rb b/app/api/v1/articles.rb index cc24eac..787c13a 100644 --- a/app/api/v1/articles.rb +++ b/app/api/v1/articles.rb @@ -64,7 +64,7 @@ module Api render_api_error!(_('The article couldn\'t be removed due to some problem. Please contact the administrator.'), 400) end end - + desc 'Report a abuse and/or violent content in a article by id' do detail 'Submit a abuse (in general, a content violation) report about a specific article' params Entities::Article.documentation diff --git a/app/concerns/authenticated_system.rb b/app/concerns/authenticated_system.rb index cfda584..87c92a6 100644 --- a/app/concerns/authenticated_system.rb +++ b/app/concerns/authenticated_system.rb @@ -27,8 +27,8 @@ module AuthenticatedSystem @current_user ||= begin user = nil if session[:external] - user = User.new #FIXME: User needs to have at least email - external_person = ExternalPerson.where(id: session[:external]).last + user = User.new + external_person = ExternalPerson.find_by(id: session[:external]) if external_person user.external_person_id = external_person.id user.email = external_person.email @@ -36,8 +36,7 @@ module AuthenticatedSystem session[:external] = nil end else - id = session[:user] - user = User.where(id: id).first if id + user = User.find_by(id: user_id) if user_id end user.session = session if user User.current = user diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index b709b5e..cf44d9d 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -398,7 +398,7 @@ class ProfileController < PublicController def icon size = params[:size] || :portrait image, mime = profile_icon(profile, size.to_sym, true) - + unless image.match(/^\/\/www\.gravatar\.com/).nil? redirect_to 'https:' + image else diff --git a/app/helpers/theme_loader_helper.rb b/app/helpers/theme_loader_helper.rb index e2e0be2..9136af3 100644 --- a/app/helpers/theme_loader_helper.rb +++ b/app/helpers/theme_loader_helper.rb @@ -2,7 +2,7 @@ module ThemeLoaderHelper def current_theme @current_theme ||= begin - if !(defined?(session)).nil? && session[:user_theme] + if defined?(session).present? && session[:user_theme] session[:user_theme] else # utility for developers: set the theme to 'random' in development mode and @@ -34,9 +34,9 @@ module ThemeLoaderHelper end def theme_path - if !(defined?(session)).nil? && session[:user_theme] + if defined?(session).present? && session[:user_theme] '/user_themes/' + current_theme - elsif session[:theme] + elsif defined?(session).present? && session[:theme] '/designs/themes/' + session[:theme] else '/designs/themes/' + current_theme diff --git a/app/models/concerns/external_user.rb b/app/models/concerns/external_user.rb index 6b1a84e..7ce6f3c 100644 --- a/app/models/concerns/external_user.rb +++ b/app/models/concerns/external_user.rb @@ -19,7 +19,7 @@ module ExternalUser def webfinger_lookup(login, domain, environment) if login && domain && environment.has_federated_network?(domain) url = URI.parse('https://'+ domain +'/.well-known/webfinger?resource=acct:'+ - login+'@'+Environment.default.external_environments.find_by_url(domain)) + login+'@'+domain) req = Net::HTTP::Get.new(url.to_s) res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) } JSON.parse(res.body) @@ -67,26 +67,29 @@ module ExternalUser # Authenticates a user from an external social network def external_authenticate(username, password, environment) - login, domain = username.split('@') - webfinger = User.webfinger_lookup(login, domain, environment) - if webfinger - user = User.external_login(login, password, domain) - if user - u = User.new - u.email = user['user']['email'] - u.login = login - webfinger = OpenStruct.new( - identifier: webfinger['properties']['identifier'], - name: webfinger['titles']['name'], - created_at: webfinger['properties']['created_at'], - domain: domain, - email: user['user']['email'] - ) - u.external_person_id = ExternalPerson.get_or_create(webfinger).id - return u + if username && username.include?('@') + login, domain = username.split('@') + webfinger = User.webfinger_lookup(login, domain, environment) + if webfinger + user = User.external_login(login, password, domain) + if user + u = User.new + u.email = user['user']['email'] + u.login = login + webfinger = OpenStruct.new( + identifier: webfinger['properties']['identifier'], + name: webfinger['titles']['name'], + created_at: webfinger['properties']['created_at'], + domain: domain, + email: user['user']['email'] + ) + u.external_person_id = ExternalPerson.get_or_create(webfinger).id + return u + end end end nil end + end end diff --git a/config/routes.rb b/config/routes.rb index 076ed0f..809205f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -86,7 +86,7 @@ Noosfero::Application.routes.draw do # comments match 'profile/:profile/comment/:action/:id', controller: 'comment', profile: /#{Noosfero.identifier_format_in_url}/i, via: :all - + # icon match 'profile/:profile/icon(/:size)', controller: 'profile', action: 'icon', size: /(big|minor|thumb|portrait|icon)/, profile: /#{Noosfero.identifier_format_in_url}/i, via: :get diff --git a/features/external_login.feature b/features/external_login.feature index 9ae3302..8236b10 100644 --- a/features/external_login.feature +++ b/features/external_login.feature @@ -6,7 +6,7 @@ Feature: external login @selenium Scenario: login from portal homepage Given feature "allow_change_of_redirection_after_login" is disabled on environment - And the following federated networks + And the following external environments | identifier | name | url | | test | Test | http://federated.noosfero.org | And the following external users @@ -25,7 +25,7 @@ Feature: external login @selenium Scenario: not login from portal homepage Given feature "allow_change_of_redirection_after_login" is disabled on environment - And the following federated networks + And the following external environments | identifier | name | url | | test | Test | http://federated.noosfero.org | And I am not logged in diff --git a/plugins/organization_ratings/test/functional/organization_ratings_plugin_profile_controller_test.rb b/plugins/organization_ratings/test/functional/organization_ratings_plugin_profile_controller_test.rb index 77c6111..85ccb58 100644 --- a/plugins/organization_ratings/test/functional/organization_ratings_plugin_profile_controller_test.rb +++ b/plugins/organization_ratings/test/functional/organization_ratings_plugin_profile_controller_test.rb @@ -173,6 +173,7 @@ class OrganizationRatingsPluginProfileControllerTest < ActionController::TestCas logout @controller.stubs(:logged_in?).returns(false) + @controller.stubs(:current_user).returns(nil) get :new_rating, profile: @community.identifier assert_no_tag :tag => 'p', :content => /Report waiting for approval/, :attributes => {:class =>/comment-rejected-msg/} diff --git a/plugins/organization_ratings/views/shared/_make_report_block.html.erb b/plugins/organization_ratings/views/shared/_make_report_block.html.erb index bb16b3e..d2fdf51 100644 --- a/plugins/organization_ratings/views/shared/_make_report_block.html.erb +++ b/plugins/organization_ratings/views/shared/_make_report_block.html.erb @@ -1,6 +1,6 @@ <% logged_in_image = link_to profile_image(current_person, :portrait), current_person.url if current_user %> <% logged_in_name = link_to current_person.name, current_person.url if current_user %> -<% logged_out_image = image_tag('plugins/organization_ratings/images/user-not-logged.png') %> +<% logged_out_image = image_tag('plugins/organization_ratings/public/images/user-not-logged.png') %>
diff --git a/test/api/federation/webfinger_test.rb b/test/api/federation/webfinger_test.rb index 69f31ac..cb75064 100644 --- a/test/api/federation/webfinger_test.rb +++ b/test/api/federation/webfinger_test.rb @@ -4,7 +4,7 @@ class WebfingerTest < ActiveSupport::TestCase def setup Domain.create(name: 'example.com') Environment.default.domains << Domain.last - User.create(login: 'ze', email: 'ze@localdomain.localdomain', + User.create(login: 'ze', email: 'ze@localdomain.localdomain', password: 'zeze', password_confirmation: 'zeze') end diff --git a/test/api/people_test.rb b/test/api/people_test.rb index dad9b7a..60f7a2e 100644 --- a/test/api/people_test.rb +++ b/test/api/people_test.rb @@ -376,7 +376,7 @@ class PeopleTest < ActiveSupport::TestCase get "/api/v1/people/#{profile.id}/icon?#{params.to_query}" assert_equal 200, last_response.status json = JSON.parse(last_response.body) - assert_match /^https?:\/\/.*portrait\.png$/, json['icon'] + assert_match(/^https?:\/\/.*portrait\.png$/, json['icon']) end should 'return icon in provided size if there is a profile image' do @@ -386,7 +386,7 @@ class PeopleTest < ActiveSupport::TestCase get "/api/v1/people/#{profile.id}/icon?#{params.to_query}&size=big" assert_equal 200, last_response.status json = JSON.parse(last_response.body) - assert_match /^https?:\/\/.*big\.png$/, json['icon'] + assert_match(/^https?:\/\/.*big\.png$/, json['icon']) end should 'return icon from gravatar without size if there is no profile image' do @@ -395,7 +395,7 @@ class PeopleTest < ActiveSupport::TestCase get "/api/v1/people/#{profile.id}/icon?#{params.to_query}" assert_equal 200, last_response.status json = JSON.parse(last_response.body) - assert_match /^https:\/\/www\.gravatar\.com.*size=64/, json['icon'] + assert_match(/^https:\/\/www\.gravatar\.com.*size=64/, json['icon']) end should 'return icon from gravatar with size if there is no profile image' do @@ -404,7 +404,7 @@ class PeopleTest < ActiveSupport::TestCase get "/api/v1/people/#{profile.id}/icon?#{params.to_query}&size=big" assert_equal 200, last_response.status json = JSON.parse(last_response.body) - assert_match /^https:\/\/www\.gravatar\.com.*size=150/, json['icon'] + assert_match(/^https:\/\/www\.gravatar\.com.*size=150/, json['icon']) end PERSON_ATTRIBUTES = %w(vote_count comments_count articles_count following_articles_count) diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index fee1442..d61973d 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -1935,7 +1935,7 @@ class ProfileControllerTest < ActionController::TestCase should 'return portrait icon if size is not provided and there is a profile image' do img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')) profile = fast_create(Person, image_id: img.id) - + get :icon, profile: profile.identifier, size: nil assert_response :success assert_equal 'image/png', @response.header['Content-Type'] @@ -1945,7 +1945,7 @@ class ProfileControllerTest < ActionController::TestCase should 'return icon in provided size if there is a profile image' do img = Image.create!(uploaded_data: fixture_file_upload('/files/rails.png', 'image/png')) profile = fast_create(Person, image_id: img.id) - + get :icon, profile: profile.identifier, size: :big assert_response :success assert_equal 'image/png', @response.header['Content-Type'] @@ -1954,14 +1954,14 @@ class ProfileControllerTest < ActionController::TestCase should 'return icon from gravatar without size if there is no profile image' do profile = fast_create(Person) - + get :icon, profile: profile.identifier assert_redirected_to /^https:\/\/www\.gravatar\.com\/.*/ end should 'return icon from gravatar with size if there is no profile image' do profile = fast_create(Person) - + get :icon, profile: profile.identifier, size: :thumb assert_redirected_to /^https:\/\/www\.gravatar\.com\/.*/ end -- libgit2 0.21.2