blob: 409c16e7ed09e35d83b05012d031787cbd8e7fee [file] [log] [blame]
/*
* Copyright 2015 Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
"use strict";
function randomEnabled(){
var levels = ["enabled", "disabled"];
return levels[Math.round(Math.random())];
};
angular.module('mCord')
.factory('SetCSRFToken', function setCSRFToken($cookies) {
return {
request: function(request){
request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
request.headers['sessionId'] = $cookies.get('sessionid');
return request;
}
};
})
.factory('clearCacheOnUpdate', function setCSRFToken($cookies) {
// return {
// request: function(request){
// request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
// request.headers['sessionId'] = $cookies.get('sessionid');
// return request;
// }
// };
})
.service('User', function($http, $q, $cookies, cordConfig){
this.login = function(username, password){
var deferred = $q.defer();
var user;
// logging in the user
$http.post(cordConfig.url + '/api/utility/login/', {username: username, password: password})
.then(function(res){
$cookies.put('user', res.data.user);
$cookies.put('sessionid', res.data.xossessionid);
user = JSON.parse(res.data.user);
return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id);
})
.then(function(subscribers){
// subscribers are an array because the way Django perform query
// but one user is related to only one subscriber
if(subscribers.data[0]){
$cookies.put('subscriberId', subscribers.data[0].id);
}
deferred.resolve(user);
})
.catch(function(e){
deferred.reject(e);
throw new Error(e);
});
return deferred.promise;
};
this.isLoggedIn = function(){
var user = $cookies.get('user');
if( angular.isDefined(user)){
return true;
}
return false;
};
this.logout = function(){
var deferred = $q.defer();
var sessionId = $cookies.get('sessionid');
$http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
.then(function(res){
$cookies.remove('user');
deferred.resolve();
})
.catch(function(e){
throw new Error(e);
});
return deferred.promise;
};
});
}());