Commit 4dba87a9b0eaa585c4c1158f2e364ec82b6bf4f3

Authored by Diego Camarinha
Committed by Rafael Manzo
1 parent c1f0c0f1

Reading group ownerships.

Signed-off-by: Fellipe Souto <fllsouto@gmail.com>
Signed-off-by: Renan Fichberg <rfichberg@gmail.com>
app/controllers/concerns/ownership_authentication.rb
1 module OwnershipAuthentication 1 module OwnershipAuthentication
2 extend ActiveSupport::Concern 2 extend ActiveSupport::Concern
3 3
4 - def check_project_ownership  
5 - check_ownership(params[:id]) 4 + def project_owner?
  5 + check_project_ownership(params[:id])
6 end 6 end
7 7
8 - def check_repository_ownership  
9 - check_ownership(params[:project_id]) 8 + def repository_owner?
  9 + check_project_ownership(params[:project_id])
  10 + end
  11 +
  12 + def reading_group_owner?
  13 + check_reading_group_ownership(params[:id])
10 end 14 end
11 15
12 - def check_ownership(id) 16 + def check_project_ownership(id)
13 if current_user.project_ownerships.find_by_project_id(id).nil? 17 if current_user.project_ownerships.find_by_project_id(id).nil?
14 respond_to do |format| 18 respond_to do |format|
15 format.html { redirect_to projects_url, notice: "You're not allowed to do this operation" } 19 format.html { redirect_to projects_url, notice: "You're not allowed to do this operation" }
@@ -17,4 +21,13 @@ module OwnershipAuthentication @@ -17,4 +21,13 @@ module OwnershipAuthentication
17 end 21 end
18 end 22 end
19 end 23 end
  24 +
  25 + def check_reading_group_ownership(id)
  26 + if current_user.reading_group_ownerships.find_by_reading_group_id(id).nil?
  27 + respond_to do |format|
  28 + format.html { redirect_to reading_group_url, notice: "You're not allowed to do this operation" }
  29 + format.json { head :no_content }
  30 + end
  31 + end
  32 + end
20 end 33 end
app/controllers/projects_controller.rb
@@ -3,7 +3,7 @@ include OwnershipAuthentication @@ -3,7 +3,7 @@ include OwnershipAuthentication
3 class ProjectsController < ApplicationController 3 class ProjectsController < ApplicationController
4 before_action :authenticate_user!, 4 before_action :authenticate_user!,
5 except: [:index, :show] 5 except: [:index, :show]
6 - before_action :check_project_ownership, only: [:edit, :update, :destroy] 6 + before_action :project_owner?, only: [:edit, :update, :destroy]
7 7
8 # GET /projects/new 8 # GET /projects/new
9 def new 9 def new
app/controllers/repositories_controller.rb
@@ -3,7 +3,7 @@ include OwnershipAuthentication @@ -3,7 +3,7 @@ include OwnershipAuthentication
3 class RepositoriesController < ApplicationController 3 class RepositoriesController < ApplicationController
4 before_action :authenticate_user!, except: [:show, :state] 4 before_action :authenticate_user!, except: [:show, :state]
5 before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :process_repository] 5 before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :process_repository]
6 - before_action :check_repository_ownership, except: [:show, :state] 6 + before_action :repository_owner?, except: [:show, :state]
7 7
8 # GET /projects/1/repositories/1 8 # GET /projects/1/repositories/1
9 # GET /projects/1/repositories/1.json 9 # GET /projects/1/repositories/1.json
app/models/reading_group_ownership.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +class ReadingGroupOwnership < ActiveRecord::Base
  2 + belongs_to :user
  3 + validates :reading_group_id, presence: true
  4 +
  5 + def reading_group
  6 + ReadingGroup.find(reading_group_id)
  7 + end
  8 +end
app/models/user.rb
@@ -9,10 +9,15 @@ class User &lt; ActiveRecord::Base @@ -9,10 +9,15 @@ class User &lt; ActiveRecord::Base
9 validates :email, uniqueness: true 9 validates :email, uniqueness: true
10 10
11 has_many :project_ownerships 11 has_many :project_ownerships
  12 +
  13 + has_many :reading_group_ownerships
12 # Alert: when adding new parameters to this model, they should also be added to registrations_controller 14 # Alert: when adding new parameters to this model, they should also be added to registrations_controller
13 15
14 def projects 16 def projects
15 - #raise project_ownerships.inspect  
16 project_ownerships.map { |project_ownership| project_ownership.project } 17 project_ownerships.map { |project_ownership| project_ownership.project }
17 end 18 end
  19 +
  20 + def reading_groups
  21 + reading_group_ownerships.map { |reading_group_ownership| reading_group_ownership.reading_group }
  22 + end
18 end 23 end
db/migrate/20131219115819_create_reading_group_ownerships.rb 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +class CreateReadingGroupOwnerships < ActiveRecord::Migration
  2 + def change
  3 + create_table :reading_group_ownerships do |t|
  4 + t.integer :user_id
  5 + t.integer :reading_group_id
  6 +
  7 + t.timestamps
  8 + end
  9 + end
  10 +end
