0608c5db614ced4639c1f97ffc82c97a90b47f01
3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 // DescriptorModelMeta API (NSD + VNFD)
21 var UserManagement
= {};
22 var Promise
= require('bluebird');
23 var rp
= require('request-promise');
24 var Promise
= require('promise');
25 var constants
= require('../../api_utils/constants');
26 var utils
= require('../../api_utils/utils');
27 var _
= require('lodash');
29 UserManagement
.get = function(req
) {
31 var api_server
= req
.query
['api_server'];
33 return new Promise(function(resolve
, reject
) {
36 uri
: utils
.confdPort(api_server
) + '/api/operational/user-config/user',
38 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
39 'Authorization': req
.session
&& req
.session
.authorization
41 forever
: constants
.FOREVER_ON
,
42 rejectUnauthorized
: false,
43 resolveWithFullResponse
: true
45 ]).then(function(result
) {
47 response
['data'] = {};
49 response
['data']['user'] = JSON
.parse(result
[0].body
)['rw-user:user'];
51 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
54 }).catch(function(error
) {
56 console
.log('Problem with UserManagement.get', error
);
57 response
.statusCode
= error
.statusCode
|| 500;
58 response
.errorMessage
= {
59 error
: 'Failed to get UserManagement' + error
66 UserManagement
.getProfile = function(req
) {
68 var api_server
= req
.query
['api_server'];
69 return new Promise(function(resolve
, reject
) {
72 userId
: req
.session
.userdata
.username
,
73 projectId
: req
.session
.projectId
75 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
80 UserManagement
.create = function(req
) {
82 var api_server
= req
.query
['api_server'];
87 return new Promise(function(resolve
, reject
) {
90 uri
: utils
.confdPort(api_server
) + '/api/config/user-config',
92 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
93 'Authorization': req
.session
&& req
.session
.authorization
95 forever
: constants
.FOREVER_ON
,
97 rejectUnauthorized
: false,
98 resolveWithFullResponse
: true
100 ]).then(function(result
) {
102 response
['data'] = {};
103 if (result
[0].body
) {
104 response
['data'] = result
[0].body
;
106 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
109 }).catch(function(error
) {
111 console
.log('Problem with UserManagement.create', error
);
112 response
.statusCode
= error
.statusCode
|| 500;
113 response
.errorMessage
= {
114 error
: 'Failed to create user' + error
120 UserManagement
.update = function(req
) {
122 var api_server
= req
.query
['api_server'];
123 var bodyData
= req
.body
;
127 var updateTasks
= [];
128 if(bodyData
.hasOwnProperty('old-password')) {
130 uri
: utils
.confdPort(api_server
) + '/api/operations/change-password',
132 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
133 'Authorization': req
.session
&& req
.session
.authorization
135 forever
: constants
.FOREVER_ON
,
138 'user-name' : bodyData
['user-name'],
139 'user-domain' : bodyData
['user-domain'],
140 'old-password' : bodyData
['old-password'],
141 'new-password' : bodyData
['new-password'],
142 'confirm-password' : bodyData
['confirm-password'],
145 rejectUnauthorized
: false,
146 resolveWithFullResponse
: true
148 updateTasks
.push(changePW
);
150 var updateUser
= rp({
151 uri
: utils
.confdPort(api_server
) + '/api/config/user-config',
153 headers
: _
.extend({}, constants
.HTTP_HEADERS
.accept
.data
, {
154 'Authorization': req
.session
&& req
.session
.authorization
156 forever
: constants
.FOREVER_ON
,
158 rejectUnauthorized
: false,
159 resolveWithFullResponse
: true
161 updateTasks
.push(updateUser
)
162 return new Promise(function(resolve
, reject
) {
165 ]).then(function(result
) {
167 response
['data'] = {};
168 if (result
[0].body
) {
169 response
['data'] = result
[0].body
;
171 response
.statusCode
= constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
174 }).catch(function(error
) {
176 console
.log('Problem with UserManagement.passwordChange', error
);
177 response
.statusCode
= error
.statusCode
|| 500;
178 response
.errorMessage
= {
179 error
: 'Failed to passwordChange user' + error
186 UserManagement
.delete = function(req
) {
188 var username
= req
.params
.username
;
189 var domain
= req
.params
.domain
;
190 var api_server
= req
.query
["api_server"];
191 var requestHeaders
= {};
192 var url
= `${utils.confdPort(api_server)}/api/config/user-config/user/${username},${domain}`
193 return new Promise(function(resolve
, reject
) {
194 _
.extend(requestHeaders
,
195 constants
.HTTP_HEADERS
.accept
.data
,
196 constants
.HTTP_HEADERS
.content_type
.data
, {
197 'Authorization': req
.session
&& req
.session
.authorization
202 headers
: requestHeaders
,
203 forever
: constants
.FOREVER_ON
,
204 rejectUnauthorized
: false,
205 }, function(error
, response
, body
) {
206 if (utils
.validateResponse('UserManagement.DELETE', error
, response
, body
, resolve
, reject
)) {
208 statusCode
: response
.statusCode
,
209 data
: JSON
.stringify(response
.body
)
215 module
.exports
= UserManagement
;