Merge pull request #1135 from consul/budget-permissions

Budget permissions
This commit is contained in:
Juanjo Bazán
2016-05-30 14:36:47 +02:00
7 changed files with 65 additions and 3 deletions

View File

@@ -4,7 +4,6 @@ module Abilities
def initialize(user)
self.merge Abilities::Moderation.new(user)
self.merge Abilities::Valuator.new(user)
can :restore, Comment
cannot :restore, Comment, hidden_at: nil
@@ -41,7 +40,12 @@ module Abilities
can :manage, Annotation
can [:read, :update, :destroy, :summary], SpendingProposal
can [:read, :update, :valuate, :destroy, :summary], SpendingProposal
can [:create, :update], Budget
can [:hide, :update], Budget::Investment
can :valuate, Budget::Investment, budget: { valuating: true }
can :create, Budget::ValuatorAssignment
end
end
end

View File

@@ -19,6 +19,7 @@ module Abilities
can [:retire_form, :retire], Proposal, author_id: user.id
can :read, SpendingProposal
can :read, Budget::Investment
can :create, Comment
can :create, Debate
@@ -46,6 +47,9 @@ module Abilities
can :vote_featured, Proposal
can :vote, SpendingProposal
can :create, SpendingProposal
can :create, Budget::Investment, budget: { phase: "accepting" }
can :vote, Budget::Investment, budget: { phase: "selecting" }
can :create, Budget::Ballot, budget: { phase: "balloting" }
end
can :create, Annotation

View File

@@ -3,7 +3,9 @@ module Abilities
include CanCan::Ability
def initialize(user)
valuator = user.valuator
can [:read, :update, :valuate], SpendingProposal
can [:update, :valuate], Budget::Investment, id: valuator.investment_ids, budget: { valuating: true }
end
end
end
end

View File

@@ -4,6 +4,8 @@ class Valuator < ActiveRecord::Base
has_many :valuation_assignments, dependent: :destroy
has_many :spending_proposals, through: :valuation_assignments
has_many :valuator_assignments, dependent: :destroy, class_name: 'Budget::ValuatorAssignment'
has_many :investments, through: :valuator_assignments, class_name: 'Budget::Investment'
validates :user_id, presence: true, uniqueness: true

View File

@@ -56,4 +56,16 @@ describe "Abilities::Administrator" do
it { should be_able_to(:update, SpendingProposal) }
it { should be_able_to(:valuate, SpendingProposal) }
it { should be_able_to(:destroy, SpendingProposal) }
it { should be_able_to(:create, Budget) }
it { should be_able_to(:update, Budget) }
it { should be_able_to(:create, Budget::ValuatorAssignment) }
it { should be_able_to(:update, Budget::Investment) }
it { should be_able_to(:hide, Budget::Investment) }
it { should be_able_to(:valuate, create(:budget_investment, budget: create(:budget, valuating: true))) }
it { should_not be_able_to(:valuate, create(:budget_investment, budget: create(:budget, valuating: false))) }
end

View File

@@ -9,6 +9,15 @@ describe "Abilities::Common" do
let(:debate) { create(:debate) }
let(:comment) { create(:comment) }
let(:proposal) { create(:proposal) }
let(:accepting_budget) { create(:budget, phase: 'accepting') }
let(:selecting_budget) { create(:budget, phase: 'selecting') }
let(:balloting_budget) { create(:budget, phase: 'balloting') }
let(:investment_in_accepting_budget) { create(:budget_investment, budget: accepting_budget) }
let(:investment_in_selecting_budget) { create(:budget_investment, budget: selecting_budget) }
let(:investment_in_balloting_budget) { create(:budget_investment, budget: balloting_budget) }
let(:ballot_in_accepting_budget) { create(:budget_ballot, budget: accepting_budget) }
let(:ballot_in_selecting_budget) { create(:budget_ballot, budget: selecting_budget) }
let(:ballot_in_balloting_budget) { create(:budget_ballot, budget: balloting_budget) }
let(:own_debate) { create(:debate, author: user) }
let(:own_comment) { create(:comment, author: user) }
let(:own_proposal) { create(:proposal, author: user) }
@@ -93,6 +102,19 @@ describe "Abilities::Common" do
it { should be_able_to(:create, SpendingProposal) }
it { should_not be_able_to(:destroy, create(:spending_proposal)) }
it { should_not be_able_to(:destroy, own_spending_proposal) }
it { should be_able_to(:create, investment_in_accepting_budget) }
it { should_not be_able_to(:create, investment_in_selecting_budget) }
it { should_not be_able_to(:create, investment_in_balloting_budget) }
it { should_not be_able_to(:vote, investment_in_accepting_budget) }
it { should be_able_to(:vote, investment_in_selecting_budget) }
it { should_not be_able_to(:vote, investment_in_balloting_budget) }
it { should_not be_able_to(:create, ballot_in_accepting_budget) }
it { should_not be_able_to(:create, ballot_in_selecting_budget) }
it { should be_able_to(:create, ballot_in_balloting_budget) }
end
describe "when level 3 verified" do

View File

@@ -5,8 +5,24 @@ describe "Abilities::Valuator" do
subject(:ability) { Ability.new(user) }
let(:user) { valuator.user }
let(:valuator) { create(:valuator) }
let(:non_assigned_investment) { create(:budget_investment) }
let(:assigned_investment) { create(:budget_investment, budget: create(:budget, valuating: true)) }
before(:each) { assigned_investment.valuators << valuator }
let(:assigned_investment_not_valuating) { create(:budget_investment, budget: create(:budget, valuating: false)) }
before(:each) { assigned_investment_not_valuating.valuators << valuator }
it { should be_able_to(:read, SpendingProposal) }
it { should be_able_to(:update, SpendingProposal) }
it { should be_able_to(:valuate, SpendingProposal) }
it { should_not be_able_to(:update, non_assigned_investment) }
it { should_not be_able_to(:valuate, non_assigned_investment) }
it { should be_able_to(:update, assigned_investment) }
it { should be_able_to(:valuate, assigned_investment) }
it { should_not be_able_to(:update, assigned_investment_not_valuating) }
it { should_not be_able_to(:valuate, assigned_investment_not_valuating) }
end