From e27d063b615af6f7e8904efec75b18c1dc79d567 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Fri, 2 Sep 2022 13:57:52 +0200 Subject: [PATCH] Make permission checks for card selection async [SCI-7157] --- app/assets/javascripts/projects/index.js | 11 ++++++++++- app/assets/javascripts/projects/show.js | 12 +++++++++++- app/controllers/experiments_controller.rb | 14 +++++++++++++- app/controllers/projects_controller.rb | 15 +++++++++++++-- app/models/repository_snapshot.rb | 2 +- app/views/projects/index/_project_card.html.erb | 5 +---- app/views/projects/show/_experiment_card.html.erb | 6 +----- config/routes.rb | 2 ++ 8 files changed, 52 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/projects/index.js b/app/assets/javascripts/projects/index.js index 1cfb98a2c..aa365057f 100644 --- a/app/assets/javascripts/projects/index.js +++ b/app/assets/javascripts/projects/index.js @@ -689,7 +689,16 @@ var ProjectsIndex = (function() { selectedProjects.splice(index, 1); } - updateProjectsToolbar(); + if (this.checked) { + $.get(projectCard.data('permissions-url'), function(result) { + PERMISSIONS.forEach((permission) => { + projectCard.data(permission, result[permission]); + }); + updateProjectsToolbar(); + }); + } else { + updateProjectsToolbar(); + } }); } diff --git a/app/assets/javascripts/projects/show.js b/app/assets/javascripts/projects/show.js index e6ca04831..cb152fe67 100644 --- a/app/assets/javascripts/projects/show.js +++ b/app/assets/javascripts/projects/show.js @@ -220,7 +220,17 @@ $(this).closest('.experiment-card').removeClass('selected'); selectedExperiments.splice(index, 1); } - updateExperimentsToolbar(); + + if (this.checked) { + $.get(card.data('permissions-url'), function(result) { + PERMISSIONS.forEach((permission) => { + card.data(permission, result[permission]); + }); + updateExperimentsToolbar(); + }); + } else { + updateExperimentsToolbar(); + } }); } diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 498a2d3c9..934bc84f0 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -64,6 +64,18 @@ class ExperimentsController < ApplicationController } end + def permissions + if stale?([@experiment, @experiment.project]) + render json: { + editable: can_manage_experiment?(@experiment), + moveable: can_move_experiment?(@experiment), + archivable: can_archive_experiment?(@experiment), + restorable: can_restore_experiment?(@experiment), + duplicable: can_clone_experiment?(@experiment) + } + end + end + def canvas redirect_to module_archive_experiment_path(@experiment) if @experiment.archived_branch? @project = @experiment.project @@ -278,7 +290,7 @@ class ExperimentsController < ApplicationController format.json do render json: { workflowimg: render_to_string( - partial: 'projects/show/workflow_img', + partial: 'projects/show/workflow_img.html.erb', locals: { experiment: @experiment } ) } diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 1ff2f57e9..5d25e374a 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -12,10 +12,10 @@ class ProjectsController < ApplicationController helper_method :current_folder before_action :switch_team_with_param, only: :index - before_action :load_vars, only: %i(show edit update notifications + before_action :load_vars, only: %i(show permissions edit update notifications sidebar experiments_cards view_type actions_dropdown) before_action :load_current_folder, only: %i(index cards new show) - before_action :check_view_permissions, only: %i(show notifications sidebar + before_action :check_view_permissions, only: %i(show permissions notifications sidebar experiments_cards view_type actions_dropdown) before_action :check_create_permissions, only: %i(new create) before_action :check_manage_permissions, only: :edit @@ -82,6 +82,17 @@ class ProjectsController < ApplicationController end end + def permissions + if stale?([@product, current_team]) + render json: { + editable: can_manage_project?(@project), + moveable: can_update_team?(current_team), + archivable: can_archive_project?(@project), + restorable: can_restore_project?(@project) + } + end + end + def sidebar @current_sort = @project.current_view_state(current_user).state.dig('experiments', params[:view_mode], 'sort') render json: { diff --git a/app/models/repository_snapshot.rb b/app/models/repository_snapshot.rb index bd20c0e6b..1318c23ca 100644 --- a/app/models/repository_snapshot.rb +++ b/app/models/repository_snapshot.rb @@ -10,7 +10,7 @@ class RepositorySnapshot < RepositoryBase class_name: 'Repository', inverse_of: :repository_snapshots, optional: true - belongs_to :my_module, optional: true + belongs_to :my_module, optional: true, touch: true has_one :repository_stock_consumption_column, -> { where(data_type: 'RepositoryStockConsumptionValue') }, class_name: 'RepositoryColumn', diff --git a/app/views/projects/index/_project_card.html.erb b/app/views/projects/index/_project_card.html.erb index f13ccb631..fd8c22d15 100644 --- a/app/views/projects/index/_project_card.html.erb +++ b/app/views/projects/index/_project_card.html.erb @@ -1,10 +1,7 @@
+ data-permissions-url="<%= permissions_project_path(project) %>">
diff --git a/app/views/projects/show/_experiment_card.html.erb b/app/views/projects/show/_experiment_card.html.erb index 922223b3d..c74a8888c 100644 --- a/app/views/projects/show/_experiment_card.html.erb +++ b/app/views/projects/show/_experiment_card.html.erb @@ -1,11 +1,7 @@
+ data-permissions-url="<%= permissions_experiment_path(experiment) %>">
<% if project_is_managable %>
diff --git a/config/routes.rb b/config/routes.rb index 722c885b1..9ba4579cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -300,6 +300,7 @@ Rails.application.routes.draw do member do # Notifications popup for individual project in projects index get 'notifications' + get 'permissions' get 'experiments_cards' get 'sidebar' get 'actions_dropdown' @@ -334,6 +335,7 @@ Rails.application.routes.draw do get 'move_modal', action: :move_modal end member do + get 'permissions' get 'canvas' # Overview/structure for single experiment # AJAX-loaded canvas edit mode (from canvas) get 'canvas/edit', to: 'canvas#edit'