blob: 45ec41b0c2ba54ef68555128b15775b415788c22 [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);
Matteo Scandolocddae202016-01-27 17:59:00 -080028 $cookies.put('sessionid', res.data.xossessionid);
Matteo Scandolo94992fd2016-01-27 13:51:07 -080029 deferred.resolve(JSON.parse(res.data.user));
Matteo Scandolo39c65a52016-01-27 12:00:18 -080030 })
31 .catch(function(e){
32 throw new Error(e);
33 });
34
35 return deferred.promise;
36 };
Matteo Scandolo94992fd2016-01-27 13:51:07 -080037
38 this.isLoggedIn = function(){
39 var user = $cookies.get('user');
40 if( angular.isDefined(user)){
41 return true;
42 }
43 return false;
44 };
45
46 this.logout = function(){
47 var deferred = $q.defer();
Matteo Scandolocddae202016-01-27 17:59:00 -080048 var sessionId = $cookies.get('sessionid');
49 $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
50 .then(function(res){
51 $cookies.remove('user');
52 deferred.resolve();
53 })
54 .catch(function(e){
55 throw new Error(e);
56 });
57
Matteo Scandolo94992fd2016-01-27 13:51:07 -080058 return deferred.promise;
59 };
Matteo Scandolo39c65a52016-01-27 12:00:18 -080060 })
Matteo Scandolo61bea662016-01-26 17:21:39 -080061 .service('Subscribers', function($resource, cordConfig){
62 return $resource(cordConfig.url + '/xoslib/rs/subscriber');
63 })
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080064 .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){
Matteo Scandolo61bea662016-01-26 17:21:39 -080065 return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, {
66 query: {
67 method: 'GET',
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080068 isArray: false,
69 interceptor: {
70 response: function(res){
71 // this is used to fake some data that are not XOS related,
72 // but can be provided by any external services
73
74 // add an icon to the user
75 res.data.users.map(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080076 switch (user.name){
77 case 'Mom\'s PC':
78 user['icon_id'] = 'mom';
79 break
80 case 'Jack\'s Laptop':
81 user['icon_id'] = 'boy2';
82 break
83 case 'Jill\'s Laptop':
84 user['icon_id'] = 'girl1';
85 break
86 case 'Dad\'s PC':
87 user['icon_id'] = 'dad';
88 break
89 }
90
91 return user;
92 });
93
94 // add a random login date to the user
95 res.data.users.forEach(function(user){
96 if(!angular.isDefined(cordConfig.userActivity[user.id])){
97 var date = Helpers.randomDate(new Date(2015, 0, 1), new Date());
98 cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime');
99 }
100 });
101 return res.data;
102 }
103 }
Matteo Scandolo61bea662016-01-26 17:21:39 -0800104 }
105 });
Matteo Scandolo39c65a52016-01-27 12:00:18 -0800106 })
Matteo Scandolo90621b32016-01-27 16:40:21 -0800107 .service('SubscriberUsersUrlFilterLevel', function($q, $http, cordConfig){
108 this.updateUrlFilterLevel = function(subscriberId, userId, level){
109 var deferred = $q.defer();
110
111 $http.put(cordConfig.url + '/xoslib/rs/subscriber/' + subscriberId + '/users/' + userId + '/url_filter/' + level)
112 .then(function(res){
113 deferred.resolve(res);
114 })
115 .catch(function(e){
116 throw new Error(e);
117 });
118
119 return deferred.promise;
120 };
Matteo Scandoloe0628502016-01-27 14:42:42 -0800121 });
122}());