diff --git a/app/controllers/my_module_shareable_links_controller.rb b/app/controllers/my_module_shareable_links_controller.rb new file mode 100644 index 000000000..814cf14be --- /dev/null +++ b/app/controllers/my_module_shareable_links_controller.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +class MyModuleShareableLinksController < ApplicationController + before_action :load_my_module + before_action :load_shareable_link, only: %i(update destroy) + before_action :check_view_permissions, only: :show + before_action :check_manage_permissions, except: :show + + def show + render json: @my_module.shareable_link + end + + def create + @my_module.shareable_link.create!( + signed_id: @my_module.signed_id, + description: params[:description], + team: @my_module.team, + created_by: current_user + ) + + render json: @my_module.shareable_link + end + + def update + @my_module.shareable_link.update!( + description: params[:description], + last_modified_by: current_user + ) + + render json: @my_module.shareable_link + end + + def destroy + @my_module.shareable_link.destroy! + + render json: {} + end + + private + + def load_my_module + @my_module = MyModule.find_by(id: params[:my_module_id]) + render_404 unless @my_module + end + + def check_view_permissions + render_403 unless can_view_my_module?(@my_module) + end + + def check_manage_permissions + render_403 unless can_manage_my_module?(@my_module) + end +end diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index f1e07b1de..1b04ae2e4 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -83,7 +83,11 @@ class TeamsController < ApplicationController } end - def shared_tasks_toggle; end + def shared_tasks_toggle + return render_403 unless can_manage_team?(@team) + + @team.toggle!(:shareable_links_enabled) + end def routing_error(error = 'Routing error', status = :not_found, exception=nil) redirect_to root_path diff --git a/app/models/my_module.rb b/app/models/my_module.rb index bba5a02f7..73ad70235 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -47,6 +47,7 @@ class MyModule < ApplicationRecord belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User', optional: true belongs_to :experiment, inverse_of: :my_modules, touch: true has_one :project, through: :experiment, autosave: false + has_one :shareable_link, as: :shareable, dependent: :destroy delegate :team, to: :project belongs_to :my_module_group, inverse_of: :my_modules, optional: true belongs_to :my_module_status, optional: true diff --git a/app/models/shareable_link.rb b/app/models/shareable_link.rb new file mode 100644 index 000000000..a8ba4dfcf --- /dev/null +++ b/app/models/shareable_link.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class ShareableLink < ApplicationRecord + validates :shareable_type, uniqueness: { scope: :shareable_id } + validates :description, length: { maximum: Constants::RICH_TEXT_MAX_LENGTH } + + belongs_to :shareable, polymorphic: true, inverse_of: :shareable_link + belongs_to :team + belongs_to :created_by, class_name: 'User' + belongs_to :last_modified_by, class_name: 'User', optional: true +end diff --git a/app/models/team.rb b/app/models/team.rb index 582ced446..b71070582 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -12,6 +12,7 @@ class Team < ApplicationRecord # output in space_taken related functions include ActionView::Helpers::NumberHelper + before_save -> { shareable_links.destroy_all }, if: -> { !shareable_links_enabled? } after_create :generate_template_project after_create :create_default_label_templates scope :teams_select, -> { select(:id, :name).order(name: :asc) } @@ -70,6 +71,7 @@ class Team < ApplicationRecord through: :repository_sharing_user_assignments, source: :assignable, source_type: 'RepositoryBase' + has_many :shareable_links, inverse_of: :team, dependent: :destroy attr_accessor :without_templates @@ -193,12 +195,8 @@ class Team < ApplicationRecord ) end - def team_task_shareable? - false # dummy data before backend implementation - end - def number_of_task_shared - 1 # dummy data before backend implementation + shareable_links.count end private diff --git a/app/models/user.rb b/app/models/user.rb index 6529ecb19..da54bdfc7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -307,6 +307,7 @@ class User < ApplicationRecord foreign_key: 'last_modified_by_id', inverse_of: :last_modified_by, dependent: :nullify + has_many :shareable_links, inverse_of: :created_by, dependent: :destroy has_many :user_notifications, inverse_of: :user has_many :notifications, through: :user_notifications diff --git a/app/views/users/settings/teams/show.html.erb b/app/views/users/settings/teams/show.html.erb index 200f839b8..68c382e58 100644 --- a/app/views/users/settings/teams/show.html.erb +++ b/app/views/users/settings/teams/show.html.erb @@ -56,7 +56,7 @@
<%= t("users.settings.teams.show.tasks_share.enable_label") %> - <%= check_box_tag 'select_team_share_permission', 0, @team.team_task_shareable?, { class: 'sci-toggle-checkbox team-share-permission', + <%= check_box_tag 'select_team_share_permission', 0, @team.shareable_links_enabled?, { class: 'sci-toggle-checkbox team-share-permission', data: { disable_url: disable_tasks_sharing_modal_team_path(), enable_url: shared_tasks_toggle_team_path() }, diff --git a/db/migrate/20230627091027_add_shareable_links.rb b/db/migrate/20230627091027_add_shareable_links.rb new file mode 100644 index 000000000..bc3434baf --- /dev/null +++ b/db/migrate/20230627091027_add_shareable_links.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class AddShareableLinks < ActiveRecord::Migration[6.1] + def change + create_table :shareable_links do |t| + t.string :signed_id, index: true + t.string :description + t.references :shareable, polymorphic: true, index: true + t.references :team, index: true, foreign_key: { to_table: :teams } + t.references :created_by, index: true, foreign_key: { to_table: :users } + t.references :last_modified_by, index: true, foreign_key: { to_table: :users } + + t.timestamps + end + + add_column :teams, :shareable_links_enabled, :boolean, default: false, null: false + end +end diff --git a/db/structure.sql b/db/structure.sql index ab6f2c7c9..b68a9a41e 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -531,7 +531,8 @@ CREATE TABLE public.teams ( created_by_id bigint, last_modified_by_id bigint, description character varying, - space_taken bigint DEFAULT 1048576 NOT NULL + space_taken bigint DEFAULT 1048576 NOT NULL, + shareable_links_enabled boolean DEFAULT false NOT NULL ); @@ -2507,6 +2508,43 @@ CREATE SEQUENCE public.settings_id_seq ALTER SEQUENCE public.settings_id_seq OWNED BY public.settings.id; +-- +-- Name: shareable_links; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.shareable_links ( + id bigint NOT NULL, + signed_id character varying, + description character varying, + shareable_type character varying, + shareable_id bigint, + team_id bigint, + created_by_id bigint, + last_modified_by_id bigint, + created_at timestamp(6) without time zone NOT NULL, + updated_at timestamp(6) without time zone NOT NULL +); + + +-- +-- Name: shareable_links_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.shareable_links_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: shareable_links_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.shareable_links_id_seq OWNED BY public.shareable_links.id; + + -- -- Name: step_assets; Type: TABLE; Schema: public; Owner: - -- @@ -3934,6 +3972,13 @@ ALTER TABLE ONLY public.results ALTER COLUMN id SET DEFAULT nextval('public.resu ALTER TABLE ONLY public.settings ALTER COLUMN id SET DEFAULT nextval('public.settings_id_seq'::regclass); +-- +-- Name: shareable_links id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.shareable_links ALTER COLUMN id SET DEFAULT nextval('public.shareable_links_id_seq'::regclass); + + -- -- Name: step_assets id; Type: DEFAULT; Schema: public; Owner: - -- @@ -4674,6 +4719,14 @@ ALTER TABLE ONLY public.settings ADD CONSTRAINT settings_pkey PRIMARY KEY (id); +-- +-- Name: shareable_links shareable_links_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.shareable_links + ADD CONSTRAINT shareable_links_pkey PRIMARY KEY (id); + + -- -- Name: step_assets step_assets_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -6536,6 +6589,41 @@ CREATE INDEX index_results_on_user_id ON public.results USING btree (user_id); CREATE UNIQUE INDEX index_settings_on_type ON public.settings USING btree (type); +-- +-- Name: index_shareable_links_on_created_by_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_shareable_links_on_created_by_id ON public.shareable_links USING btree (created_by_id); + + +-- +-- Name: index_shareable_links_on_last_modified_by_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_shareable_links_on_last_modified_by_id ON public.shareable_links USING btree (last_modified_by_id); + + +-- +-- Name: index_shareable_links_on_shareable; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_shareable_links_on_shareable ON public.shareable_links USING btree (shareable_type, shareable_id); + + +-- +-- Name: index_shareable_links_on_signed_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_shareable_links_on_signed_id ON public.shareable_links USING btree (signed_id); + + +-- +-- Name: index_shareable_links_on_team_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_shareable_links_on_team_id ON public.shareable_links USING btree (team_id); + + -- -- Name: index_step_assets_on_step_id_and_asset_id; Type: INDEX; Schema: public; Owner: - -- @@ -7355,6 +7443,14 @@ ALTER TABLE ONLY public.oauth_access_grants ADD CONSTRAINT fk_rails_330c32d8d9 FOREIGN KEY (resource_owner_id) REFERENCES public.users(id); +-- +-- Name: shareable_links fk_rails_34ccd51b3f; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.shareable_links + ADD CONSTRAINT fk_rails_34ccd51b3f FOREIGN KEY (last_modified_by_id) REFERENCES public.users(id); + + -- -- Name: my_module_statuses fk_rails_357ee33309; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -7483,6 +7579,14 @@ ALTER TABLE ONLY public.repository_list_items ADD CONSTRAINT fk_rails_4e75dc8e18 FOREIGN KEY (created_by_id) REFERENCES public.users(id); +-- +-- Name: shareable_links fk_rails_58d7b77ced; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.shareable_links + ADD CONSTRAINT fk_rails_58d7b77ced FOREIGN KEY (created_by_id) REFERENCES public.users(id); + + -- -- Name: repository_date_time_values fk_rails_5d809fca2c; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -7915,6 +8019,14 @@ ALTER TABLE ONLY public.active_storage_variant_records ADD CONSTRAINT fk_rails_993965df05 FOREIGN KEY (blob_id) REFERENCES public.active_storage_blobs(id); +-- +-- Name: shareable_links fk_rails_9985e5ac7c; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.shareable_links + ADD CONSTRAINT fk_rails_9985e5ac7c FOREIGN KEY (team_id) REFERENCES public.teams(id); + + -- -- Name: reports fk_rails_9a0a9c9bec; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -8803,6 +8915,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20230223142119'), ('20230227131215'), ('20230414091215'), -('20230426112548'); +('20230426112548'), +('20230505104830'), +('20230627091027');