143 lines
3.1 KiB
JavaScript
143 lines
3.1 KiB
JavaScript
|
|
mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
|
|
|
|
/* Create profile structure to store informations
|
|
* about current session
|
|
*/
|
|
var profile={};
|
|
profile.username=null;
|
|
profile.projectname=null;
|
|
profile.token=null;
|
|
|
|
/* var tokenFragment=9;
|
|
|
|
var saveTokenInCookies=function(){
|
|
var i=0;
|
|
var currentStart=0;
|
|
var currentEnd=parseInt(profile.token.length/tokenFragment);
|
|
var facto=currentEnd;
|
|
|
|
alert("current ren" + currentEnd);
|
|
for(i=0;i<tokenFragment-1;i++){
|
|
|
|
$cookies.put("token-"+i, profile.token.substring(currentStart, currentEnd));
|
|
|
|
|
|
currentStart=currentEnd+1;
|
|
currentEnd=currentEnd+facto;
|
|
|
|
|
|
}
|
|
|
|
$cookies.put("token-"+(i+1), profile.token.substring(currentEnd, profile.token.length));
|
|
};
|
|
|
|
var loadTokenInCookies=function(){
|
|
var i=0;
|
|
|
|
profile.token=$cookies.get("token-0");
|
|
for(i=1;i<tokenFragment;i++){
|
|
|
|
profile.token=profile.token+$cookies.get("token-"+i)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
};*/
|
|
|
|
|
|
|
|
var saveCookieForSession=function(){
|
|
$cookies.putObject('profile', 5);
|
|
//saveTokenInCookies();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var isAlreadyLogin=function(){
|
|
var profileInCookie=$cookies.getObject('profile');
|
|
console.log(profileInCookie);
|
|
|
|
if(typeof profileInCookie !== 'undefined'){
|
|
angular.extend(profile, profileInCookie);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
var logout=function(){
|
|
$cookies.remove('profile');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} response The response to parse
|
|
* @param {boolean} to check if the request is send or not
|
|
* @returns {requestParserResult} Formated data
|
|
*/
|
|
var parseLoginAnswer=function(response, failedToSendRequest){
|
|
|
|
|
|
var requestParserResult={};
|
|
requestParserResult.status=1;
|
|
requestParserResult.failReason=null;
|
|
|
|
if (typeof response.data.token !== 'undefined') {
|
|
requestParserResult.status=0;
|
|
profile.token=response.data.token;
|
|
saveCookieForSession();
|
|
}
|
|
else if(failedToSendRequest){
|
|
requestParserResult.failReason="Failed to send request";
|
|
}
|
|
else{
|
|
requestParserResult.failReason="Please check your username, password and project name !";
|
|
}
|
|
|
|
return requestParserResult;
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
* Function to connect to OpenStack
|
|
*
|
|
* @param {object} $http Angular $http service
|
|
* @param {string} username The user name
|
|
* @param {string} password The user password
|
|
* @param {string} projectname The user project name
|
|
* @param {function} function to call when data is avalaible
|
|
*/
|
|
var login=function(username, password,projectname, callback){
|
|
|
|
// Set profile information (early)
|
|
profile.username=username;
|
|
profile.projectname=projectname;
|
|
|
|
var result=$http.post('../server/index.php',
|
|
$.param({"task" : "Authenticate", "user" : username, "password" : password, "project" : projectname}));
|
|
|
|
// Wait and handle the response
|
|
result.then(function (response){
|
|
callback(parseLoginAnswer(response), false);
|
|
},function(response){
|
|
callback(parseLoginAnswer(response), true)
|
|
});
|
|
};
|
|
|
|
// Return services objects
|
|
return {
|
|
login: login,
|
|
profile: profile,
|
|
isAlreadyLogin: isAlreadyLogin,
|
|
logout:logout
|
|
};
|
|
|
|
|
|
}]);
|