CADI AAF Integration and merging the code
[portal.git] / ecomp-portal-FE-common / client / app / views / account-onboarding / account-add-details / account-add-details.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 'use strict';
39 (function () {
40     class AccountAddDetailsCtrl {
41         constructor($scope, $log, $interval, basicAuthAccountService, $modalInstance, errorMessageByCode, ECOMP_URL_REGEX, $window, confirmBoxService, $cookies,items) {
42          
43            this.addEndpoint = () => {
44                    confirmBoxService.showInformation('Please add the roles to this Username/MechId through AAF Screen ').then(isConfirmed => {});
45                return;
46                  /* this.account.endpointList.push({
47                           valid: true
48                   }); */
49            }
50             let init = () => {
51                 this.account = [];
52                 this.account.endpointList = [];
53                 this.passwordMatched = true;
54                 this.dupliateName = false;
55                 this.emptyAccountName = false;
56                 this.emptyAccountUsername = false;
57                 this.accountList = items.list;
58                 
59                 if (items&& items.account) {
60                     this.isEditMode = true;
61                     this.account = _.clone(items.account);
62                     this.account.repassword = this.account.password;
63                     this.account.endpointList = this.account.endpoints;
64                     if(this.account.isActive == 'Y')
65                         this.account.active = true;
66                     else
67                         this.account.active = false;
68                 } else {
69                     this.isEditMode = false;
70                     this.account.active = true;
71                 } 
72                 console.log(this.account);
73             };
74             
75             let resetConflict = fieldName => {
76                 delete this.conflictMessages[fieldName];
77                 if($scope.widgetForm[fieldName]){
78                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
79                 }
80             };
81
82             
83           /*  this.closeThisDialog = () => {
84                 $scope.closeThisDialog(true);
85             }*/
86             
87             this.removeEndpointItem = (endpoint) => {
88                         for(var i = 0; i < this.account.endpointList.length; i++){
89                                 if(this.account.endpointList[i].name == endpoint.name){
90                                         this.account.endpointList.splice(i, 1);
91                                         return; 
92                                 }
93                         }
94             }
95             
96             this.updateUsername = () => {
97                 this.emptyAccountUsername = false;
98             }
99             
100             this.updateAccountName = () => {
101                 this.dupliateName = false;
102                 for(var i = 0; i < this.accountList.length; i++){
103                         if(this.accountList[i].applicationName == this.account.applicationName){
104                                 this.dupliateName = true;
105                                 return;
106                         }
107                 }
108             }
109             
110             this.updateAccountEndpoint = (endpoint) => {
111                 endpoint.valid = true;
112             }
113            
114             this.saveChanges = () => {
115
116                 var isValid = true;
117                 var r = /\/[^ "]+$/;
118                 
119                 for(var i = 0; i < this.account.endpointList.length; i++){
120                         if(this.account.endpointList[i].name == undefined
121                         || this.account.endpointList[i].name == null
122                         || this.account.endpointList[i].name == ""){
123                                 this.account.endpointList.splice(i, 1);
124                                 i--;
125                         }else{
126                                 if(!this.account.endpointList[i].name.startsWith("/")){
127                                         this.account.endpointList[i].name = "/" + this.account.endpointList[i].name;
128                                 }
129                                 if(!r.test(this.account.endpointList[i].name)){
130                                         this.account.endpointList[i].valid = false;
131                                         isValid = false;
132                                 }
133                                 
134                         }
135                 }
136                 
137                 if(this.account.applicationName == ''
138                 || this.account.applicationName == undefined){
139                         this.emptyAccountName = true;
140                         isValid = false;
141                 }
142                 
143                 if(this.account.username == ''
144                 || this.account.username == undefined){
145                         this.emptyAccountUsername = true;
146                     isValid = false;
147                 }
148                 
149                 if(this.dupliateName == true){
150                    isValid = false;
151                 }
152                 
153                 if(this.account.password != this.account.repassword){
154                         this.passwordMatched =  false;
155                         isValid = false;
156                 }
157                 
158                 if(!isValid)
159                         return;
160                 
161                 
162                 
163                 var active = 'N';
164                 if(this.account.active == true)
165                         active = 'Y';
166                 
167                 var newAccount = {
168                                 applicationName: this.account.applicationName,
169                                 username: this.account.username,
170                                 password: this.account.password,
171                                 endpoints: this.account.endpointList,
172                                 isActive: active
173                 };
174                 
175                 
176                 if(this.isEditMode){
177                         var message = "Are you sure you want to change '" + this.account.applicationName + "'?"
178                         confirmBoxService.editItem(message).then(isConfirmed => {
179                                 if(isConfirmed){
180                                         basicAuthAccountService.updateAccount(this.account.id, newAccount).then(() => {
181                                                 $modalInstance.close("confirmed");
182                                         });
183                                         }
184                         });
185                 }else{
186                         basicAuthAccountService.createAccount(newAccount).then(() => {
187                                  $modalInstance.close("confirmed");
188                         });
189                 }
190             }
191             
192             init();
193             $scope.$on('$stateChangeStart', e => {
194                 e.preventDefault();
195             });
196         }
197     }
198     AccountAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'basicAuthAccountService', '$modalInstance', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window', 'confirmBoxService', '$cookies','items'];
199     angular.module('ecompApp').controller('AccountAddDetailsCtrl', AccountAddDetailsCtrl);
200 })();