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