diff options
author | Felipe Artur <felipefac@gmail.com> | 2016-03-17 01:44:33 +0300 |
---|---|---|
committer | Felipe Artur <felipefac@gmail.com> | 2016-03-17 01:44:33 +0300 |
commit | ec20fdf366843e60ed30abb5322c3c1b8f471b4a (patch) | |
tree | fd33118e0af74924bcb20a1696d91f85f6cd463c /app/services/groups | |
parent | de251bcf6d0f1db8858fa38ac14e108c1b9ea00f (diff) |
Code improvements and add Create group service
Diffstat (limited to 'app/services/groups')
-rw-r--r-- | app/services/groups/base_service.rb | 4 | ||||
-rw-r--r-- | app/services/groups/create_service.rb | 17 | ||||
-rw-r--r-- | app/services/groups/update_service.rb | 13 |
3 files changed, 24 insertions, 10 deletions
diff --git a/app/services/groups/base_service.rb b/app/services/groups/base_service.rb index 5becd475d3a..644ec7c013e 100644 --- a/app/services/groups/base_service.rb +++ b/app/services/groups/base_service.rb @@ -5,5 +5,9 @@ module Groups def initialize(group, user, params = {}) @group, @current_user, @params = group, user, params.dup end + + def add_error_message(message) + group.errors.add(:visibility_level, message) + end end end diff --git a/app/services/groups/create_service.rb b/app/services/groups/create_service.rb new file mode 100644 index 00000000000..e2875aafb94 --- /dev/null +++ b/app/services/groups/create_service.rb @@ -0,0 +1,17 @@ +module Groups + class CreateService < Groups::BaseService + def execute + return false unless visibility_level_allowed?(params[:visibility_level]) + @group.name = @group.path.dup unless @group.name + @group.save(params) && @group.add_owner(current_user) + end + + private + + def visibility_level_allowed?(level) + allowed = Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level]) + add_error_message("Visibility level restricted by admin.") unless allowed + allowed + end + end +end diff --git a/app/services/groups/update_service.rb b/app/services/groups/update_service.rb index acb6c529c17..a7382c1e07c 100644 --- a/app/services/groups/update_service.rb +++ b/app/services/groups/update_service.rb @@ -5,7 +5,8 @@ module Groups class UpdateService < Groups::BaseService def execute - visibility_level_allowed?(params[:visibility_level]) ? group.update_attributes(params) : false + return false unless visibility_level_allowed?(params[:visibility_level]) + group.update_attributes(params) end private @@ -22,7 +23,7 @@ module Groups def visibility_by_project(level) projects_visibility = group.projects.pluck(:visibility_level) - allowed_by_projects = !projects_visibility.any?{|project_visibility| level.to_i < project_visibility } + allowed_by_projects = !projects_visibility.any?{ |project_visibility| level.to_i < project_visibility } add_error_message("Cannot be changed. There are projects with higher visibility permissions.") unless allowed_by_projects allowed_by_projects end @@ -32,13 +33,5 @@ module Groups add_error_message("You are not authorized to set this permission level.") unless allowed_by_user allowed_by_user end - - def add_error_message(message) - level_name = Gitlab::VisibilityLevel.level_name(params[:visibility_level]) - group.errors.add(:visibility_level, message) - end end end - - - |