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