Replace ecomp references
[portal.git] / ecomp-portal-FE-common / client / app / services / admins / admins.service.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 /**
39  * Created by nnaffar on 11/22/2015.
40  */
41 'use strict';
42
43 (function () {
44     class AdminsService {
45         constructor($q, $log, $http, conf, uuid, utilsService) {
46             this.$q = $q;
47             this.$log = $log;
48             this.$http = $http;
49             this.conf = conf;
50             this.uuid = uuid;
51             this.utilsService = utilsService;
52         }
53
54         getAccountAdmins() {
55             let deferred = this.$q.defer();
56             //this.$log.info('AdminsService::get all applications admins list');
57             this.$http({
58                     method: "GET",
59                     cache: false,
60                     url: this.conf.api.accountAdmins,
61                     headers: {
62                         'X-ECOMP-RequestID':this.uuid.generate()
63                     }
64                 })
65                 .then( res => {
66                     // If response comes back as a redirected HTML page which IS NOT a success
67                     if (this.utilsService.isValidJSON(res)=== false) {
68                         this.$log.error('AdminsService::getAccountAdmins Failed');
69                         deferred.reject("AdminsService::getAccountAdmins Failed");
70                     } else {
71                         // this.$log.info('AdminsService::getAccountAdmins Succeeded');
72                         deferred.resolve(res.data);
73                     }
74                 })
75                 .catch( status => {
76                         this.$log.error('AdminsService::getAccountAdmins Failed', status);
77                     deferred.reject(status);
78                 });
79            
80             return deferred.promise;
81         }
82
83         getAdminAppsRoles(orgUserId) {
84             let deferred = this.$q.defer();
85             //this.$log.info('AdminsService::getAdminAppsRoles.adminAppsRoles');
86
87             this.$http({
88                 method: "GET",
89                 url: this.conf.api.adminAppsRoles,
90                 params: {user: orgUserId},
91                 cache: false,
92                 headers: {
93                     'X-ECOMP-RequestID':this.uuid.generate()
94                 }
95             }).then( res => {
96                     // If response comes back as a redirected HTML page which IS NOT a success
97                     if (this.utilsService.isValidJSON(res)=== false) {
98                         this.$log.error('AdminsService::getAdminAppsRoles.adminAppsRoles Failed');
99                         deferred.reject("AdminsService::getAdminAppsRoles.adminAppsRoles Failed");
100                     } else {
101                         // this.$log.info('AdminsService::getAdminAppsRoles.adminAppsRoles Succeeded');
102                         deferred.resolve(res.data);
103                     }
104                 })
105                 .catch( status => {
106                         this.$log.error('AdminsService::getAdminAppsRoles.adminAppsRoles Failed', status);
107                     deferred.reject(status);
108                 });
109
110             return deferred.promise;
111         }
112         /*Author: Rui*/
113         getRolesByApp(appId) {
114             let deferred = this.$q.defer();
115             this.$log.info('AdminsService::getRolesByApp');
116             let url = this.conf.api.adminAppsRoles + '/' + appId;
117             this.$http({
118                 method: "GET",
119                 url: url,
120                 cache: false,
121                 headers: {
122                     'X-ECOMP-RequestID':this.uuid.generate()
123                 }
124             }).then( res => {
125                     // If response comes back as a redirected HTML page which IS NOT a success
126                     if (Object.keys(res.data).length == 0) {
127                         deferred.reject("AdminsService::getAdminAppsRoles.getRolesByApp Failed");
128                     } else {
129                         this.$log.info('AdminsService::getAdminAppsRoles.getRolesByApp Succeeded');
130                         deferred.resolve(res.data);
131                     }
132                 })
133                 .catch( status => {
134                     deferred.reject(status);
135                 });
136             
137             return deferred.promise;
138         }
139         
140         updateAdminAppsRoles(newAdminAppRoles) {
141             let deferred = this.$q.defer();
142             // this.$log.info('AdminsService::updateAdminAppsRoles');
143             this.$http({
144                 method: "PUT",
145                 url: this.conf.api.adminAppsRoles,
146                 data: newAdminAppRoles,
147                 headers: {
148                     'X-ECOMP-RequestID':this.uuid.generate()
149                 }
150             }).then( res => {
151                 if (this.utilsService.isValidJSON(res)=== false) {
152                     this.$log.error('AdminsService::updateAdminAppsRoles Failed');
153                     deferred.reject("AdminsService::updateAdminAppsRoles Failed");
154                 } else {
155                     //this.$log.info('AdminsService::updateAdminAppsRoles success:');
156                     deferred.resolve(res.data);
157                 }
158
159                 })
160                 .catch( status => {
161                     this.$log.error('AdminsService::updateAdminAppsRoles: rejection:' + status);
162                     deferred.reject(status);
163                 });
164
165             return deferred.promise;
166         }
167         
168         /**
169          * Tests the specified password against complexity requirements.
170          * Returns an explanation message if the test fails; null if it passes.
171          */
172         isComplexPassword(str) {
173                 let minLength = 8;
174                 let message = 'Password is too simple.  Minimum length is '+ minLength + ', '
175                                           + 'and it must use letters, digits and special characters.';
176                 if (str == null)
177                         return message;
178
179                 let hasLetter = false;
180                 let hasDigit = false;
181                 let hasSpecial = false;
182                 var code, i, len;
183                 for (i = 0, len = str.length; i < len; i++) {
184                         code = str.charCodeAt(i);
185                         if (code > 47 && code < 58) // numeric (0-9)
186                                 hasDigit = true;
187                         else if ((code > 64 && code < 91) || (code > 96 && code < 123)) // A-Z, a-z
188                                 hasLetter = true;
189                         else
190                                 hasSpecial = true;
191                 } // for
192
193                 if (str.length < minLength || !hasLetter || !hasDigit || !hasSpecial)
194                         return message;
195                 
196                 // All is well.
197                 return null;
198         }
199         
200         addNewUser(newUser,checkDuplicate) {
201                 // this.$log.info(newContactUs)
202                 let deferred = this.$q.defer();
203             // this.$log.info('ContactUsService:: add Contact Us' + JSON.stringify(newContactUs));
204             
205             var newUserObj={
206                         firstName:newUser.firstName,
207                         middleInitial:newUser.middleName,
208                         lastName:newUser.lastName,
209                         email:newUser.emailAddress,
210                         loginId:newUser.loginId,
211                         loginPwd:newUser.loginPwd,                      
212             };
213             this.$http({
214                 url: this.conf.api.saveNewUser + "?isCheck=" + checkDuplicate,
215                 method: 'POST',
216                 cache: false,
217                 headers: {
218                     'X-ECOMP-RequestID':this.uuid.generate()
219                 },
220                 data: newUserObj
221             }).then(res => {
222                 // this.$log.info('ContactUsService:: add Contact Us res' ,res);
223                 // If response comes back as a redirected HTML page which IS NOT a success
224                 if (res==null || Object.keys(res.data).length == 0 || res.data.message == 'failure') {
225                     deferred.reject("Add new User failed");
226                     this.$log.error('adminService:: add New User failed');
227                 } else {
228                     deferred.resolve(res.data);
229                 }
230             }).catch(errRes => {
231                    deferred.reject(errRes);
232              });
233             return deferred.promise;
234         }
235         
236     }
237     AdminsService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
238     angular.module('ecompApp').service('adminsService', AdminsService)
239 })();