Skip to content

Commit

Permalink
BUGFIX: IP lookup wasn't working when using HTTPS
Browse files Browse the repository at this point in the history
REFACTOR: the ip locator into a ip-lookup component
  • Loading branch information
ZogStriP committed Jul 7, 2014
1 parent 18fafa1 commit 59b5ba7
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 64 deletions.
43 changes: 43 additions & 0 deletions 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);
}
}
});
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/admin/models/admin_user.js
Expand Up @@ -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);
Expand Down
54 changes: 0 additions & 54 deletions app/assets/javascripts/admin/templates/ip_locator.js.handlebars

This file was deleted.

10 changes: 5 additions & 5 deletions app/assets/javascripts/admin/templates/user_index.js.handlebars
Expand Up @@ -76,10 +76,10 @@
<div class='value'>{{ip_address}}</div>
<div class='controls'>
{{#if currentUser.admin}}
<button class='btn' {{action refreshBrowsers target="content"}}>
{{i18n admin.user.refresh_browsers}}
</button>
{{view Discourse.AdminIpLocatorView ipBinding="ip_address"}}
<button class='btn' {{action refreshBrowsers target="content"}}>
{{i18n admin.user.refresh_browsers}}
</button>
{{ip-lookup ip=ip_address user_id=id}}
{{/if}}
</div>
</div>
Expand All @@ -89,7 +89,7 @@
<div class='value'>{{registration_ip_address}}</div>
<div class='controls'>
{{#if currentUser.admin}}
{{view Discourse.AdminIpLocatorView ipBinding="registration_ip_address"}}
{{ip-lookup ip=registration_ip_address user_id=id}}
{{/if}}
</div>
</div>
Expand Down
@@ -0,0 +1,54 @@
{{#if ip}}
<button class="btn" {{action lookup}}>
<span class="fa fa-globe"></span>&nbsp;{{i18n admin.user.ip_lookup}}
</button>
{{/if}}
{{#if show}}
<div class="location-box">
<h4>{{i18n ip_lookup.title}}</h4>
<dl>
{{#if location}}
{{#if location.hostname}}
<dt>{{i18n ip_lookup.hostname}}</dt>
<dd>{{location.hostname}}</dd>
{{/if}}

<dt>{{i18n ip_lookup.location}}</dt>
<dd>
{{#if location.loc}}
<a href="https://maps.google.com/maps?q={{unbound location.loc}}" target="_blank">{{location.loc}}</a><br>
{{city}}
{{else}}
{{i18n ip_lookup.location_not_found}}
{{/if}}
</dd>

{{#if location.org}}
<dt>{{i18n ip_lookup.organisation}}</dt>
<dd>{{location.org}}</dd>
{{/if}}

{{#if location.phone}}
<dt>{{i18n ip_lookup.phone}}</dt>
<dd>{{location.phone}}</dd>
{{/if}}
{{else}}
<div class="spinner">{{i18n loading}}</div>
{{/if}}

<dt>{{i18n ip_lookup.other_accounts}}</dt>
<dd>
{{#if other_accounts_loading}}
<div class="spinner">{{i18n loading}}</div>
{{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}}
<dd>
</dl>
<button class="btn close" {{action hide}}>{{i18n close}}</button>
</div>
{{/if}}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/common/admin/admin_base.scss
Expand Up @@ -86,7 +86,7 @@ td.flaggers td {
};
}

.iplocator {
.ip-lookup {
position: relative;
display: inline-block;

Expand Down
9 changes: 9 additions & 0 deletions app/controllers/admin/users_controller.rb
Expand Up @@ -183,6 +183,15 @@ def badges
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

Expand Down
2 changes: 1 addition & 1 deletion config/locales/client.en.yml
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -48,6 +48,7 @@
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
Expand Down
1 change: 0 additions & 1 deletion lib/admin_user_index_query.rb
Expand Up @@ -40,7 +40,6 @@ def filter_by_search
end
end


def filter_by_ip
if params[:ip].present?
@query.where('ip_address = :ip or registration_ip_address = :ip', ip: params[:ip])
Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/admin/users_controller_spec.rb
Expand Up @@ -371,6 +371,15 @@
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

0 comments on commit 59b5ba7

Please sign in to comment.