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