'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);
|
});
|
},
|
|
};
|
}]);
|
});
|