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
99
100
101
102
103
104
105
106
107
108
'use strict';
 
define([
    'app',
    'angular'
], function (app, angular) {
    app.factory('Auth', ['$rootScope', '$state', '$q', 'Principal', 'AuthSession', '$log', '$resourceProvider', 'Permission', 'Workspace',
        function ($rootScope, $state, $q, Principal, AuthSession, $log, $resourceProvider, Permission, Workspace) {
            return {
                login : function (credentials, callback) {
                    var cb = callback || angular.noop;
                    var deferred = $q.defer();
 
                    AuthSession.login(credentials).then(function (result) {
                        // retrieve the logged account information
                        Principal.identity(true).then(function (account) {
                            deferred.resolve(account);
                        });
 
                        return cb();
                    }).catch(function (err) {
                        deferred.reject(err);
                        return cb(err);
                    }.bind(this));
 
                    return deferred.promise;
                },
                logout : function () {
                    AuthSession.logout();
                    Principal.authenticate(undefined);
                    // Reset state memory
                    $rootScope.previousStateName = undefined;
                    $rootScope.previousStateNameParams = undefined;
                },
                authorize : function (force) {
                    return Principal.identity(force)
                        .then(function () {
                            var isAuthenticated = Principal.isAuthenticated();
 
                            // an authenticated user can't access to login and register pages - 로그인된 사용자는 로그인 페이지로 가지 못하게 한다.
 
                            $log.debug("$rootScope.toState.name 확인 : " , $rootScope.toState.name);
                            $log.debug("isAuthenticated 확인 : " , isAuthenticated);
 
                            if (isAuthenticated && ($rootScope.toState.name === 'login' || $rootScope.toState.name === 'join')) {
                                $state.go('dashboards.dashboard');
                            }
 
                            if (!angular.isDefined($rootScope.authorities)) {
                                $rootScope.authorities = {};
                            }
 
                            //  권한 오브젝트 갯수 확인
                            var keys = Object.keys($rootScope.authorities);
 
                            if (Principal.isIdentityResolved()) {
                                if (keys.length < 1) {
                                    Permission.findByUserId().then(function (response) {
                                        if (response.data.message.status === "success") {
                                            checkAuthority();
                                        }
                                    });
                                }
                                else {
                                    checkAuthority();
                                }
                            }
                            else {
                                checkAuthority();
                            }
 
                            //  권한 체크
                            function checkAuthority() {
                                if (Object.keys($rootScope.authorities).length >= 0) {
 
                                    if ($rootScope.toState.data.authorities && $rootScope.toState.data.authorities.length > 0 && !Principal.hasAnyAuthority($rootScope.toState.data.authorities)) {
                                        if (isAuthenticated) {
                                            $log.debug("페이지 거부 - 권한이 없음, 이전 화면으로 이동");
 
                                            //  권한 거부 당할시 현재 화면/과거 화면이 같으면 무한로딩 현상 수정.
                                            if ($rootScope.toState.name === $rootScope.previousStateName) {
                                                $rootScope.previousStateName = null;
                                            }
 
                                            $rootScope.back();
                                        }
                                        else {
                                            // user is not authenticated. stow the state they wanted before you
                                            // send them to the signin state, so you can return them when you're done
                                            $rootScope.previousStateName = $rootScope.toState.name;
                                            $rootScope.previousStateNameParams = $rootScope.toStateParams;
                                            // now, send them to the signin state so they can log in
                                            $state.go('login');
                                        }
                                    }
                                }
                            }
 
                            $log.debug("인증 여부 : ", isAuthenticated);
                            $log.debug("해당 페이지의 권한 : ", $rootScope.toState.data.authorities);
                            $log.debug("사용자 권한 체크 : ", Principal.hasAnyAuthority($rootScope.toState.data.authorities));
                            $log.debug("사용자 권한 : ", $rootScope.authorities);
                        });
                },
 
            };
        }]);
});