blob: db356176c9448d37eec86ed1cd3691a8ed9a2ac2 [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');
25 return request;
26 }
27 };
28 })
Matteo Scandolo94992fd2016-01-27 13:51:07 -080029 .service('User', function($http, $q, $cookies, cordConfig){
Matteo Scandolo39c65a52016-01-27 12:00:18 -080030 this.login = function(username, password){
31 var deferred = $q.defer();
Matteo Scandolo61f05f52016-01-28 09:41:01 -080032 var user;
Matteo Scandolo39c65a52016-01-27 12:00:18 -080033
Matteo Scandolo61f05f52016-01-28 09:41:01 -080034 // logging in the user
Matteo Scandolo39c65a52016-01-27 12:00:18 -080035 $http.post(cordConfig.url + '/xoslib/login/', {username: username, password: password})
36 .then(function(res){
Matteo Scandolo94992fd2016-01-27 13:51:07 -080037 $cookies.put('user', res.data.user);
Matteo Scandolocddae202016-01-27 17:59:00 -080038 $cookies.put('sessionid', res.data.xossessionid);
Matteo Scandolo61f05f52016-01-28 09:41:01 -080039 user = JSON.parse(res.data.user);
40 return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id);
41 }).then(function(subscriber){
42 console.log(subscriber);
43 deferred.resolve(user);
Matteo Scandolo39c65a52016-01-27 12:00:18 -080044 })
45 .catch(function(e){
46 throw new Error(e);
47 });
48
49 return deferred.promise;
50 };
Matteo Scandolo94992fd2016-01-27 13:51:07 -080051
52 this.isLoggedIn = function(){
53 var user = $cookies.get('user');
54 if( angular.isDefined(user)){
55 return true;
56 }
57 return false;
58 };
59
60 this.logout = function(){
61 var deferred = $q.defer();
Matteo Scandolocddae202016-01-27 17:59:00 -080062 var sessionId = $cookies.get('sessionid');
63 $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
64 .then(function(res){
65 $cookies.remove('user');
66 deferred.resolve();
67 })
68 .catch(function(e){
69 throw new Error(e);
70 });
71
Matteo Scandolo94992fd2016-01-27 13:51:07 -080072 return deferred.promise;
73 };
Matteo Scandolo39c65a52016-01-27 12:00:18 -080074 })
Matteo Scandolo61bea662016-01-26 17:21:39 -080075 .service('Subscribers', function($resource, cordConfig){
76 return $resource(cordConfig.url + '/xoslib/rs/subscriber');
77 })
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080078 .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){
Matteo Scandolo61bea662016-01-26 17:21:39 -080079 return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, {
80 query: {
81 method: 'GET',
Matteo Scandolo61f05f52016-01-28 09:41:01 -080082 isArray: true,
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080083 interceptor: {
84 response: function(res){
85 // this is used to fake some data that are not XOS related,
86 // but can be provided by any external services
87
88 // add an icon to the user
Matteo Scandolo61f05f52016-01-28 09:41:01 -080089 res.data.map(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080090 switch (user.name){
91 case 'Mom\'s PC':
92 user['icon_id'] = 'mom';
93 break
94 case 'Jack\'s Laptop':
95 user['icon_id'] = 'boy2';
96 break
97 case 'Jill\'s Laptop':
98 user['icon_id'] = 'girl1';
99 break
100 case 'Dad\'s PC':
101 user['icon_id'] = 'dad';
102 break
103 }
104
105 return user;
106 });
107
108 // add a random login date to the user
Matteo Scandolo61f05f52016-01-28 09:41:01 -0800109 res.data.forEach(function(user){
Matteo Scandolo10fb87c2016-01-27 14:30:22 -0800110 if(!angular.isDefined(cordConfig.userActivity[user.id])){
111 var date = Helpers.randomDate(new Date(2015, 0, 1), new Date());
112 cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime');
113 }
114 });
115 return res.data;
116 }
117 }
Matteo Scandolo61bea662016-01-26 17:21:39 -0800118 }
119 });
Matteo Scandolo39c65a52016-01-27 12:00:18 -0800120 })
Matteo Scandolo90621b32016-01-27 16:40:21 -0800121 .service('SubscriberUsersUrlFilterLevel', function($q, $http, cordConfig){
122 this.updateUrlFilterLevel = function(subscriberId, userId, level){
123 var deferred = $q.defer();
124
125 $http.put(cordConfig.url + '/xoslib/rs/subscriber/' + subscriberId + '/users/' + userId + '/url_filter/' + level)
126 .then(function(res){
127 deferred.resolve(res);
128 })
129 .catch(function(e){
130 throw new Error(e);
131 });
132
133 return deferred.promise;
134 };
Matteo Scandoloe0628502016-01-27 14:42:42 -0800135 });
136}());