From a9d2881d81cbe2e10a075a24e4176cb59297708f Mon Sep 17 00:00:00 2001 From: Junv Date: Wed, 14 Nov 2012 11:51:10 +0800 Subject: [PATCH] add all accessable team to show --- app/models/person.rb | 16 ++++++++++++++-- app/models/team.rb | 7 +++++++ app/views/teams/index.html.slim | 12 ++++++++++++ lib/omnipotence.rb | 5 ----- lib/team_filter.rb | 2 +- spec/models/person_spec.rb | 22 ++++++++++++++++++++++ 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index 10dd8a3..f42da11 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -52,12 +52,24 @@ class Person < ActiveRecord::Base def allowed_to_view? person return false unless person - blessed? or person == self or !(teams & person.approved_teams).empty? + omnipotent? or person == self or !(teams & person.approved_teams).empty? end def allowed_to_view_team? team return false unless team - blessed? or team.creator == self or teams.include?(team) + team.public? or blessed?(team) or team.creator == self or teams.include?(team) end + def viewable_teams + Team.find(:all).select{|team| allowed_to_view_team?(team)} + end + + def blessed?(team) + return true if omnipotent? + team.blessed?(email_domain) + end + + def email_domain + email.split('@').last + end end \ No newline at end of file diff --git a/app/models/team.rb b/app/models/team.rb index f3b9f07..37741cd 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -17,4 +17,11 @@ class Team < ActiveRecord::Base att[:members] = memberships.includes(:person).approved.map(&:person).map(&:api_attributes) if params[:include] and params[:include].include? :members att end + + def blessed?(domain) + organisations.map{|org| org.domains.split(',')}.flatten.include?(domain) + end + def public? + organisations.empty? + end end \ No newline at end of file diff --git a/app/views/teams/index.html.slim b/app/views/teams/index.html.slim index 3fbe2e2..03488f6 100644 --- a/app/views/teams/index.html.slim +++ b/app/views/teams/index.html.slim @@ -44,3 +44,15 @@ div id="add_new" style="display:none;" = form_tag "/membership/leave" do input type="hidden" name="id" value="#{membership.id}" = submit_tag 'Leave', class: 'btn btn-danger btn-block' +div.clearfix +- current_person.viewable_teams.each do |team| + a href="/teams/#{team.slug}" + span.badge.badge-info Viewable + div.hero-unit.highlight + div.row + div.span4 + img src="/assets/team.png" + div.span4 + h1 = team.name + p = team.description + diff --git a/lib/omnipotence.rb b/lib/omnipotence.rb index 248f05a..7d8addf 100644 --- a/lib/omnipotence.rb +++ b/lib/omnipotence.rb @@ -6,9 +6,4 @@ module Omnipotence def omnipotent? Omnipotence.omnipotent? email end - - def blessed? - return true if omnipotent? - (ENV['BLESSED_DOMAINS'] || '').split(',').include? email.split('@').last - end end \ No newline at end of file diff --git a/lib/team_filter.rb b/lib/team_filter.rb index 2a75e63..0e48d69 100644 --- a/lib/team_filter.rb +++ b/lib/team_filter.rb @@ -1,7 +1,7 @@ module TeamFilter def with_team @team = Team.find_by_slug params[:slug] - unless current_person.blessed? + unless current_person.blessed?(@team) @team = nil unless current_person.teams.include?(@team) or @team.creator == current_person end if @team diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index 9728827..cf485db 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -44,4 +44,26 @@ describe Person do created_person.account.should =~ /\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/ end end + + describe :viewable_teams do + let(:email) { 'fake_user@thoughtworks.com' } + let(:person) { Person.create_for_email(email) } + let(:blessed_organisation) { Organisation.create(name: 'ThoughtWorks', domains: 'thoughtworks.com') } + let(:viewable_team) { blessed_organisation.teams.create(name: 'TW Project', slug: 'tw_project') } + let(:non_blessed_organisation) { Organisation.create(name: 'Microsoft', domains: 'microsoft.com') } + let(:non_viewable_team){ non_blessed_organisation.teams.create(name: 'MS Project', slug: 'ms_project') } + let(:public_team){Team.create(name: 'Public Test Team', slug: 'public.com')} + + before(:each) do + viewable_team.reload + public_team.reload + non_viewable_team.reload + end + + it 'lists viewable teams but not non-viewable teams' do + person.viewable_teams.should be_include(viewable_team) + person.viewable_teams.should_not be_include(non_viewable_team) + person.viewable_teams.should be_include(public_team) + end + end end \ No newline at end of file