Skip to content

Commit

Permalink
Merge pull request #727 from puzzle/feature/712/file-without-credentials
Browse files Browse the repository at this point in the history
Feature/712/file without credentials
  • Loading branch information
TheWalkingLeek authored Jul 28, 2023
2 parents a9263f6 + f91b791 commit f2149de
Show file tree
Hide file tree
Showing 35 changed files with 560 additions and 137 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

![logo](https://raw.githubusercontent.com/puzzle/cryptopus/57f8ad8de410e4a0ba16227620727787f22c7d1c/frontend/public/assets/images/cryptopussy.svg)

[![Cryptopus CI build](https://github.com/puzzle/cryptopus/actions/workflows/build.yml/badge.svg)](https://github.com/puzzle/cryptopus/actions/workflows/build.yml)
Expand Down
27 changes: 13 additions & 14 deletions app/controllers/api/encryptables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Api::EncryptablesController < ApiController

self.permitted_attrs = [:name, :description, :file]
self.permitted_attrs = [:name, :description, :file, :folder_id]

helper_method :team

Expand Down Expand Up @@ -68,7 +68,7 @@ def model_class
end

def define_model_class
if credential_id.present?
if credential_id.present? || params[:folder_id].present?
Encryptable::File
else
Encryptable::Credentials
Expand All @@ -81,10 +81,14 @@ def build_entry
super
end

def file_credential
def credential
Encryptable::Credentials.find(credential_id)
end

def folder
Folder.find(params[:folder_id])
end

def fetch_entry
model_scope.find(entry_id)
end
Expand Down Expand Up @@ -126,10 +130,10 @@ 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]
elsif model_class == Encryptable::Credentials
permitted_attrs + [:cleartext_username, :cleartext_password, :cleartext_token,
:cleartext_pin, :cleartext_email, :folder_id,
:cleartext_pin, :cleartext_email,
:cleartext_custom_attr, :cleartext_custom_attr_label]
else
[]
Expand Down Expand Up @@ -165,23 +169,18 @@ def fetch_encryptable_files
def build_encryptable_file
filename = params[:file].original_filename

file = new_file(file_credential, params[:description], filename)
file = Encryptable::File.new(description: params[:description], name: filename)
file.encryptable_credential = credential if credential_id.present?
file.folder = folder if credential_id.blank?
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)
Encryptable::File.new(encryptable_credential: parent_encryptable,
description: description,
name: name)
end

def credential_id
return params[:id] if params[:id].present?

params[:credential_id]
nil_param?(params[:credential_id])
end

def encrypt(encryptable)
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/concerns/param_converters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ module ParamConverters
def true?(value)
%w[1 yes true].include?(value.to_s.downcase)
end

def nil_param?(value)
value == 'null' ? nil : value
end
end
1 change: 1 addition & 0 deletions app/models/encryptable/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Encryptable::File < Encryptable
foreign_key: :credential_id

validates :name, uniqueness: { scope: :credential_id }, if: :credential_id
validates :name, uniqueness: { scope: :folder_id }, if: :folder_id

validate :file_size, on: [:create, :update]

Expand Down
4 changes: 2 additions & 2 deletions app/serializers/encryptable/file_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

class Encryptable::FileSerializer < ApplicationSerializer
attributes :id, :name, :description, :sender_name
attributes :id, :name, :description, :sender_name, :created_at

def sender_name
object.sender&.label
end

belongs_to :encryptable_credential
belongs_to :encryptable_credential, if: -> { object.encryptable_credential.present? }
end
2 changes: 1 addition & 1 deletion config/locales/ch_be.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ch_be:
encryptable/file:
attributes:
name:
taken: Datei isch schomau ufägladä wordä
taken: Dateiname isch schomau verwändet worde

#Pundit
pundit:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ de:
encryptable/file:
attributes:
name:
taken: Datei ist schon hochgeladen worden
taken: Dateiname wurde bereits verwendet

#Pundit
pundit:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ en:
encryptable/file:
attributes:
name:
taken: File has already been taken
taken: File name has already been taken

#Pundit
pundit:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fr:
encryptable/file:
attributes:
name:
taken: Le fichier a déjà été pris
taken: Le nom du fichier a déjà été utilisé

#Pundit
pundit:
Expand Down
91 changes: 79 additions & 12 deletions frontend/app/components/encryptable-file/form.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,93 @@
import BaseFormComponent from "../base-form-component";
import EncryptableFileValidations from "../../validations/encryptable-file";
import {
credentialsAttachment,
encryptableFile
} from "../../validations/encryptable-file";
import lookupValidator from "ember-changeset-validations";
import Changeset from "ember-changeset";
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 errors;
@tracked assignableTeams;

EncryptableFileValidations = EncryptableFileValidations;
@tracked errors;

constructor() {
super(...arguments);

const FileValidation = this.args.attachment
? credentialsAttachment
: encryptableFile;
this.record = this.store.createRecord("encryptable-file");
this.record.encryptable = this.args.encryptable;
this.record.csrfToken = ENV.CSRFToken;

this.changeset = new Changeset(
this.record,
lookupValidator(EncryptableFileValidations),
EncryptableFileValidations
lookupValidator(FileValidation),
FileValidation
);

this.store.findAll("team").then((teams) => {
this.assignableTeams = teams;
});

this.changeset.csrfToken = ENV.CSRFToken;
}

@action
abort() {
if (this.args.onAbort) {
setDefaults() {
this.presetTeamAndFolder();
this.changeset.validate();
}

get availableFolders() {
return isPresent(this.changeset.team)
? this.store
.peekAll("folder")
.filter(
(folder) => folder.team.get("id") === this.changeset.team.get("id")
)
: [];
}

presetTeamAndFolder() {
let selectedFolder = this.args.folder || this.navService.selectedFolder;
let selectedTeam =
selectedFolder?.get("team") || this.navService.selectedTeam;

if (!isEmpty(selectedTeam)) {
this.changeset.set("team", selectedTeam);
}
if (!isEmpty(selectedFolder)) {
this.changeset.set("folder", selectedFolder);
}
}

@action
setSelectedTeam(selectedTeam) {
this.changeset.set("team", selectedTeam);
this.setSelectedFolder(null);
}

@action
setSelectedFolder(selectedFolder) {
this.changeset.folder = selectedFolder;
this.changeset.set("folder", selectedFolder);
}

@action
abort(byButton) {
if (this.args.onAbort && byButton) {
this.args.onAbort();
}
}
Expand All @@ -46,19 +100,32 @@ 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;
}
return false;
}

showSuccessMessage() {
let msg = this.intl.t("flashes.encryptable_files.uploaded");
this.notify.success(msg);
}

handleSubmitSuccess() {
this.abort();
handleSubmitSuccess(savedRecords) {
this.abort(true);
if (!this.args.attachment) {
this.saveEditedData(savedRecords);
}
}

saveEditedData(savedRecords) {
if (isPresent(savedRecords)) {
savedRecords[0]
.json()
.then((body) =>
this.router.transitionTo("encryptables.show", body.data.id)
);
}
}

handleSubmitError(response) {
Expand Down
3 changes: 0 additions & 3 deletions frontend/app/components/encryptable/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export default class ShowComponent extends Component {
@tracked
isFileCreating = false;

@tracked
isFile = this.args.encryptable.isFile;

@tracked
isCredentialSharing = false;

Expand Down
15 changes: 11 additions & 4 deletions frontend/app/components/folder/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export default class ShowComponent extends Component {
isFolderEditing = false;

@tracked
isNewEncryptable = false;
isNewEncryptableCredential = false;

@tracked
isNewEncryptableFile = false;

get shouldRenderEncryptables() {
return !isEmpty(this.args.folder.encryptables) && !this.isCollapsed;
Expand All @@ -33,9 +36,13 @@ export default class ShowComponent extends Component {
}

@action
toggleEncryptableCreating() {
this.navService.setSelectedFolderById(this.args.folder.id);
this.isNewEncryptable = !this.isNewEncryptable;
toggleEncryptableCredentialCreating() {
this.isNewEncryptableCredential = !this.isNewEncryptableCredential;
}

@action
toggleEncryptableFileCreating() {
this.isNewEncryptableFile = !this.isNewEncryptableFile;
}

expandSelectedFolder() {
Expand Down
14 changes: 11 additions & 3 deletions frontend/app/components/nav-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export default class NavBarComponent extends Component {
searchInterval;

@tracked
isNewEncryptable = false;
isNewEncryptableCredential = false;

@tracked
isNewEncryptableFile = false;

@tracked
isNewFolder = false;
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions frontend/app/helpers/file-upload-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { helper } from "@ember/component/helper";

const TenMB = 10000000;
export function fileUploadValidation(file, intl, notify) {
if (file.size > TenMB) {
if (file?.size > TenMB) {
let msg = intl.t("flashes.encryptable_files.uploaded_size_to_high");
notify.error(msg);
return false;
} else if (file.size === 0) {
} else if (file?.size === 0) {
let msg = intl.t("flashes.encryptable_files.uploaded_file_blank");
notify.error(msg);
return false;
} else if (file.name === "") {
} else if (file?.name === "") {
let msg = intl.t("flashes.encryptable_files.uploaded_filename_is_empty");
notify.error(msg);
return false;
Expand Down
Loading

0 comments on commit f2149de

Please sign in to comment.