2019-08-19 19:30:56 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
class RepositoriesController < ApplicationController
|
2019-09-02 16:30:29 +08:00
|
|
|
include InventoriesHelper
|
|
|
|
include ActionView::Helpers::TagHelper
|
|
|
|
include ActionView::Context
|
|
|
|
include IconsHelper
|
|
|
|
|
2017-06-19 20:05:37 +08:00
|
|
|
before_action :load_vars,
|
2019-04-18 21:18:10 +08:00
|
|
|
except: %i(index create create_modal parse_sheet)
|
2017-06-12 23:49:42 +08:00
|
|
|
before_action :load_parent_vars, except:
|
2019-04-18 21:18:10 +08:00
|
|
|
%i(repository_table_index parse_sheet)
|
2017-05-22 23:54:30 +08:00
|
|
|
before_action :check_view_all_permissions, only: :index
|
2018-04-25 17:56:30 +08:00
|
|
|
before_action :check_view_permissions, only: %i(export_repository show)
|
2018-02-16 01:46:29 +08:00
|
|
|
before_action :check_manage_permissions, only:
|
2017-06-12 23:49:42 +08:00
|
|
|
%i(destroy destroy_modal rename_modal update)
|
2019-08-20 22:07:22 +08:00
|
|
|
before_action :check_share_permissions, only: :share_modal
|
2017-06-06 17:10:55 +08:00
|
|
|
before_action :check_create_permissions, only:
|
2019-08-29 19:39:22 +08:00
|
|
|
%i(create_modal create)
|
|
|
|
before_action :check_copy_permissions, only:
|
|
|
|
%i(copy_modal copy)
|
2019-08-19 19:30:56 +08:00
|
|
|
before_action :set_inline_name_editing, only: %i(show)
|
2017-05-22 23:54:30 +08:00
|
|
|
|
2018-03-28 20:12:11 +08:00
|
|
|
layout 'fluid'
|
2018-03-21 16:22:55 +08:00
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def index
|
2019-08-19 19:30:56 +08:00
|
|
|
redirect_to repository_path(@repositories.first) and return unless @repositories.length.zero? && current_team
|
2018-03-20 21:45:57 +08:00
|
|
|
render 'repositories/index'
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
|
|
|
|
2019-08-19 19:30:56 +08:00
|
|
|
def show; end
|
2017-06-08 17:44:24 +08:00
|
|
|
|
2017-06-06 21:50:43 +08:00
|
|
|
def create_modal
|
|
|
|
@repository = Repository.new
|
2017-05-24 15:29:44 +08:00
|
|
|
respond_to do |format|
|
2017-06-06 19:33:57 +08:00
|
|
|
format.json do
|
2017-05-24 15:29:44 +08:00
|
|
|
render json: {
|
2017-06-06 19:33:57 +08:00
|
|
|
html: render_to_string(
|
2017-06-06 21:50:43 +08:00
|
|
|
partial: 'create_repository_modal.html.erb'
|
2017-06-06 19:33:57 +08:00
|
|
|
)
|
2017-05-24 15:29:44 +08:00
|
|
|
}
|
2017-06-06 19:33:57 +08:00
|
|
|
end
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-05 19:07:29 +08:00
|
|
|
def share_modal
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: { html: render_to_string(partial: 'share_repository_modal.html.erb') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 15:29:44 +08:00
|
|
|
def create
|
|
|
|
@repository = Repository.new(
|
|
|
|
team: @team,
|
|
|
|
created_by: current_user
|
|
|
|
)
|
|
|
|
@repository.assign_attributes(repository_params)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2017-06-06 19:33:57 +08:00
|
|
|
format.json do
|
|
|
|
if @repository.save
|
2019-03-07 02:27:40 +08:00
|
|
|
log_activity(:create_inventory)
|
|
|
|
|
2017-06-06 19:33:57 +08:00
|
|
|
flash[:success] = t('repositories.index.modal_create.success_flash',
|
|
|
|
name: @repository.name)
|
2018-03-21 16:22:55 +08:00
|
|
|
render json: { url: repository_path(@repository) },
|
2017-05-24 15:29:44 +08:00
|
|
|
status: :ok
|
2017-06-06 19:33:57 +08:00
|
|
|
else
|
2017-05-24 15:29:44 +08:00
|
|
|
render json: @repository.errors,
|
|
|
|
status: :unprocessable_entity
|
2017-06-06 19:33:57 +08:00
|
|
|
end
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 21:11:28 +08:00
|
|
|
def destroy_modal
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'delete_repository_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 21:09:09 +08:00
|
|
|
def destroy
|
2017-06-02 23:38:46 +08:00
|
|
|
flash[:success] = t('repositories.index.delete_flash',
|
|
|
|
name: @repository.name)
|
2019-03-07 02:27:40 +08:00
|
|
|
|
|
|
|
log_activity(:delete_inventory) # Log before delete id
|
|
|
|
|
2018-05-25 17:41:43 +08:00
|
|
|
@repository.discard
|
2018-05-29 21:33:02 +08:00
|
|
|
@repository.destroy_discarded(current_user.id)
|
2017-06-05 23:56:09 +08:00
|
|
|
redirect_to team_repositories_path
|
2017-06-01 01:24:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def rename_modal
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'rename_repository_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2019-09-12 23:21:48 +08:00
|
|
|
@repository.update(repository_params)
|
2017-05-30 21:09:09 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-06-01 01:24:38 +08:00
|
|
|
format.json do
|
2017-06-02 23:38:46 +08:00
|
|
|
if @repository.save
|
2019-03-07 02:27:40 +08:00
|
|
|
log_activity(:rename_inventory) # Acton only for renaming
|
|
|
|
|
2017-06-03 01:08:51 +08:00
|
|
|
render json: {
|
2017-06-05 23:56:09 +08:00
|
|
|
url: team_repositories_path(repository: @repository)
|
2017-06-03 01:08:51 +08:00
|
|
|
}, status: :ok
|
2017-06-01 01:24:38 +08:00
|
|
|
else
|
2017-06-02 23:38:46 +08:00
|
|
|
render json: @repository.errors, status: :unprocessable_entity
|
2017-06-01 01:24:38 +08:00
|
|
|
end
|
|
|
|
end
|
2017-05-30 21:09:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-08 01:11:25 +08:00
|
|
|
def copy_modal
|
|
|
|
@tmp_repository = Repository.new(
|
|
|
|
team: @team,
|
|
|
|
created_by: current_user,
|
|
|
|
name: @repository.name
|
|
|
|
)
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'copy_repository_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy
|
|
|
|
@tmp_repository = Repository.new(
|
|
|
|
team: @team,
|
|
|
|
created_by: current_user
|
|
|
|
)
|
|
|
|
@tmp_repository.assign_attributes(repository_params)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
if !@tmp_repository.valid?
|
|
|
|
render json: @tmp_repository.errors, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
copied_repository =
|
|
|
|
@repository.copy(current_user, @tmp_repository.name)
|
|
|
|
|
|
|
|
if !copied_repository
|
|
|
|
render json: { 'name': ['Server error'] },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
flash[:success] = t(
|
|
|
|
'repositories.index.copy_flash',
|
|
|
|
old: @repository.name,
|
|
|
|
new: copied_repository.name
|
|
|
|
)
|
|
|
|
render json: {
|
|
|
|
url: team_repositories_path(repository: copied_repository)
|
|
|
|
}, status: :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
# AJAX actions
|
|
|
|
def repository_table_index
|
2019-07-17 22:00:49 +08:00
|
|
|
if @repository.nil? || !can_read_repository?(@repository)
|
2017-06-06 23:35:29 +08:00
|
|
|
render_403
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: ::RepositoryDatatable.new(view_context,
|
|
|
|
@repository,
|
|
|
|
nil,
|
|
|
|
current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-13 14:10:10 +08:00
|
|
|
def parse_sheet
|
2019-08-21 21:32:45 +08:00
|
|
|
repository = Repository.accessible_by_teams(current_team).find_by_id(import_params[:id])
|
|
|
|
|
|
|
|
render_403 unless can_create_repository_rows?(repository)
|
2017-06-14 15:11:20 +08:00
|
|
|
|
2018-01-16 01:01:35 +08:00
|
|
|
unless import_params[:file]
|
2017-07-14 22:19:03 +08:00
|
|
|
repository_response(t('teams.parse_sheet.errors.no_file_selected'))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
parsed_file = ImportRepository::ParseRepository.new(
|
2018-01-16 01:01:35 +08:00
|
|
|
file: import_params[:file],
|
2017-07-14 22:19:03 +08:00
|
|
|
repository: repository,
|
|
|
|
session: session
|
|
|
|
)
|
|
|
|
if parsed_file.too_large?
|
|
|
|
repository_response(t('general.file.size_exceeded',
|
2018-06-19 20:15:14 +08:00
|
|
|
file_size: Rails.configuration.x.file_max_size_mb))
|
2018-05-31 16:45:31 +08:00
|
|
|
elsif parsed_file.has_too_many_rows?
|
|
|
|
repository_response(
|
|
|
|
t('repositories.import_records.error_message.items_limit',
|
|
|
|
items_size: Constants::IMPORT_REPOSITORY_ITEMS_LIMIT)
|
|
|
|
)
|
2017-07-14 22:19:03 +08:00
|
|
|
else
|
|
|
|
@import_data = parsed_file.data
|
2017-10-12 20:43:25 +08:00
|
|
|
|
2017-10-17 21:23:54 +08:00
|
|
|
if @import_data.header.empty? || @import_data.columns.empty?
|
2017-10-12 20:43:25 +08:00
|
|
|
return repository_response(t('teams.parse_sheet.errors.empty_file'))
|
|
|
|
end
|
|
|
|
|
2017-10-17 20:42:06 +08:00
|
|
|
if (@temp_file = parsed_file.generate_temp_file)
|
2017-07-14 22:19:03 +08:00
|
|
|
respond_to do |format|
|
2017-06-14 15:11:20 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repositories/parse_records_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2017-07-14 22:19:03 +08:00
|
|
|
else
|
|
|
|
repository_response(t('teams.parse_sheet.errors.temp_file_failure'))
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
|
|
|
end
|
2017-07-14 22:19:03 +08:00
|
|
|
rescue ArgumentError, CSV::MalformedCSVError
|
|
|
|
repository_response(t('teams.parse_sheet.errors.invalid_file',
|
|
|
|
encoding: ''.encoding))
|
|
|
|
rescue TypeError
|
|
|
|
repository_response(t('teams.parse_sheet.errors.invalid_extension'))
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
2017-06-13 14:10:10 +08:00
|
|
|
end
|
|
|
|
|
2017-06-19 20:05:37 +08:00
|
|
|
def import_records
|
2019-08-21 21:32:45 +08:00
|
|
|
render_403 unless can_create_repository_rows?(Repository.accessible_by_teams(current_team)
|
|
|
|
.find_by_id(import_params[:id]))
|
|
|
|
|
2017-06-21 20:45:50 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-06-26 22:50:40 +08:00
|
|
|
# Check if there exist mapping for repository record (it's mandatory)
|
2018-01-16 01:01:35 +08:00
|
|
|
if import_params[:mappings].value?('-1')
|
2017-06-26 22:50:40 +08:00
|
|
|
import_records = repostiory_import_actions
|
|
|
|
status = import_records.import!
|
|
|
|
|
|
|
|
if status[:status] == :ok
|
2019-04-26 17:34:53 +08:00
|
|
|
log_activity(:import_inventory_items,
|
|
|
|
num_of_items: status[:nr_of_added])
|
|
|
|
|
2017-06-26 22:50:40 +08:00
|
|
|
flash[:success] = t('repositories.import_records.success_flash',
|
2018-06-15 17:12:11 +08:00
|
|
|
number_of_rows: status[:nr_of_added],
|
|
|
|
total_nr: status[:total_nr])
|
2017-06-26 22:50:40 +08:00
|
|
|
render json: {}, status: :ok
|
|
|
|
else
|
2017-07-18 20:54:35 +08:00
|
|
|
flash[:alert] =
|
|
|
|
t('repositories.import_records.partial_success_flash',
|
|
|
|
nr: status[:nr_of_added], total_nr: status[:total_nr])
|
2017-06-26 22:50:40 +08:00
|
|
|
render json: {}, status: :unprocessable_entity
|
|
|
|
end
|
2017-06-21 20:45:50 +08:00
|
|
|
else
|
2017-06-26 22:50:40 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'shared/flash_errors.html.erb',
|
|
|
|
locals: { error_title: t('repositories.import_records'\
|
|
|
|
'.error_message.errors_list_title'),
|
|
|
|
error: t('repositories.import_records.error_message'\
|
|
|
|
'.no_repository_name') }
|
|
|
|
)
|
|
|
|
},
|
|
|
|
status: :unprocessable_entity
|
2017-06-21 20:45:50 +08:00
|
|
|
end
|
|
|
|
end
|
2017-06-21 15:31:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 22:12:34 +08:00
|
|
|
def export_repository
|
2019-09-05 22:24:38 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
if params[:row_ids] && params[:header_ids]
|
|
|
|
RepositoryZipExport.generate_zip(params, @repository, current_user)
|
|
|
|
log_activity(:export_inventory_items)
|
|
|
|
render json: { message: t('zip_export.export_request_success') }, status: :ok
|
|
|
|
else
|
|
|
|
render json: { message: t('zip_export.export_error') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2017-06-06 22:12:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-21 15:31:39 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def repostiory_import_actions
|
|
|
|
ImportRepository::ImportRecords.new(
|
2018-01-16 01:01:35 +08:00
|
|
|
temp_file: TempFile.find_by_id(import_params[:file_id]),
|
2019-08-21 21:32:45 +08:00
|
|
|
repository: Repository.accessible_by_teams(current_team).find_by_id(import_params[:id]),
|
2018-01-16 01:01:35 +08:00
|
|
|
mappings: import_params[:mappings],
|
2017-06-19 20:05:37 +08:00
|
|
|
session: session,
|
|
|
|
user: current_user
|
|
|
|
)
|
2017-06-13 14:10:10 +08:00
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def load_vars
|
2017-06-12 23:49:42 +08:00
|
|
|
repository_id = params[:id] || params[:repository_id]
|
2019-08-12 16:55:30 +08:00
|
|
|
@repository = Repository.accessible_by_teams(current_team).find_by_id(repository_id)
|
2017-06-12 23:49:42 +08:00
|
|
|
render_404 unless @repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_parent_vars
|
2018-03-20 21:45:57 +08:00
|
|
|
@team = current_team
|
2017-05-22 23:54:30 +08:00
|
|
|
render_404 unless @team
|
2019-08-19 15:23:59 +08:00
|
|
|
@repositories = Repository.accessible_by_teams(@team).order('repositories.created_at ASC')
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
|
|
|
|
2019-08-19 19:30:56 +08:00
|
|
|
def set_inline_name_editing
|
|
|
|
return unless can_manage_repository?(@repository)
|
|
|
|
|
|
|
|
@inline_editable_title_config = {
|
|
|
|
name: 'title',
|
|
|
|
params_group: 'repository',
|
|
|
|
field_to_udpate: 'name',
|
2019-09-02 16:30:29 +08:00
|
|
|
path_to_update: team_repository_path(@repository),
|
|
|
|
label_after: "<span class=\"repository-share-icon\">#{inventory_shared_status_icon(@repository, current_team)}</span>"
|
2019-08-19 19:30:56 +08:00
|
|
|
}
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def check_view_all_permissions
|
2017-12-12 18:45:19 +08:00
|
|
|
render_403 unless can_read_team?(@team)
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
2017-06-01 01:24:38 +08:00
|
|
|
|
2017-06-12 23:49:42 +08:00
|
|
|
def check_view_permissions
|
2019-07-17 22:00:49 +08:00
|
|
|
render_403 unless can_read_repository?(@repository)
|
2017-06-12 23:49:42 +08:00
|
|
|
end
|
|
|
|
|
2017-05-24 15:29:44 +08:00
|
|
|
def check_create_permissions
|
2018-05-10 00:03:16 +08:00
|
|
|
unless can_create_repositories?(@team) ||
|
|
|
|
@team.repositories.count < Rails.configuration.x.repositories_limit
|
|
|
|
render_403
|
|
|
|
end
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2019-08-29 19:39:22 +08:00
|
|
|
def check_copy_permissions
|
|
|
|
render_403 if !can_create_repositories?(@team) ||
|
|
|
|
@team.repositories.count >= Rails.configuration.x.repositories_limit ||
|
|
|
|
@repository.shared_with?(current_team)
|
|
|
|
end
|
|
|
|
|
2018-02-16 01:46:29 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_repository?(@repository)
|
2017-06-08 01:11:25 +08:00
|
|
|
end
|
|
|
|
|
2019-08-20 22:07:22 +08:00
|
|
|
def check_share_permissions
|
2019-08-23 17:29:03 +08:00
|
|
|
render_403 if !can_share_repository?(@repository) || current_user.teams.count <= 1
|
2019-08-20 22:07:22 +08:00
|
|
|
end
|
|
|
|
|
2017-06-01 01:24:38 +08:00
|
|
|
def repository_params
|
|
|
|
params.require(:repository).permit(:name)
|
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
|
2018-01-16 01:01:35 +08:00
|
|
|
def import_params
|
|
|
|
params.permit(:id, :file, :file_id, mappings: {}).to_h
|
|
|
|
end
|
|
|
|
|
2017-06-14 15:11:20 +08:00
|
|
|
def repository_response(message)
|
2017-07-14 22:19:03 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
flash[:alert] = message
|
2017-12-08 00:59:23 +08:00
|
|
|
redirect_back(fallback_location: root_path)
|
2017-07-14 22:19:03 +08:00
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: { message: message },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
|
|
|
end
|
2019-03-07 02:27:40 +08:00
|
|
|
|
2019-04-18 21:18:10 +08:00
|
|
|
def log_activity(type_of, message_items = {})
|
|
|
|
message_items = { repository: @repository.id }.merge(message_items)
|
|
|
|
|
2019-03-07 02:27:40 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @repository,
|
|
|
|
team: @team,
|
2019-04-18 21:18:10 +08:00
|
|
|
message_items: message_items)
|
2019-03-07 02:27:40 +08:00
|
|
|
end
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|