Commit a98a6fca authored by Michael Ott's avatar Michael Ott
Browse files

add patch and delete user

parent 0076ccde
// INI constants // INI constants
constant INI_FILE "web-backend.ini"; constant CGI_INI_FILE "web-backend.ini";
constant SECTION "Config"; constant CGI_SECTION "config";
constant EMAIL "Email"; constant WEBMASTER "server";
constant KEYS "Keys"; constant DEFAULT_WEBMASTER "server@simpol.com";
constant WEBMASTER "webmaster"; constant CGI_PPCS_SERVER "cgippcsserver";
constant DEFAULT_WEBMASTER "webmaster@simpol.com";
//constant sCGIPARTIALURL "cgipartialurl";
//constant sLANGUAGE "language";
//constant sCGIINIFILELIB "lib.ini";
constant PPCS_SERVER "cgippcsserver";
constant DEFAULT_IP_ADDRESS "127.0.0.1:4000"; constant DEFAULT_IP_ADDRESS "127.0.0.1:4000";
//constant sCGIPPCSSERVER2 "cgippcsserver2"; constant KEY_SESSION_FILE "session-file";
//constant sBASEHREF "basehref"; constant DEFAULT_SESSION_FILE "SESSIONS!";
//constant sDEFIPADDRESS "127.0.0.1:1285"; constant KEY_USER_DATA "user-data";
constant SESSION_FILE "session-file"; constant DEFAULT_USER_DATA "USERDATA";
constant DEFAULT_SESSION_FILE "SESSIONS";
constant USER_DATA "user-data";
constant DEFAULT_USER_DATA "userdata";
// TODO log file in ini file
constant LOG_FILE "web-backend.log"; constant LOG_FILE "web-backend.log";
constant KEY_LOG_FILE "log-file";
constant DEFAULT_LOG_FILE "web-backend.log";
constant KEY_PROJECT "project"; constant KEY_PROJECT "project";
constant DEFAULT_PROJECT "simpol"; constant DEFAULT_PROJECT "simpol";
......
...@@ -309,18 +309,30 @@ function WebBackend.users(WebBackend me) ...@@ -309,18 +309,30 @@ function WebBackend.users(WebBackend me)
me.create_json_response(ERROR_NOT_IMPLEMENTED, "Not implemented"); me.create_json_response(ERROR_NOT_IMPLEMENTED, "Not implemented");
end if end if
else else
string str, todo; if me.user !@= .nul
integer id, len; string str, todo;
integer id, len;
str = .substr(me.path, 8, .inf);
len = .instr(str, "/"); str = .substr(me.path, 8, .inf);
if len != .nul and len != 0 len = .instr(str, "/");
todo = .substr(str, len + 1, .inf); if len != .nul and len != 0
str = .substr(str, 0, len - 1); todo = .substr(str, len + 1, .inf);
str = .substr(str, 0, len - 1);
end if
id = me.decode(str);
if me.request_method == "GET"
me.get_user(id);
else if me.request_method == "PATCH"
me.patch_user(id);
else if me.request_method == "DELETE"
me.delete_user(id);
else
me.create_json_response(ERROR_NOT_IMPLEMENTED, "Not implemented");
end if
else
me.create_json_response(ERROR_AUTH, "You have to log in to use this function");
end if end if
id = me.decode(str);
me.get_user(id);
end if end if
end function result end function result
...@@ -474,8 +486,8 @@ function WebBackend.create_user(WebBackend me) ...@@ -474,8 +486,8 @@ function WebBackend.create_user(WebBackend me)
data.members.addmember(KEY_NAME, JSON_STRING.new(simpolstringtojsonblob(name, me.encoding))); data.members.addmember(KEY_NAME, JSON_STRING.new(simpolstringtojsonblob(name, me.encoding)));
data.members.addmember(KEY_MAIL_ADDRESS, \ data.members.addmember(KEY_MAIL_ADDRESS, \
JSON_STRING.new(simpolstringtojsonblob(mail_address, me.encoding))); JSON_STRING.new(simpolstringtojsonblob(mail_address, me.encoding)));
me.create_json_response(ERROR_OK, "Successfully created an account, please login!", data); me.create_json_response(ERROR_OK, "Successfully created an user, please login!", data);
me.log_handler.info_message("Created new account: " + login + ", email: " + mail_address); me.log_handler.info_message("Created new user: " + login + ", email: " + mail_address);
if new_user_mail == .true if new_user_mail == .true
// TODO send password // TODO send password
end if end if
...@@ -554,12 +566,12 @@ end function result; ...@@ -554,12 +566,12 @@ end function result;
// //
// Parameters // Parameters
// ========== // ==========
// me WebBackend object // Webbackend me WebBackend object
// integer user_id User id // integer user_id User id
// //
// Description // Description
// =========== // ===========
// Get information about a user // Get information about an user
// //
// Return Value // Return Value
// ============ // ============
...@@ -586,16 +598,129 @@ function WebBackend.get_user(WebBackend me, integer user_id) ...@@ -586,16 +598,129 @@ function WebBackend.get_user(WebBackend me, integer user_id)
me.create_json_response(ERROR_OK, "", data); me.create_json_response(ERROR_OK, "", data);
result = .true; result = .true;
end if end if
end function result
end function result; //////////////////////////////////////////////////////////////////////////
// WebBackend.patch_user()
//
// Parameters
// ==========
// Webbackend me WebBackend object
// integer user_id User id
//
// Description
// ===========
// Get information about an user
//
// Return Value
// ============
// True if there no problem
//////////////////////////////////////////////////////////////////////////
function WebBackend.patch_user(WebBackend me, integer user_id)
boolean result;
ppcstype1record record;
result = .false;
record =@ me.find_user(user_id, .true);
if record !@= .nul
if me.json_request !@= .nul
JSON_PAIR item;
// TODO add missing keys
string mail_address, name, role, error_msg;
integer errno;
mail_address = .nul;
name = .nul;
role = .nul;
errno = 0;
error_msg = "";
item @= me.json_request.members.getfirst();
while item !@= .nul
if item.name == KEY_MAIL_ADDRESS
record!EMailAddress = item.value.getsimpolstring();
else if item.name == KEY_ROLE
record!rights = item.value.getsimpolstring();
else if item.name == KEY_NAME
record!Nameline = item.value.getsimpolstring();
end if
item =@ item.getnext();
end while
if record.save(error = errno, errortext = error_msg) =@= .nul
JSON_OBJECT data;
data =@ JSON_OBJECT.new(.false, me.encoding);
data.members.addmember(KEY_ID, JSON_STRING.new(simpolstringtojsonblob(me.encode(user_id), me.encoding)));
data.members.addmember(KEY_LOGIN, \
JSON_STRING.new(simpolstringtojsonblob(record!LoginName, me.encoding)));
data.members.addmember(KEY_NAME, JSON_STRING.new(simpolstringtojsonblob(record!Nameline, me.encoding)));
data.members.addmember(KEY_MAIL_ADDRESS, \
JSON_STRING.new(simpolstringtojsonblob(record!EMailAddress, me.encoding)));
me.create_json_response(ERROR_OK, "Successful update an user!", data);
me.log_handler.info_message("Update user: " + .tostr(user_id, 10) + " " + record!LoginName \
+ ", email: " + mail_address);
result = .true;
else
me.create_json_response(ERROR_INTERNAL, "Error update user");
me.log_handler.error_message("User: Cannot update user " + .tostr(user_id, 10) + ": " + error_msg);
end if
else
me.create_json_response(ERROR_BAD_REQUEST, "Missing value(s)", me.json_request);
end if
end if
end function result
//////////////////////////////////////////////////////////////////////////
// WebBackend.delete_user()
//
// Parameters
// ==========
// Webbackend me WebBackend object
// integer user_id User id
//
// Description
// ===========
// Get information about an user
//
// Return Value
// ============
// True if there no problem
//////////////////////////////////////////////////////////////////////////
function WebBackend.delete_user(WebBackend me, integer user_id)
boolean result;
ppcstype1record record;
result = .false;
record =@ me.find_user(user_id, .true);
if record !@= .nul
integer errno;
string error_msg;
errno = 0;
error_msg = "";
if record.delete(error = errno, errortext = error_msg) !@= .nul
me.create_json_response(ERROR_INTERNAL, "Cannot delete user");
me.log_handler.error_message("User: Cannot delete user " + .tostr(user_id, 10) + ": " + error_msg);
else
me.create_json_response(ERROR_OK, "Successful delete user!");
me.log_handler.info_message("Delete user: " + .tostr(user_id, 10));
result = .true;
end if
end if
end function result
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// WebBackend.find_user() // WebBackend.find_user()
// //
// Parameters // Parameters
// ========== // ==========
// me WebBackend object // Webbackend me WebBackend object
// user_id User id // integer user_id User id
// booleand lock lock entry (default: false)
// //
// Description // Description
// =========== // ===========
...@@ -605,14 +730,14 @@ end function result; ...@@ -605,14 +730,14 @@ end function result;
// ============ // ============
// True if create user was successful // True if create user was successful
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
function WebBackend.find_user(WebBackend me, integer user_id) function WebBackend.find_user(WebBackend me, integer user_id, boolean lock = .false)
ppcstype1record record; ppcstype1record record;
integer errno; integer errno;
record =@ .nul; record =@ .nul;
errno = 0; errno = 0;
record =@ me.user_data.CustomerNumber.index.selectkey(user_id, error = errno); record =@ me.user_data.CustomerNumber.index.selectkey(user_id, lock = lock, error = errno);
if record =@= .nul if record =@= .nul
me.create_json_response(ERROR_AUTH, "Cannot find user"); me.create_json_response(ERROR_AUTH, "Cannot find user");
me.log_handler.info_message("Cannot find user with id " + .tostr(user_id, 10)); me.log_handler.info_message("Cannot find user with id " + .tostr(user_id, 10));
......
...@@ -77,6 +77,8 @@ type WebBackend export ...@@ -77,6 +77,8 @@ type WebBackend export
function create_user readonly; function create_user readonly;
function get_user_list readonly; function get_user_list readonly;
function get_user readonly; function get_user readonly;
function patch_user readonly;
function delete_user readonly;
function find_user; function find_user;
end type end type
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment