From 59b5ba7c0f3d4f2f86d00b5f08e0cfef8de4ffe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 7 Jul 2014 22:18:18 +0200 Subject: [PATCH] BUGFIX: IP lookup wasn't working when using HTTPS REFACTOR: the ip locator into a ip-lookup component --- .../admin/components/ip-lookup.js.es6 | 43 +++++++++++++++ .../admin_users_list_controller.js | 2 +- .../javascripts/admin/models/admin_user.js | 2 +- .../admin/templates/ip_locator.js.handlebars | 54 ------------------- .../admin/templates/user_index.js.handlebars | 10 ++-- .../components/ip-lookup.js.handlebars | 54 +++++++++++++++++++ .../stylesheets/common/admin/admin_base.scss | 2 +- app/controllers/admin/users_controller.rb | 9 ++++ config/locales/client.en.yml | 2 +- config/routes.rb | 1 + lib/admin_user_index_query.rb | 1 - .../admin/users_controller_spec.rb | 9 ++++ 12 files changed, 125 insertions(+), 64 deletions(-) create mode 100644 app/assets/javascripts/admin/components/ip-lookup.js.es6 delete mode 100644 app/assets/javascripts/admin/templates/ip_locator.js.handlebars create mode 100644 app/assets/javascripts/discourse/templates/components/ip-lookup.js.handlebars diff --git a/app/assets/javascripts/admin/components/ip-lookup.js.es6 b/app/assets/javascripts/admin/components/ip-lookup.js.es6 new file mode 100644 index 000000000..d93d44c90 --- /dev/null +++ b/app/assets/javascripts/admin/components/ip-lookup.js.es6 @@ -0,0 +1,43 @@ +export default Ember.Component.extend({ + classNames: ["ip-lookup"], + + city: function () { + return [ + this.get("location.city"), + this.get("location.region"), + this.get("location.country") + ].filter(Boolean).join(", "); + }.property("location.@{city,region,country}"), + + actions: { + lookup: function () { + var self = this; + this.set("show", true); + + if (!this.get("location")) { + Discourse.ajax("/admin/users/ip-info.json", { + data: { ip: this.get("ip") } + }).then(function (location) { + self.set("location", Em.Object.create(location)); + }); + } + + if (!this.get("other_accounts")) { + this.set("other_accounts_loading", true); + Discourse.AdminUser.findAll("active", { + "ip": this.get("ip"), + "exclude": this.get("user_id") + }).then(function (users) { + self.setProperties({ + other_accounts: users, + other_accounts_loading: false, + }); + }); + } + }, + + hide: function () { + this.set("show", false); + } + } +}); diff --git a/app/assets/javascripts/admin/controllers/admin_users_list_controller.js b/app/assets/javascripts/admin/controllers/admin_users_list_controller.js index 7322aa2d9..5239056e1 100644 --- a/app/assets/javascripts/admin/controllers/admin_users_list_controller.js +++ b/app/assets/javascripts/admin/controllers/admin_users_list_controller.js @@ -91,7 +91,7 @@ Discourse.AdminUsersListController = Ember.ArrayController.extend(Discourse.Pres var adminUsersListController = this; adminUsersListController.set('loading', true); - Discourse.AdminUser.findAll(this.get('query'), this.get('username')).then(function (result) { + Discourse.AdminUser.findAll(this.get('query'), { filter: this.get('username') }).then(function (result) { adminUsersListController.set('content', result); adminUsersListController.set('loading', false); }); diff --git a/app/assets/javascripts/admin/models/admin_user.js b/app/assets/javascripts/admin/models/admin_user.js index f84cc9a3b..981e1be36 100644 --- a/app/assets/javascripts/admin/models/admin_user.js +++ b/app/assets/javascripts/admin/models/admin_user.js @@ -431,7 +431,7 @@ Discourse.AdminUser.reopenClass({ findAll: function(query, filter) { return Discourse.ajax("/admin/users/list/" + query + ".json", { - data: { filter: filter } + data: filter }).then(function(users) { return users.map(function(u) { return Discourse.AdminUser.create(u); diff --git a/app/assets/javascripts/admin/templates/ip_locator.js.handlebars b/app/assets/javascripts/admin/templates/ip_locator.js.handlebars deleted file mode 100644 index 379e61fe7..000000000 --- a/app/assets/javascripts/admin/templates/ip_locator.js.handlebars +++ /dev/null @@ -1,54 +0,0 @@ -{{#if view.ip}} - -{{/if}} -{{#if view.showBox }} -
-

{{i18n ip_info.title}}

