blob: 8ffd3c4dd45d2275796ad229546ab28ff13d8d04 [file] [log] [blame]
Matteo Scandoloe0628502016-01-27 14:42:42 -08001/*
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 Scandolo61bea662016-01-26 17:21:39 -080016
Matteo Scandoloe0628502016-01-27 14:42:42 -080017(function () {
18 "use strict";
19
20 angular.module('cordRest', [])
Matteo Scandolo94992fd2016-01-27 13:51:07 -080021 .service('User', function($http, $q, $cookies, cordConfig){
Matteo Scandolo39c65a52016-01-27 12:00:18 -080022 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 Scandolo94992fd2016-01-27 13:51:07 -080027 $cookies.put('user', res.data.user);
28 deferred.resolve(JSON.parse(res.data.user));
Matteo Scandolo39c65a52016-01-27 12:00:18 -080029 })
30 .catch(function(e){
31 throw new Error(e);
32 });
33
34 return deferred.promise;
35 };
Matteo Scandolo94992fd2016-01-27 13:51:07 -080036
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 Scandolo39c65a52016-01-27 12:00:18 -080051 })
Matteo Scandolo61bea662016-01-26 17:21:39 -080052 .service('Subscribers', function($resource, cordConfig){
53 return $resource(cordConfig.url + '/xoslib/rs/subscriber');
54 })
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080055 .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){
Matteo Scandolo61bea662016-01-26 17:21:39 -080056 return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, {
57 query: {
58 method: 'GET',
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080059 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 Scandolo10fb87c2016-01-27 14:30:22 -080067 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 Scandolo61bea662016-01-26 17:21:39 -080095 }
96 });
Matteo Scandolo39c65a52016-01-27 12:00:18 -080097 })
Matteo Scandolo90621b32016-01-27 16:40:21 -080098 .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 Scandoloe0628502016-01-27 14:42:42 -0800112 });
113}());