3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import SshKeyActions
from './sshKeyActions.js';
19 import SshKeySource
from './sshKeySource.js';
20 import GUID
from 'utils/guid.js';
21 import AppHeaderActions
from 'widgets/header/headerActions.js';
22 import Alt
from '../alt';
23 import _
from 'lodash';
26 export default class SshKeyStore
{
32 this.dataCache
= _
.cloneDeep(this.data
);
37 this.bindActions(SshKeyActions
);
38 this.registerAsync(SshKeySource
);
39 this.exportPublicMethods({
40 updateNewKeyValue
: this.updateNewKeyValue
,
41 updateSshKeyPair
: this.updateSshKeyPair
,
42 editSshKeyPair
: this.editSshKeyPair
,
43 cancelEditSshKeyPair
: this.cancelEditSshKeyPair
,
44 saveEditSshKeyPair
: this.saveEditSshKeyPair
,
45 deleteSshKeyPair
: this.deleteSshKeyPair
,
46 updateEditSshKeyPair
: this.updateEditSshKeyPair
49 updateNewKeyValue
= (k
, field
) => {
52 let value
= e
.target
.value
;
53 let ref
= self
.newKey
;
55 self
.setState({newKey
: ref
});
58 editSshKeyPair
= (k
) => {
62 let ref
= data
.entities
[k
];
63 ref
.isEditable
= true;
64 self
.setState({data
:data
});
67 cancelEditSshKeyPair
= (k
) => {
70 let data
= _
.cloneDeep(self
.data
);
71 data
.entities
[k
].key
= self
.dataCache
.entities
[k
].key
;
72 data
.entities
[k
].isEditable
= false;
73 self
.setState({data
:data
});
76 saveEditSshKeyPair
= (data
) => {
79 if(self
.validate(data
)) {
80 self
.getInstance().saveSshKey(self
.trimName(data
)).then(function() {})
82 self
.alt
.actions
.global
.showNotification('Make sure all fields are filled in and that you are using only alphanumeric characters for the name');
86 updateEditSshKeyPair
= (k
) => {
89 self
.getInstance().updateSshKey(self
.cleanUpKeyPayload(self
.data
.entities
[k
]))
92 deleteSshKeyPair
= (k
) => {
95 if(window
.confirm('Are you sure you want to delete this key?')) {
96 self
.getInstance().deleteSshKey(k
);
100 saveSshKeySuccess
= (data
) => {
101 let keys
= this.data
;
102 keys
.keys
.push(data
.name
);
103 keys
.entities
[data
.name
] = {
109 dataCache
: _
.cloneDeep(keys
),
117 updateSshKeySuccess
= (data
) => {
118 let keys
= this.data
;
119 keys
.entities
[data
.name
] = {
125 dataCache
: _
.cloneDeep(keys
),
133 deleteSshKeySuccess
= (data
) => {
134 let keys
= this.data
;
135 keys
.keys
.splice(keys
.keys
.indexOf(data
.name
), 1);
136 delete keys
.entities
[data
.name
];
138 dataCache
: _
.cloneDeep(keys
),
142 saveEditSshKeyPairSuccess
= () => {
145 getSshKeySuccess
= (data
) => {
146 let flattened
= this.flattenKeys(data
);
149 dataCache
: _
.cloneDeep(flattened
)
152 updateSshKeyPair
= (k
, field
) => {
155 let value
= e
.target
.value
;
156 let data
= self
.data
;
157 let ref
= data
.entities
[k
];
159 self
.setState({data
:data
});
167 data
&& data
.map(function(d
){
168 fd
.keys
.push(d
.name
);
169 fd
.entities
[d
.name
] = _
.merge({isEditable
: false}, d
)
173 cleanUpKeyPayload(payload
) {
174 return {key
: payload
.key
, name
: payload
.name
};
177 if((data
['name'].trim() == '') || ((/[^\w _-]/).test(data
['name']))) {
183 data
.name
= data
.name
.trim();