blob: e97f09177879646816f8472058120dd0dd8684de [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 })
Matteo Scandolo3eaeb3e2016-01-28 13:26:50 -080043 .then(function(subscribers){
44 // subscribers are an array because the way Django perform query
45 // but one user is related to only one subscriber
46
47 $cookies.put('subscriberId', subscribers.data[0].id);
Matteo Scandolob0e65a62016-01-28 11:05:15 -080048 deferred.resolve(user);
Matteo Scandolo39c65a52016-01-27 12:00:18 -080049 })
50 .catch(function(e){
Matteo Scandolob0e65a62016-01-28 11:05:15 -080051 deferred.reject(e);
Matteo Scandolo39c65a52016-01-27 12:00:18 -080052 throw new Error(e);
53 });
54
55 return deferred.promise;
56 };
Matteo Scandolo94992fd2016-01-27 13:51:07 -080057
58 this.isLoggedIn = function(){
59 var user = $cookies.get('user');
60 if( angular.isDefined(user)){
61 return true;
62 }
63 return false;
64 };
65
66 this.logout = function(){
67 var deferred = $q.defer();
Matteo Scandolocddae202016-01-27 17:59:00 -080068 var sessionId = $cookies.get('sessionid');
69 $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
70 .then(function(res){
71 $cookies.remove('user');
72 deferred.resolve();
73 })
74 .catch(function(e){
75 throw new Error(e);
76 });
77
Matteo Scandolo94992fd2016-01-27 13:51:07 -080078 return deferred.promise;
79 };
Matteo Scandolo39c65a52016-01-27 12:00:18 -080080 })
Matteo Scandolo61bea662016-01-26 17:21:39 -080081 .service('Subscribers', function($resource, cordConfig){
82 return $resource(cordConfig.url + '/xoslib/rs/subscriber');
83 })
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080084 .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){
Matteo Scandolo61bea662016-01-26 17:21:39 -080085 return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, {
86 query: {
87 method: 'GET',
Matteo Scandolo61f05f52016-01-28 09:41:01 -080088 isArray: true,
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080089 interceptor: {
90 response: function(res){
91 // this is used to fake some data that are not XOS related,
92 // but can be provided by any external services
93
94 // add an icon to the user
Matteo Scandolo61f05f52016-01-28 09:41:01 -080095 res.data.map(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080096 switch (user.name){
97 case 'Mom\'s PC':
98 user['icon_id'] = 'mom';
99 break
100 case 'Jack\'s Laptop':
101 user['icon_id'] = 'boy2';
102 break
103 case 'Jill\'s Laptop':
104 user['icon_id'] = 'girl1';
105 break
106 case 'Dad\'s PC':
107 user['icon_id'] = 'dad';
108 break
109 }
110
111 return user;
112 });
113
114 // add a random login date to the user
Matteo Scandolo61f05f52016-01-28 09:41:01 -0800115 res.data.forEach(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -0800116 if(!angular.isDefined(cordConfig.userActivity[user.id])){
117 var date = Helpers.randomDate(new Date(2015, 0, 1), new Date());
118 cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime');
119 }
120 });
121 return res.data;
122 }
123 }
Matteo Scandolo61bea662016-01-26 17:21:39 -0800124 }
125 });
Matteo Scandolo39c65a52016-01-27 12:00:18 -0800126 })
Matteo Scandolo90621b32016-01-27 16:40:21 -0800127 .service('SubscriberUsersUrlFilterLevel', function($q, $http, cordConfig){
128 this.updateUrlFilterLevel = function(subscriberId, userId, level){
129 var deferred = $q.defer();
130
131 $http.put(cordConfig.url + '/xoslib/rs/subscriber/' + subscriberId + '/users/' + userId + '/url_filter/' + level)
132 .then(function(res){
133 deferred.resolve(res);
134 })
135 .catch(function(e){
136 throw new Error(e);
137 });
138
139 return deferred.promise;
140 };
Matteo Scandoloe0628502016-01-27 14:42:42 -0800141 });
142}());