Would have liked to use the contract here to validate the role_ids before they're being written to attributes, but that doesn't seem to work.pull/5719/head
parent
b2b499a7fd
commit
dae84cffb0
@ -0,0 +1,93 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License version 3. |
||||
# |
||||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
||||
# Copyright (C) 2006-2017 Jean-Philippe Lang |
||||
# Copyright (C) 2010-2013 the ChiliProject Team |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# as published by the Free Software Foundation; either version 2 |
||||
# of the License, or (at your option) any later version. |
||||
# |
||||
# This program is distributed in the hope that it will be useful, |
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
# GNU General Public License for more details. |
||||
# |
||||
# You should have received a copy of the GNU General Public License |
||||
# along with this program; if not, write to the Free Software |
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||||
# |
||||
# See doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module Members |
||||
class EditMembershipService |
||||
attr_reader :current_user, :member, :do_save |
||||
|
||||
def initialize(member, save:, current_user:) |
||||
@current_user = current_user |
||||
@member = member |
||||
@do_save = do_save |
||||
end |
||||
|
||||
def call(attributes: {}) |
||||
User.execute_as current_user do |
||||
|
||||
process_attributes! attributes |
||||
|
||||
unless validate_attributes! attributes |
||||
return make_result(success: false) |
||||
end |
||||
|
||||
set_attributes(attributes) |
||||
|
||||
success = |
||||
if do_save |
||||
member.save |
||||
else |
||||
member.validate |
||||
end |
||||
|
||||
make_result(success: success) |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def make_result(success:) |
||||
ServiceResult.new(success: success, errors: member.errors, result: member) |
||||
end |
||||
|
||||
def process_attributes!(attributes) |
||||
# Reject any blank values from unselected values |
||||
if attributes.key? :role_ids |
||||
attributes[:role_ids].reject!(&:blank?) |
||||
end |
||||
end |
||||
|
||||
## |
||||
# We need to validate attributes before passing them to user |
||||
# because role_ids are modified instantly and may cause errors if empty |
||||
def validate_attributes!(attributes) |
||||
# We need to check for empty roles here because that _implicitly_ |
||||
# deletes the membership and causes failures |
||||
if attributes[:role_ids].empty? |
||||
member.errors.add :roles, :role_blank |
||||
return false |
||||
end |
||||
|
||||
true |
||||
end |
||||
|
||||
def set_attributes(attributes) |
||||
member.attributes = attributes |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue