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