Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Admin login, resolves #283

  • Loading branch information...
rasvaan committed Jun 29, 2016
1 parent dde2d63 commit f997b282061a4b076c75501df2a368f7d8bac7c8
@@ -57,6 +57,7 @@
:- http_handler(cliopatria(annotations), annotations_api, []).
:- http_handler(cliopatria(expertise_topics), expertise_topics_api, []).
:- http_handler(cliopatria(expertise_values), expertise_values_api, []).
:- http_handler(cliopatria(login_user), login_user, []).
:- http_handler(cliopatria(register_user), register_user, []).
:- http_handler(cliopatria(get_user), get_user, []).
:- http_handler(cliopatria(get_user_settings), get_user_settings, []).
@@ -5,13 +5,15 @@
get_user/1,
get_user_settings/1,
save_user_info/1,
get_user_info/1]).
get_user_info/1,
login_user/1]).

/** <module> Domain
*/
:- use_module(library(semweb/rdf_db)).
:- use_module(library(http/http_json)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_parameters)).
:- use_module(user(user_db)).

:- rdf_register_prefix(edm, 'http://www.europeana.eu/schemas/edm/').
@@ -56,16 +58,40 @@
h1('Successfully registered user'))
).

%% login_user(+Request)
%
% Log user in and return something sensible.
login_user(Request) :-
http_parameters(Request,
[user(User,
[description('User id'), optional(false)]),
password(Password,
[description('Password'), optional(false)])
]),
validate_password(User, Password),
login(User), !,
user_property(User, realname(RealName)),
admin(User, Admin),
reply_json_dict(user{login:true, user:User, real_name:RealName, admin:Admin}).
login_user(_Request) :-
reply_json_dict(user{login:false}).

%% get_user(+Request)
%
% Get the id of a user.
get_user(_Request) :-
logged_on(User), !,
user_property(User, realname(RealName)),
reply_json_dict(user{login:true, user:User, real_name:RealName}).
admin(User, Admin),
reply_json_dict(user{login:true, user:User, real_name:RealName, admin:Admin}).
get_user(_Request) :-
reply_json_dict(user{login:false}).

admin(User, true) :-
user_property(User, allow(Permissions)),
member(admin(_), Permissions), !.
admin(_User, false).

%% get_user_settings(+Request)
%
% Return saved domain and locale of user.
@@ -371,7 +371,7 @@ function userLoggedIn() {
});
}

function login(onSuccess, onDismissal) {
function login(onSuccess, onDismissal, permission) {
var ui = "http://accurator.nl/ui/generic#loginModal";
var locale = getLocale();

@@ -383,7 +383,7 @@ function login(onSuccess, onDismissal) {
$("#loginDivLogin").modal();
$("#loginInpUsername").focus();

loginButtonEvents(onSuccess, onDismissal, labels);
loginButtonEvents(onSuccess, onDismissal, labels, permission);
})
}

@@ -403,20 +403,20 @@ function initModalLabels(data) {
return labels;
}

function loginButtonEvents(onSuccess, onDismissal, labels) {
function loginButtonEvents(onSuccess, onDismissal, labels, permission) {
// add events to login modal
$("#loginBtnLogin").click(function() {
processLogin(onSuccess, labels);
});
// login on pressing enter
$("#loginInpPassword").keypress(function(event) {
if (event.which == 13) {
processLogin(onSuccess, labels);
processLogin(onSuccess, labels, permission);
}
});
$("#loginInpUsername").keypress(function(event) {
if (event.which == 13) {
processLogin(onSuccess, labels);
processLogin(onSuccess, labels, permission);
}
});
// run onDismissal if modal is dismissed
@@ -428,44 +428,54 @@ function loginButtonEvents(onSuccess, onDismissal, labels) {
});
}

