Skip to content

Commit 775b09c

Browse files
committed
Add ip whitelist support for organizations
1 parent 02fc680 commit 775b09c

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

app/controllers/api/v8/organizations_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ class OrganizationsController < Api::V8::BaseController
4646
end
4747

4848
def index
49-
orgs = Organization.visible_organizations.map { |o| { name: o.name, information: o.information, slug: o.slug, logo_path: o.logo.url, pinned: o.pinned } }
49+
orgs = Organization
50+
.visible_organizations
51+
.select { |org| org.visibility_allowed?(request) }
52+
.map { |o| { name: o.name, information: o.information, slug: o.slug, logo_path: o.logo.url, pinned: o.pinned } }
5053
authorize! :read, orgs
5154
present(orgs)
5255
end

app/controllers/courses_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def assign_show_view_vars
205205

206206
def set_organization
207207
@organization = Organization.find_by!(slug: params[:organization_id])
208+
unauthorized! unless @organization.visibility_allowed?(request)
208209
end
209210

210211
def set_course

app/controllers/organizations_controller.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ def index
1010
@organizations = Organization
1111
.accepted_organizations
1212
.order(ordering)
13-
.reject { |org| org.hidden? && !can?(:view_hidden_organizations, nil)}
14-
@my_organizations = Organization.taught_organizations(current_user)
15-
@my_organizations |= Organization.assisted_organizations(current_user)
16-
@my_organizations |= Organization.participated_organizations(current_user)
13+
.reject { |org| org.hidden? && !can?(:view_hidden_organizations, nil) || !org.visibility_allowed?(request) }
14+
@my_organizations = Organization.taught_organizations(current_user).select { |org| org.visibility_allowed?(request) }
15+
@my_organizations |= Organization.assisted_organizations(current_user).select { |org| org.visibility_allowed?(request) }
16+
@my_organizations |= Organization.participated_organizations(current_user).select { |org| org.visibility_allowed?(request) }
1717
@my_organizations.natsort_by!(&:name)
1818
@courses_under_initial_refresh = Course.where(initial_refresh_ready: false)
1919
@pinned_organizations = Organization
2020
.accepted_organizations
2121
.where(pinned: true)
2222
.order(ordering)
23+
.select { |org| org.visibility_allowed?(request) }
2324
.reject { |org| org.hidden? && !can?(:view_hidden_organizations, nil)}
2425
render layout: 'landing'
2526
end
@@ -99,6 +100,7 @@ def percent_completed_hash(courses, user)
99100

100101
def set_organization
101102
@organization = Organization.find_by(slug: params[:id])
103+
unauthorized! unless @organization.visibility_allowed?(request)
102104
raise ActiveRecord::RecordNotFound, 'Invalid organization id' if @organization.nil?
103105
end
104106

app/models/organization.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def find_by_slug(slug)
7171
Organization.where(slug: slug)
7272
end
7373

74+
def visibility_allowed?(request)
75+
return true unless whitelisted_ips
76+
whitelisted_ips.include?(request.remote_ip)
77+
end
78+
7479
def valid_slug? # slug must not be an existing route (/org/new etc)
7580
errors.add(:slug, 'is a system reserved word') if %w(new list_requests).include? slug
7681
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddWhitelistedIpsToOrganization < ActiveRecord::Migration
2+
def change
3+
add_column :organizations, :whitelisted_ips, :string, array: true
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20180425091037) do
14+
ActiveRecord::Schema.define(version: 20180508095009) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -238,6 +238,7 @@
238238
t.string "email"
239239
t.text "website"
240240
t.boolean "pinned", default: false, null: false
241+
t.string "whitelisted_ips", array: true
241242
end
242243

243244
create_table "points_upload_queues", force: :cascade do |t|

0 commit comments

Comments
 (0)