Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
EZP-29286: As a Platform UI User, I would like my session to be exten…
Browse files Browse the repository at this point in the history
…ded while I still use Platform UI (#982)

* EZP-29286: As a Platform UI User, I would like my session to be extended while I still use Platform UI
* Fixed JS unit tests for ez-viewservice
  • Loading branch information
sunpietro authored and andrerom committed Dec 18, 2018
1 parent 307e869 commit eb973ed
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Controller/PlatformUIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function shellAction(Request $request)

return $this->render(
'eZPlatformUIBundle:PlatformUI:shell.html.twig',
['parameters' => $this->configAggregator->getConfig()]
[
'parameters' => $this->configAggregator->getConfig(),
'rest_session_time' => ini_get('session.gc_maxlifetime'),
]
);
}

Expand Down
16 changes: 16 additions & 0 deletions Resources/public/js/apps/ez-platformuiapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ YUI.add('ez-platformuiapp', function (Y) {
if (oldService && newService) {
oldService.setNextViewServiceParameters(newService);
}

if (oldService) {
oldService.killSessionRenewal();
}
});

this.after('*:navigateTo', function (e) {
Expand Down Expand Up @@ -1241,6 +1245,18 @@ YUI.add('ez-platformuiapp', function (Y) {
readOnly: true,
value: true,
},

/**
* Information about REST session time
*
* @attribute restSessionTime
* @default 0
* @type {Number}
*/
restSessionTime: {
writeOnce: 'initOnly',
value: 0
}
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ YUI.add('ez-contentcreateviewservice', function (Y) {
Y.eZ.ContentCreateViewService = Y.Base.create('contentCreateViewService', Y.eZ.ContentEditViewService, [], {
initializer: function () {
this.on('*:changeLanguage', this._selectLanguage);
this.after('contentEditView:activeChange', this._refreshSession);
},

_load: function (next) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ YUI.add('ez-contenteditviewservice', function (Y) {
this.after('*:closeView', this._redirectAfterClose);
this.after('discardedDraft', this._redirectAfterDiscard);
this.after('publishedDraft', this._redirectAfterPublish);
this.after('contentEditView:activeChange', this._refreshSession);

this._setLanguageCode();
this._setBaseLanguageCode();
Expand Down
67 changes: 67 additions & 0 deletions Resources/public/js/views/services/ez-viewservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ YUI.add('ez-viewservice', function (Y) {
* @extends Base
*/
Y.eZ.ViewService = Y.Base.create('viewService', Y.Base, [], {
initializer: function () {
this._restSessionRenewalInterval = null;
},

/**
* Triggers the error event when the message parameter in the event
* facade
Expand Down Expand Up @@ -158,6 +162,69 @@ YUI.add('ez-viewservice', function (Y) {
*/
_getViewParameters: function () {
return {};
},

/**
* Refreshes a user session in interval
*
* @method _refreshSession
* @private
*/
_refreshSession: function (event) {
if (!event.newVal) {
return;
}

// `restSessionTime` is set in seconds, but the interval takes milliseconds,
// thus I'm multiplying the value by 1000 then I'm dividing by 2.
// Refreshing after base session timeout is not recommended,
// because an user might be logged out before the refresh request is successful.
var refreshTimeout = this.get('app').get('restSessionTime') * 1000 / 2;

this.killSessionRenewal();

this._restSessionRenewalInterval = window.setInterval(Y.bind(function () {
this.get('capi').refreshSession(Y.bind(this._handleSessionRenewal, this));
}, this), refreshTimeout);
},

/**
* If error occurs then it displays an error notification
*
* @method _handleSessionRenewal
* @private
* @param {Boolean} error
*/
_handleSessionRenewal: function (error) {
if (!error) {
return;
}

this.fire('notify', {
notification: {
text: Y.eZ.trans(
'cannot.refresh.session',
{},
'contentedit'
),
identifier: 'error-session-renewal',
state: 'error',
timeout: 0,
}
});
},

/**
* Prevents from renewing session
*
* @method killSessionRenewal
*/
killSessionRenewal: function () {
window.clearInterval(this._restSessionRenewalInterval);
},

destructor: function () {
this.killSessionRenewal();
}
}, {
ATTRS: {
Expand Down
6 changes: 6 additions & 0 deletions Resources/translations/contentedit.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@
<note>key: version.not.exist.in</note>
<jms:reference-file>Resources/public/js/views/services/ez-contenteditviewservice.js</jms:reference-file>
</trans-unit>
<trans-unit id="217746443dd8e7aff0175db05603443dec7df180" resname="cannot.refresh.session">
<source>Cannot refresh the user session</source>
<target>Cannot refresh the user session</target>
<note>key: cannot.refresh.session</note>
<jms:reference-file>Resources/public/js/views/services/ez-contenteditviewservice.js</jms:reference-file>
</trans-unit>
</body>
</file>
</xliff>
1 change: 1 addition & 0 deletions Resources/views/PlatformUI/shell.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
viewContainer: '.ez-view-container',
config: {{ parameters|json_encode|raw }},
plugins: Y.eZ.PluginRegistry.getPlugins(Y.eZ.PlatformUIApp.NAME),
restSessionTime: parseInt({{ rest_session_time }}, 10),
});
app.on('ready', function () {
Y.one(Y.config.doc.documentElement).addClass('ez-platformui-app-ready');
Expand Down
4 changes: 4 additions & 0 deletions Tests/js/apps/assets/ez-platformuiapp-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ YUI.add('ez-platformuiapp-tests', function (Y) {
args: [secondService]
});

Y.Mock.expect(firstService, {
method: 'killSessionRenewal',
});

this.app._set('activeViewService', firstService);
this.app._set('activeViewService', secondService);

Expand Down

0 comments on commit eb973ed

Please # to comment.