spec/controllers/repositories_controller_spec.rb
@@ -11,7 +11,7 @@ describe RepositoriesController do @@ -11,7 +11,7 @@ describe RepositoriesController do
11 context 'when the current user owns the project' do 11 context 'when the current user owns the project' do
12 before :each do 12 before :each do
13 Repository.expects(:repository_types).returns([]) 13 Repository.expects(:repository_types).returns([])
14 - subject.expects(:check_repository_ownership).returns true 14 + subject.expects(:repository_owner?).returns true
15 15
16 get :new, project_id: project.id.to_s 16 get :new, project_id: project.id.to_s
17 end 17 end
@@ -40,7 +40,7 @@ describe RepositoriesController do @@ -40,7 +40,7 @@ describe RepositoriesController do
40 40
41 context 'when the current user owns the project' do 41 context 'when the current user owns the project' do
42 before :each do 42 before :each do
43 - subject.expects(:check_repository_ownership).returns true 43 + subject.expects(:repository_owner?).returns true
44 end 44 end
45 45
46 context 'with valid fields' do 46 context 'with valid fields' do
@@ -117,7 +117,7 @@ describe RepositoriesController do @@ -117,7 +117,7 @@ describe RepositoriesController do
117 117
118 context 'when the user owns the project' do 118 context 'when the user owns the project' do
119 before :each do 119 before :each do
120 - subject.expects(:check_repository_ownership).returns true 120 + subject.expects(:repository_owner?).returns true
121 repository.expects(:destroy) 121 repository.expects(:destroy)
122 Repository.expects(:find).at_least_once.with(repository.id).returns(repository) 122 Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
123 123
@@ -159,7 +159,7 @@ describe RepositoriesController do @@ -159,7 +159,7 @@ describe RepositoriesController do
159 159
160 context 'when the user owns the repository' do 160 context 'when the user owns the repository' do
161 before :each do 161 before :each do
162 - subject.expects(:check_repository_ownership).returns true 162 + subject.expects(:repository_owner?).returns true
163 Repository.expects(:find).at_least_once.with(repository.id).returns(repository) 163 Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
164 Repository.expects(:repository_types).returns(["SUBVERSION"]) 164 Repository.expects(:repository_types).returns(["SUBVERSION"])
165 get :edit, id: repository.id, project_id: project.id.to_s 165 get :edit, id: repository.id, project_id: project.id.to_s
@@ -201,7 +201,7 @@ describe RepositoriesController do @@ -201,7 +201,7 @@ describe RepositoriesController do
201 201
202 context 'when user owns the repository' do 202 context 'when user owns the repository' do
203 before :each do 203 before :each do
204 - subject.expects(:check_repository_ownership).returns true 204 + subject.expects(:repository_owner?).returns true
205 end 205 end
206 206
207 context 'with valid fields' do 207 context 'with valid fields' do
@@ -327,7 +327,7 @@ describe RepositoriesController do @@ -327,7 +327,7 @@ describe RepositoriesController do
327 let(:repository) { FactoryGirl.build(:repository) } 327 let(:repository) { FactoryGirl.build(:repository) }
328 before :each do 328 before :each do
329 sign_in FactoryGirl.create(:user) 329 sign_in FactoryGirl.create(:user)
330 - subject.expects(:check_repository_ownership).returns true 330 + subject.expects(:repository_owner?).returns true
331 repository.expects(:process) 331 repository.expects(:process)
332 Repository.expects(:find).at_least_once.with(repository.id).returns(repository) 332 Repository.expects(:find).at_least_once.with(repository.id).returns(repository)
333 KalibroGem::Entities::Configuration.expects(:find).with(repository.id).returns(FactoryGirl.build(:configuration)) 333 KalibroGem::Entities::Configuration.expects(:find).with(repository.id).returns(FactoryGirl.build(:configuration))
spec/factories/reading_group_ownerships.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +# Read about factories at https://github.com/thoughtbot/factory_girl
  2 +
  3 +FactoryGirl.define do
  4 + factory :reading_group_ownership do
  5 + user_id 1
  6 + reading_group_id 1
  7 + end
  8 +end
spec/models/reading_group_ownership_spec.rb 0 → 100644
@@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
  1 +require 'spec_helper'
  2 +
  3 +describe ReadingGroupOwnership do
  4 + describe 'associations' do
  5 + it { should belong_to(:user) }
  6 + end
  7 +
  8 + describe 'methods' do
  9 + describe 'reading_group' do
  10 + subject {FactoryGirl.build(:reading_group_ownership)}
  11 + let(:reading_group) {FactoryGirl.build(:reading_group)}
  12 +
  13 + before :each do
  14 + ReadingGroup.expects(:find).with(subject.reading_group_id).returns(reading_group)
  15 + end
  16 +
  17 + it 'should return the reading_group' do
  18 + subject.reading_group.should eq(reading_group)
  19 + end
  20 + end
  21 + end
  22 +end
spec/models/user_spec.rb
@@ -11,6 +11,7 @@ describe User do @@ -11,6 +11,7 @@ describe User do
11 11
12 describe 'associations' do 12 describe 'associations' do
13 it { should have_many(:project_ownerships) } 13 it { should have_many(:project_ownerships) }
  14 + it { should have_many(:reading_group_ownerships) }
14 end 15 end
15 16
16 describe 'methods' do 17 describe 'methods' do
@@ -27,6 +28,21 @@ describe User do @@ -27,6 +28,21 @@ describe User do
27 it 'should return a list of projects owned by the user' do 28 it 'should return a list of projects owned by the user' do
28 subject.projects.should eq([project]) 29 subject.projects.should eq([project])
29 end 30 end
30 - end 31 + end
  32 +
  33 + describe 'reading_groups' do
  34 + subject { FactoryGirl.build(:user) }
  35 + let(:reading_group) {FactoryGirl.build(:reading_group)}
  36 + let(:reading_group_ownership) {FactoryGirl.build(:reading_group_ownership)}
  37 +
  38 + before :each do
  39 + reading_group_ownership.expects(:reading_group).returns(reading_group)
  40 + subject.expects(:reading_group_ownerships).returns([reading_group_ownership])
  41 + end
  42 +
  43 + it 'should return a list of reading groups owned by the user' do
  44 + subject.reading_groups.should eq([reading_group])
  45 + end
  46 + end
31 end 47 end
32 end 48 end