2017-10-20 13:30:01 -04:00
|
|
|
var angular = require('angular');
|
|
|
|
|
2018-08-14 05:13:59 -04:00
|
|
|
angular.module('KeyCtrl', ['KeySvc', 'AuthSvc']).controller('ApiController', ['$scope', 'KeyService', 'AuthService', function ($scope, KeyService, AuthService) {
|
2018-01-14 10:27:05 -05:00
|
|
|
// Transforms an array of period-separated properties ex. ["file.upload", "user.view", "user.ban"]
|
|
|
|
// to json ex. { "file": "upload", "user": ["view", "ban"] }
|
2017-10-21 17:47:55 -04:00
|
|
|
function splitScope(scope) {
|
|
|
|
var res = {};
|
|
|
|
for (var i in scope) {
|
2018-01-14 10:27:05 -05:00
|
|
|
if (scope.hasOwnProperty(i)) {
|
|
|
|
var perm = scope[i];
|
|
|
|
var prefix = perm.substr(0, perm.indexOf('.'));
|
|
|
|
var postfix = perm.substr(perm.indexOf('.') + 1);
|
|
|
|
if (!res[prefix]) res[prefix] = [];
|
|
|
|
res[prefix].push({name: postfix});
|
|
|
|
}
|
2017-10-21 17:47:55 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-01-14 10:27:05 -05:00
|
|
|
// Called on init, retrieves the user's scope from the server.
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.parseScope = function () {
|
2018-08-14 05:13:59 -04:00
|
|
|
AuthService.whoami(function (res) {
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.scopeObj = splitScope(res.scope);
|
2018-01-14 10:27:05 -05:00
|
|
|
$scope.currKeyScope = [];
|
2017-10-21 17:47:55 -04:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-01-14 10:27:05 -05:00
|
|
|
// Triggered when a checkbox for a permission changes.
|
|
|
|
// Updates the currKeyScope object with the addition or removal.
|
|
|
|
$scope.updateCurrKeyPerm = function(prefix, perm) {
|
|
|
|
var index = $scope.scopeObj[prefix].indexOf(perm);
|
|
|
|
if ($scope.scopeObj[prefix][index].isChecked) {
|
|
|
|
$scope.currKeyScope.push(prefix + '.' + perm.name);
|
|
|
|
} else {
|
|
|
|
index = $scope.currKeyScope.indexOf(prefix + '.' + perm.name);
|
|
|
|
$scope.currKeyScope.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.getKeys = function () {
|
2018-08-14 05:13:59 -04:00
|
|
|
KeyService.getAllKeys(function (keys) {
|
2017-10-18 13:31:08 -04:00
|
|
|
$scope.keys = keys;
|
|
|
|
});
|
|
|
|
};
|
2017-10-21 15:10:24 -04:00
|
|
|
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.deleteKey = function (key) {
|
2018-08-14 05:13:59 -04:00
|
|
|
KeyService.deleteKey(key, function () {
|
2017-10-21 15:10:24 -04:00
|
|
|
var index = $scope.keys.indexOf(key);
|
|
|
|
$scope.keys.splice(index, 1);
|
|
|
|
$scope.hideKeyInfo();
|
|
|
|
$scope.currKey = {};
|
|
|
|
});
|
|
|
|
};
|
2017-10-21 17:47:55 -04:00
|
|
|
|
|
|
|
$scope.createKey = function () {
|
2018-01-14 10:27:05 -05:00
|
|
|
if ($scope.currKeyScope.length === 0 || !$scope.currKeyIdentifier)
|
2017-10-21 17:47:55 -04:00
|
|
|
return;
|
|
|
|
|
2018-08-14 05:13:59 -04:00
|
|
|
KeyService.createKey($scope.currKeyIdentifier, $scope.currKeyScope,
|
|
|
|
function (res) {
|
2017-10-21 17:47:55 -04:00
|
|
|
if (res.key) {
|
|
|
|
$scope.hideNewKey();
|
|
|
|
$scope.getKeys();
|
|
|
|
}
|
|
|
|
});
|
2018-01-14 10:27:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Hide/show new key modal dialog
|
|
|
|
$scope.hideNewKey = function () {
|
|
|
|
$scope.nModalShow = false;
|
|
|
|
};
|
|
|
|
$scope.showNewKey = function () {
|
|
|
|
$scope.nModalShow = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hide/show key info modal dialog
|
|
|
|
$scope.hideKeyInfo = function () {
|
|
|
|
$scope.kModalShow = false;
|
|
|
|
};
|
|
|
|
$scope.showKeyInfo = function (key) {
|
|
|
|
$scope.kModalShow = true;
|
|
|
|
$scope.currKey = key;
|
|
|
|
$scope.currKey.scopeObj = splitScope($scope.currKey.scope);
|
|
|
|
};
|
2017-10-18 13:31:08 -04:00
|
|
|
}]);
|