mirror of
https://github.com/ansible/awx.git
synced 2026-02-02 18:18:12 -03:30
Latest UI changes. Credential List and Edit working -need to add ask pw option and validation around team vs. user. Lookup dialog is now a generic helper that can be shared by all controllers. Credentials controller is first to use api-loader which reads the \/api and \/api\/v1 to determine base set of urls. And more...
This commit is contained in:
358
lib/ui/static/js/controllers/Credentials.js
Normal file
358
lib/ui/static/js/controllers/Credentials.js
Normal file
@@ -0,0 +1,358 @@
|
||||
/************************************
|
||||
* Copyright (c) 2013 AnsibleWorks, Inc.
|
||||
*
|
||||
*
|
||||
* Credentials.js
|
||||
*
|
||||
* Controller functions for the Credential model.
|
||||
*
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
function CredentialsList ($scope, $rootScope, $location, $log, $routeParams, Rest, Alert, CredentialList,
|
||||
GenerateList, LoadBreadCrumbs, Prompt, SearchInit, PaginateInit, ReturnToCaller,
|
||||
ClearScope, ProcessErrors, GetBasePath)
|
||||
{
|
||||
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
|
||||
//scope.
|
||||
var list = CredentialList;
|
||||
var defaultUrl = GetBasePath('credentials');
|
||||
var view = GenerateList;
|
||||
var paths = $location.path().replace(/^\//,'').split('/');
|
||||
var mode = (paths[0] == 'credentials') ? 'edit' : 'select'; // if base path 'credentials', we're here to add/edit
|
||||
var scope = view.inject(CredentialList, { mode: mode }); // Inject our view
|
||||
scope.selected = [];
|
||||
|
||||
SearchInit({ scope: scope, set: 'credentials', list: list, url: defaultUrl });
|
||||
PaginateInit({ scope: scope, list: list, url: defaultUrl });
|
||||
scope.search(list.iterator);
|
||||
|
||||
LoadBreadCrumbs();
|
||||
|
||||
scope.addCredential = function() {
|
||||
$location.path($location.path() + '/add');
|
||||
}
|
||||
|
||||
scope.editCredential = function(id) {
|
||||
$location.path($location.path() + '/' + id);
|
||||
}
|
||||
|
||||
scope.deleteCredential = function(id, name) {
|
||||
|
||||
var action = function() {
|
||||
var url = defaultUrl + id + '/';
|
||||
Rest.setUrl(url);
|
||||
Rest.delete()
|
||||
.success( function(data, status, headers, config) {
|
||||
$('#prompt-modal').modal('hide');
|
||||
scope.search(list.iterator);
|
||||
})
|
||||
.error( function(data, status, headers, config) {
|
||||
$('#prompt-modal').modal('hide');
|
||||
ProcessErrors(scope, data, status, null,
|
||||
{ hdr: 'Error!', msg: 'Call to ' + url + ' failed. DELETE returned status: ' + status });
|
||||
});
|
||||
};
|
||||
|
||||
Prompt({ hdr: 'Delete',
|
||||
body: 'Are you sure you want to delete ' + name + '?',
|
||||
action: action
|
||||
});
|
||||
}
|
||||
|
||||
scope.finishSelection = function() {
|
||||
Rest.setUrl(GetBasePath('base') + $location.path() + '/'); // We're assuming the path matches the api path.
|
||||
// Will this always be true??
|
||||
scope.queue = [];
|
||||
|
||||
scope.$on('callFinished', function() {
|
||||
// We call the API for each selected user. We need to hang out until all the api
|
||||
// calls are finished.
|
||||
if (scope.queue.length == scope.selected.length) {
|
||||
// All the api calls finished
|
||||
$('input[type="checkbox"]').prop("checked",false);
|
||||
scope.selected = [];
|
||||
var errors = 0;
|
||||
for (var i=0; i < scope.queue.length; i++) {
|
||||
if (scope.queue[i].result == 'error') {
|
||||
errors++;
|
||||
// there is no way to know which user raised the error. no data comes
|
||||
// back from the api call.
|
||||
// $('td.username-column').each(function(index) {
|
||||
// if ($(this).text() == scope.queue[i].username) {
|
||||
// $(this).addClass("error");
|
||||
// }
|
||||
// });
|
||||
}
|
||||
}
|
||||
if (errors > 0) {
|
||||
Alert('Error', 'There was an error while adding one or more of the selected Credentials.');
|
||||
}
|
||||
else {
|
||||
ReturnToCaller(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (scope.selected.length > 0 ) {
|
||||
var credential = null;
|
||||
for (var i=0; i < scope.selected.length; i++) {
|
||||
for (var j=0; j < scope.Credentials.length; j++) {
|
||||
if (scope.credentials[j].id == scope.selected[i]) {
|
||||
credential = scope.credentials[j];
|
||||
}
|
||||
}
|
||||
if (credential !== null) {
|
||||
Rest.post(credential)
|
||||
.success( function(data, status, headers, config) {
|
||||
scope.queue.push({ result: 'success', data: data, status: status });
|
||||
scope.$emit('callFinished');
|
||||
})
|
||||
.error( function(data, status, headers, config) {
|
||||
scope.queue.push({ result: 'error', data: data, status: status, headers: headers });
|
||||
scope.$emit('callFinished');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
ReturnToCaller();
|
||||
}
|
||||
}
|
||||
|
||||
scope.toggle_credential = function(idx) {
|
||||
if (scope.selected.indexOf(idx) > -1) {
|
||||
scope.selected.splice(scope.selected.indexOf(idx),1);
|
||||
}
|
||||
else {
|
||||
scope.selected.push(idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CredentialsList.$inject = [ '$scope', '$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'CredentialList', 'GenerateList',
|
||||
'LoadBreadCrumbs', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'ProcessErrors',
|
||||
'GetBasePath'];
|
||||
|
||||
|
||||
function CredentialsAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, CredentialForm,
|
||||
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope,
|
||||
GenerateList, SearchInit, PaginateInit, LookUpInit, UserList, TeamList, GetBasePath)
|
||||
{
|
||||
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
|
||||
//scope.
|
||||
|
||||
// Inject dynamic view
|
||||
var defaultUrl = GetBasePath('credentials');
|
||||
var form = CredentialForm;
|
||||
var generator = GenerateForm;
|
||||
var scope = generator.inject(form, {mode: 'add', related: false});
|
||||
generator.reset();
|
||||
LoadBreadCrumbs();
|
||||
|
||||
LookUpInit({
|
||||
scope: scope,
|
||||
form: form,
|
||||
current_item: ($routeParams.user_id) ? $routeParams.user_id : null,
|
||||
list: UserList,
|
||||
field: 'user'
|
||||
});
|
||||
|
||||
LookUpInit({
|
||||
scope: scope,
|
||||
form: form,
|
||||
current_item: ($routeParams.team_id) ? $routeParams.team_id : null,
|
||||
list: TeamList,
|
||||
field: 'team'
|
||||
});
|
||||
|
||||
// Save
|
||||
scope.formSave = function() {
|
||||
Rest.setUrl(defaultUrl);
|
||||
var data = {}
|
||||
for (var fld in form.fields) {
|
||||
data[fld] = scope[fld];
|
||||
}
|
||||
Rest.post(data)
|
||||
.success( function(data, status, headers, config) {
|
||||
ReturnToCaller();
|
||||
})
|
||||
.error( function(data, status, headers, config) {
|
||||
ProcessErrors(scope, data, status, form,
|
||||
{ hdr: 'Error!', msg: 'Failed to add new Credential. Post returned status: ' + status });
|
||||
});
|
||||
};
|
||||
|
||||
// Reset
|
||||
scope.formReset = function() {
|
||||
// Defaults
|
||||
generator.reset();
|
||||
};
|
||||
|
||||
// Password change
|
||||
scope.clearPWConfirm = function(fld) {
|
||||
// If password value changes, make sure password_confirm must be re-entered
|
||||
scope[fld] = '';
|
||||
scope[form.name + '_form'][fld].$setValidity('awpassmatch', false);
|
||||
}
|
||||
}
|
||||
|
||||
CredentialsAdd.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'CredentialForm', 'GenerateForm',
|
||||
'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'GenerateList',
|
||||
'SearchInit', 'PaginateInit', 'LookUpInit', 'UserList', 'TeamList', 'GetBasePath' ];
|
||||
|
||||
|
||||
function CredentialsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, CredentialForm,
|
||||
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, RelatedSearchInit,
|
||||
RelatedPaginateInit, ReturnToCaller, ClearScope, LookUpInit,
|
||||
UserList, TeamList, Prompt, GetBasePath)
|
||||
{
|
||||
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
|
||||
//scope.
|
||||
|
||||
var defaultUrl=GetBasePath('credentials');
|
||||
var generator = GenerateForm;
|
||||
var form = CredentialForm;
|
||||
var scope = generator.inject(form, {mode: 'edit', related: true});
|
||||
generator.reset();
|
||||
var base = $location.path().replace(/^\//,'').split('/')[0];
|
||||
var master = {};
|
||||
var id = $routeParams.id;
|
||||
var relatedSets = {};
|
||||
|
||||
LookUpInit({
|
||||
scope: scope,
|
||||
form: form,
|
||||
current_item: ($routeParams.user_id) ? $routeParams.user_id : null,
|
||||
list: UserList,
|
||||
field: 'user'
|
||||
});
|
||||
|
||||
LookUpInit({
|
||||
scope: scope,
|
||||
form: form,
|
||||
current_item: ($routeParams.team_id) ? $routeParams.team_id : null,
|
||||
list: TeamList,
|
||||
field: 'team'
|
||||
});
|
||||
|
||||
// After Credential is loaded, retrieve each related set and any lookups
|
||||
scope.$on('credentialLoaded', function() {
|
||||
for (var set in relatedSets) {
|
||||
scope.search(relatedSets[set].iterator);
|
||||
}
|
||||
});
|
||||
|
||||
// Retrieve detail record and prepopulate the form
|
||||
Rest.setUrl(defaultUrl + ':id/');
|
||||
Rest.get({ params: {id: id} })
|
||||
.success( function(data, status, headers, config) {
|
||||
LoadBreadCrumbs({ path: '/Credentials/' + id, title: data.name });
|
||||
for (var fld in form.fields) {
|
||||
if (data[fld]) {
|
||||
scope[fld] = data[fld];
|
||||
master[fld] = scope[fld];
|
||||
}
|
||||
if (form.fields[fld].type == 'lookup' && data.summary_fields[form.fields[fld].sourceModel]) {
|
||||
scope[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField] =
|
||||
data.summary_fields[form.fields[fld].sourceModel][form.fields[fld].sourceField];
|
||||
master[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField] =
|
||||
scope[form.fields[fld].sourceModel + '_' + form.fields[fld].sourceField];
|
||||
}
|
||||
}
|
||||
var related = data.related;
|
||||
for (var set in form.related) {
|
||||
if (related[set]) {
|
||||
relatedSets[set] = { url: related[set], iterator: form.related[set].iterator };
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize related search functions. Doing it here to make sure relatedSets object is populated.
|
||||
RelatedSearchInit({ scope: scope, form: form, relatedSets: relatedSets });
|
||||
RelatedPaginateInit({ scope: scope, relatedSets: relatedSets });
|
||||
scope.$emit('credentialLoaded');
|
||||
})
|
||||
.error( function(data, status, headers, config) {
|
||||
ProcessErrors(scope, data, status, form,
|
||||
{ hdr: 'Error!', msg: 'Failed to retrieve Credential: ' + $routeParams.id + '. GET status: ' + status });
|
||||
});
|
||||
|
||||
// Save changes to the parent
|
||||
scope.formSave = function() {
|
||||
Rest.setUrl(defaultUrl + $routeParams.id + '/');
|
||||
var data = {}
|
||||
for (var fld in form.fields) {
|
||||
data[fld] = scope[fld];
|
||||
}
|
||||
Rest.put(data)
|
||||
.success( function(data, status, headers, config) {
|
||||
var base = $location.path().replace(/^\//,'').split('/')[0];
|
||||
(base == 'Credentials') ? ReturnToCaller() : ReturnToCaller(1);
|
||||
})
|
||||
.error( function(data, status, headers, config) {
|
||||
ProcessErrors(scope, data, status, form,
|
||||
{ hdr: 'Error!', msg: 'Failed to update Credential: ' + $routeParams.id + '. PUT status: ' + status });
|
||||
});
|
||||
};
|
||||
|
||||
// Cancel
|
||||
scope.formReset = function() {
|
||||
generator.reset();
|
||||
for (var fld in master) {
|
||||
scope[fld] = master[fld];
|
||||
}
|
||||
};
|
||||
|
||||
// Related set: Add button
|
||||
scope.add = function(set) {
|
||||
$rootScope.flashMessage = null;
|
||||
$location.path('/' + base + '/' + $routeParams.id + '/' + set + '/add');
|
||||
};
|
||||
|
||||
// Related set: Edit button
|
||||
scope.edit = function(set, id, name) {
|
||||
$rootScope.flashMessage = null;
|
||||
$location.path('/' + base + '/' + $routeParams.id + '/' + set + '/' + id);
|
||||
};
|
||||
|
||||
// Related set: Delete button
|
||||
scope.delete = function(set, itm_id, name, title) {
|
||||
$rootScope.flashMessage = null;
|
||||
|
||||
var action = function() {
|
||||
var url = defaultUrl + id + '/' + set + '/';
|
||||
Rest.setUrl(url);
|
||||
Rest.post({ id: itm_id, disassociate: 1 })
|
||||
.success( function(data, status, headers, config) {
|
||||
$('#prompt-modal').modal('hide');
|
||||
scope.search(form.related[set].iterator);
|
||||
})
|
||||
.error( function(data, status, headers, config) {
|
||||
$('#prompt-modal').modal('hide');
|
||||
ProcessErrors(scope, data, status, null,
|
||||
{ hdr: 'Error!', msg: 'Call to ' + url + ' failed. POST returned status: ' + status });
|
||||
});
|
||||
};
|
||||
|
||||
Prompt({ hdr: 'Delete',
|
||||
body: 'Are you sure you want to remove ' + name + ' from ' + scope.name + ' ' + title + '?',
|
||||
action: action
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Password change
|
||||
scope.clearPWConfirm = function(fld) {
|
||||
// If password value changes, make sure password_confirm must be re-entered
|
||||
scope[fld] = '';
|
||||
scope[form.name + '_form'][fld].$setValidity('awpassmatch', false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CredentialsEdit.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'CredentialForm',
|
||||
'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'RelatedSearchInit',
|
||||
'RelatedPaginateInit', 'ReturnToCaller', 'ClearScope', 'LookUpInit', 'UserList', 'TeamList',
|
||||
'Prompt', 'GetBasePath' ];
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
function OrganizationsList ($scope, $rootScope, $location, $log, Rest, Alert, LoadBreadCrumbs, Prompt, GetAPIDefaults,
|
||||
function OrganizationsList ($scope, $rootScope, $location, $log, Rest, Alert, LoadBreadCrumbs, Prompt,
|
||||
GenerateList, OrganizationList, SearchInit, PaginateInit, ClearScope, ProcessErrors)
|
||||
{
|
||||
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
|
||||
@@ -66,7 +66,7 @@ function OrganizationsList ($scope, $rootScope, $location, $log, Rest, Alert, Lo
|
||||
}
|
||||
}
|
||||
|
||||
OrganizationsList.$inject=[ '$scope', '$rootScope', '$location', '$log', 'Rest', 'Alert', 'LoadBreadCrumbs', 'Prompt', 'GetAPIDefaults',
|
||||
OrganizationsList.$inject=[ '$scope', '$rootScope', '$location', '$log', 'Rest', 'Alert', 'LoadBreadCrumbs', 'Prompt',
|
||||
'GenerateList', 'OrganizationList', 'SearchInit', 'PaginateInit', 'ClearScope', 'ProcessErrors'];
|
||||
|
||||
|
||||
@@ -110,8 +110,8 @@ OrganizationsAdd.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$
|
||||
|
||||
|
||||
function OrganizationsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, OrganizationForm,
|
||||
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, GetAPIDefaults,
|
||||
RelatedSearchInit, RelatedPaginateInit, Prompt, ClearScope)
|
||||
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, RelatedSearchInit,
|
||||
RelatedPaginateInit, Prompt, ClearScope)
|
||||
{
|
||||
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
|
||||
//scope.
|
||||
@@ -230,5 +230,5 @@ function OrganizationsEdit ($scope, $rootScope, $compile, $location, $log, $rout
|
||||
}
|
||||
|
||||
OrganizationsEdit.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'OrganizationForm',
|
||||
'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'GetAPIDefaults',
|
||||
'RelatedSearchInit', 'RelatedPaginateInit', 'Prompt', 'ClearScope'];
|
||||
'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'RelatedSearchInit',
|
||||
'RelatedPaginateInit', 'Prompt', 'ClearScope'];
|
||||
|
||||
@@ -284,7 +284,7 @@ function TeamsEdit ($scope, $rootScope, $compile, $location, $log, $routeParams,
|
||||
// Related set: Add button
|
||||
scope.add = function(set) {
|
||||
$rootScope.flashMessage = null;
|
||||
$location.path('/' + base + '/' + $routeParams.id + '/' + set + '/add');
|
||||
$location.path('/' + base + '/' + $routeParams.id + '/' + set);
|
||||
};
|
||||
|
||||
// Related set: Edit button
|
||||
|
||||
@@ -177,10 +177,10 @@ function UsersAdd ($scope, $rootScope, $compile, $location, $log, $routeParams,
|
||||
};
|
||||
|
||||
// Password change
|
||||
scope.clearPWConfirm = function() {
|
||||
scope.clearPWConfirm = function(fld) {
|
||||
// If password value changes, make sure password_confirm must be re-entered
|
||||
scope.password_confirm = '';
|
||||
scope[form.name]['password_confirm'].$setValidity('awpassmatch', false);
|
||||
scope[fld] = '';
|
||||
scope[form.name][fld].$setValidity('awpassmatch', false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ function UsersEdit ($scope, $rootScope, $compile, $location, $log, $routeParams,
|
||||
|
||||
// Save changes to the parent
|
||||
scope.formSave = function() {
|
||||
Rest.setUrl(defaultUrl + $routeParams.id);
|
||||
Rest.setUrl(defaultUrl + $routeParams.id + '/');
|
||||
var data = {}
|
||||
for (var fld in form.fields) {
|
||||
data[fld] = scope[fld];
|
||||
@@ -270,10 +270,10 @@ function UsersEdit ($scope, $rootScope, $compile, $location, $log, $routeParams,
|
||||
};
|
||||
|
||||
// Password change
|
||||
scope.clearPWConfirm = function() {
|
||||
scope.clearPWConfirm = function(fld) {
|
||||
// If password value changes, make sure password_confirm must be re-entered
|
||||
scope.password_confirm = '';
|
||||
scope[form.name]['password_confirm'].$setValidity('awpassmatch', false);
|
||||
scope[fld] = '';
|
||||
scope[form.name + '_form'][fld].$setValidity('awpassmatch', false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user