keycloak-aplcache

Details

diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js
index 5fd7365..fffbfc0 100755
--- a/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js
+++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js
@@ -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");
diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js
index c0e3c6a..8e7e6c5 100755
--- a/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js
+++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js
@@ -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;
-});
\ No newline at end of file
+});
+
+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;
+});
diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-tokens.html b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-tokens.html
index 4921101..7a2b92b 100755
--- a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-tokens.html
+++ b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-tokens.html
@@ -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>