1/**
2 * userModel
3 *
4 * @module app/common/services/userModel
5 * @exports userModel
6 * @name userModel
7
8 */
9
10window.angular && (function(angular) {
11  'use strict';
12
13  angular.module('app.common.services').service('userModel', [
14    '$cookies', 'APIUtils',
15    function($cookies, APIUtils) {
16      return {
17        login: function(username, password, callback) {
18          APIUtils.login(username, password, function(response, error) {
19            if (response &&
20                (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS ||
21                 response.status === undefined)) {
22              sessionStorage.setItem('LOGIN_ID', username);
23              callback(true);
24            } else if (
25                response && response.data && response.data.data &&
26                response.data.data.description) {
27              callback(false, response.data.data.description);
28            } else if (response && response.statusText) {
29              callback(false, response.statusText);
30            } else if (error) {
31              callback(false, 'Server unreachable');
32            } else {
33              callback(false, 'Internal error');
34            }
35          });
36        },
37        isLoggedIn: function() {
38          if ((sessionStorage.getItem('LOGIN_ID') === null) &&
39              (($cookies.get('IsAuthenticated') === undefined) ||
40               ($cookies.get('IsAuthenticated') == 'false'))) {
41            return false;
42          }
43          return true;
44        },
45        logout: function(callback) {
46          APIUtils.logout(function(response, error) {
47            if (response &&
48                response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS) {
49              sessionStorage.removeItem('LOGIN_ID');
50              sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
51              $cookies.remove('IsAuthenticated');
52              callback(true);
53            } else if (response.status == APIUtils.API_RESPONSE.ERROR_STATUS) {
54              callback(false);
55            } else {
56              callback(false, error);
57            }
58          });
59        }
60      };
61    }
62  ]);
63})(window.angular);
64