2016-04-14 15:10:57 +00:00
|
|
|
module Gitlab
|
2016-03-10 14:35:44 +00:00
|
|
|
module ImportExport
|
|
|
|
module ProjectFactory
|
|
|
|
extend self
|
|
|
|
|
2016-05-04 15:42:02 +00:00
|
|
|
def create(project_params:, user:, namespace_id:)
|
2016-03-10 14:35:44 +00:00
|
|
|
project = Project.new(project_params.except('id'))
|
|
|
|
project.creator = user
|
2016-05-04 15:42:02 +00:00
|
|
|
check_namespace(namespace_id, project, user)
|
2016-03-10 14:35:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_namespace(namespace_id, project, user)
|
|
|
|
if namespace_id
|
|
|
|
# Find matching namespace and check if it allowed
|
|
|
|
# for current user if namespace_id passed.
|
2016-05-04 15:42:02 +00:00
|
|
|
if allowed_namespace?(user, namespace_id)
|
|
|
|
project.namespace_id = namespace_id
|
|
|
|
else
|
2016-03-10 14:35:44 +00:00
|
|
|
project.namespace_id = nil
|
|
|
|
deny_namespace(project)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Set current user namespace if namespace_id is nil
|
|
|
|
project.namespace_id = user.namespace_id
|
|
|
|
end
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def allowed_namespace?(user, namespace_id)
|
|
|
|
namespace = Namespace.find_by(id: namespace_id)
|
|
|
|
user.can?(:create_projects, namespace)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deny_namespace(project)
|
|
|
|
project.errors.add(:namespace, "is not valid")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|