OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2021-10-14 3052936fed9166521b0557a36df83eb11a5e51ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
 
define(['app'], function (app) {
    app.factory('Principal', ['$q', 'User', '$log', '$rootScope', '$translate', function ($q, User, $log, $rootScope, $translate) {
        //  신분, 진짜인지 증명 값.
        var _identity,
            _authenticated = false;
 
        return {
            getIdentity: function () {
                return _identity;
            },
            isIdentityResolved: function () {
                return angular.isDefined(_identity);
            },
            isAuthenticated: function () {
                return _authenticated;
            },
            hasAuthority: function (authority) {
                if (!_authenticated) {
                    return $q.when(false);
                }
 
                return this.identity().then(function (_id) {
                    return _id.authorities && _id.authorities.indexOf(authority) !== -1;
                }, function (err) {
                    return false;
                });
            },
            hasAnyAuthority: function (authorities) {
                if (!_authenticated || !_identity) {
                    return false;
                }
 
                for (var count = 0; count < authorities.length; count++) {
                    if ($rootScope.authorities[authorities[count]] !== undefined) {
                        return $rootScope.authorities[authorities[count]];
                    }
                }
 
                return false;
            },
            authenticate: function (identity) {
                _identity = identity;
                _authenticated = (identity !== null && identity !== undefined) ? true : false;
            },
            identity: function (force) {
                var deferred = $q.defer();
 
                if (force === true) {
                    _identity = undefined;
                }
 
                // check and see if we have retrieved the identity data from the server.
                // if we have, reuse it by immediately resolving
                if (angular.isDefined(_identity)) {
                    deferred.resolve(_identity);
 
                    return deferred.promise;
                }
 
                // retrieve the identity data from the server, update the identity object, and then resolve.
                User.getUserSession({}).then(function (result) {
                        if (result.message.status === "success") {
                            if (result.data != null) {
                                $rootScope.user = result.data;  //  전역으로 사용하는 로그인 사용자 정보.
                                _identity = result.data;
                                _authenticated = true;
 
                                //  서버에서 마지막으로 사용한 다국어 클라이언트에 적용
                                $rootScope.language = result.data.language;
                                $translate.use(result.data.language);
                                $translate.refresh(result.data.language);
                                $rootScope.$broadcast("languageChange", {language : result.data.language});
 
                                //  웹 소켓 시작
                                $rootScope.$broadcast("startSocket");
                                deferred.resolve(_identity);
                            }
                            else {
                                throw {message: "사용자 세션이 존재하지 않습니다."};
                            }
                        }
                        else {
                            throw {message: "사용자 세션이 존재하지 않습니다."};
                        }
                    })
                    .catch(function () {
                        _identity = undefined;
                        _authenticated = false;
                        deferred.resolve(_identity);
                    });
 
                return deferred.promise;
            }
        };
    }]);
});