(function () { 'use strict'; var app = angular.module('developerKey'); var templatePath = modulesSharedResourcesUrl + 'Modules/DeveloperKey/Views/'; app.directive('developerKey', ['config', 'datacontext', 'developerKeyService', 'common', '$location', '$modal', '$q', '$upload', 'OrganisationAdminService', 'features', '$rootScope', function (config, datacontext, developerKeyService, common, $location, $modal, $q, $upload, OrganisationAdminService, features, $rootScope) { return { restrict: 'E', templateUrl: templatePath + 'developerkey.html', link: link, scope: { allowCreate: '=', allowToken: '=' }, }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('developerKey'); var logSuccess = getLogFn('developerKey', "success"); activate(); function activate() { NProgress.done(); log('Activated integrations View'); } OrganisationAdminService.getTopLevelOrg().then(function (org) { $scope.topLevelOrg = org; features.getCurrentFeatures(org.id).then(function (data) { $scope.features = data; $scope.developerKeyLoaded = true; getIntegrations(); }); }); $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; $scope.generateNewDeveloperKey = function () { var k = randomPKey(60, true, false, true); logSuccess("API Key successfully generated."); $scope.canCreate = false; OrganisationAdminService.getCurrentOrg().then(function (org) { var integration = { organisationId: org.id, connectionType: 8, connectionCode: org.id, connectionName: "DeveloperKey", primaryKey: "", secondaryKey: k, }; datacontext.createIntegration(integration).then(function (data) { console.log(data.secondaryKey); getAuthenticationToken(data.secondaryKey, data.organisationId); getIntegrations(); }); }); }; function getAuthenticationToken(dkey, orgid) { datacontext.getAuthenticationToken(dkey, orgid).then(function (data) { // generate pop up $scope.authToken = data; $modal.open({ templateUrl: templatePath + 'token.html', controller: tokenController, size: 'sm', backdrop: 'static', resolve: { authtoken: function () { return $scope.authToken; } } }); }); } function getIntegrations() { OrganisationAdminService.getCurrentOrg().then(function (org) { datacontext.getIntegrations(org.id, 8).then(function (data) { console.log(data); $scope.developerKey = data; $scope.hasKeys = ($scope.developerKey.length > 0); $scope.canCreate = false; if (data.length === 0) { $scope.canCreate = true; } if (config.appCode === 'CUPPA') { $scope.features.push('DA'); } }); }); } $scope.deleteKey = function (integration) { datacontext.deleteIntegration(integration).then(function (data) { getIntegrations(); logSuccess("API Key successfully deleted."); }); }; $scope.refreshKey = function (integration) { console.log(integration); getAuthenticationToken(integration.secondaryKey, integration.organisationId); }; $scope.successfullyCopied = function () { logSuccess("API key copied to clipboard."); }; function randomPKey(length, addUpper, addSymbols, addNums) { var lower = "abcdefghijklmnopqrstuvwxyz"; var upper = addUpper ? lower.toUpperCase() : ""; var nums = addNums ? "0123456789" : ""; var symbols = addSymbols ? "!#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~" : ""; var all = lower + upper + nums + symbols; while (true) { var pass = ""; for (var i = 0; i < length; i++) { pass += all[Math.random() * all.length | 0]; } // criteria: if (!/[a-z]/.test(pass)) continue; // lowercase is a must if (addUpper && !/[A-Z]/.test(pass)) continue; // check uppercase if (addSymbols && !/\W/.test(pass)) continue; // check symbols if (addNums && !/\d/.test(pass)) continue; // check nums return pass; // all good } } // Controller for showing auth token var tokenController = function ($scope, common, $modalInstance, authtoken) { var nextYear = new Date(); nextYear.setYear(nextYear.getFullYear() + 10); $scope.year = nextYear; $scope.authToken = authtoken; //cancel the modal, user doesn't want to chnage password $scope.cancelModel = function () { $modalInstance.close(); }; $scope.successfullyCopied = function () { logSuccess("Authentication token copied to clipboard."); }; } } }]); app.directive('createDeveloperkey', ['config', 'datacontext', 'developerKeyService', 'common', '$location', '$modal', '$q', '$upload', 'OrganisationAdminService', 'features', '$rootScope', function (config, datacontext, developerKeyService, common, $location, $modal, $q, $upload, OrganisationAdminService, features, $rootScope) { return { restrict: 'E', templateUrl: templatePath + 'createdeveloperkey.html', link: link }; function link($scope, elem, attrs) { var getLogFn = common.logger.getLogFn; var log = getLogFn('createDeveloperkey'); var logSuccess = getLogFn('createDeveloperkey', "success"); activate(); function activate() { NProgress.done(); log('Activated integrations View'); } OrganisationAdminService.getCurrentOrg().then(function (org) { $scope.topLevelOrg = org; getIntegrations(org.id); }); function getIntegrations(orgId) { datacontext.getIntegrations(orgId, 8).then(function (data) { if (data) { $scope.developerKey = data[0]; console.log(data[0]); } else { $scope.developerKey = null; } if (config.appCode === 'CUPPA') { $scope.features.push('DA'); } $scope.developerKeysLoaded = true; }); } $scope.update = function () { OrganisationAdminService.getCurrentOrg().then(function (org) { var integration = { organisationId: org.id, connectionType: 8, connectionCode: org.id, connectionName: "DeveloperKey", primaryKey: "", secondaryKey: $scope.developerKey.secondaryKey }; if ($scope.developerKey.id) { datacontext.deleteIntegrationById($scope.developerKey.id).then(function (data) { datacontext.createIntegration(integration).then(function (data) { logSuccess("Key successfully updated."); }); }); } else { datacontext.createIntegration(integration).then(function (data) { logSuccess("Key successfully updated."); }); } }); }; } }]); })();