Commit c0bee9a7 authored by Imre Farkas's avatar Imre Farkas

Merge branch 'refactor/generic-member-function-project-group' into 'master'

Rework group/project member function into common generic one

See merge request gitlab-org/gitlab!77820
parents 3792292c 3462432c
......@@ -627,14 +627,13 @@ class Group < Namespace
end
end
def group_member(user)
def member(user)
if group_members.loaded?
group_members.find { |gm| gm.user_id == user.id }
else
group_members.find_by(user_id: user)
end
end
alias_method :resource_member, :group_member
def highest_group_member(user)
GroupMember.where(source_id: self_and_ancestors_ids, user_id: user.id).order(:access_level).last
......
......@@ -1661,14 +1661,13 @@ class Project < ApplicationRecord
attrs
end
def project_member(user)
def member(user)
if project_members.loaded?
project_members.find { |member| member.user_id == user.id }
else
project_members.find_by(user_id: user)
end
end
alias_method :resource_member, :project_member
def membership_locked?
false
......
......@@ -1336,7 +1336,7 @@ class User < ApplicationRecord
def can_leave_project?(project)
project.namespace != namespace &&
project.project_member(self)
project.member(self)
end
def full_website_url
......
......@@ -43,13 +43,9 @@ module ResourceAccessTokens
def find_member
strong_memoize(:member) do
if resource.is_a?(Project)
resource.project_member(bot_user)
elsif resource.is_a?(Group)
resource.group_member(bot_user)
else
false
end
next false unless resource.is_a?(Project) || resource.is_a?(Group)
resource.member(bot_user)
end
end
......
......@@ -55,7 +55,7 @@
- else
%span.token-never-expires-label= _('Never')
- if project
%td= project.project_member(token.user).human_access
%td= project.member(token.user).human_access
%td= link_to _('Revoke'), revoke_route_helper.call(token), method: :put, class: "gl-button btn btn-danger btn-sm float-right qa-revoke-button #{'btn-danger-secondary' unless token.expires?}", data: { confirm: _('Are you sure you want to revoke this %{type}? This action cannot be undone.') % { type: type } }
- else
.settings-message.text-center
......
......@@ -13,7 +13,7 @@ module EE
def success_message(token)
if resource_type == 'project'
"Created project access token with token_id: #{token.id} with scopes: #{token.scopes} and #{resource.project_member(token.user).human_access} access level."
"Created project access token with token_id: #{token.id} with scopes: #{token.scopes} and #{resource.member(token.user).human_access} access level."
else
"Created #{resource_type} token with token_id: #{token.id} with scopes: #{token.scopes}."
end
......
......@@ -36,7 +36,7 @@ module GroupSaml
end
def group_membership
@group_membership ||= group.group_member(user)
@group_membership ||= group.member(user)
end
end
end
......
......@@ -128,7 +128,7 @@ module EE
def member
strong_memoize(:member) do
next @group.group_member(user) if existing_member?(user)
next @group.member(user) if existing_member?(user)
@group.add_user(user, default_membership_role) if user.valid?
end
......
......@@ -123,7 +123,7 @@ RSpec.describe Boards::IssuesController do
context 'with unauthorized user' do
before do
group.group_member(user).destroy!
group.member(user).destroy!
end
it 'returns a forbidden 403 response' do
......
......@@ -33,7 +33,7 @@ RSpec.describe Boards::ListsController do
context 'with unauthorized user' do
before do
group.group_member(user).destroy!
group.member(user).destroy!
end
it 'returns a forbidden 403 response' do
......
......@@ -81,7 +81,7 @@ RSpec.describe ::EE::Gitlab::Scim::ProvisioningService do
it 'sets the access level of the member as specified in saml_provider' do
service.execute
access_level = group.group_member(user).access_level
access_level = group.member(user).access_level
expect(access_level).to eq(Gitlab::Access::DEVELOPER)
end
......
......@@ -28,7 +28,7 @@ RSpec.describe ::EE::Gitlab::Scim::ReprovisionService do
it 'creates the member with the access level as specified in saml_provider' do
service.execute
access_level = group.group_member(user).access_level
access_level = group.member(user).access_level
expect(access_level).to eq(Gitlab::Access::DEVELOPER)
end
......
......@@ -64,6 +64,6 @@ RSpec.describe IncidentManagement::OncallParticipant do
private
def remove_user_from_project(user, project)
Members::DestroyService.new(user).execute(project.project_member(user))
Members::DestroyService.new(user).execute(project.member(user))
end
end
......@@ -78,7 +78,7 @@ RSpec.describe Issues::CloneService do
context 'user can not update the epic' do
before do
group.group_member(user).destroy!
group.member(user).destroy!
old_project.add_reporter(user)
new_project.add_reporter(user)
end
......
......@@ -8,7 +8,7 @@ module API
if options[:project_members]
options[:project_members].find { |member| member.source_id == project.id }
else
project.project_member(options[:current_user])
project.member(options[:current_user])
end
end
......
......@@ -4,7 +4,7 @@ module API
module Entities
class ResourceAccessToken < Entities::PersonalAccessToken
expose :access_level do |token, options|
options[:resource].resource_member(token.user).access_level
options[:resource].member(token.user).access_level
end
end
end
......
......@@ -425,7 +425,7 @@ RSpec.describe API::MavenPackages do
context 'internal project' do
before do
group.group_member(user).destroy!
group.member(user).destroy!
project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
end
......
......@@ -244,7 +244,7 @@ RSpec.describe 'merge requests discussions' do
context 'when current_user role changes' do
before do
Members::UpdateService.new(owner, access_level: Gitlab::Access::GUEST).execute(project.project_member(user))
Members::UpdateService.new(owner, access_level: Gitlab::Access::GUEST).execute(project.member(user))
end
it_behaves_like 'cache miss' do
......
......@@ -50,7 +50,7 @@ RSpec.shared_examples 'project access tokens available #create' do
expect(created_token.name).to eq(access_token_params[:name])
expect(created_token.scopes).to eq(access_token_params[:scopes])
expect(created_token.expires_at).to eq(access_token_params[:expires_at])
expect(project.project_member(created_token.user).access_level).to eq(access_level)
expect(project.member(created_token.user).access_level).to eq(access_level)
end
it 'creates project bot user' do
......
......@@ -233,7 +233,7 @@ RSpec.shared_examples 'snippet visibility' do
project.update!(visibility_level: Gitlab::VisibilityLevel.level_value(project_visibility.to_s), snippets_access_level: feature_visibility)
if user_type == :external
member = project.project_member(external)
member = project.member(external)
if project.private?
project.add_developer(external) unless member
......
......@@ -3,7 +3,7 @@
RSpec.shared_examples 'inherited access level as a member of entity' do
let(:parent_entity) { create(:group) }
let(:user) { create(:user) }
let(:member) { entity.is_a?(Group) ? entity.group_member(user) : entity.project_member(user) }
let(:member) { entity.member(user) }
context 'with root parent_entity developer member' do
before do
......@@ -49,7 +49,7 @@ RSpec.shared_examples 'inherited access level as a member of entity' do
entity.add_maintainer(non_member_user)
non_member = entity.is_a?(Group) ? entity.group_member(non_member_user) : entity.project_member(non_member_user)
non_member = entity.member(non_member_user)
expect { non_member.update!(access_level: Gitlab::Access::GUEST) }
.to change { non_member.reload.access_level }
......
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