blob: 409c16e7ed09e35d83b05012d031787cbd8e7fee [file] [log] [blame]
Matteo Scandolobf7c3662016-06-02 20:30:15 -07001/*
Brian O'Connor8fb63ec2017-08-03 22:46:35 -07002 * Copyright 2015 Open Networking Foundation
Matteo Scandolobf7c3662016-06-02 20:30:15 -07003 *
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 })
Matteo Scandolobe342fa2016-06-08 15:54:55 -070036 .factory('clearCacheOnUpdate', function setCSRFToken($cookies) {
37 // return {
38 // request: function(request){
39 // request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
40 // request.headers['sessionId'] = $cookies.get('sessionid');
41 // return request;
42 // }
43 // };
44 })
Matteo Scandolobf7c3662016-06-02 20:30:15 -070045 .service('User', function($http, $q, $cookies, cordConfig){
46 this.login = function(username, password){
47 var deferred = $q.defer();
48 var user;
49
50 // logging in the user
51 $http.post(cordConfig.url + '/api/utility/login/', {username: username, password: password})
52 .then(function(res){
53 $cookies.put('user', res.data.user);
54 $cookies.put('sessionid', res.data.xossessionid);
55 user = JSON.parse(res.data.user);
56 return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id);
57 })
58 .then(function(subscribers){
59 // subscribers are an array because the way Django perform query
60 // but one user is related to only one subscriber
Matteo Scandolo3b82a592016-06-21 12:10:35 +020061 if(subscribers.data[0]){
62 $cookies.put('subscriberId', subscribers.data[0].id);
63 }
Matteo Scandolobf7c3662016-06-02 20:30:15 -070064 deferred.resolve(user);
65 })
66 .catch(function(e){
67 deferred.reject(e);
68 throw new Error(e);
69 });
70
71 return deferred.promise;
72 };
73
74 this.isLoggedIn = function(){
75 var user = $cookies.get('user');
76 if( angular.isDefined(user)){
77 return true;
78 }
79 return false;
80 };
81
82 this.logout = function(){
83 var deferred = $q.defer();
84 var sessionId = $cookies.get('sessionid');
85 $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
86 .then(function(res){
87 $cookies.remove('user');
88 deferred.resolve();
89 })
90 .catch(function(e){
91 throw new Error(e);
92 });
93
94 return deferred.promise;
95 };
96 });
97}());