snappymail/dev/Settings/User/Security.js

162 lines
3.6 KiB
JavaScript
Raw Normal View History

2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-21 23:08:34 +08:00
var
2015-02-03 07:58:58 +08:00
_ = require('_'),
2014-08-25 23:49:01 +08:00
ko = require('ko'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Translator = require('Common/Translator'),
2014-08-25 15:10:51 +08:00
2014-10-18 21:43:44 +08:00
Remote = require('Storage/User/Remote')
2014-08-21 23:08:34 +08:00
;
/**
* @constructor
*/
function SecurityUserSettings()
2014-08-21 23:08:34 +08:00
{
this.processing = ko.observable(false);
this.clearing = ko.observable(false);
this.secreting = ko.observable(false);
this.viewUser = ko.observable('');
this.viewEnable = ko.observable(false);
this.viewEnable.subs = true;
this.twoFactorStatus = ko.observable(false);
this.viewSecret = ko.observable('');
this.viewBackupCodes = ko.observable('');
this.viewUrl = ko.observable('');
this.bFirst = true;
this.viewTwoFactorStatus = ko.computed(function () {
Translator.trigger();
return Translator.i18n(
2014-08-21 23:08:34 +08:00
this.twoFactorStatus() ?
'SETTINGS_SECURITY/TWO_FACTOR_SECRET_CONFIGURED_DESC' :
'SETTINGS_SECURITY/TWO_FACTOR_SECRET_NOT_CONFIGURED_DESC'
);
}, this);
this.onResult = _.bind(this.onResult, this);
this.onSecretResult = _.bind(this.onSecretResult, this);
}
SecurityUserSettings.prototype.showSecret = function ()
2014-08-21 23:08:34 +08:00
{
this.secreting(true);
Remote.showTwoFactorSecret(this.onSecretResult);
};
SecurityUserSettings.prototype.hideSecret = function ()
2014-08-21 23:08:34 +08:00
{
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrl('');
};
SecurityUserSettings.prototype.createTwoFactor = function ()
2014-08-21 23:08:34 +08:00
{
this.processing(true);
Remote.createTwoFactor(this.onResult);
};
SecurityUserSettings.prototype.testTwoFactor = function ()
2014-08-21 23:08:34 +08:00
{
require('Knoin/Knoin').showScreenPopup(require('View/Popup/TwoFactorTest'));
2014-08-21 23:08:34 +08:00
};
SecurityUserSettings.prototype.clearTwoFactor = function ()
2014-08-21 23:08:34 +08:00
{
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrl('');
this.clearing(true);
Remote.clearTwoFactor(this.onResult);
};
SecurityUserSettings.prototype.onShow = function ()
2014-08-21 23:08:34 +08:00
{
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrl('');
};
SecurityUserSettings.prototype.onResult = function (sResult, oData)
2014-08-21 23:08:34 +08:00
{
this.processing(false);
this.clearing(false);
if (Enums.StorageResultType.Success === sResult && oData && oData.Result)
{
this.viewUser(Utils.pString(oData.Result.User));
this.viewEnable(!!oData.Result.Enable);
this.twoFactorStatus(!!oData.Result.IsSet);
this.viewSecret(Utils.pString(oData.Result.Secret));
this.viewBackupCodes(Utils.pString(oData.Result.BackupCodes).replace(/[\s]+/g, ' '));
this.viewUrl(Utils.pString(oData.Result.Url));
}
else
{
this.viewUser('');
this.viewEnable(false);
this.twoFactorStatus(false);
this.viewSecret('');
this.viewBackupCodes('');
this.viewUrl('');
}
if (this.bFirst)
{
this.bFirst = false;
2015-02-03 07:58:58 +08:00
2014-08-21 23:08:34 +08:00
var self = this;
this.viewEnable.subscribe(function (bValue) {
if (this.viewEnable.subs)
{
Remote.enableTwoFactor(function (sResult, oData) {
if (Enums.StorageResultType.Success !== sResult || !oData || !oData.Result)
{
self.viewEnable.subs = false;
self.viewEnable(false);
self.viewEnable.subs = true;
}
}, bValue);
}
}, this);
}
};
SecurityUserSettings.prototype.onSecretResult = function (sResult, oData)
2014-08-21 23:08:34 +08:00
{
this.secreting(false);
if (Enums.StorageResultType.Success === sResult && oData && oData.Result)
{
this.viewSecret(Utils.pString(oData.Result.Secret));
this.viewUrl(Utils.pString(oData.Result.Url));
}
else
{
this.viewSecret('');
this.viewUrl('');
}
};
SecurityUserSettings.prototype.onBuild = function ()
2014-08-21 23:08:34 +08:00
{
this.processing(true);
Remote.getTwoFactor(this.onResult);
};
module.exports = SecurityUserSettings;
2014-08-21 23:08:34 +08:00
2014-09-05 06:49:03 +08:00
}());