Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [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 | */ |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 16 | |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 17 | (function () { |
| 18 | "use strict"; |
| 19 | |
| 20 | angular.module('cordRest', []) |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 21 | .factory('SetCSRFToken', function setCSRFToken($cookies) { |
| 22 | return { |
| 23 | request: function(request){ |
| 24 | request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken'); |
Matteo Scandolo | b0e65a6 | 2016-01-28 11:05:15 -0800 | [diff] [blame] | 25 | request.headers['sessionId'] = $cookies.get('sessionid'); |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 26 | return request; |
| 27 | } |
| 28 | }; |
| 29 | }) |
Matteo Scandolo | 94992fd | 2016-01-27 13:51:07 -0800 | [diff] [blame] | 30 | .service('User', function($http, $q, $cookies, cordConfig){ |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 31 | this.login = function(username, password){ |
| 32 | var deferred = $q.defer(); |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 33 | var user; |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 34 | |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 35 | // logging in the user |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 36 | $http.post(cordConfig.url + '/xoslib/login/', {username: username, password: password}) |
| 37 | .then(function(res){ |
Matteo Scandolo | 94992fd | 2016-01-27 13:51:07 -0800 | [diff] [blame] | 38 | $cookies.put('user', res.data.user); |
Matteo Scandolo | cddae20 | 2016-01-27 17:59:00 -0800 | [diff] [blame] | 39 | $cookies.put('sessionid', res.data.xossessionid); |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 40 | user = JSON.parse(res.data.user); |
| 41 | return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id); |
Matteo Scandolo | b0e65a6 | 2016-01-28 11:05:15 -0800 | [diff] [blame] | 42 | }) |
Matteo Scandolo | 3eaeb3e | 2016-01-28 13:26:50 -0800 | [diff] [blame] | 43 | .then(function(subscribers){ |
| 44 | // subscribers are an array because the way Django perform query |
| 45 | // but one user is related to only one subscriber |
| 46 | |
| 47 | $cookies.put('subscriberId', subscribers.data[0].id); |
Matteo Scandolo | b0e65a6 | 2016-01-28 11:05:15 -0800 | [diff] [blame] | 48 | deferred.resolve(user); |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 49 | }) |
| 50 | .catch(function(e){ |
Matteo Scandolo | b0e65a6 | 2016-01-28 11:05:15 -0800 | [diff] [blame] | 51 | deferred.reject(e); |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 52 | throw new Error(e); |
| 53 | }); |
| 54 | |
| 55 | return deferred.promise; |
| 56 | }; |
Matteo Scandolo | 94992fd | 2016-01-27 13:51:07 -0800 | [diff] [blame] | 57 | |
| 58 | this.isLoggedIn = function(){ |
| 59 | var user = $cookies.get('user'); |
| 60 | if( angular.isDefined(user)){ |
| 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | }; |
| 65 | |
| 66 | this.logout = function(){ |
| 67 | var deferred = $q.defer(); |
Matteo Scandolo | cddae20 | 2016-01-27 17:59:00 -0800 | [diff] [blame] | 68 | var sessionId = $cookies.get('sessionid'); |
| 69 | $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId}) |
| 70 | .then(function(res){ |
| 71 | $cookies.remove('user'); |
| 72 | deferred.resolve(); |
| 73 | }) |
| 74 | .catch(function(e){ |
| 75 | throw new Error(e); |
| 76 | }); |
| 77 | |
Matteo Scandolo | 94992fd | 2016-01-27 13:51:07 -0800 | [diff] [blame] | 78 | return deferred.promise; |
| 79 | }; |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 80 | }) |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 81 | .service('Subscribers', function($resource, cordConfig){ |
| 82 | return $resource(cordConfig.url + '/xoslib/rs/subscriber'); |
| 83 | }) |
Matteo Scandolo | 10fb87c | 2016-01-27 14:30:22 -0800 | [diff] [blame] | 84 | .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){ |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 85 | return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, { |
| 86 | query: { |
| 87 | method: 'GET', |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 88 | isArray: true, |
Matteo Scandolo | 10fb87c | 2016-01-27 14:30:22 -0800 | [diff] [blame] | 89 | interceptor: { |
| 90 | response: function(res){ |
| 91 | // this is used to fake some data that are not XOS related, |
| 92 | // but can be provided by any external services |
| 93 | |
| 94 | // add an icon to the user |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 95 | res.data.map(function(user){ |
Matteo Scandolo | 10fb87c | 2016-01-27 14:30:22 -0800 | [diff] [blame] | 96 | switch (user.name){ |
| 97 | case 'Mom\'s PC': |
| 98 | user['icon_id'] = 'mom'; |
| 99 | break |
| 100 | case 'Jack\'s Laptop': |
| 101 | user['icon_id'] = 'boy2'; |
| 102 | break |
| 103 | case 'Jill\'s Laptop': |
| 104 | user['icon_id'] = 'girl1'; |
| 105 | break |
| 106 | case 'Dad\'s PC': |
| 107 | user['icon_id'] = 'dad'; |
| 108 | break |
| 109 | } |
| 110 | |
| 111 | return user; |
| 112 | }); |
| 113 | |
| 114 | // add a random login date to the user |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 115 | res.data.forEach(function(user){ |
Matteo Scandolo | 10fb87c | 2016-01-27 14:30:22 -0800 | [diff] [blame] | 116 | if(!angular.isDefined(cordConfig.userActivity[user.id])){ |
| 117 | var date = Helpers.randomDate(new Date(2015, 0, 1), new Date()); |
| 118 | cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime'); |
| 119 | } |
| 120 | }); |
| 121 | return res.data; |
| 122 | } |
| 123 | } |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 124 | } |
| 125 | }); |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 126 | }) |
Matteo Scandolo | 90621b3 | 2016-01-27 16:40:21 -0800 | [diff] [blame] | 127 | .service('SubscriberUsersUrlFilterLevel', function($q, $http, cordConfig){ |
| 128 | this.updateUrlFilterLevel = function(subscriberId, userId, level){ |
| 129 | var deferred = $q.defer(); |
| 130 | |
| 131 | $http.put(cordConfig.url + '/xoslib/rs/subscriber/' + subscriberId + '/users/' + userId + '/url_filter/' + level) |
| 132 | .then(function(res){ |
| 133 | deferred.resolve(res); |
| 134 | }) |
| 135 | .catch(function(e){ |
| 136 | throw new Error(e); |
| 137 | }); |
| 138 | |
| 139 | return deferred.promise; |
| 140 | }; |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 141 | }); |
| 142 | }()); |