snipe-it/resources/assets/js/components/passport/PersonalAccessTokens.vue

302 lines
10 KiB
Vue
Raw Normal View History

2016-12-14 10:06:05 -08:00
<style scoped>
.action-link {
cursor: pointer;
}
.m-b-none {
margin-bottom: 0;
}
</style>
<template>
<div>
<div>
<div class="panel panel-default">
<div class="panel-heading">
2020-10-09 16:02:30 -07:00
<div class="text-right" style="display: flex; justify-content: space-between; align-items: center;">
2016-12-14 10:06:05 -08:00
2020-10-09 16:02:30 -07:00
<a class="btn btn-info btn-sm action-link pull-right" @click="showCreateTokenForm">
2016-12-14 10:06:05 -08:00
Create New Token
</a>
</div>
</div>
<div class="panel-body">
<!-- No Tokens Notice -->
<p class="m-b-none" v-if="tokens.length === 0">
You have not created any personal access tokens.
</p>
<!-- Personal Access Tokens -->
<table class="table table-borderless m-b-none" v-if="tokens.length > 0">
<thead>
<tr>
2020-10-09 16:02:30 -07:00
<th class="col-md-9">Name</th>
<th class="col-md-3"><span class="sr-only">Delete</span></th>
2016-12-14 10:06:05 -08:00
</tr>
</thead>
<tbody>
<tr v-for="token in tokens">
<!-- Client Name -->
<td style="vertical-align: middle;">
{{ token.name }}
</td>
<!-- Delete Button -->
2020-10-09 16:02:30 -07:00
<td style="vertical-align: middle;" class="text-right">
<a class="action-link btn btn-danger btn-sm" @click="revoke(token)">
<i class="fa fa-trash"></i>
2016-12-14 10:06:05 -08:00
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- Create Token Modal -->
<div class="modal fade" id="modal-create-token" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button " class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">
2016-12-14 10:06:05 -08:00
Create Token
</h4>
</div>
<div class="modal-body">
<!-- Form Errors -->
<div class="alert alert-danger" v-if="form.errors.length > 0">
<p><strong>Whoops!</strong> Something went wrong!</p>
<br>
<ul>
<li v-for="error in form.errors">
{{ error }}
</li>
</ul>
</div>
<!-- Create Token Form -->
<form class="form-horizontal" role="form" @submit.prevent="store">
<!-- Name -->
<div class="form-group">
2020-04-01 03:51:08 -07:00
<label class="col-md-4 control-label" for="name">Name</label>
2016-12-14 10:06:05 -08:00
<div class="col-md-6">
2020-04-01 03:51:08 -07:00
<input id="create-token-name" type="text" aria-label="name" class="form-control" name="name" v-model="form.name">
2016-12-14 10:06:05 -08:00
</div>
</div>
<!-- Scopes -->
<div class="form-group" v-if="scopes.length > 0">
<label class="col-md-4 control-label">Scopes</label>
<div class="col-md-6">
<div v-for="scope in scopes">
<div class="checkbox">
<label>
<input type="checkbox"
@click="toggleScope(scope.id)"
:checked="scopeIsAssigned(scope.id)">
{{ scope.id }}
</label>
</div>
</div>
</div>
</div>
</form>
</div>
<!-- Modal Actions -->
<div class="modal-footer">
2020-04-01 03:51:08 -07:00
<button type="button" class="btn primary" data-dismiss="modal">Close</button>
2016-12-14 10:06:05 -08:00
<button type="button" class="btn btn-primary" @click="store">
Create
</button>
</div>
</div>
</div>
</div>
<!-- Access Token Modal -->
<div class="modal fade" id="modal-access-token" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button " class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">
2016-12-14 10:06:05 -08:00
Personal Access Token
</h4>
</div>
<div class="modal-body">
<p>
Here is your new personal access token. This is the only time it will be shown so don't lose it!
You may now use this token to make API requests.
</p>
<pre><code>{{ accessToken }}</code></pre>
</div>
<!-- Modal Actions -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: ['tokenUrl', 'scopesUrl'],
2016-12-14 10:06:05 -08:00
/*
* The component's data.
*/
data() {
return {
accessToken: null,
tokens: [],
scopes: [],
form: {
name: '',
scopes: [],
errors: []
}
};
},
/**
* Prepare the component (Vue 1.x).
*/
ready() {
this.prepareComponent();
},
/**
* Prepare the component (Vue 2.x).
*/
mounted() {
this.prepareComponent();
},
methods: {
/**
* Prepare the component.
*/
prepareComponent() {
this.getTokens();
this.getScopes();
$('#modal-create-token').on('shown.bs.modal', () => {
$('#create-token-name').focus();
});
},
/**
* Get all of the personal access tokens for the user.
*/
getTokens() {
this.$http.get(this.tokenUrl)
2016-12-14 10:06:05 -08:00
.then(response => {
this.tokens = response.data;
});
},
/**
* Get all of the available scopes.
*/
getScopes() {
this.$http.get(this.scopesUrl)
2016-12-14 10:06:05 -08:00
.then(response => {
this.scopes = response.data;
});
},
/**
* Show the form for creating new tokens.
*/
showCreateTokenForm() {
$('#modal-create-token').modal('show');
},
/**
* Create a new personal access token.
*/
store() {
this.accessToken = null;
this.form.errors = [];
this.$http.post(this.tokenUrl, this.form)
2016-12-14 10:06:05 -08:00
.then(response => {
this.form.name = '';
this.form.scopes = [];
this.form.errors = [];
this.tokens.push(response.data.token);
this.showAccessToken(response.data.accessToken);
})
.catch(response => {
if (typeof response.data === 'object') {
this.form.errors = _.flatten(_.toArray(response.data));
} else {
2017-01-11 08:44:34 -08:00
console.dir(this.form);
2016-12-14 10:06:05 -08:00
this.form.errors = ['Something went wrong. Please try again.'];
}
});
},
/**
* Toggle the given scope in the list of assigned scopes.
*/
toggleScope(scope) {
if (this.scopeIsAssigned(scope)) {
this.form.scopes = _.reject(this.form.scopes, s => s == scope);
} else {
this.form.scopes.push(scope);
}
},
/**
* Determine if the given scope has been assigned to the token.
*/
scopeIsAssigned(scope) {
return _.indexOf(this.form.scopes, scope) >= 0;
},
/**
* Show the given access token to the user.
*/
showAccessToken(accessToken) {
$('#modal-create-token').modal('hide');
this.accessToken = accessToken;
$('#modal-access-token').modal('show');
},
/**
* Revoke the given token.
*/
revoke(token) {
this.$http.delete(this.tokenUrl +'/'+ token.id)
2016-12-14 10:06:05 -08:00
.then(response => {
this.getTokens();
});
}
}
}
</script>