Repository navigation
Expand file tree
/
Copy pathmyminioUserConfig.js
More file actions
172 lines (152 loc) · 5.4 KB
/
Copy pathmyminioUserConfig.js
File metadata and controls
172 lines (152 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Module that controls the My MinIO user settings. Includes Knockout view-model
* for syncing data.
*/
var ko = require('knockout');
var $ = require('jquery');
var Raven = require('raven-js');
var bootbox = require('bootbox');
require('js/osfToggleHeight');
var language = require('js/osfLanguage').Addons.myminio;
var osfHelpers = require('js/osfHelpers');
var addonSettings = require('js/addonSettings');
var ChangeMessageMixin = require('js/changeMessage');
var ExternalAccount = addonSettings.ExternalAccount;
var $modal = $('#myminioInputCredentials');
function ViewModel(url) {
var self = this;
self.properName = 'My MinIO';
self.accessKey = ko.observable();
self.secretKey = ko.observable();
self.account_url = '/api/v1/settings/myminio/accounts/';
self.accounts = ko.observableArray();
ChangeMessageMixin.call(self);
/** Reset all fields from My MinIO credentials input modal */
self.clearModal = function() {
self.message('');
self.messageClass('text-info');
self.accessKey(null);
self.secretKey(null);
};
/** Send POST request to authorize My MinIO */
self.connectAccount = function() {
// Selection should not be empty
if( !self.accessKey() && !self.secretKey() ){
self.changeMessage('Please enter both an API access key and secret key.', 'text-danger');
return;
}
if (!self.accessKey() ){
self.changeMessage('Please enter an API access key.', 'text-danger');
return;
}
if (!self.secretKey() ){
self.changeMessage('Please enter an API secret key.', 'text-danger');
return;
}
return osfHelpers.postJSON(
self.account_url,
ko.toJS({
access_key: self.accessKey,
secret_key: self.secretKey,
})
).done(function() {
self.clearModal();
$modal.modal('hide');
self.updateAccounts();
}).fail(function(xhr, textStatus, error) {
var errorMessage = (xhr.status === 400 && xhr.responseJSON.message !== undefined) ? xhr.responseJSON.message : language.authError;
self.changeMessage(errorMessage, 'text-danger');
Raven.captureMessage('Could not authenticate with My MinIO', {
extra: {
url: self.account_url,
textStatus: textStatus,
error: error
}
});
});
};
self.updateAccounts = function() {
return $.ajax({
url: url,
type: 'GET',
dataType: 'json'
}).done(function (data) {
self.accounts($.map(data.accounts, function(account) {
var externalAccount = new ExternalAccount(account);
externalAccount.accessKey = account.oauth_key;
externalAccount.secretKey = account.oauth_secret;
return externalAccount;
}));
$('#myminio-header').osfToggleHeight({height: 160});
}).fail(function(xhr, status, error) {
self.changeMessage(language.userSettingsError, 'text-danger');
Raven.captureMessage('Error while updating addon account', {
extra: {
url: url,
status: status,
error: error
}
});
});
};
self.askDisconnect = function(account) {
var self = this;
bootbox.confirm({
title: 'Disconnect My MinIO Account?',
message: '<p class="overflow">' +
'Are you sure you want to disconnect the My MinIO account <strong>' +
osfHelpers.htmlEscape(account.name) + '</strong>? This will revoke access to My MinIO for all projects associated with this account.' +
'</p>',
callback: function (confirm) {
if (confirm) {
self.disconnectAccount(account);
}
},
buttons:{
confirm:{
label:'Disconnect',
className:'btn-danger'
}
}
});
};
self.disconnectAccount = function(account) {
var self = this;
var url = '/api/v1/oauth/accounts/' + account.id + '/';
var request = $.ajax({
url: url,
type: 'DELETE'
});
request.done(function(data) {
self.updateAccounts();
});
request.fail(function(xhr, status, error) {
Raven.captureMessage('Error while removing addon authorization for ' + account.id, {
extra: {
url: url,
status: status,
error: error
}
});
});
return request;
};
self.selectionChanged = function() {
self.changeMessage('','');
};
self.updateAccounts();
}
$.extend(ViewModel.prototype, ChangeMessageMixin.prototype);
function MyMinIOUserConfig(selector, url) {
// Initialization code
var self = this;
self.selector = selector;
self.url = url;
// On success, instantiate and bind the ViewModel
self.viewModel = new ViewModel(url);
osfHelpers.applyBindings(self.viewModel, self.selector);
}
module.exports = {
MyMinIOViewModel: ViewModel,
MyMinIOUserConfig: MyMinIOUserConfig
};