[go: nahoru, domu]

Skip to content

Commit

Permalink
ui: create component to add public key
Browse files Browse the repository at this point in the history
This commit is part of the future refactoring of the public edit. Because in
this other, only the editing part will remain.

Signed-off-by: Leonardo R.S. Joao <leonardo.joao@ossystems.com.br>
  • Loading branch information
leonardojoao authored and gustavosbarreto committed Feb 11, 2022
1 parent 024a938 commit d5e43eb
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 14 deletions.
7 changes: 3 additions & 4 deletions ui/src/components/box/BoxMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@
v-else-if="typeMessage == 'publicKey'"
@click="publicKeyCreateShow = !publicKeyCreateShow"
>
<PublicKeyCreate
:create-key="true"
<PublicKeyFormDialogAdd
:show.sync="publicKeyCreateShow"
@update="refreshPublicKey"
/>
Expand All @@ -83,15 +82,15 @@
import DeviceAdd from '@/components/device/DeviceAdd';
import FirewallRuleFormDialogAdd from '@/components/firewall_rule/FirewallRuleFormDialogAdd';
import PublicKeyCreate from '@/components/public_key/KeyFormDialog';
import PublicKeyFormDialogAdd from '@/components/public_key/PublicKeyFormDialogAdd';
export default {
name: 'BoxMessageComponent',
components: {
DeviceAdd,
FirewallRuleFormDialogAdd,
PublicKeyCreate,
PublicKeyFormDialogAdd,
},
props: {
Expand Down
10 changes: 4 additions & 6 deletions ui/src/components/public_key/PublicKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
<v-spacer />

<span @click="publicKeyCreateShow = !publicKeyCreateShow">
<PublicKeyCreate
:create-key="true"
:show.sync="publicKeyCreateShow"
data-test="publicKeyCreate-component"
<PublicKeyFormDialogAdd
data-test="publicKeyFormDialogAdd-component"
@update="refresh"
/>
</span>
Expand All @@ -32,14 +30,14 @@

<script>
import PublicKeyCreate from '@/components/public_key/KeyFormDialog';
import PublicKeyFormDialogAdd from '@/components/public_key/PublicKeyFormDialogAdd';
import BoxMessagePublicKey from '@/components/box/BoxMessage';
export default {
name: 'PublickeyComponent',
components: {
PublicKeyCreate,
PublicKeyFormDialogAdd,
BoxMessagePublicKey,
},
Expand Down
212 changes: 212 additions & 0 deletions ui/src/components/public_key/PublicKeyFormDialogAdd.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<template>
<fragment>
<v-tooltip
bottom
:disabled="hasAuthorization"
>
<template #activator="{ on }">
<div v-on="on">
<v-btn
:disabled="!hasAuthorization"
color="primary"
data-test="createKey-btn"
@click="dialog = !dialog"
v-text="'Add Public Key'"
/>
</div>
</template>

<span>
You don't have this kind of authorization.
</span>
</v-tooltip>

<v-dialog
v-model="dialog"
max-width="400"
@click:outside="close"
>
<v-card data-test="publicKeyFormDialog-card">
<v-card-title
class="headline primary"
data-test="text-title"
v-text="'New Public Key'"
/>

<ValidationObserver
ref="obs"
v-slot="{ passes }"
>
<v-card-text>
<ValidationProvider
v-slot="{ errors }"
ref="providerName"
vid="name"
name="Name"
rules="required"
>
<v-text-field
v-model="publicKey.name"
label="Name"
:error-messages="errors"
required
data-test="name-field"
/>
</ValidationProvider>

<ValidationProvider
v-slot="{ errors }"
name="Hostname"
>
<v-text-field
v-model="publicKey.hostname"
label="Hostname"
:error-messages="errors"
data-test="hostname-field"
/>
</ValidationProvider>

<ValidationProvider
v-slot="{ errors }"
name="Username"
data-test="username-validationProvider"
>
<v-text-field
v-model="publicKey.username"
label="Username"
:error-messages="errors"
data-test="username-field"
/>
</ValidationProvider>

<ValidationProvider
v-slot="{ errors }"
ref="providerData"
vid="key"
name="Data"
:rules="`required|parseKey:${action}`"
>
<v-textarea
v-model="publicKey.data"
label="Data"
:error-messages="errors"
required
:messages="supportedKeys"
data-test="data-field"
/>
</ValidationProvider>
</v-card-text>

<v-card-actions>
<v-spacer />

<v-btn
text
data-test="cancel-btn"
@click="close"
v-text="'Cancel'"
/>

<v-btn
text
data-test="create-btn"
@click="passes(create)"
v-text="'Create'"
/>
</v-card-actions>
</ValidationObserver>
</v-card>
</v-dialog>
</fragment>
</template>

<script>
import {
ValidationObserver,
ValidationProvider,
} from 'vee-validate';
import hasPermission from '@/components/filter/permission';
export default {
name: 'PublickKeyFormDialogAdd',
filters: { hasPermission },
components: {
ValidationProvider,
ValidationObserver,
},
data() {
return {
dialog: false,
action: 'create',
publicKey: {
name: '',
hostname: '',
username: '',
data: '',
},
supportedKeys: 'Supports RSA, DSA, ECDSA (nistp-*) and ED25519 key types, in PEM (PKCS#1, PKCS#8) and OpenSSH formats.',
};
},
computed: {
hasAuthorization() {
const role = this.$store.getters['auth/role'];
if (role !== '') {
return hasPermission(
this.$authorizer.role[role],
this.$actions.publicKey[this.action],
);
}
return false;
},
},
async updated() {
await this.setLocalVariable();
},
methods: {
setLocalVariable() {
this.publicKey.name = '';
this.publicKey.hostname = '';
this.publicKey.username = '';
this.publicKey.data = '';
},
async create() {
try {
const keySend = this.publicKey;
keySend.data = btoa(this.publicKey.data);
await this.$store.dispatch('publickeys/post', keySend);
this.$store.dispatch('snackbar/showSnackbarSuccessAction', this.$success.publicKeyCreating);
this.update();
} catch (error) {
if (error.response.status === 409) {
this.$refs.obs.setErrors({
key: error.response.data.message,
});
} else {
this.$store.dispatch('snackbar/showSnackbarErrorAction', this.$errors.snackbar.publicKeyCreating);
}
}
},
update() {
this.$emit('update');
this.close();
},
close() {
this.dialog = false;
this.$refs.obs.reset();
},
},
};
</script>
4 changes: 2 additions & 2 deletions ui/tests/unit/components/public_key/PublicKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('PublicKey', () => {
//////

it('Renders the template with components', () => {
expect(wrapper.find('[data-test="publicKeyCreate-component"]').exists()).toBe(true);
expect(wrapper.find('[data-test="publicKeyFormDialogAdd-component"]').exists()).toBe(true);
expect(wrapper.find('[data-test="boxMessagePublicKey-component"]').exists()).toBe(true);
});
});
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('PublicKey', () => {
//////

it('Renders the template with components', () => {
expect(wrapper.find('[data-test="publicKeyCreate-component"]').exists()).toBe(true);
expect(wrapper.find('[data-test="publicKeyFormDialogAdd-component"]').exists()).toBe(true);
expect(wrapper.find('[data-test="boxMessagePublicKey-component"]').exists()).toBe(false);
});
});
Expand Down
Loading

0 comments on commit d5e43eb

Please sign in to comment.