function processLogin(onSuccess, labels) {
function processLogin(onSuccess, labels, permission) {
// login based upon values provided in modal
var user = getUserUri($("#loginInpUsername").val());
var password = $("#loginInpPassword").val();
if (!permission) permission = "normal"; // set the needed permission

if(user == "" || password == "") {
$("#loginTxtWarning").html(
$.el.p({'class':'text-danger'}, labels.loginTxtIncomplete)
);
} else {
loginServer(user, password)
.then(function(data) {
if(data.indexOf("Login failed") != -1) {
.then(function(user) {
if (!user.login) {
// show warning login failed
$("#loginTxtWarning").html(
$.el.p({'class':'text-danger'}, labels.loginTxtWarning)
);
} else if (data.indexOf("Login ok") != -1) {
// set user settings, hide modal and execute onSuccess
setUserSettingsLocal()
.then(function() {
$("#loginDivLogin").off('hidden.bs.modal');
$("#loginDivLogin").modal('hide');

return userLoggedIn();
})
.then(function(userData) {
onSuccess(userData);
});
} else if (user.login) {
// login succesful, check needed privileges
if ((permission === "admin" && user.admin) || permission === "normal") {
// set user settings, hide modal and execute onSuccess
setUserSettingsLocal()
.then(function() {
$("#loginDivLogin").off('hidden.bs.modal');
$("#loginDivLogin").modal('hide');

return userLoggedIn();
})
.then(function(userData) {
onSuccess(userData);
});
} else {
// show permission warning
$("#loginTxtWarning").html(
// TODO: add label for warning
$.el.p({'class':'text-danger'}, "You don't have the required permissions to access this page.")
);
}
}
});
}
}

function loginServer(user, password) {
var dataLogin = {"user": user, "password": password};
var dataLogin = {"user":user, "password":password};

return $.ajax({type: "POST", url: "/user/login", data: dataLogin});
return $.ajax({type: "POST", url: "/login_user", data:dataLogin});
}

function logout() {
@@ -475,6 +485,19 @@ function logout() {
});
}

function adminLoggedIn() {
// see if user is logged in with admin priviliges
return $.getJSON("/get_user")
.then(function(user) {
if (user.login && user.admin) {
return user;
}
if (!user.login || !user.admin) {
return jQuery.Deferred().reject(user).promise();
}
});
}

/*******************************************************************************
User registration
Code for registering a new user
@@ -11,14 +11,14 @@ function annotationInit() {
var filteredAnnotations = filterAnnotations(annotations);

// TODO: change to check admin priviliges
userLoggedIn()
adminLoggedIn()
.then(function() {
// user is logged in as admin, so draw page
drawPage();
}, function() {
// user is not logged in as admin, show modal
var onDismissal = function() {document.location.href = "intro.html";};
login(drawPage, onDismissal);
var onDismissal = function() {document.location.href = "/intro.html";};
login(drawPage, onDismissal, "admin");
});

function drawPage() {
@@ -14,14 +14,14 @@ function annotationsInit() {
clearLocalStorage("annotations"); // will be generating new list of annotations

// TODO: change to check admin priviliges
userLoggedIn()
adminLoggedIn()
.then(function() {
// user is logged in as admin, so draw page
drawPage();
}, function() {
// user is not logged in as admin, show modal
var onDismissal = function() {document.location.href = "intro.html";};
login(drawPage, onDismissal);
var onDismissal = function() {document.location.href = "/intro.html";};
login(drawPage, onDismissal, "admin");
});

function drawPage() {
@@ -61,7 +61,6 @@ function objectAnnotations(annotations) {
delete objectAnnotation.object;

if (objectIndex >= 0) {
console.log("Addding another to ", objectIndex);
// add annotation to existing object
var annotationIndex = objects[objectIndex].annotations.length;
objects[objectIndex].annotations[annotationIndex] = objectAnnotation;
@@ -7,14 +7,14 @@ Code for showing the dashboard page.

function dashboardInit() {
// TODO: change to check admin priviliges
userLoggedIn()
adminLoggedIn()
.then(function() {
// user is logged in as admin, so draw page
drawPage();
}, function() {
// user is not logged in as admin, show modal
var onDismissal = function() {document.location.href = "intro.html";};
login(drawPage, onDismissal);
var onDismissal = function() {document.location.href = "/intro.html";};
login(drawPage, onDismissal, "admin");
});

function drawPage() {

0 comments on commit f997b28

Please sign in to comment.
You can’t perform that action at this time.