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