From 65a1d037ffc7b1b4fc650fac7f69b3ec6010a040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20J=C3=A4ggi?= Date: Mon, 19 Jun 2023 18:07:50 +0200 Subject: [PATCH 01/29] start implementation of file creation without credential --- .../api/encryptables_controller.rb | 24 +++++++-- .../app/components/encryptable-file/form.js | 50 +++++++++++++++++++ frontend/app/components/nav-bar.js | 14 ++++-- frontend/app/models/encryptable-file.js | 5 +- .../components/encryptable-file/form.hbs | 31 ++++++++++++ .../templates/components/encryptable/row.hbs | 2 +- .../templates/components/encryptable/show.hbs | 2 +- .../templates/components/file-transfer.hbs | 6 +-- frontend/app/templates/components/nav-bar.hbs | 16 ++++-- frontend/translations/ch_be.yml | 2 +- frontend/translations/de.yml | 2 +- frontend/translations/en.yml | 8 ++- frontend/translations/fr.yml | 2 +- 13 files changed, 143 insertions(+), 21 deletions(-) diff --git a/app/controllers/api/encryptables_controller.rb b/app/controllers/api/encryptables_controller.rb index 1d0e9d980..53d452264 100644 --- a/app/controllers/api/encryptables_controller.rb +++ b/app/controllers/api/encryptables_controller.rb @@ -85,6 +85,10 @@ def file_credential Encryptable::Credentials.find(credential_id) end + def file_folder + Folder.find(folder_id) + end + def fetch_entry model_scope.find(entry_id) end @@ -126,7 +130,7 @@ def permitted_attrs permitted_attrs = self.class.permitted_attrs.deep_dup if model_class == Encryptable::File - permitted_attrs + [:filename, :credentials_id, :file] + permitted_attrs + [:filename, :credentials_id, :folder_id, :file] elsif model_class == Encryptable::Credentials permitted_attrs + [:cleartext_username, :cleartext_password, :cleartext_token, :cleartext_pin, :cleartext_email, :folder_id, @@ -165,25 +169,39 @@ def fetch_encryptable_files def build_encryptable_file filename = params[:file].original_filename - file = new_file(file_credential, params[:description], filename) + file = if credential_id.present? + new_credential_file(file_credential, params[:description], filename) + else + new_file(file_folder, params[:description], filename) + end file.content_type = params[:file].content_type file.cleartext_file = params[:file].read instance_variable_set(:"@#{ivar_name}", file) end - def new_file(parent_encryptable, description, name) + def new_credential_file(parent_encryptable, description, name) Encryptable::File.new(encryptable_credential: parent_encryptable, description: description, name: name) end + def new_file(parent_folder, description, name) + Encryptable::File.new(folder: parent_folder, + description: description, + name: name) + end + def credential_id return params[:id] if params[:id].present? params[:credential_id] end + def folder_id + params[:folder_id] + end + def encrypt(encryptable) if encryptable.folder_id_changed? # if folder id changed recheck team permission diff --git a/frontend/app/components/encryptable-file/form.js b/frontend/app/components/encryptable-file/form.js index 64e25e23c..72705a6d1 100644 --- a/frontend/app/components/encryptable-file/form.js +++ b/frontend/app/components/encryptable-file/form.js @@ -6,12 +6,18 @@ import { action } from "@ember/object"; import { inject as service } from "@ember/service"; import { tracked } from "@glimmer/tracking"; import ENV from "frontend/config/environment"; +import { isPresent, isEmpty } from "@ember/utils"; import { fileUploadValidation } from "../../helpers/file-upload-validation"; export default class Form extends BaseFormComponent { @service store; @service router; @service fileQueue; + @service navService; + + @tracked selectedTeam; + @tracked assignableTeams; + @tracked errors; @@ -29,6 +35,49 @@ export default class Form extends BaseFormComponent { lookupValidator(EncryptableFileValidations), EncryptableFileValidations ); + + this.presetTeamAndFolder(); + + this.store.findAll("team").then((teams) => { + this.assignableTeams = teams; + }); + + this.presetTeamIfFolderSelected(); + + this.changeset.encryptable = this.args.encryptable; + this.changeset.csrfToken = ENV.CSRFToken; + } + + get availableFolders() { + return isPresent(this.selectedTeam) + ? this.store + .peekAll("folder") + .filter( + (folder) => folder.team.get("id") === this.selectedTeam.get("id") + ) + : []; + } + + presetTeamAndFolder() { + let selectedTeam = this.navService.selectedTeam; + let selectedFolder = this.navService.selectedFolder; + + this.selectedTeam = selectedTeam; + if (!isEmpty(selectedFolder)) { + this.changeset.folder = selectedFolder; + } + } + + @action + setSelectedTeam(selectedTeam) { + this.selectedTeam = selectedTeam; + this.setFolder(null); + } + + presetTeamIfFolderSelected() { + if (isPresent(this.changeset.folder)) { + this.selectedTeam = this.changeset.folder.get("team"); + } } @action @@ -48,6 +97,7 @@ export default class Form extends BaseFormComponent { if (isFileValid) { await this.changeset.validate(); this.record.encryptableCredential = this.args.encryptableCredential; + this.record.folder = this.changeset.folder; return this.changeset.isValid; } } diff --git a/frontend/app/components/nav-bar.js b/frontend/app/components/nav-bar.js index c5e587f90..b00293ae2 100644 --- a/frontend/app/components/nav-bar.js +++ b/frontend/app/components/nav-bar.js @@ -13,7 +13,10 @@ export default class NavBarComponent extends Component { searchInterval; @tracked - isNewEncryptable = false; + isNewEncryptableCredential = false; + + @tracked + isNewEncryptableFile = false; @tracked isNewFolder = false; @@ -38,8 +41,13 @@ export default class NavBarComponent extends Component { } @action - toggleEncryptableCreating() { - this.isNewEncryptable = !this.isNewEncryptable; + toggleEncryptableCredentialCreating() { + this.isNewEncryptableCredential = !this.isNewEncryptableCredential; + } + + @action + toggleEncryptableFileCreating() { + this.isNewEncryptableFile = !this.isNewEncryptableFile; } @action diff --git a/frontend/app/models/encryptable-file.js b/frontend/app/models/encryptable-file.js index 63ecc72b2..9fdb71992 100644 --- a/frontend/app/models/encryptable-file.js +++ b/frontend/app/models/encryptable-file.js @@ -8,6 +8,7 @@ export default class EncryptableFile extends Encryptable { inverse: "encryptableFiles" }) encryptableCredential; + @belongsTo("folder") folder; async save() { if (this.isDeleted) { @@ -15,11 +16,13 @@ export default class EncryptableFile extends Encryptable { } const url = `/api/encryptables`; const credentialId = await this.encryptableCredential.get("id"); + const folderId = await this.folder.get("id"); const opts = { data: { description: this.description || "", - credential_id: credentialId + ...(credentialId !== undefined && { credential_id: credentialId }), + ...(folderId !== undefined && { folder_id: folderId }), }, headers: { "X-CSRF-Token": this.csrfToken diff --git a/frontend/app/templates/components/encryptable-file/form.hbs b/frontend/app/templates/components/encryptable-file/form.hbs index e2d61169b..159ef9f3e 100644 --- a/frontend/app/templates/components/encryptable-file/form.hbs +++ b/frontend/app/templates/components/encryptable-file/form.hbs @@ -21,6 +21,37 @@ {{/if}}
+ + {{team.name}} + + + {{folder.name}} + + + {{t (if this.changeset.file "encryptable_files.new.reupload" "encryptable_files.new.upload_file")}} + {{#let (file-queue name="files" onFileAdded=this.uploadFile) as |queue| }}
diff --git a/frontend/app/templates/components/encryptable/show.hbs b/frontend/app/templates/components/encryptable/show.hbs index 2cb1c4e1c..f413e91da 100644 --- a/frontend/app/templates/components/encryptable/show.hbs +++ b/frontend/app/templates/components/encryptable/show.hbs @@ -33,7 +33,7 @@

{{t "encryptable/credentials.show.transferred_at"}}: {{moment-format @encryptable.createdAt "DD.MM.YYYY hh:mm"}}

-

{{t "encryptable/transferring/file.show.sender_name"}}: {{@encryptable.sender_name}}

+

{{t "encryptable/file.show.sender_name"}}: {{@encryptable.sender_name}}

diff --git a/frontend/app/templates/components/file-transfer.hbs b/frontend/app/templates/components/file-transfer.hbs index d187335bd..1a94924bb 100644 --- a/frontend/app/templates/components/file-transfer.hbs +++ b/frontend/app/templates/components/file-transfer.hbs @@ -1,12 +1,12 @@ - +
-
{{t "encryptable/transferring/file.description_for_sender"}}
+
{{t "encryptable/file.description_for_sender"}}
@@ -58,7 +58,7 @@
- +