blob: c2647c980b5ba556559cc6fff75da380a1a27bd1 [file] [log] [blame]
Matteo Scandolobf14f882016-06-02 10:01:34 -07001/*
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 */
16
17(function () {
18 "use strict";
19
20 function randomEnabled(){
21 var levels = ["enabled", "disabled"];
22 return levels[Math.round(Math.random())];
23 };
24
25
Matteo Scandoloa3844ec2016-06-02 15:45:19 -070026 angular.module('mCord')
Matteo Scandolobf14f882016-06-02 10:01:34 -070027 .factory('SetCSRFToken', function setCSRFToken($cookies) {
28 return {
29 request: function(request){
30 request.headers['X-CSRFToken'] = $cookies.get('xoscsrftoken');
31 request.headers['sessionId'] = $cookies.get('sessionid');
32 return request;
33 }
34 };
35 })
36 .service('User', function($http, $q, $cookies, cordConfig){
37 this.login = function(username, password){
38 var deferred = $q.defer();
39 var user;
40
41 // logging in the user
42 $http.post(cordConfig.url + '/api/utility/login/', {username: username, password: password})
43 .then(function(res){
44 $cookies.put('user', res.data.user);
45 $cookies.put('sessionid', res.data.xossessionid);
46 user = JSON.parse(res.data.user);
47 return $http.get(cordConfig.url + '/xos/tenantrootprivileges?user=' + user.id);
48 })
49 .then(function(subscribers){
50 // subscribers are an array because the way Django perform query
51 // but one user is related to only one subscriber
52
53 $cookies.put('subscriberId', subscribers.data[0].id);
54 deferred.resolve(user);
55 })
56 .catch(function(e){
57 deferred.reject(e);
58 throw new Error(e);
59 });
60
61 return deferred.promise;
62 };
63
64 this.isLoggedIn = function(){
65 var user = $cookies.get('user');
66 if( angular.isDefined(user)){
67 return true;
68 }
69 return false;
70 };
71
72 this.logout = function(){
73 var deferred = $q.defer();
74 var sessionId = $cookies.get('sessionid');
75 $http.post(cordConfig.url + '/xoslib/logout/', {xossessionid: sessionId})
76 .then(function(res){
77 $cookies.remove('user');
78 deferred.resolve();
79 })
80 .catch(function(e){
81 throw new Error(e);
82 });
83
84 return deferred.promise;
85 };
86 })
87 .service('Subscribers', function($resource, cordConfig){
88 return $resource(cordConfig.url + '/xoslib/rs/subscriber');
89 })
90 .service('SubscriberUsers', function($resource, $filter, cordConfig, Helpers){
91 return $resource(cordConfig.url + '/xoslib/rs/subscriber/:subscriberId/users/:id', {}, {
92 query: {
93 method: 'GET',
94 isArray: true,
95 cache: true,
96 interceptor: {
97 response: function(res){
98 // this is used to fake some data that are not XOS related,
99 // but can be provided by any external services
100
101 // add an icon to the user
Matteo Scandolo8cb844e2016-06-02 11:39:02 -0700102 res.data = res.data.map(function(user, i){
103 switch (i){
Matteo Scandolobf14f882016-06-02 10:01:34 -0700104 case 0:
105 user['icon_id'] = 'student1';
106 break
107 case 1:
108 user['icon_id'] = 'student2';
109 break
110 case 2:
111 user['icon_id'] = 'student3';
112 break
113 case 3:
114 user['icon_id'] = 'student4';
115 break
116 }
117
118 user.level = randomEnabled()
119
120 return user;
121 });
122
123 // add a random login date to the user
124 res.data.forEach(function(user){
125 if(!angular.isDefined(cordConfig.userActivity[user.id])){
126 var date = Helpers.randomDate(new Date(2015, 0, 1), new Date());
127 cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime');
128 }
129 });
130 return res.data;
131 }
132 }
133 }
134 });
135 })
136 .service('SubscriberUsersUrlFilterLevel', function($q, $http, cordConfig){
137 this.updateUrlFilterLevel = function(subscriberId, userId, level){
138 var deferred = $q.defer();
139
140 $http.put(cordConfig.url + '/xoslib/rs/subscriber/' + subscriberId + '/users/' + userId + '/url_filter/' + level)
141 .then(function(res){
142 deferred.resolve(res);
143 })
144 .catch(function(e){
145 throw new Error(e);
146 });
147
148 return deferred.promise;
149 };
150 });
151}());