1414 */
1515'use strict' ;
1616
17- angular . module ( 'zeppelinWebApp' ) . controller ( 'CredentialCtrl' , function ( $scope , $route , $routeParams , $location ,
18- $rootScope , $ http, baseUrlSrv ) {
17+ angular . module ( 'zeppelinWebApp' ) . controller ( 'CredentialCtrl' , function ( $scope , $route , $routeParams , $location , $rootScope ,
18+ $http , baseUrlSrv , ngToast ) {
1919 $scope . _ = _ ;
2020
21- $scope . credentialEntity = '' ;
22- $scope . credentialUsername = '' ;
23- $scope . credentialPassword = '' ;
21+ $scope . credentialInfo = [ ] ;
22+ $scope . showAddNewCredentialInfo = false ;
2423
25- $scope . updateCredentials = function ( ) {
26- if ( _ . isEmpty ( $scope . credentialEntity . trim ( ) ) ||
27- _ . isEmpty ( $scope . credentialUsername . trim ( ) ) ) {
28- BootstrapDialog . alert ( {
29- closable : true ,
30- message : 'Username \\ Entity can not be empty.'
24+ var getCredentialInfo = function ( ) {
25+ $http . get ( baseUrlSrv . getRestApiBase ( ) + '/credential' ) .
26+ success ( function ( data , status , headers , config ) {
27+
28+ $scope . credentialInfo = _ . map ( data . body . userCredentials , function ( value , prop ) {
29+ return { entity : prop , password : value . password , username : value . username } ;
30+ } ) ;
31+ console . log ( 'Success %o %o' , status , $scope . credentialInfo ) ;
32+ } ) .
33+ error ( function ( data , status , headers , config ) {
34+ console . log ( 'Error %o %o' , status , data . message ) ;
35+ } ) ;
36+ } ;
37+
38+ $scope . addNewCredentialInfo = function ( ) {
39+ if ( $scope . entity && _ . isEmpty ( $scope . entity . trim ( ) ) &&
40+ $scope . username && _ . isEmpty ( $scope . username . trim ( ) ) ) {
41+ ngToast . danger ( {
42+ content : 'Username \\ Entity can not be empty.' ,
43+ verticalPosition : 'bottom' ,
44+ timeout : '3000'
3145 } ) ;
3246 return ;
3347 }
3448
35- $http . put ( baseUrlSrv . getRestApiBase ( ) + '/credential' ,
36- { 'entity' : $scope . credentialEntity ,
37- 'username' : $scope . credentialUsername ,
38- 'password' : $scope . credentialPassword
49+ var newCredential = {
50+ 'entity' : $scope . entity ,
51+ 'username' : $scope . username ,
52+ 'password' : $scope . password
53+ } ;
54+
55+ $http . put ( baseUrlSrv . getRestApiBase ( ) + '/credential' , newCredential ) .
56+ success ( function ( data , status , headers , config ) {
57+ ngToast . success ( {
58+ content : 'Successfully saved credentials.' ,
59+ verticalPosition : 'bottom' ,
60+ timeout : '3000'
61+ } ) ;
62+ $scope . credentialInfo . push ( newCredential ) ;
63+ resetCredentialInfo ( ) ;
64+ $scope . showAddNewCredentialInfo = false ;
65+ console . log ( 'Success %o %o' , status , data . message ) ;
66+ } ) .
67+ error ( function ( data , status , headers , config ) {
68+ ngToast . danger ( {
69+ content : 'Error saving credentials' ,
70+ verticalPosition : 'bottom' ,
71+ timeout : '3000'
72+ } ) ;
73+ console . log ( 'Error %o %o' , status , data . message ) ;
74+ } ) ;
75+ } ;
76+
77+ $scope . cancelCredentialInfo = function ( ) {
78+ $scope . showAddNewCredentialInfo = false ;
79+ resetCredentialInfo ( ) ;
80+ } ;
81+
82+ var resetCredentialInfo = function ( ) {
83+ $scope . entity = '' ;
84+ $scope . username = '' ;
85+ $scope . password = '' ;
86+ } ;
87+
88+ $scope . copyOriginCredentialsInfo = function ( ) {
89+ ngToast . info ( {
90+ content : 'Since entity is a unique key, you can edit only username & password' ,
91+ verticalPosition : 'bottom' ,
92+ timeout : '3000'
93+ } ) ;
94+ } ;
95+
96+ $scope . updateCredentialInfo = function ( form , data , entity ) {
97+ var request = {
98+ entity : entity ,
99+ username : data . username ,
100+ password : data . password
101+ } ;
102+
103+ $http . put ( baseUrlSrv . getRestApiBase ( ) + '/credential/' , request ) .
104+ success ( function ( data , status , headers , config ) {
105+ var index = _ . findIndex ( $scope . credentialInfo , { 'entity' : entity } ) ;
106+ $scope . credentialInfo [ index ] = request ;
107+ return true ;
39108 } ) .
40- success ( function ( data , status , headers , config ) {
41- BootstrapDialog . alert ( {
42- closable : true ,
43- message : 'Successfully saved credentials.'
109+ error ( function ( data , status , headers , config ) {
110+ console . log ( 'Error %o %o' , status , data . message ) ;
111+ ngToast . danger ( {
112+ content : 'We couldn\'t save the credential' ,
113+ verticalPosition : 'bottom' ,
114+ timeout : '3000'
115+ } ) ;
116+ form . $show ( ) ;
44117 } ) ;
45- $scope . credentialEntity = '' ;
46- $scope . credentialUsername = '' ;
47- $scope . credentialPassword = '' ;
48- console . log ( 'Success %o %o' , status , data . message ) ;
49- } ) .
50- error ( function ( data , status , headers , config ) {
51- alert ( 'Error saving credentials' ) ;
52- console . log ( 'Error %o %o' , status , data . message ) ;
118+ return false ;
119+ } ;
120+
121+ $scope . removeCredentialInfo = function ( entity ) {
122+ BootstrapDialog . confirm ( {
123+ closable : false ,
124+ closeByBackdrop : false ,
125+ closeByKeyboard : false ,
126+ title : '' ,
127+ message : 'Do you want to delete this credential information?' ,
128+ callback : function ( result ) {
129+ if ( result ) {
130+ $http . delete ( baseUrlSrv . getRestApiBase ( ) + '/credential/' + entity ) .
131+ success ( function ( data , status , headers , config ) {
132+ var index = _ . findIndex ( $scope . credentialInfo , { 'entity' : entity } ) ;
133+ $scope . credentialInfo . splice ( index , 1 ) ;
134+ console . log ( 'Success %o %o' , status , data . message ) ;
135+ } ) .
136+ error ( function ( data , status , headers , config ) {
137+ console . log ( 'Error %o %o' , status , data . message ) ;
138+ } ) ;
139+ }
140+ }
53141 } ) ;
54142 } ;
55143
56- } ) ;
144+ var init = function ( ) {
145+ getCredentialInfo ( ) ;
146+ } ;
147+
148+ init ( ) ;
149+ } ) ;
0 commit comments