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