[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-common / client / app / services / admins / admins.service.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 /**
21  * Created by nnaffar on 11/22/2015.
22  */
23 'use strict';
24
25 (function () {
26     class AdminsService {
27         constructor($q, $log, $http, conf, uuid, utilsService) {
28             this.$q = $q;
29             this.$log = $log;
30             this.$http = $http;
31             this.conf = conf;
32             this.uuid = uuid;
33             this.utilsService = utilsService;
34         }
35
36         getAccountAdmins() {
37             let deferred = this.$q.defer();
38             //this.$log.info('AdminsService::get all applications admins list');
39             this.$http({
40                     method: "GET",
41                     cache: false,
42                     url: this.conf.api.accountAdmins,
43                     headers: {
44                         'X-ECOMP-RequestID':this.uuid.generate()
45                     }
46                 })
47                 .then( res => {
48                     // If response comes back as a redirected HTML page which IS NOT a success
49                     if (this.utilsService.isValidJSON(res)=== false) {
50                         this.$log.error('AdminsService::getAccountAdmins Failed');
51                         deferred.reject("AdminsService::getAccountAdmins Failed");
52                     } else {
53                         // this.$log.info('AdminsService::getAccountAdmins Succeeded');
54                         deferred.resolve(res.data);
55                     }
56                 })
57                 .catch( status => {
58                         this.$log.error('AdminsService::getAccountAdmins Failed', status);
59                     deferred.reject(status);
60                 });
61            
62             return deferred.promise;
63         }
64
65         getAdminAppsRoles(orgUserId) {
66             let deferred = this.$q.defer();
67             //this.$log.info('AdminsService::getAdminAppsRoles.adminAppsRoles');
68
69             this.$http({
70                 method: "GET",
71                 url: this.conf.api.adminAppsRoles,
72                 params: {user: orgUserId},
73                 cache: false,
74                 headers: {
75                     'X-ECOMP-RequestID':this.uuid.generate()
76                 }
77             }).then( res => {
78                     // If response comes back as a redirected HTML page which IS NOT a success
79                     if (this.utilsService.isValidJSON(res)=== false) {
80                         this.$log.error('AdminsService::getAdminAppsRoles.adminAppsRoles Failed');
81                         deferred.reject("AdminsService::getAdminAppsRoles.adminAppsRoles Failed");
82                     } else {
83                         // this.$log.info('AdminsService::getAdminAppsRoles.adminAppsRoles Succeeded');
84                         deferred.resolve(res.data);
85                     }
86                 })
87                 .catch( status => {
88                         this.$log.error('AdminsService::getAdminAppsRoles.adminAppsRoles Failed', status);
89                     deferred.reject(status);
90                 });
91
92             return deferred.promise;
93         }
94         /*Author: Rui*/
95         getRolesByApp(appId) {
96             let deferred = this.$q.defer();
97             this.$log.info('AdminsService::getRolesByApp');
98             let url = this.conf.api.adminAppsRoles + '/' + appId;
99             this.$http({
100                 method: "GET",
101                 url: url,
102                 cache: false,
103                 headers: {
104                     'X-ECOMP-RequestID':this.uuid.generate()
105                 }
106             }).then( res => {
107                     // If response comes back as a redirected HTML page which IS NOT a success
108                     if (Object.keys(res.data).length == 0) {
109                         deferred.reject("AdminsService::getAdminAppsRoles.getRolesByApp Failed");
110                     } else {
111                         this.$log.info('AdminsService::getAdminAppsRoles.getRolesByApp Succeeded');
112                         deferred.resolve(res.data);
113                     }
114                 })
115                 .catch( status => {
116                     deferred.reject(status);
117                 });
118             
119             return deferred.promise;
120         }
121         
122         updateAdminAppsRoles(newAdminAppRoles) {
123             let deferred = this.$q.defer();
124             // this.$log.info('AdminsService::updateAdminAppsRoles');
125             this.$http({
126                 method: "PUT",
127                 url: this.conf.api.adminAppsRoles,
128                 data: newAdminAppRoles,
129                 headers: {
130                     'X-ECOMP-RequestID':this.uuid.generate()
131                 }
132             }).then( res => {
133                 if (this.utilsService.isValidJSON(res)=== false) {
134                     this.$log.error('AdminsService::updateAdminAppsRoles Failed');
135                     deferred.reject("AdminsService::updateAdminAppsRoles Failed");
136                 } else {
137                     //this.$log.info('AdminsService::updateAdminAppsRoles success:');
138                     deferred.resolve(res.data);
139                 }
140
141                 })
142                 .catch( status => {
143                     this.$log.error('AdminsService::updateAdminAppsRoles: rejection:' + status);
144                     deferred.reject(status);
145                 });
146
147             return deferred.promise;
148         }
149         
150         /**
151          * Tests the specified password against complexity requirements.
152          * Returns an explanation message if the test fails; null if it passes.
153          */
154         isComplexPassword(str) {
155                 let minLength = 8;
156                 let message = 'Password is too simple.  Minimum length is '+ minLength + ', '
157                                           + 'and it must use letters, digits and special characters.';
158                 if (str == null)
159                         return message;
160
161                 let hasLetter = false;
162                 let hasDigit = false;
163                 let hasSpecial = false;
164                 var code, i, len;
165                 for (i = 0, len = str.length; i < len; i++) {
166                         code = str.charCodeAt(i);
167                         if (code > 47 && code < 58) // numeric (0-9)
168                                 hasDigit = true;
169                         else if ((code > 64 && code < 91) || (code > 96 && code < 123)) // A-Z, a-z
170                                 hasLetter = true;
171                         else
172                                 hasSpecial = true;
173                 } // for
174
175                 if (str.length < minLength || !hasLetter || !hasDigit || !hasSpecial)
176                         return message;
177                 
178                 // All is well.
179                 return null;
180         }
181         
182         addNewUser(newUser,checkDuplicate) {
183                 // this.$log.info(newContactUs)
184                 let deferred = this.$q.defer();
185             // this.$log.info('ContactUsService:: add Contact Us' + JSON.stringify(newContactUs));
186             
187             var newUserObj={
188                         firstName:newUser.firstName,
189                         middleInitial:newUser.middleName,
190                         lastName:newUser.lastName,
191                         email:newUser.emailAddress,
192                         loginId:newUser.loginId,
193                         loginPwd:newUser.loginPwd,                      
194             };
195             this.$http({
196                 url: this.conf.api.saveNewUser + "?isCheck=" + checkDuplicate,
197                 method: 'POST',
198                 cache: false,
199                 headers: {
200                     'X-ECOMP-RequestID':this.uuid.generate()
201                 },
202                 data: newUserObj
203             }).then(res => {
204                 // this.$log.info('ContactUsService:: add Contact Us res' ,res);
205                 // If response comes back as a redirected HTML page which IS NOT a success
206                 if (res==null || Object.keys(res.data).length == 0 || res.data.message == 'failure') {
207                     deferred.reject("Add new User failed");
208                     this.$log.error('adminService:: add New User failed');
209                 } else {
210                     deferred.resolve(res.data);
211                 }
212             }).catch(errRes => {
213                    deferred.reject(errRes);
214              });
215             return deferred.promise;
216         }
217         
218     }
219     AdminsService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
220     angular.module('ecompApp').service('adminsService', AdminsService)
221 })();