blob: 6d3baef465299b4e1979fc1827884a2281933d67 [file] [log] [blame]
Matteo Scandolobf7c3662016-06-02 20:30:15 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17(function () {
18 "use strict";
19
20 function randomEnabled(){
21 var levels = ["enabled", "disabled"];
22 return levels[Math.round(Math.random())];
23 };
24
25
26 angular.module('mCord')
27 .factory('SetCSRFToken', function setCSRFToken($cookies) {
28 return {
29 request: function(request){
30 request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
31 request.headers['sessionId'] = $cookies.get('sessionid');
32 return request;
33 }
34 };
35 })
36 .service('User', function($http, $q, $cookies, cordConfig){
37 this.login = function(username, password){
38 var deferred = $q.defer();
39 var user;
40
41 // logging in the user
42 $http.post(cordConfig.url + '/api/utility/login/', {username: username, password: password})
43 .then(function(res){
44 $cookies.put('user', res.data.user);
45 $cookies.put('sessionid', res.data.xossessionid);
46 user = JSON.parse(res.data.user);
47 return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id);
48 })
49 .then(function(subscribers){
50 // subscribers are an array because the way Django perform query
51 // but one user is related to only one subscriber
52
53 $cookies.put('subscriberId', subscribers.data[0].id);
54 deferred.resolve(user);
55 })
56 .catch(function(e){
57 deferred.reject(e);
58 throw new Error(e);
59 });
60
61 return deferred.promise;
62 };
63
64 this.isLoggedIn = function(){
65 var user = $cookies.get('user');
66 if( angular.isDefined(user)){
67 return true;
68 }
69 return false;
70 };
71
72 this.logout = function(){
73 var deferred = $q.defer();
74 var sessionId = $cookies.get('sessionid');
75 $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
76 .then(function(res){
77 $cookies.remove('user');
78 deferred.resolve();
79 })
80 .catch(function(e){
81 throw new Error(e);
82 });
83
84 return deferred.promise;
85 };
86 });
87}());