Commit 91633281 authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Drop Project#authorized_for_user? in favor of ProjectTeam#member?

Closes #23938
parent 6efd68fead8c
......@@ -93,7 +93,7 @@ def self.visible_to_user(user)
# Check if we are scoped to a specific project's issues
if owner_project
if owner_project.authorized_for_user?(user, Gitlab::Access::REPORTER)
if owner_project.team.member?(user, Gitlab::Access::REPORTER)
# If the project is authorized for the user, they can see all issues in the project
return all
else
......
......@@ -1293,14 +1293,6 @@ def secret_variables
end
end
# Checks if `user` is authorized for this project, with at least the
# `min_access_level` (if given).
def authorized_for_user?(user, min_access_level = nil)
return false unless user
user.authorized_project?(self, min_access_level)
end
def append_or_update_attribute(name, value)
old_values = public_send(name.to_s)
......
......@@ -141,8 +141,12 @@ def master?(user)
max_member_access(user.id) == Gitlab::Access::MASTER
end
def member?(user, min_member_access = Gitlab::Access::GUEST)
max_member_access(user.id) >= min_member_access
# Checks if `user` is authorized for this project, with at least the
# `min_access_level` (if given).
def member?(user, min_access_level = Gitlab::Access::GUEST)
return false unless user
user.authorized_project?(project, min_access_level)
end
def human_max_access(user_id)
......
......@@ -10,7 +10,7 @@
end
describe '#remove_member_message' do
let(:requester) { build(:user) }
let(:requester) { create(:user) }
let(:project) { create(:empty_project, :public, :access_requestable) }
let(:project_member) { build(:project_member, project: project) }
let(:project_member_invite) { build(:project_member, project: project).tap { |m| m.generate_invite_token! } }
......@@ -31,7 +31,7 @@
end
describe '#remove_member_title' do
let(:requester) { build(:user) }
let(:requester) { create(:user) }
let(:project) { create(:empty_project, :public, :access_requestable) }
let(:project_member) { build(:project_member, project: project) }
let(:project_member_request) { project.request_access(requester) }
......
......@@ -331,7 +331,7 @@
end
context 'with a user' do
let(:user) { build(:user) }
let(:user) { create(:user) }
let(:issue) { build(:issue) }
it 'returns true when the issue is readable' do
......
......@@ -1507,57 +1507,6 @@ def create_build(new_pipeline = pipeline, name = 'test')
end
end
describe 'authorized_for_user' do
let(:group) { create(:group) }
let(:developer) { create(:user) }
let(:master) { create(:user) }
let(:personal_project) { create(:project, namespace: developer.namespace) }
let(:group_project) { create(:project, namespace: group) }
let(:members_project) { create(:project) }
let(:shared_project) { create(:project) }
before do
group.add_master(master)
group.add_developer(developer)
members_project.team << [developer, :developer]
members_project.team << [master, :master]
create(:project_group_link, project: shared_project, group: group, group_access: Gitlab::Access::DEVELOPER)
end
it 'returns false for no user' do
expect(personal_project.authorized_for_user?(nil)).to be(false)
end
it 'returns true for personal projects of the user' do
expect(personal_project.authorized_for_user?(developer)).to be(true)
end
it 'returns true for projects of groups the user is a member of' do
expect(group_project.authorized_for_user?(developer)).to be(true)
end
it 'returns true for projects for which the user is a member of' do
expect(members_project.authorized_for_user?(developer)).to be(true)
end
it 'returns true for projects shared on a group the user is a member of' do
expect(shared_project.authorized_for_user?(developer)).to be(true)
end
it 'checks for the correct minimum level access' do
expect(group_project.authorized_for_user?(developer, Gitlab::Access::MASTER)).to be(false)
expect(group_project.authorized_for_user?(master, Gitlab::Access::MASTER)).to be(true)
expect(members_project.authorized_for_user?(developer, Gitlab::Access::MASTER)).to be(false)
expect(members_project.authorized_for_user?(master, Gitlab::Access::MASTER)).to be(true)
expect(shared_project.authorized_for_user?(developer, Gitlab::Access::MASTER)).to be(false)
expect(shared_project.authorized_for_user?(master, Gitlab::Access::MASTER)).to be(false)
expect(shared_project.authorized_for_user?(developer, Gitlab::Access::DEVELOPER)).to be(true)
expect(shared_project.authorized_for_user?(master, Gitlab::Access::DEVELOPER)).to be(true)
end
end
describe 'change_head' do
let(:project) { create(:project) }
......
......@@ -261,6 +261,57 @@
end
end
describe '#member?' do
let(:group) { create(:group) }
let(:developer) { create(:user) }
let(:master) { create(:user) }
let(:personal_project) { create(:project, namespace: developer.namespace) }
let(:group_project) { create(:project, namespace: group) }
let(:members_project) { create(:project) }
let(:shared_project) { create(:project) }
before do
group.add_master(master)
group.add_developer(developer)
members_project.team << [developer, :developer]
members_project.team << [master, :master]
create(:project_group_link, project: shared_project, group: group)
end
it 'returns false for no user' do
expect(personal_project.team.member?(nil)).to be(false)
end
it 'returns true for personal projects of the user' do
expect(personal_project.team.member?(developer)).to be(true)
end
it 'returns true for projects of groups the user is a member of' do
expect(group_project.team.member?(developer)).to be(true)
end
it 'returns true for projects for which the user is a member of' do
expect(members_project.team.member?(developer)).to be(true)
end
it 'returns true for projects shared on a group the user is a member of' do
expect(shared_project.team.member?(developer)).to be(true)
end
it 'checks for the correct minimum level access' do
expect(group_project.team.member?(developer, Gitlab::Access::MASTER)).to be(false)
expect(group_project.team.member?(master, Gitlab::Access::MASTER)).to be(true)
expect(members_project.team.member?(developer, Gitlab::Access::MASTER)).to be(false)
expect(members_project.team.member?(master, Gitlab::Access::MASTER)).to be(true)
expect(shared_project.team.member?(developer, Gitlab::Access::MASTER)).to be(false)
expect(shared_project.team.member?(master, Gitlab::Access::MASTER)).to be(false)
expect(shared_project.team.member?(developer, Gitlab::Access::DEVELOPER)).to be(true)
expect(shared_project.team.member?(master, Gitlab::Access::DEVELOPER)).to be(true)
end
end
shared_examples_for "#max_member_access_for_users" do |enable_request_store|
describe "#max_member_access_for_users" do
before do
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment