Skip to content

Commit 1d6099a

Browse files
committed
Adding IP address translation for UI presentation (based on translation table from UI config).
1 parent b63d725 commit 1d6099a

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

etc/env.json.example

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@
1313
"cs": "Univerzitní login",
1414
"en": "University login"
1515
},
16-
"EXTERNAL_AUTH_HELPDESK_URL": "mailto:cas@some.other.domain"
16+
"EXTERNAL_AUTH_HELPDESK_URL": "mailto:cas@some.other.domain",
17+
"KNOWN_IPS": {
18+
"127.0.0.1": "localhost",
19+
"192.168.1.1": {
20+
"cs": "Cože? Privátní adresa?",
21+
"en": "What? A private IP?"
22+
}
23+
}
1724
}

src/components/Groups/LockedStudentsTable/LockedStudentsTable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Table } from 'react-bootstrap';
55
import { lruMemoize } from 'reselect';
66

77
import UsersName from '../../Users/UsersName';
8+
import IpAddress from '../../widgets/IpAddress/IpAddress';
89
import ExamUnlockButtonContainer from '../../../containers/ExamUnlockButtonContainer';
910
import { createUserNameComparator } from '../../helpers/users.js';
1011

@@ -28,7 +29,7 @@ const LockedStudentsTable = ({ groupId, lockedStudents, currentUser, intl: { loc
2829
listItem
2930
/>
3031
</td>
31-
<td>{student.privateData?.ipLock && <code>{student.privateData.ipLock}</code>}</td>
32+
<td>{student.privateData?.ipLock && <IpAddress ip={student.privateData.ipLock} />}</td>
3233
<td className="text-end">
3334
<ExamUnlockButtonContainer groupId={groupId} userId={student.id} size="xs" />
3435
</td>

src/components/Groups/LocksTable/LocksTable.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { lruMemoize } from 'reselect';
66

77
import UsersNameContainer from '../../../containers/UsersNameContainer';
88
import DateTime from '../../widgets/DateTime';
9+
import IpAddress from '../../widgets/IpAddress/IpAddress';
910
import { LockIcon, UnlockIcon } from '../../icons';
1011

1112
const sortLocks = lruMemoize(locks => [...locks].sort(({ createdAt: c1 }, { createdAt: c2 }) => c1 - c2));
@@ -31,9 +32,7 @@ const LocksTable = ({ locks }) =>
3132
</>
3233
)}
3334
</td>
34-
<td className="shrink-col text-nowrap">
35-
<code>{lock.remoteAddr}</code>
36-
</td>
35+
<td className="shrink-col text-nowrap">{lock.remoteAddr && <IpAddress ip={lock.remoteAddr} />}</td>
3736
</tr>
3837
))}
3938
</tbody>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { injectIntl } from 'react-intl';
4+
import { OverlayTrigger, Tooltip } from 'react-bootstrap';
5+
import Icon from '../../icons/Icon.js';
6+
import { getConfigVar } from '../../../helpers/config.js';
7+
8+
const KNOWN_IPS = getConfigVar('KNOWN_IPS', {});
9+
10+
const getIpLabel = (ip, locale) => {
11+
if (typeof KNOWN_IPS[ip] === 'object') {
12+
if (locale in KNOWN_IPS[ip]) {
13+
return KNOWN_IPS[ip][locale];
14+
}
15+
if ('en' in KNOWN_IPS[ip]) {
16+
return KNOWN_IPS[ip].en;
17+
}
18+
const keys = Object.keys(KNOWN_IPS[ip]);
19+
if (keys.length > 0) {
20+
return KNOWN_IPS[ip][keys[0]];
21+
}
22+
}
23+
return KNOWN_IPS[ip];
24+
};
25+
26+
// Inset panel replaces old <Well> component from bootstrap 3
27+
const IpAddress = ({ ip, intl: { locale }, ...props }) =>
28+
KNOWN_IPS[ip] ? (
29+
<OverlayTrigger
30+
placement="bottom"
31+
overlay={
32+
<Tooltip id={ip}>
33+
<code>{ip}</code>
34+
</Tooltip>
35+
}>
36+
<span>
37+
<Icon icon="location-crosshairs" gapRight={1} className="opacity-50" /> {getIpLabel(ip, locale)}
38+
</span>
39+
</OverlayTrigger>
40+
) : (
41+
<code {...props}>{ip}</code>
42+
);
43+
44+
IpAddress.propTypes = {
45+
ip: PropTypes.string.isRequired,
46+
intl: PropTypes.object.isRequired,
47+
};
48+
49+
export default injectIntl(IpAddress);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './IpAddress.js';

0 commit comments

Comments
 (0)