Skip to content

Commit

Permalink
Store each profile with a UUID
Browse files Browse the repository at this point in the history
This makes deletes a little safer and prevents us from needing to use
the iteration index for deletes.
  • Loading branch information
bturner-r7 committed Aug 6, 2018
1 parent 728d7a4 commit c5470b1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
17 changes: 17 additions & 0 deletions api/routes/configure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const https = require('https');
const express = require('express');
const uuidv4 = require('uuid/v4');
const xmldom = require('xmldom');
const xpath = require('xpath.js');
const config = require('../config');
Expand All @@ -17,6 +18,21 @@ module.exports = (app, auth) => {
router.get('/', (req, res) => {
const storedMetadataUrls = Storage.get('metadataUrls') || [];

// Migrate metadataUrls to include a profileUuid. This makes
// profile deletes/edits a little safer since they will no longer be
// based on the iteration index.
let migrated = false;

storedMetadataUrls.forEach((metadata) => {
if (metadata.profileUuid === undefined) {
migrated = true;
metadata.profileUuid = uuidv4();
}
});
if (migrated) {
Storage.set('metadataUrls', storedMetadataUrls);
}

// We populate the value of the metadata url field on the following (in order of precedence):
// 1. Use the current session's metadata url (may have been rejected).
// 2. Use the latest validated metadata url.
Expand Down Expand Up @@ -132,6 +148,7 @@ module.exports = (app, auth) => {
profile ? metadataUrls : metadataUrls.concat([
{
name: profileName || metadataUrl,
profileUuid: uuidv4(),
url: metadataUrl,
},
])
Expand Down
7 changes: 4 additions & 3 deletions api/routes/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const router = express.Router();

module.exports = () => {
router.delete('/', (req, res) => {
let {profile} = url.parse(req.url, true).query;
let idx = parseInt(profile, 10);
let {profileUuid} = url.parse(req.url, true).query;
let metadataUrls = Storage.get('metadataUrls');

metadataUrls = metadataUrls.map((metadataUrl, i) => (i !== idx) ? metadataUrl : null).filter((el) => !!el);
metadataUrls = metadataUrls.map((metadata) =>
(metadata.profileUuid !== profileUuid) ? metadata : null
).filter((el) => !!el);
Storage.set('metadataUrls', metadataUrls);

res.status(200).end();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"passport-saml": "^0.33.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"uuid": "^3.3.2",
"xmldom": "^0.1.27",
"xpath.js": "^1.1.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/containers/configure/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoginComponent extends Component {
static propTypes = {
deleteProfile: PropTypes.func.isRequired,
pretty: PropTypes.string,
profileId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
profileUuid: PropTypes.string,
submitConfigure: PropTypes.func.isRequired,
url: PropTypes.string,
};
Expand Down Expand Up @@ -62,7 +62,7 @@ class LoginComponent extends Component {
handleDelete = (event) => {
event.preventDefault();
const payload = {
profile: this.props.profileId,
profileUuid: this.props.profileUuid,
};

this.props.deleteProfile(payload);
Expand Down Expand Up @@ -101,7 +101,7 @@ class LoginComponent extends Component {
</ProfileInputGroup>
</summary>
<InputGroupWithCopyButton
id={this.props.profileId}
id={this.props.profileUuid}
name={this.props.pretty}
value={this.props.url}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/containers/configure/RecentLogins.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ class RecentLogins extends Component {
</SearchContainer>
<ScrollableListGroup>
{
metadataUrls.map(({url, name}, i) =>
metadataUrls.map(({url, name, profileUuid}) =>
(
<Login
key={url}
pretty={name}
profileId={i}
profileUuid={profileUuid}
url={url}
/>
)
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8294,6 +8294,10 @@ uuid@^3.1.0:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"

uuid@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"

validate-npm-package-license@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.3.tgz#81643bcbef1bdfecd4623793dc4648948ba98338"
Expand Down

0 comments on commit c5470b1

Please sign in to comment.