From 08db1ad59aaa40cd325b86527454dc216094e15c Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Sat, 18 Apr 2015 23:55:34 +0300 Subject: [PATCH] #465: Fixed: undefined method 'uname' for nil:NilClass --- app/policies/project_policy.rb | 5 ++- app/views/projects/git/trees/_show.html.haml | 3 +- spec/policies/project_policy_spec.rb | 37 +++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/policies/project_policy.rb b/app/policies/project_policy.rb index 8ef0c6de7..15134685d 100644 --- a/app/policies/project_policy.rb +++ b/app/policies/project_policy.rb @@ -15,11 +15,14 @@ class ProjectPolicy < ApplicationPolicy local_reader? end alias_method :read?, :show? - alias_method :fork?, :show? alias_method :archive?, :show? alias_method :get_id?, :show? alias_method :refs_list?, :show? + def fork? + !user.guest? && show? + end + def create? return false if user.guest? return true if is_admin? diff --git a/app/views/projects/git/trees/_show.html.haml b/app/views/projects/git/trees/_show.html.haml index 77940e436..380a67938 100644 --- a/app/views/projects/git/trees/_show.html.haml +++ b/app/views/projects/git/trees/_show.html.haml @@ -3,7 +3,8 @@ 'ng-init' => "init('#{@project.name_with_owner}', '#{@treeish}', '#{@path}')" } .files .pull-left= render 'whereami' - .pull-right= render 'fork' + - if policy(@project).fork? + .pull-right= render 'fork' .clearfix %table.table.table-hover %thead diff --git a/spec/policies/project_policy_spec.rb b/spec/policies/project_policy_spec.rb index 77dcaf296..1cfdd5ac2 100644 --- a/spec/policies/project_policy_spec.rb +++ b/spec/policies/project_policy_spec.rb @@ -16,7 +16,7 @@ RSpec.describe ProjectPolicy, type: :policy do end end - %i(show? read? fork? archive? get_id? refs_list?).each do |perm| + %i(show? read? archive? get_id? refs_list?).each do |perm| permissions perm do it "grants access to anonymous user" do expect(subject).to permit(User.new, project) @@ -53,6 +53,41 @@ RSpec.describe ProjectPolicy, type: :policy do end end + permissions :fork? do + it "denies access to anonymous user" do + expect(subject).to_not permit(User.new, project) + end + + it "grants access to user" do + expect(subject).to permit(user, project) + end + + context 'hidden project' do + before do + project.visibility = 'hidden' + end + + it "grants access for owner of project" do + expect(subject).to permit(project.owner, project) + end + + it "grants access for member of project owner group" do + project = FactoryGirl.build(:group_project) + allow_any_instance_of(ProjectPolicy).to receive(:user_group_ids).and_return([project.owner_id]) + expect(subject).to permit(user, project) + end + + it "grants access for reader of project" do + allow_any_instance_of(ProjectPolicy).to receive(:local_reader?).and_return(true) + expect(subject).to permit(user, project) + end + + it "grants access for to global admin" do + expect(subject).to permit(FactoryGirl.create(:admin), project) + end + end + end + permissions :create? do it "denies access to anonymous user" do expect(subject).to_not permit(User.new, project)