nexus site path corrected
[portal.git] / ecomp-portal-FE / 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 'use strict';
21
22 (function () {
23     class AdminsService {
24         constructor($q, $log, $http, conf,uuid) {
25             this.$q = $q;
26             this.$log = $log;
27             this.$http = $http;
28             this.conf = conf;
29             this.uuid = uuid;
30         }
31
32         getAccountAdmins() {
33             let deferred = this.$q.defer();
34             this.$log.info('AdminsService::get all applications admins list');
35             this.$http({
36                     method: "GET",
37                     cache: false,
38                     url: this.conf.api.accountAdmins,
39                     headers: {
40                         'X-ECOMP-RequestID':this.uuid.generate()
41                     }
42                 })
43                 .then( res => {
44                     if (Object.keys(res.data).length == 0) {
45                         deferred.reject("AdminsService::getAccountAdmins Failed");
46                     } else {
47                         this.$log.info('AdminsService::getAccountAdmins Succeeded');
48                         deferred.resolve(res.data);
49                     }
50                 })
51                 .catch( status => {
52                     deferred.reject(status);
53                 });
54            
55             return deferred.promise;
56         }
57
58         getAdminAppsRoles(orgUserId) {
59             let deferred = this.$q.defer();
60             this.$log.info('AdminsService::getAdminAppsRoles.adminAppsRoles');
61
62             this.$http({
63                 method: "GET",
64                 url: this.conf.api.adminAppsRoles,
65                 params: {orgUserId: orgUserId},
66                 cache: false,
67                 headers: {
68                     'X-ECOMP-RequestID':this.uuid.generate()
69                 }
70             }).then( res => {
71                     if (Object.keys(res.data).length == 0) {
72                         deferred.reject("AdminsService::getAdminAppsRoles.adminAppsRoles Failed");
73                     } else {
74                         this.$log.info('AdminsService::getAdminAppsRoles.adminAppsRoles Succeeded');
75                         deferred.resolve(res.data);
76                     }
77                 })
78                 .catch( status => {
79                     deferred.reject(status);
80                 });
81
82             return deferred.promise;
83         }
84
85         updateAdminAppsRoles(newAdminAppRoles) {
86             let deferred = this.$q.defer();
87             this.$log.info('AdminsService::updateAdminAppsRoles');
88             this.$http({
89                 method: "PUT",
90                 url: this.conf.api.adminAppsRoles,
91                 data: newAdminAppRoles,
92                 headers: {
93                     'X-ECOMP-RequestID':this.uuid.generate()
94                 }
95             }).then( res => {
96                     deferred.resolve(res.data);
97                 })
98                 .catch( status => {
99                     deferred.reject(status);
100                 });
101
102             return deferred.promise;
103         }
104         
105         addNewUser(newUser,checkDuplicate) {
106                 // this.$log.info(newContactUs)
107                 let deferred = this.$q.defer();
108             // this.$log.info('ContactUsService:: add Contact Us' + JSON.stringify(newContactUs));
109             
110             var newUserObj={
111                         firstName:newUser.firstName,
112                         middleInitial:newUser.middleName,
113                         lastName:newUser.lastName,
114                         email:newUser.emailAddress,
115                         loginId:newUser.loginId,
116                         loginPwd:newUser.loginPwd,                      
117             };
118             this.$http({
119                 url: this.conf.api.saveNewUser + "?isCheck=" + checkDuplicate,
120                 method: 'POST',
121                 cache: false,
122                 headers: {
123                     'X-ECOMP-RequestID':this.uuid.generate()
124                 },
125                 data: newUserObj
126             }).then(res => {
127                 // this.$log.info('ContactUsService:: add Contact Us res' ,res);
128                 // If response comes back as a redirected HTML page which IS NOT a success
129                 if (res==null || Object.keys(res.data).length == 0 || res.data.message == 'failure') {
130                     deferred.reject("Add new User failed");
131                     this.$log.error('adminService:: add New User failed');
132                 } else {
133                     deferred.resolve(res.data);
134                 }
135             }).catch(errRes => {
136                    deferred.reject(errRes);
137              });
138             return deferred.promise;
139         }
140                 
141         /**
142          * Tests the specified password against complexity requirements.
143          * Returns an explanation message if the test fails; null if it passes.
144          */
145         isComplexPassword(str) {
146                 let minLength = 8;
147                 let message = 'Password is too simple.  Minimum length is '+ minLength + ', '
148                                           + 'and it must use letters, digits and special characters.';
149                 if (str == null)
150                         return message;
151
152                 let hasLetter = false;
153                 let hasDigit = false;
154                 let hasSpecial = false;
155                 var code, i, len;
156                 for (i = 0, len = str.length; i < len; i++) {
157                         code = str.charCodeAt(i);
158                         if (code > 47 && code < 58) // numeric (0-9)
159                                 hasDigit = true;
160                         else if ((code > 64 && code < 91) || (code > 96 && code < 123)) // A-Z, a-z
161                                 hasLetter = true;
162                         else
163                                 hasSpecial = true;
164                 } // for
165
166                 if (str.length < minLength || !hasLetter || !hasDigit || !hasSpecial)
167                         return message;
168                 
169                 // All is well.
170                 return null;
171         }
172
173     }
174     AdminsService.$inject = ['$q', '$log', '$http', 'conf','uuid4'];
175     angular.module('ecompApp').service('adminsService', AdminsService)
176 })();