2017-10-20 13:30:01 -04:00
|
|
|
var angular = require('angular');
|
|
|
|
|
2017-10-21 17:47:55 -04:00
|
|
|
angular.module('ApiCtrl', ['ApiSvc', 'AuthSvc']).controller('ApiController', ['$scope', 'ApiService', 'AuthService', function ($scope, ApiService, AuthService) {
|
|
|
|
function splitScope(scope) {
|
|
|
|
var res = {};
|
|
|
|
for (var i in scope) {
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.checkCkPerm = function(prefix, perm) {
|
|
|
|
var index = $scope.scopeObj[prefix].indexOf(perm);
|
|
|
|
if ($scope.scopeObj[prefix][index].isChecked) {
|
|
|
|
$scope.ckScope.push(prefix + '.' + perm.name);
|
|
|
|
} else {
|
|
|
|
var index = $scope.ckScope.indexOf(prefix + '.' + perm.name);
|
|
|
|
$scope.ckScope.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.parseScope = function () {
|
|
|
|
AuthService.currentUser(function (res) {
|
|
|
|
$scope.scopeObj = splitScope(res.scope);
|
|
|
|
$scope.ckScope = [];
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.getKeys = function () {
|
|
|
|
ApiService.getAll(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.hideNewKey = function () {
|
2017-10-21 15:10:24 -04:00
|
|
|
$scope.nModalShow = false;
|
|
|
|
};
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.showNewKey = function () {
|
2017-10-21 15:10:24 -04:00
|
|
|
$scope.nModalShow = true;
|
|
|
|
};
|
|
|
|
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.hideKeyInfo = function () {
|
2017-10-21 15:10:24 -04:00
|
|
|
$scope.kModalShow = false;
|
|
|
|
};
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.showKeyInfo = function (key) {
|
2017-10-21 15:10:24 -04:00
|
|
|
$scope.kModalShow = true;
|
|
|
|
$scope.currKey = key;
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.currKey.scopeObj = splitScope($scope.currKey.scope);
|
2017-10-21 15:10:24 -04:00
|
|
|
};
|
|
|
|
|
2017-10-21 17:47:55 -04:00
|
|
|
$scope.deleteKey = function (key) {
|
|
|
|
ApiService.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 () {
|
|
|
|
if ($scope.ckScope.length === 0 || !$scope.ckIdentifier)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ApiService.createKey({
|
|
|
|
identifier: $scope.ckIdentifier,
|
|
|
|
scope: JSON.stringify($scope.ckScope)
|
|
|
|
}, function (res) {
|
|
|
|
if (res.key) {
|
|
|
|
$scope.hideNewKey();
|
|
|
|
$scope.getKeys();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-18 13:31:08 -04:00
|
|
|
}]);
|