Skip to content

Commit 79ea683

Browse files
committed
config-ip: Add optional "requireLogin" flag for templates. If set to true, users need to login to view graph.
1 parent e134f82 commit 79ea683

File tree

3 files changed

+82
-13
lines changed

3 files changed

+82
-13
lines changed

build/app/assets/templates/_default.template

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22

33
"_comments": [ "/// NetCreate Template File ///",
4+
"/// IMPORTANT: As of 8/7/2020, the following funcationality is available: ///",
5+
"/// * `"requireLogin": true` will require users to login to view graphs. If the ///",
6+
"/// parameter is `false` or missing, graphs are public, and login is not required ///",
47
"/// IMPORTANT: As of 1/31/2019, the following functionality is available: ///",
58
"/// * `label` parameter for each field may be customized ///",
69
"/// * `hidden` parameter may be used to hide fields ///",
@@ -12,9 +15,11 @@
1215
"/// The rest of the definitions are there as placeholders for future functionality ///"
1316
],
1417

15-
"name": "Tacitus",
18+
"name": "Blank",
1619

17-
"description": "Tacitus with all genders included in a single Person node type",
20+
"description": "This graph left intentionally blank for serving static files.",
21+
22+
"requireLogin": true,
1823

1924
"citationPrompts":{
2025
"citation": "Citation Text",

build/app/unisys/component/SessionShell.jsx

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,28 @@ if (window.NC_DBG) console.log(`inc ${module.id}`);
2626
It then uses Unisys.SetAppState to set "SESSION" to the decoded value.
2727
if a groupId is detected, then it forces a redirect.
2828
29-
TODO: if an invalid URL is entered, should reset
29+
30+
A change in logged in status can come from four places:
31+
32+
1. User has typed in login field.
33+
=> This is handled by `handleChange()`
34+
2. User has hit the "Login" submit button.
35+
=> This is handled by `onSubmit()`
36+
3. User has entered a full URL for the first time
37+
=> This is handled by `componentWillMount()`
38+
4. User has changed an existing URL
39+
=> This does not trigger `compomentWillMount()`
40+
so it needs special handling.
41+
Changing the url does trigger:
42+
* render()
43+
render is triggered because the props for the token
44+
passed by the Route change.
45+
And render does detect the correct login state.
46+
However, if this represents a change in login state,
47+
the change in state needs to be broadcast.
48+
* componentDidUpdate()
49+
componentDidUpdate checks for the change in loggedinIn
50+
status and sends an AppStateChanged event as needed.
3051
3152
\*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ * //////////////////////////////////////*/
3253

@@ -87,17 +108,16 @@ class SessionShell extends UNISYS.Component {
87108
this.renderLoggedIn = this.renderLoggedIn.bind(this);
88109
this.handleChange = this.handleChange.bind(this);
89110
this.onSubmit = this.onSubmit.bind(this);
90-
this.state = {
111+
this.state = { // `state` tracks the login input field
91112
token: null,
92113
classId: null,
93114
projId: null,
94115
hashedId: null,
95116
subId: null,
96117
groupId: null,
97-
subId: null,
98118
isValid: false
99119
};
100-
120+
this.previousIsValid = false; // to track changes in loggedIn status
101121
}
102122

103123
/// ROUTE RENDER FUNCTIONS ////////////////////////////////////////////////////
@@ -211,13 +231,20 @@ class SessionShell extends UNISYS.Component {
211231
// to a valid token string AFTER the changeHandler() detected a valid
212232
// login after a ForceReload. This is a bit hacky and the app would benefit
213233
// from not relying on forced reloads. See handleChange().
234+
//
235+
// This is only called with the initial page load.
236+
// Subsequent changes to the URL (e.g. changing token directly in the url)
237+
// do not result in a second componentWillMount call. These are handled
238+
// by the componentDidUpdate() call.
214239
let token = this.props.match.params.token;
215240
let decoded = SESSION.DecodeToken(token, window.NC_CONFIG.dataset) || {};
216241
this.SetAppState("SESSION", decoded);
242+
this.previousIsValid = decoded.isValid;
217243
}
218244
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219245
/*/ Main Render Function
220-
/*/ render() {
246+
/*/
247+
render() {
221248
// FUN FACTS
222249
// this.state set in constructor
223250
// this.props.history, location, match added by withRouter(AppShell)
@@ -237,9 +264,12 @@ class SessionShell extends UNISYS.Component {
237264
</FormGroup>
238265
);
239266
}
240-
// no token so just render login
267+
241268
let token = this.props.match.params.token;
269+
270+
// no token so just render login
242271
if (!token) return this.renderLogin();
272+
243273
// try to decode token
244274
let decoded = SESSION.DecodeToken(token, window.NC_CONFIG.dataset);
245275
if (decoded.isValid) {
@@ -248,6 +278,22 @@ class SessionShell extends UNISYS.Component {
248278
return this.renderLogin(token);
249279
}
250280
}
281+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
282+
componentDidUpdate(prevProps) {
283+
// SIDE EFFECT
284+
// Check for changes in logged in status and
285+
// trigger AppStateChange if necessary
286+
287+
// Read token from props (because they are not saved in state)
288+
// We have to decode again here because we're using props directly
289+
let token = this.props.match.params.token;
290+
if (!token) return; // don't bother to check if this was a result of changes from the form
291+
let decoded = SESSION.DecodeToken(token, window.NC_CONFIG.dataset);
292+
if (decoded.isValid !== this.previousIsValid) {
293+
this.SetAppState("SESSION", decoded);
294+
this.previousIsValid = decoded.isValid;
295+
}
296+
}
251297

252298
/// EVENT HANDLERS ////////////////////////////////////////////////////////////
253299
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

build/app/view/netcreate/NetCreate.jsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ const NCLOGIC = require('./nc-logic'); // require to bootstrap data loading
5959
super();
6060
UNISYS.ForceReloadOnNavigation();
6161
this.state = {
62-
isConnected: true
62+
isConnected: true,
63+
isLoggedIn: false,
64+
requireLogin: this.AppState('TEMPLATE').requireLogin
6365
};
6466
this.OnDOMReady(()=>{
6567
if (DBG) console.log(PR,'OnDOMReady');
@@ -79,9 +81,17 @@ const NCLOGIC = require('./nc-logic'); // require to bootstrap data loading
7981
this.OnDisconnect(()=>{
8082
this.setState({ isConnected: false });
8183
});
84+
this.onStateChange_SESSION = this.onStateChange_SESSION.bind(this);
85+
this.OnAppStateChange('SESSION',this.onStateChange_SESSION);
8286
}
8387

8488

89+
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90+
/*/ SESSION is called by SessionSHell when the ID changes
91+
Show or hide netgraph depending on template settings.
92+
/*/ onStateChange_SESSION( decoded ) {
93+
this.setState({ isLoggedIn: decoded.isValid });
94+
}
8595

8696

8797
/// REACT LIFECYCLE METHODS ///////////////////////////////////////////////////
@@ -93,9 +103,17 @@ const NCLOGIC = require('./nc-logic'); // require to bootstrap data loading
93103
let dragger = document.getElementById('dragger');
94104
dragger.onmousedown = this.handleMouseDown;
95105
}
106+
107+
componentWillUnmount() {
108+
this.AppStateChangeOff('SESSION',this.onStateChange_SESSION);
109+
}
110+
96111
/// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97112
/*/ Define the component structure of the web application
98113
/*/ render() {
114+
const { isLoggedIn } = this.state;
115+
let hideGraph = false;
116+
if (this.state.requireLogin && !isLoggedIn) hideGraph = true;
99117
return (
100118
<div>
101119
<div hidden={this.state.isConnected} style={{ width:'100%',height:'100%',position:'fixed',backgroundColor:'rgba(0,0,0,0.5',display:'flex',flexDirection:'column',justifyContent:'space-evenly',zIndex:'3000'}}>
@@ -104,13 +122,13 @@ const NCLOGIC = require('./nc-logic'); // require to bootstrap data loading
104122
<p>Please contact your administrator to restart the graph.</p>
105123
</div>
106124
</div>
107-
<div style={{display:'flex', flexFlow:'row nowrap',
125+
<Route path='/edit/:token' exact={true} component={SessionShell}/>
126+
<Route path='/edit' exact={true} component={SessionShell}/>
127+
<Route path='/' exact={true} component={SessionShell}/>
128+
<div hidden={hideGraph} style={{display:'flex', flexFlow:'row nowrap',
108129
width:'100%', height:'100vh',overflow:'hidden'}}>
109130
<div id="left" style={{backgroundColor:'#EEE',flex:'1 1 25%',maxWidth:'400px',padding:'10px',overflow:'scroll',marginTop:'38px'}}>
110131
<div style={{display:'flex',flexFlow:'column nowrap'}}>
111-
<Route path='/edit/:token' exact={true} component={SessionShell}/>
112-
<Route path='/edit' exact={true} component={SessionShell}/>
113-
<Route path='/' exact={true} component={SessionShell}/>
114132
<Search/>
115133
<NodeSelector/>
116134
</div>

0 commit comments

Comments
 (0)