Skip to content

Commit

Permalink
#89 Add API key section to user settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Apr 13, 2018
1 parent 3919769 commit 25a3066
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
55 changes: 53 additions & 2 deletions www/src/app/pages/settings/settings.page.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export default class SettingsPageController {
$scope,
$state,
UserService,
ModalService,
NotificationService,
resizeService
resizeService,
clipboard
) {
'ngInject';

Expand All @@ -16,8 +18,10 @@ export default class SettingsPageController {
this.$state = $state;

this.UserService = UserService;
this.ModalService = ModalService;
this.NotificationService = NotificationService;
this.resizeService = resizeService;
this.clipboard = clipboard;

this.basicData = {};
this.passData = {
Expand All @@ -26,6 +30,7 @@ export default class SettingsPageController {
password: '',
passwordConfirm: ''
};
this.keyData = {};
}

$onInit() {
Expand All @@ -41,6 +46,9 @@ export default class SettingsPageController {
this.canChangePass =
this.config.config.capabilities.indexOf('changePassword') !== -1;

this.canChangeKey =
this.config.config.capabilities.indexOf('authByKey') !== -1;

this.$scope.$watch('$ctrl.avatar', value => {
if (!value) {
return;
Expand Down Expand Up @@ -93,7 +101,11 @@ export default class SettingsPageController {
this.passData.password !== '' &&
this.passData.password === this.passData.passwordConfirm
) {
this.UserService.changePass(this.currentUser.id, this.passData.currentPassword, this.passData.password)
this.UserService.changePass(
this.currentUser.id,
this.passData.currentPassword,
this.passData.password
)
.then(() => {
this.NotificationService.log(
'Your password has been successfully updated',
Expand Down Expand Up @@ -134,4 +146,43 @@ export default class SettingsPageController {
form.avatar.$setValidity('maxsize', true);
form.avatar.$setPristine(true);
}

getKey() {
this.UserService.getKey(this.currentUser.id).then(key => {
this.keyData.key = key;
});
}

createKey() {
let modalInstance = this.ModalService.confirm(
'Renew API Key',
'Are you sure you want to renew your API Key?',
{
flavor: 'danger',
okText: 'Yes, renew it'
}
);

modalInstance.result
.then(() => this.UserService.setKey(this.currentUser.id))
.then(() => {
delete this.keyData.key;
this.NotificationService.success(
'API key has been successfully renewed.'
);
})
.catch(err => {
if (!_.isString(err)) {
this.NotificationService.error('Unable to lock the user.');
}
});
}

copyKey() {
this.clipboard.copyText(this.keyData.key);
delete this.keyData.key;
this.NotificationService.success(
'API key has been successfully copied to clipboard.'
);
}
}
30 changes: 30 additions & 0 deletions www/src/app/pages/settings/settings.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,36 @@ <h3 class="box-title">
</div>
</form>

<!-- Update API Key -->
<form ng-if="$ctrl.canChangeKey">
<div class="box box-primary">
<div class="box-header" ng-if="$ctrl.currentUser.hasKey">
<h3 class="box-title">API Key</h3>
</div>
<div class="box-body">
<div class="callout callout-warning mv-0" ng-if="!$ctrl.currentUser.hasKey">
<h4>You don't have any API key.</h4>
<p> Please contact your organization's administrator.</p>
</div>
<div class="form-group" ng-if="$ctrl.currentUser.hasKey">
<p>You have an API key defined. You can renew it if needed.</p>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default" ng-click="$ctrl.createKey()">Renew</span>
<span class="btn btn-primary" ng-click="$ctrl.getKey()" ng-if="!$ctrl.keyData.key">Reveal</span>
</span>
<input class="form-control" readonly ng-model="$ctrl.keyData.key" ng-if="$ctrl.keyData.key">
<span class="input-group-btn" ng-if="$ctrl.keyData.key">
<button class="btn btn-primary" type="button" ng-click="$ctrl.copyKey()">
<i class="fa fa-copy"></i>
</button>
</span>
</div>
</div>
</div>
</div>
</form>

</div>
</div>

Expand Down

0 comments on commit 25a3066

Please sign in to comment.