KEYCLOAK-165

Confusing automatic converstion of time units
This commit is contained in:
Stian Thorgersen 2013-11-26 13:31:46 +00:00
parent 2deaca8fc6
commit 36f91ac75c
3 changed files with 75 additions and 33 deletions

View file

@ -484,16 +484,29 @@ module.controller('RealmSocialCtrl', function($scope, realm, Realm, $location, N
});
module.controller('RealmTokenDetailCtrl', function($scope, Realm, realm, $http, $location, Dialog, Notifications) {
module.controller('RealmTokenDetailCtrl', function($scope, Realm, realm, $http, $location, Dialog, Notifications, TimeUnit) {
console.log('RealmTokenDetailCtrl');
$scope.realm = { id : realm.id, realm : realm.realm, social : realm.social, registrationAllowed : realm.registrationAllowed,
tokenLifespan : realm.tokenLifespan, accessCodeLifespan : realm.accessCodeLifespan,
accessCodeLifespanUserAction : realm.accessCodeLifespanUserAction };
$scope.realm = { id : realm.id, realm : realm.realm, social : realm.social, registrationAllowed : realm.registrationAllowed };
$scope.realm.tokenLifespanUnit = TimeUnit.autoUnit(realm.tokenLifespan);
$scope.realm.tokenLifespan = TimeUnit.toUnit(realm.tokenLifespan, $scope.realm.tokenLifespanUnit);
$scope.$watch('realm.tokenLifespanUnit', function(to, from) {
$scope.realm.tokenLifespan = TimeUnit.convert($scope.realm.tokenLifespan, from, to);
});
$scope.realm.accessCodeLifespanUnit = TimeUnit.autoUnit(realm.accessCodeLifespan);
$scope.realm.accessCodeLifespan = TimeUnit.toUnit(realm.accessCodeLifespan, $scope.realm.accessCodeLifespanUnit);
$scope.$watch('realm.accessCodeLifespanUnit', function(to, from) {
$scope.realm.accessCodeLifespan = TimeUnit.convert($scope.realm.accessCodeLifespan, from, to);
});
$scope.realm.accessCodeLifespanUserActionUnit = TimeUnit.autoUnit(realm.accessCodeLifespanUserAction);
$scope.realm.accessCodeLifespanUserAction = TimeUnit.toUnit(realm.accessCodeLifespanUserAction, $scope.realm.accessCodeLifespanUserActionUnit);
$scope.$watch('realm.accessCodeLifespanUserActionUnit', function(to, from) {
$scope.realm.accessCodeLifespanUserAction = TimeUnit.convert($scope.realm.accessCodeLifespanUserAction, from, to);
});
$scope.realm.tokenLifespanUnit = 'Seconds';
$scope.realm.accessCodeLifespanUnit = 'Seconds';
$scope.realm.accessCodeLifespanUserActionUnit = 'Seconds';
var oldCopy = angular.copy($scope.realm);
$scope.changed = false;
@ -509,27 +522,11 @@ module.controller('RealmTokenDetailCtrl', function($scope, Realm, realm, $http,
delete realmCopy["tokenLifespanUnit"];
delete realmCopy["accessCodeLifespanUnit"];
delete realmCopy["accessCodeLifespanUserActionUnit"];
if ($scope.realm.tokenLifespanUnit == 'Minutes') {
realmCopy.tokenLifespan = $scope.realm.tokenLifespan * 60;
} else if ($scope.realm.tokenLifespanUnit == 'Hours') {
realmCopy.tokenLifespan = $scope.realm.tokenLifespan * 60 * 60;
} else if ($scope.realm.tokenLifespanUnit == 'Days') {
realmCopy.tokenLifespan = $scope.realm.tokenLifespan * 60 * 60 * 24;
}
if ($scope.realm.accessCodeLifespanUnit == 'Minutes') {
realmCopy.accessCodeLifespan = $scope.realm.accessCodeLifespan * 60;
} else if ($scope.realm.accessCodeLifespanUnit == 'Hours') {
realmCopy.accessCodeLifespan = $scope.realm.accessCodeLifespan * 60 * 60;
} else if ($scope.realm.accessCodeLifespanUnit == 'Days') {
realmCopy.accessCodeLifespan = $scope.realm.accessCodeLifespan * 60 * 60 * 24;
}
if ($scope.realm.accessCodeLifespanUserActionUnit == 'Minutes') {
realmCopy.accessCodeLifespanUserAction = $scope.realm.accessCodeLifespanUserAction * 60;
} else if ($scope.realm.accessCodeLifespanUserActionUnit == 'Hours') {
realmCopy.accessCodeLifespanUserAction = $scope.realm.accessCodeLifespanUserAction * 60 * 60;
} else if ($scope.realm.accessCodeLifespanUserActionUnit == 'Days') {
realmCopy.accessCodeLifespanUserAction = $scope.realm.accessCodeLifespanUserAction * 60 * 60 * 24;
}
realmCopy.tokenLifespan = TimeUnit.toSeconds($scope.realm.tokenLifespan, $scope.realm.tokenLifespanUnit)
realmCopy.accessCodeLifespan = TimeUnit.toSeconds($scope.realm.accessCodeLifespan, $scope.realm.accessCodeLifespanUnit)
realmCopy.accessCodeLifespanUserAction = TimeUnit.toSeconds($scope.realm.accessCodeLifespanUserAction, $scope.realm.accessCodeLifespanUserActionUnit)
$scope.changed = false;
Realm.update(realmCopy, function () {
$location.url("/realms/" + realm.id + "/token-settings");

View file

@ -226,9 +226,6 @@ module.factory('ApplicationOrigins', function($resource) {
});
});
module.factory('Current', function(Realm, $route) {
var current = {};
@ -253,4 +250,52 @@ module.factory('Current', function(Realm, $route) {
current.refresh();
return current;
});
});
module.factory('TimeUnit', function() {
var t = {};
t.autoUnit = function(time) {
var unit = 'Seconds';
if (time % 60 == 0) {
unit = 'Minutes';
time = time / 60;
}
if (time % 60 == 0) {
unit = 'Hours';
time = time / 60;
}
if (time % 24 == 0) {
unit = 'Days'
time = time / 24;
}
return unit;
}
t.toSeconds = function(time, unit) {
switch (unit) {
case 'Seconds': return time;
case 'Minutes': return time * 60;
case 'Hours': return time * 360;
case 'Days': return time * 86400;
default: throw 'invalid unit ' + unit;
}
}
t.toUnit = function(time, unit) {
switch (unit) {
case 'Seconds': return time;
case 'Minutes': return Math.ceil(time / 60);
case 'Hours': return Math.ceil(time / 360);
case 'Days': return Math.ceil(time / 86400);
default: throw 'invalid unit ' + unit;
}
}
t.convert = function(time, from, to) {
var seconds = t.toSeconds(time, from);
return t.toUnit(seconds, to);
}
return t;
});

View file

@ -28,7 +28,7 @@
<div class="input-group">
<input type="text" data-ng-model="realm.tokenLifespan" id="tokenLifespan" name="tokenLifespan" class="tiny">
<div class="select-rcue">
<select name="tokenLifespanUnit" data-ng-model="realm.tokenLifespanUnit">
<select name="tokenLifespanUnit" data-ng-model="realm.tokenLifespanUnit" >
<option data-ng-selected="!realm.tokenLifespanUnit">Seconds</option>
<option>Minutes</option>
<option>Hours</option>