Matteo Scandolo | bf7c366 | 2016-06-02 20:30:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | }) |
Matteo Scandolo | be342fa | 2016-06-08 15:54:55 -0700 | [diff] [blame^] | 36 | .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 Scandolo | bf7c366 | 2016-06-02 20:30:15 -0700 | [diff] [blame] | 45 | .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 |
| 61 | |
| 62 | $cookies.put('subscriberId', subscribers.data[0].id); |
| 63 | deferred.resolve(user); |
| 64 | }) |
| 65 | .catch(function(e){ |
| 66 | deferred.reject(e); |
| 67 | throw new Error(e); |
| 68 | }); |
| 69 | |
| 70 | return deferred.promise; |
| 71 | }; |
| 72 | |
| 73 | this.isLoggedIn = function(){ |
| 74 | var user = $cookies.get('user'); |
| 75 | if( angular.isDefined(user)){ |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | }; |
| 80 | |
| 81 | this.logout = function(){ |
| 82 | var deferred = $q.defer(); |
| 83 | var sessionId = $cookies.get('sessionid'); |
| 84 | $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId}) |
| 85 | .then(function(res){ |
| 86 | $cookies.remove('user'); |
| 87 | deferred.resolve(); |
| 88 | }) |
| 89 | .catch(function(e){ |
| 90 | throw new Error(e); |
| 91 | }); |
| 92 | |
| 93 | return deferred.promise; |
| 94 | }; |
| 95 | }); |
| 96 | }()); |