Commit a3fdd6ac authored by Toon Claes's avatar Toon Claes

Use string based `visibility` getter & setter

Add `visibility` & `visibility=` methods to the
`Gitlab::VisibilityLevel` module so the `visibility_level` can be
get/set with a string value.
parent bc20fa9b
......@@ -93,7 +93,7 @@ class Group < Namespace
end
def visibility_level_field
visibility_level
:visibility_level
end
def visibility_level_allowed_by_projects
......
......@@ -1003,7 +1003,7 @@ class Project < ActiveRecord::Base
end
def visibility_level_field
visibility_level
:visibility_level
end
def archive!
......
......@@ -120,7 +120,7 @@ class Snippet < ActiveRecord::Base
end
def visibility_level_field
visibility_level
:visibility_level
end
def no_highlighting?
......
......@@ -12,7 +12,7 @@ module Projects
@project = Project.new(params)
# Make sure that the user is allowed to use the specified visibility level
unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
unless Gitlab::VisibilityLevel.allowed_for?(current_user, @project.visibility_level)
deny_visibility_level(@project)
return @project
end
......
......@@ -84,7 +84,7 @@ class SystemHooksService
project_id: model.id,
owner_name: owner.name,
owner_email: owner.respond_to?(:email) ? owner.email : "",
project_visibility: Project.visibility_levels.key(model.visibility_level_field).downcase
project_visibility: Project.visibility_levels.key(model.visibility_level_value).downcase
}
end
......@@ -101,7 +101,7 @@ class SystemHooksService
user_email: model.user.email,
user_id: model.user.id,
access_level: model.human_access,
project_visibility: Project.visibility_levels.key(project.visibility_level_field).downcase
project_visibility: Project.visibility_levels.key(project.visibility_level_value).downcase
}
end
......
......@@ -70,8 +70,7 @@ module API
class Project < Grape::Entity
expose :id, :description, :default_branch, :tag_list
expose :archived?, as: :archived
expose :ssh_url_to_repo, :http_url_to_repo, :web_url
expose(:visibility) { |project, _options| Gitlab::VisibilityLevel.string_level(project.visibility_level) }
expose :visibility, :ssh_url_to_repo, :http_url_to_repo, :web_url
expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group }
expose :name, :name_with_namespace
expose :path, :path_with_namespace
......@@ -132,8 +131,7 @@ module API
end
class Group < Grape::Entity
expose :id, :name, :path, :description
expose(:visibility) { |group, _options| Gitlab::VisibilityLevel.string_level(group.visibility_level) }
expose :id, :name, :path, :description, :visibility
expose :lfs_enabled?, as: :lfs_enabled
expose :avatar_url
expose :web_url
......
......@@ -268,14 +268,6 @@ module API
projects.reorder(params[:order_by] => params[:sort])
end
def map_visibility_level(attrs)
visibility = attrs.delete(:visibility)
if visibility
attrs[:visibility_level] = Gitlab::VisibilityLevel.string_options[visibility]
end
attrs
end
# file helpers
def uploaded_file(field, uploads_path)
......
......@@ -56,7 +56,7 @@ module API
end
post ":id/snippets" do
authorize! :create_project_snippet, user_project
snippet_params = map_visibility_level(declared_params).merge(request: request, api: true)
snippet_params = declared_params.merge(request: request, api: true)
snippet_params[:content] = snippet_params.delete(:code)
snippet = CreateSnippetService.new(user_project, current_user, snippet_params).execute
......@@ -89,7 +89,7 @@ module API
authorize! :update_project_snippet, snippet
snippet_params = map_visibility_level(declared_params(include_missing: false))
snippet_params = declared_params(include_missing: false)
.merge(request: request, api: true)
snippet_params[:content] = snippet_params.delete(:code) if snippet_params[:code].present?
......
......@@ -97,7 +97,7 @@ module API
use :create_params
end
post do
attrs = map_visibility_level(declared_params(include_missing: false))
attrs = declared_params(include_missing: false)
project = ::Projects::CreateService.new(current_user, attrs).execute
if project.saved?
......@@ -126,7 +126,7 @@ module API
user = User.find_by(id: params.delete(:user_id))
not_found!('User') unless user
attrs = map_visibility_level(declared_params(include_missing: false))
attrs = declared_params(include_missing: false)
project = ::Projects::CreateService.new(user, attrs).execute
if project.saved?
......@@ -211,9 +211,9 @@ module API
end
put ':id' do
authorize_admin_project
attrs = map_visibility_level(declared_params(include_missing: false))
attrs = declared_params(include_missing: false)
authorize! :rename_project, user_project if attrs[:name].present?
authorize! :change_visibility_level, user_project if attrs[:visibility_level].present?
authorize! :change_visibility_level, user_project if attrs[:visibility].present?
result = ::Projects::UpdateService.new(user_project, current_user, attrs).execute
......
......@@ -64,7 +64,7 @@ module API
desc: 'The visibility of the snippet'
end
post do
attrs = map_visibility_level(declared_params(include_missing: false)).merge(request: request, api: true)
attrs = declared_params(include_missing: false).merge(request: request, api: true)
snippet = CreateSnippetService.new(nil, current_user, attrs).execute
render_spam_error! if snippet.spam?
......@@ -95,7 +95,7 @@ module API
return not_found!('Snippet') unless snippet
authorize! :update_personal_snippet, snippet
attrs = map_visibility_level(declared_params(include_missing: false).merge(request: request, api: true))
attrs = declared_params(include_missing: false).merge(request: request, api: true)
UpdateSnippetService.new(nil, current_user, snippet, attrs).execute
......
......@@ -95,21 +95,38 @@ module Gitlab
level_name
end
def level_value(level)
return string_options[level] if level.is_a? String
level
end
def string_level(level)
string_options.key(level)
end
end
def private?
visibility_level_field == PRIVATE
visibility_level_value == PRIVATE
end
def internal?
visibility_level_field == INTERNAL
visibility_level_value == INTERNAL
end
def public?
visibility_level_field == PUBLIC
visibility_level_value == PUBLIC
end
def visibility_level_value
self[visibility_level_field]
end
def visibility
Gitlab::VisibilityLevel.string_level(visibility_level_value)
end
def visibility=(level)
self[visibility_level_field] = Gitlab::VisibilityLevel.level_value(level)
end
end
end
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