This commit is contained in:
JDonadio 2017-04-20 15:48:05 -03:00
commit dd154a3c4a
6 changed files with 172 additions and 100 deletions

View file

@ -5,14 +5,15 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
function init() {
$scope.options = [
{
method: null,
method: 'none',
label: gettextCatalog.getString('Disabled'),
disabled: false,
},
{
method: 'pin',
label: gettextCatalog.getString('Lock by PIN'),
needsBackup: null,
disabled: null,
needsBackup: false,
disabled: false,
},
];
@ -20,12 +21,12 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
$scope.options.push({
method: 'fingerprint',
label: gettextCatalog.getString('Lock by Fingerprint'),
needsBackup: null,
disabled: null,
needsBackup: false,
disabled: false,
});
}
checkAndSelectOption();
initMethodSelector();
processWallets();
};
@ -33,21 +34,40 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
init();
});
function checkAndSelectOption() {
function getSavedMethod() {
var config = configService.getSync();
var method = config.lock && config.lock.method;
if (config.lock) return config.lock.method;
return 'none';
};
if (!method) {
$scope.currentOption = $scope.options[0];
$scope.options[1].disabled = false;
$scope.options[2].disabled = false;
} else {
if (method == 'fingerprint') $scope.options[1].disabled = true;
if (method == 'pin') $scope.options[2].disabled = true;
$scope.currentOption = lodash.find($scope.options, {
'method': method
});
function initMethodSelector() {
function disable(method) {
lodash.find($scope.options, {
method: method
}).disabled = true;
};
var savedMethod = getSavedMethod();
lodash.each($scope.options, function(o) {
o.disabled = false;
});
// HACK: Disable untill we allow to change between methods directly
if (fingerprintService.isAvailable()) {
switch (savedMethod) {
case 'pin':
disable('fingerprint');
break;
case 'fingerprint':
disable('pin');
break;
}
}
$scope.currentOption = lodash.find($scope.options, {
method: savedMethod
});
$timeout(function() {
$scope.$apply();
});
@ -87,42 +107,56 @@ angular.module('copayApp.controllers').controller('lockSetupController', functio
};
$scope.select = function(selectedMethod) {
var config = configService.getSync();
var savedMethod = config.lock && config.lock.method;
var savedMethod = getSavedMethod();
if (savedMethod == selectedMethod) return;
if (!selectedMethod) {
if (!savedMethod) return;
if (savedMethod == 'pin') $state.transitionTo('tabs.pin', {
fromSettings: true,
locking: false,
});
else saveConfig();
} else if (selectedMethod == 'pin') {
if (savedMethod == 'pin') return;
$state.transitionTo('tabs.pin', {
fromSettings: true,
locking: savedMethod == 'pin' ? false : true
});
} else if (selectedMethod == 'fingerprint') {
if (savedMethod == 'fingerprint') return;
else saveConfig('fingerprint');
if (selectedMethod == 'none') {
disableMethod(savedMethod);
} else {
enableMethod(selectedMethod);
}
};
function disableMethod(method) {
switch (method) {
case 'pin':
$state.transitionTo('tabs.pin', {
action: 'disable'
});
break;
case 'fingerprint':
fingerprintService.check('unlockingApp', function(err) {
if (err) init();
else saveConfig('none');
});
break;
}
};
function enableMethod(method) {
switch (method) {
case 'pin':
$state.transitionTo('tabs.pin', {
action: 'setup'
});
break;
case 'fingerprint':
saveConfig('fingerprint');
break;
}
$timeout(function() {
$scope.$apply();
});
};
function saveConfig(method) {
var opts = {
lock: {
method: method || null,
method: method,
value: null,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
checkAndSelectOption();
initMethodSelector();
});
};
});

View file

@ -3,11 +3,11 @@
angular.module('copayApp.controllers').controller('pinController', function($state, $interval, $stateParams, $ionicHistory, $timeout, $scope, $log, configService, appConfigService) {
var ATTEMPT_LIMIT = 3;
var ATTEMPT_LOCK_OUT_TIME = 5 * 60;
var currentPin;
$scope.$on("$ionicView.beforeEnter", function(event) {
$scope.currentPin = $scope.confirmPin = '';
$scope.fromSettings = $stateParams.fromSettings == 'true' ? true : false;
$scope.locking = $stateParams.locking == 'true' ? true : false;
currentPin = $scope.confirmPin = '';
$scope.action = $stateParams.action;
$scope.match = $scope.error = $scope.disableButtons = false;
$scope.currentAttempts = 0;
$scope.appName = appConfigService.name;
@ -27,26 +27,19 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
});
function getSavedMethod() {
var config = configService.getSync();
if (config.lock) return config.lock.method;
return 'none';
};
function checkAttempts() {
$scope.currentAttempts += 1;
$log.debug('Attempts to unlock:', $scope.currentAttempts);
if ($scope.currentAttempts === ATTEMPT_LIMIT) {
$scope.currentAttempts = 0;
var limitTime = Math.floor(Date.now() / 1000) + ATTEMPT_LOCK_OUT_TIME;
var config = configService.getSync();
var opts = {
lock: {
method: 'pin',
value: config.lock.value,
bannedUntil: limitTime,
attempts: config.lock.attempts + 1,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
lockTimeControl(limitTime);
});
saveFailedAttempt(limitTime);
}
};
@ -74,7 +67,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
function reset() {
$scope.expires = $scope.error = $scope.disableButtons = null;
$scope.currentPin = $scope.confirmPin = '';
currentPin = $scope.confirmPin = '';
$interval.cancel(countDown);
$timeout(function() {
$scope.$apply();
@ -84,20 +77,20 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
};
$scope.getFilledClass = function(limit) {
return $scope.currentPin.length >= limit ? 'filled-' + $scope.appName : null;
return currentPin.length >= limit ? 'filled-' + $scope.appName : null;
};
$scope.delete = function() {
if ($scope.disableButtons) return;
if ($scope.currentPin.length > 0) {
$scope.currentPin = $scope.currentPin.substring(0, $scope.currentPin.length - 1);
if (currentPin.length > 0) {
currentPin = currentPin.substring(0, currentPin.length - 1);
$scope.error = false;
$scope.updatePin();
}
};
$scope.isComplete = function() {
if ($scope.currentPin.length < 4) return false;
if (currentPin.length < 4) return false;
else return true;
};
@ -105,7 +98,7 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
if ($scope.disableButtons) return;
$scope.error = false;
if (value && !$scope.isComplete()) {
$scope.currentPin = $scope.currentPin + value;
currentPin = currentPin + value;
$timeout(function() {
$scope.$apply();
});
@ -113,40 +106,56 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
$scope.save();
};
function isMatch(pin) {
var config = configService.getSync();
return config.lock.value == pin;
};
$scope.save = function() {
if (!$scope.isComplete()) return;
var config = configService.getSync();
$scope.match = config.lock && config.lock.method == 'pin' && config.lock.value == $scope.currentPin ? true : false;
if (!$scope.locking) {
if ($scope.match) {
if ($scope.fromSettings) saveSettings();
else {
saveSettings('pin', $scope.currentPin);
$scope.error = false;
var savedMethod = getSavedMethod();
switch ($scope.action) {
case 'setup':
applyAndCheckPin();
break;
case 'disable':
if (isMatch(currentPin)) {
deletePin();
} else {
showError();
checkAttempts();
}
} else {
$timeout(function() {
$scope.confirmPin = $scope.currentPin = '';
$scope.error = true;
}, 200);
checkAttempts();
}
} else {
processCodes();
break;
case 'check':
if (isMatch(currentPin)) return $scope.close();
showError();
break;
}
};
function processCodes() {
function showError() {
$timeout(function() {
$scope.confirmPin = currentPin = '';
$scope.error = true;
}, 200);
$timeout(function() {
$scope.$apply();
});
};
function applyAndCheckPin() {
if (!$scope.confirmPin) {
$timeout(function() {
$scope.confirmPin = $scope.currentPin;
$scope.currentPin = '';
$scope.confirmPin = currentPin;
currentPin = '';
}, 200);
} else {
if ($scope.confirmPin == $scope.currentPin)
saveSettings('pin', $scope.confirmPin);
if ($scope.confirmPin == currentPin)
savePin($scope.confirmPin);
else {
$scope.confirmPin = $scope.currentPin = '';
$scope.confirmPin = currentPin = '';
$scope.error = true;
}
}
@ -155,15 +164,12 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
};
function saveSettings(method, value) {
var config = configService.getSync();
var attempts = config.lock && config.lock.attempts ? config.lock.attempts : 0;
function deletePin() {
var opts = {
lock: {
method: method || null,
value: value || null,
method: 'none',
value: null,
bannedUntil: null,
attempts: attempts + 1,
}
};
@ -173,6 +179,34 @@ angular.module('copayApp.controllers').controller('pinController', function($sta
});
};
function savePin(value) {
var opts = {
lock: {
method: 'pin',
value: value,
bannedUntil: null,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
$scope.close();
});
};
function saveFailedAttempt(bannedUntil) {
var opts = {
lock: {
bannedUntil: bannedUntil,
}
};
configService.set(opts, function(err) {
if (err) $log.debug(err);
lockTimeControl(limitTime);
});
};
$scope.close = function(delay) {
$timeout(function() {
var shouldReturn = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName != 'starting';

View file

@ -55,7 +55,10 @@ angular.module('copayApp.controllers').controller('tabSettingsController', funct
$scope.appName = appConfigService.nameCase;
configService.whenAvailable(function(config) {
$scope.locked = config.lock && config.lock.method;
$scope.method = $scope.locked ? config.lock.method.charAt(0).toUpperCase() + config.lock.method.slice(1) : gettextCatalog.getString('Disabled');
if (!$scope.locked || $scope.locked == 'none')
$scope.method = gettextCatalog.getString('Disabled');
else
$scope.method = $scope.locked.charAt(0).toUpperCase() + config.lock.method.slice(1);
});
});

View file

@ -126,7 +126,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
*/
.state('pin', {
url: '/pin/',
url: '/pin/:action',
controller: 'pinController',
templateUrl: 'views/pin.html',
})
@ -473,7 +473,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
}
})
.state('tabs.pin', {
url: '/pin/:fromSettings/:locking',
url: '/pin/:action',
views: {
'tab-settings@tabs': {
controller: 'pinController',
@ -1214,7 +1214,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
function goTo(nextView) {
nextView = nextView || defaultView;
$state.transitionTo(nextView).then(function() {
$state.transitionTo(nextView, {
action: 'check'
}).then(function() {
if (nextView == 'lockedView')
$ionicHistory.clearHistory();
});
@ -1230,7 +1232,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
fingerprintService.check('unlockingApp', function(err) {
if (err)
goTo('lockedView');
if ($ionicHistory.currentStateName() == 'lockedView' || !onResume)
else if ($ionicHistory.currentStateName() == 'lockedView' || !onResume)
goTo('tabs.home');
});
} else if (lockMethod == 'pin') {

View file

@ -57,7 +57,6 @@ angular.module('copayApp.services').factory('configService', function(storageSer
method: null,
value: null,
bannedUntil: null,
attempts: null,
},
// External services