blob: e5ea7e0990844b9d8f3f5a11b3a5aa895956d514 [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 Scandolo61f05f52016-01-28 09:41:01 -080021 .factory('SetCSRFToken', function setCSRFToken($cookies) {
22 return {
23 request: function(request){
24 request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
Matteo Scandolob0e65a62016-01-28 11:05:15 -080025 request.headers['sessionId'] = $cookies.get('sessionid');
Matteo Scandolo61f05f52016-01-28 09:41:01 -080026 return request;
27 }
28 };
29 })
Matteo Scandolo94992fd2016-01-27 13:51:07 -080030 .service('User', function($http, $q, $cookies, cordConfig){
Matteo Scandolo39c65a52016-01-27 12:00:18 -080031 this.login = function(username, password){
32 var deferred = $q.defer();
Matteo Scandolo61f05f52016-01-28 09:41:01 -080033 var user;
Matteo Scandolo39c65a52016-01-27 12:00:18 -080034
Matteo Scandolo61f05f52016-01-28 09:41:01 -080035 // logging in the user
Matteo Scandolo39c65a52016-01-27 12:00:18 -080036 $http.post(cordConfig.url + '/xoslib/login/', {username: username, password: password})
37 .then(function(res){
Matteo Scandolo94992fd2016-01-27 13:51:07 -080038 $cookies.put('user', res.data.user);
Matteo Scandolocddae202016-01-27 17:59:00 -080039 $cookies.put('sessionid', res.data.xossessionid);
Matteo Scandolo61f05f52016-01-28 09:41:01 -080040 user = JSON.parse(res.data.user);
41 return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id);
Matteo Scandolob0e65a62016-01-28 11:05:15 -080042 })
43 .then(function(subscriber){
44 console.log(subscriber);
45 deferred.resolve(user);
Matteo Scandolo39c65a52016-01-27 12:00:18 -080046 })
47 .catch(function(e){
Matteo Scandolob0e65a62016-01-28 11:05:15 -080048 deferred.reject(e);
Matteo Scandolo39c65a52016-01-27 12:00:18 -080049 throw new Error(e);
50 });
51
52 return deferred.promise;
53 };
Matteo Scandolo94992fd2016-01-27 13:51:07 -080054
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 Scandolocddae202016-01-27 17:59:00 -080065 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 Scandolo94992fd2016-01-27 13:51:07 -080075 return deferred.promise;
76 };
Matteo Scandolo39c65a52016-01-27 12:00:18 -080077 })
Matteo Scandolo61bea662016-01-26 17:21:39 -080078 .service('Subscribers', function($resource, cordConfig){
79 return $resource(cordConfig.url + '/xoslib/rs/subscriber');
80 })
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080081 .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){
Matteo Scandolo61bea662016-01-26 17:21:39 -080082 return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, {
83 query: {
84 method: 'GET',
Matteo Scandolo61f05f52016-01-28 09:41:01 -080085 isArray: true,
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080086 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 Scandolo61f05f52016-01-28 09:41:01 -080092 res.data.map(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080093 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 Scandolo61f05f52016-01-28 09:41:01 -0800112 res.data.forEach(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -0800113 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 Scandolo61bea662016-01-26 17:21:39 -0800121 }
122 });
Matteo Scandolo39c65a52016-01-27 12:00:18 -0800123 })
Matteo Scandolo90621b32016-01-27 16:40:21 -0800124 .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 Scandoloe0628502016-01-27 14:42:42 -0800138 });
139}());