-
- {{#if view.location}} - {{#if view.location.hostname}} -
{{i18n ip_info.hostname}}
-
{{view.location.hostname}}
- {{/if}} - -
{{i18n ip_info.location}}
-
- {{#if view.location.loc}} - {{view.location.loc}}
- {{view.location.city}}, {{view.location.region}}, {{view.location.country}} - {{else}} - {{i18n ip_info.location_not_found}} - {{/if}} -
- - {{#if view.location.org}} -
{{i18n ip_info.organisation}}
-
{{view.location.org}}
- {{/if}} - - {{#if view.location.phone}} -
{{i18n ip_info.phone}}
-
{{view.location.phone}}
- {{/if}} - {{else}} -
{{i18n loading}}
- {{/if}} - -
{{i18n ip_info.other_accounts}}
-
- {{#if view.other_accounts_loading}} - {{i18n loading}} - {{else}} - {{#each view.other_accounts}} - {{#link-to 'adminUser' this}}{{avatar this usernamePath="user.username" imageSize="small"}}{{/link-to}} - {{else}} - {{i18n ip_info.no_other_accounts}} - {{/each}} - {{/if}} -
-
- -
-{{/if}} \ No newline at end of file diff --git a/app/assets/javascripts/admin/templates/user_index.js.handlebars b/app/assets/javascripts/admin/templates/user_index.js.handlebars index e3a8d41ef..b7f7cd69d 100644 --- a/app/assets/javascripts/admin/templates/user_index.js.handlebars +++ b/app/assets/javascripts/admin/templates/user_index.js.handlebars @@ -76,10 +76,10 @@
{{ip_address}}
{{#if currentUser.admin}} - - {{view Discourse.AdminIpLocatorView ipBinding="ip_address"}} + + {{ip-lookup ip=ip_address user_id=id}} {{/if}}
@@ -89,7 +89,7 @@
{{registration_ip_address}}
{{#if currentUser.admin}} - {{view Discourse.AdminIpLocatorView ipBinding="registration_ip_address"}} + {{ip-lookup ip=registration_ip_address user_id=id}} {{/if}}
diff --git a/app/assets/javascripts/discourse/templates/components/ip-lookup.js.handlebars b/app/assets/javascripts/discourse/templates/components/ip-lookup.js.handlebars new file mode 100644 index 000000000..30dec03a3 --- /dev/null +++ b/app/assets/javascripts/discourse/templates/components/ip-lookup.js.handlebars @@ -0,0 +1,54 @@ +{{#if ip}} + +{{/if}} +{{#if show}} +
+

{{i18n ip_lookup.title}}

+
+ {{#if location}} + {{#if location.hostname}} +
{{i18n ip_lookup.hostname}}
+
{{location.hostname}}
+ {{/if}} + +
{{i18n ip_lookup.location}}
+
+ {{#if location.loc}} + {{location.loc}}
+ {{city}} + {{else}} + {{i18n ip_lookup.location_not_found}} + {{/if}} +
+ + {{#if location.org}} +
{{i18n ip_lookup.organisation}}
+
{{location.org}}
+ {{/if}} + + {{#if location.phone}} +
{{i18n ip_lookup.phone}}
+
{{location.phone}}
+ {{/if}} + {{else}} +
{{i18n loading}}
+ {{/if}} + +
{{i18n ip_lookup.other_accounts}}
+
+ {{#if other_accounts_loading}} +
{{i18n loading}}
+ {{else}} + {{#each other_accounts}} + {{#link-to "adminUser" this}}{{avatar this usernamePath="user.username" imageSize="small"}}{{/link-to}} + {{else}} + {{i18n ip_lookup.no_other_accounts}} + {{/each}} + {{/if}} +
+
+ +
+{{/if}} diff --git a/app/assets/stylesheets/common/admin/admin_base.scss b/app/assets/stylesheets/common/admin/admin_base.scss index d92c4a275..0d719b299 100644 --- a/app/assets/stylesheets/common/admin/admin_base.scss +++ b/app/assets/stylesheets/common/admin/admin_base.scss @@ -86,7 +86,7 @@ td.flaggers td { }; } -.iplocator { +.ip-lookup { position: relative; display: inline-block; diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index f4ab08ed9..e5fd6d1ae 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -183,6 +183,15 @@ class Admin::UsersController < Admin::AdminController def leader_requirements end + def ip_info + params.require(:ip) + ip = params[:ip] + + # should we cache results in redis? + location = Excon.get("http://ipinfo.io/#{ip}/json", read_timeout: 30, connect_timeout: 30).body rescue nil + + render json: location + end private diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index f756cd576..14b8eaf4d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -240,7 +240,7 @@ en: one: "%{count} new post in the past %{unit}." other: "%{count} new posts in the past %{unit}." - ip_info: + ip_lookup: title: IP Address Lookup hostname: Hostname location: Location diff --git a/config/routes.rb b/config/routes.rb index 34d929fc0..8d968fb7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,6 +48,7 @@ Discourse::Application.routes.draw do resources :users, id: USERNAME_ROUTE_FORMAT do collection do get "list/:query" => "users#index" + get "ip-info" => "users#ip_info" put "approve-bulk" => "users#approve_bulk" delete "reject-bulk" => "users#reject_bulk" end diff --git a/lib/admin_user_index_query.rb b/lib/admin_user_index_query.rb index d59ab466c..2b07fb209 100644 --- a/lib/admin_user_index_query.rb +++ b/lib/admin_user_index_query.rb @@ -40,7 +40,6 @@ class AdminUserIndexQuery end end - def filter_by_ip if params[:ip].present? @query.where('ip_address = :ip or registration_ip_address = :ip', ip: params[:ip]) diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index b974dbc63..ba4dcc65f 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -371,6 +371,15 @@ describe Admin::UsersController do end end + context 'ip-info' do + + it "uses ipinfo.io webservice to retrieve the info" do + Excon.expects(:get).with("http://ipinfo.io/123.123.123.123/json", read_timeout: 30, connect_timeout: 30) + xhr :get, :ip_info, ip: "123.123.123.123" + end + + end + end end