Commit c6960ded authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactor add_users method for project and group

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent c76812c7
No related merge requests found
......@@ -125,7 +125,7 @@ class Group < Namespace
end
def add_users(users, access_level, current_user: nil, expires_at: nil)
GroupMember.add_users_to_group(
GroupMember.add_users(
self,
users,
access_level,
......
......@@ -151,6 +151,22 @@ class Member < ActiveRecord::Base
member
end
def add_users(source, users, access_level, current_user: nil, expires_at: nil)
return [] unless users.present?
self.transaction do
users.map do |user|
add_user(
source,
user,
access_level,
current_user: current_user,
expires_at: expires_at
)
end
end
end
def access_levels
Gitlab::Access.sym_options
end
......@@ -173,18 +189,6 @@ class Member < ActiveRecord::Base
# There is no current user for bulk actions, in which case anything is allowed
!current_user || current_user.can?(:"update_#{member.type.underscore}", member)
end
def add_users_to_source(source, users, access_level, current_user: nil, expires_at: nil)
users.each do |user|
add_user(
source,
user,
access_level,
current_user: current_user,
expires_at: expires_at
)
end
end
end
def real_source_type
......
......@@ -21,18 +21,6 @@ class GroupMember < Member
Gitlab::Access.sym_options_with_owner
end
def self.add_users_to_group(group, users, access_level, current_user: nil, expires_at: nil)
self.transaction do
add_users_to_source(
group,
users,
access_level,
current_user: current_user,
expires_at: expires_at
)
end
end
def group
source
end
......
......@@ -16,7 +16,7 @@ class ProjectMember < Member
before_destroy :delete_member_todos
class << self
# Add users to project teams with passed access option
# Add users to projects with passed access option
#
# access can be an integer representing a access code
# or symbol like :master representing role
......@@ -39,7 +39,7 @@ class ProjectMember < Member
project_ids.each do |project_id|
project = Project.find(project_id)
add_users_to_source(
add_users(
project,
users,
access_level,
......
......@@ -50,8 +50,8 @@ class ProjectTeam
end
def add_users(users, access_level, current_user: nil, expires_at: nil)
ProjectMember.add_users_to_projects(
[project.id],
ProjectMember.add_users(
project,
users,
access_level,
current_user: current_user,
......
---
title: Refactor add_users method for project and group
merge_request: 10850
author:
......@@ -127,7 +127,7 @@ describe Projects::LabelsController do
context 'group owner' do
before do
GroupMember.add_users_to_group(group, [user], :owner)
GroupMember.add_users(group, [user], :owner)
end
it 'gives access' do
......
......@@ -386,6 +386,31 @@ describe Member, models: true do
end
end
describe '.add_users' do
%w[project group].each do |source_type|
context "when source is a #{source_type}" do
let!(:source) { create(source_type, :public, :access_requestable) }
let!(:user) { create(:user) }
let!(:admin) { create(:admin) }
it 'returns a <Source>Member objects' do
members = described_class.add_users(source, [user], :master)
expect(members).to be_a Array
expect(members.first).to be_a "#{source_type.classify}Member".constantize
expect(members.first).to be_persisted
end
it 'returns an empty array' do
members = described_class.add_users(source, [], :master)
expect(members).to be_a Array
expect(members).to be_empty
end
end
end
end
describe '#accept_request' do
let(:member) { create(:project_member, requested_at: Time.now.utc) }
......
......@@ -13,12 +13,12 @@ describe GroupMember, models: true do
end
end
describe '.add_users_to_group' do
describe '.add_users' do
it 'adds the given users to the given group' do
group = create(:group)
users = create_list(:user, 2)
described_class.add_users_to_group(
described_class.add_users(
group,
[users.first.id, users.second],
described_class::MASTER
......
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