aqhome-cgi: more work

This commit is contained in:
Martin Preuss
2025-09-15 18:47:16 +02:00
parent 8f2bd23dc5
commit 7f301d271b
4 changed files with 358 additions and 430 deletions

View File

@@ -92,8 +92,6 @@ int AQH_ModDataClient_HandleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSIO
AQH_ModService_AddFooter(m, "en", dbuf);
AQCGI_Request_SetBufferResponseBody(rq, dbuf);
AQCGI_Request_AddResponseHeaderData(rq, "Content-type: text/html");
AQCGI_SendResponseWithStatus(rq, 200, "Ok");
GWEN_Buffer_free(dbuf);
AQH_DataClient_free(dc);
AQH_EventLoop_free(eventLoop);

View File

@@ -38,6 +38,9 @@
* ------------------------------------------------------------------------------------------------
*/
static void _createPermDefList(AQH_MODULE *m);
static void _createRoleList(AQH_MODULE *m);
static AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName);
static int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem);
static void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf);
@@ -58,6 +61,82 @@ void AQH_ModDevices_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolde
int AQH_ModDevices_Create(AQH_SERVICE *sv)
{
AQH_MODULE *m;
int rv;
m=AQH_Module_new();
AQH_Module_SetName(m, "devices");
AQH_Module_SetDescr(m, "device module");
AQH_Module_SetGuestPerms(m, 0);
_createPermDefList(m);
_createRoleList(m);
rv=AQH_Service_AddModule(sv, m);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
}
AQH_Module_free(m);
return rv;
}
void _createPermDefList(AQH_MODULE *m)
{
AQH_PERMDEF_LIST *permDefList;
permDefList=AQH_PermDef_List_new();
AQH_ModService_AddPermDef(permDefList, "DeviceRead", 0x001, "Read and list devices");
AQH_ModService_AddPermDef(permDefList, "DeviceWrite", 0x002, "Modify devices");
AQH_ModService_AddPermDef(permDefList, "DeviceAdd", 0x004, "Add devices");
AQH_ModService_AddPermDef(permDefList, "DeviceDel", 0x008, "Remove devices");
AQH_ModService_AddPermDef(permDefList, "ValueRead", 0x010, "Read and list values");
AQH_ModService_AddPermDef(permDefList, "ValueWrite", 0x020, "Modify values");
AQH_ModService_AddPermDef(permDefList, "ValueAdd", 0x040, "Add values");
AQH_ModService_AddPermDef(permDefList, "ValueDel", 0x080, "Remove values");
AQH_ModService_AddPermDef(permDefList, "ValueSet", 0x100, "Set values");
AQH_Module_SetPermDefList(m, permDefList);
}
void _createRoleList(AQH_MODULE *m)
{
AQH_ROLE_LIST *roleList;
int id=0;
roleList=AQH_Role_List_new();
AQH_ModService_AddRole(roleList, id++, "Reader",
AQH_MODDEVICES_PERMS_DEVICEREAD |
AQH_MODDEVICES_PERMS_VALUEREAD,
"Read devices and values");
AQH_ModService_AddRole(roleList, id++, "Writer",
AQH_MODDEVICES_PERMS_DEVICEREAD |
AQH_MODDEVICES_PERMS_DEVICEWRITE |
AQH_MODDEVICES_PERMS_DEVICEADD |
AQH_MODDEVICES_PERMS_DEVICEDEL |
AQH_MODDEVICES_PERMS_VALUEREAD |
AQH_MODDEVICES_PERMS_VALUEWRITE |
AQH_MODDEVICES_PERMS_VALUEADD |
AQH_MODDEVICES_PERMS_VALUEDEL |
AQH_MODDEVICES_PERMS_VALUESET,
"Read and write devices and values");
AQH_ModService_AddRole(roleList, id++, "Setter",
AQH_MODDEVICES_PERMS_DEVICEREAD |
AQH_MODDEVICES_PERMS_VALUEREAD |
AQH_MODDEVICES_PERMS_VALUESET,
"Set values");
AQH_Module_SetRoleList(m, roleList);
}
AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName)
{
return NULL;
@@ -81,146 +160,172 @@ int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const
void _runIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
{
AQH_DEVICE_LIST *deviceList;
AQH_DEVICE *device;
uint32_t perms;
deviceList=AQH_DataClient_GetDevices(dc, NULL);
if (deviceList==NULL) {
DBG_ERROR(NULL, "No device received");
GWEN_Buffer_AppendString(dbuf, "Empty device list.");
return;
perms=AQH_ModService_GetUserPerms(m);
DBG_ERROR(NULL, "Perms=%08x", perms);
if (perms & AQH_MODDEVICES_PERMS_DEVICEREAD) {
AQH_DEVICE_LIST *deviceList;
AQH_DEVICE *device;
deviceList=AQH_DataClient_GetDevices(dc, NULL);
if (deviceList==NULL) {
DBG_ERROR(NULL, "No device received");
GWEN_Buffer_AppendString(dbuf, "Empty device list.");
return;
}
GWEN_Buffer_AppendString(dbuf, "<h1>Devices</h1>\n");
GWEN_Buffer_AppendString(dbuf,
"<table class=\"datatable\">\n"
"<thead>\n"
"<tr>"
"<th>Name For System</th>"
"<th>Room</th>"
"<th>Location</th>"
"<th>Description</th>"
"<th>Type</th>"
"<th>Driver</th>"
"<th>Name</th>"
"<th>GUI Name</th>"
"<th>Manufacturer</th>"
"</tr>\n"
"</thead>\n"
"<tbody>\n");
device=AQH_Device_List_First(deviceList);
while(device) {
const char *s;
GWEN_Buffer_AppendArgs(dbuf, "<tr>");
/* name for system */
s=AQH_Device_GetNameForSystem(device);
GWEN_Buffer_AppendString(dbuf,"<td><a href=\"values.html?device=");
GWEN_Text_EscapeToBufferTolerant(s, dbuf);
GWEN_Buffer_AppendArgs(dbuf,"\">%s</a></td>", s?s:"");
/* room */
s=AQH_Device_GetRoomName(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* location */
s=AQH_Device_GetLocation(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* description */
s=AQH_Device_GetDescription(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* device type */
s=AQH_Device_GetDeviceType(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* driver */
s=AQH_Device_GetDriver(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* short device name */
s=AQH_Device_GetName(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* GUI name for device */
s=AQH_Device_GetNameForGui(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* manufacturer */
s=AQH_Device_GetManufacturer(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
GWEN_Buffer_AppendArgs(dbuf, "</tr>");
device=AQH_Device_List_Next(device);
}
GWEN_Buffer_AppendString(dbuf,
"</tbody>\n"
"</table>\n");
AQH_Device_List_free(deviceList);
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
GWEN_Buffer_AppendString(dbuf, "<h1>Devices</h1>\n");
GWEN_Buffer_AppendString(dbuf,
"<table class=\"datatable\">\n"
"<thead>\n"
"<tr>"
"<th>Name For System</th>"
"<th>Room</th>"
"<th>Location</th>"
"<th>Description</th>"
"<th>Type</th>"
"<th>Driver</th>"
"<th>Name</th>"
"<th>GUI Name</th>"
"<th>Manufacturer</th>"
"</tr>\n"
"</thead>\n"
"<tbody>\n");
device=AQH_Device_List_First(deviceList);
while(device) {
const char *s;
GWEN_Buffer_AppendArgs(dbuf, "<tr>");
/* name for system */
s=AQH_Device_GetNameForSystem(device);
GWEN_Buffer_AppendString(dbuf,"<td><a href=\"values.html?device=");
GWEN_Text_EscapeToBufferTolerant(s, dbuf);
GWEN_Buffer_AppendArgs(dbuf,"\">%s</a></td>", s?s:"");
/* room */
s=AQH_Device_GetRoomName(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* location */
s=AQH_Device_GetLocation(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* description */
s=AQH_Device_GetDescription(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* device type */
s=AQH_Device_GetDeviceType(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* driver */
s=AQH_Device_GetDriver(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* short device name */
s=AQH_Device_GetName(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* GUI name for device */
s=AQH_Device_GetNameForGui(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* manufacturer */
s=AQH_Device_GetManufacturer(device);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
GWEN_Buffer_AppendArgs(dbuf, "</tr>");
device=AQH_Device_List_Next(device);
else {
GWEN_Buffer_AppendString(dbuf, "<p>No permissions to read devices.</p>");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
GWEN_Buffer_AppendString(dbuf,
"</tbody>\n"
"</table>\n");
AQH_Device_List_free(deviceList);
}
void _runValues(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
{
GWEN_DB_NODE *dbQuery;
const char *sDeviceName;
uint32_t perms;
dbQuery=AQCGI_Request_GetDbQuery(rq);
sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL);
if (!(sDeviceName && *sDeviceName))
_runIndex(m, rq, session, dc, dbuf);
else {
AQH_VALUE_LIST *valueList;
perms=AQH_ModService_GetUserPerms(m);
DBG_ERROR(NULL, "Perms=%08x", perms);
if ((perms & AQH_MODDEVICES_PERMS_DEVICEREAD) &&
(perms & AQH_MODDEVICES_PERMS_VALUEREAD)) {
GWEN_DB_NODE *dbQuery;
const char *sDeviceName;
valueList=AQH_DataClient_GetValues(dc, sDeviceName, 0);
if (valueList) {
AQH_VALUE *value;
dbQuery=AQCGI_Request_GetDbQuery(rq);
sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL);
if (!(sDeviceName && *sDeviceName))
_runIndex(m, rq, session, dc, dbuf);
else {
AQH_VALUE_LIST *valueList;
GWEN_Buffer_AppendArgs(dbuf, "<h1>Values for Device %s</h1>\n", sDeviceName);
GWEN_Buffer_AppendString(dbuf,
"<table class=\"datatable\">\n"
"<thead>"
"<tr>"
"<th>Name</th>"
"<th>Type</th>"
"<th>Modality</th>"
"<th>Driver</th>"
"<th>Device</th>"
"<th>Name for System</th>"
"</tr>"
"</thead>\n"
"<tbody>\n");
valueList=AQH_DataClient_GetValues(dc, sDeviceName, 0);
if (valueList) {
AQH_VALUE *value;
value=AQH_Value_List_First(valueList);
while(value) {
const char *s;
GWEN_Buffer_AppendArgs(dbuf, "<h1>Values for Device %s</h1>\n", sDeviceName);
GWEN_Buffer_AppendString(dbuf,
"<table class=\"datatable\">\n"
"<thead>"
"<tr>"
"<th>Name</th>"
"<th>Type</th>"
"<th>Modality</th>"
"<th>Driver</th>"
"<th>Device</th>"
"<th>Name for System</th>"
"</tr>"
"</thead>\n"
"<tbody>\n");
GWEN_Buffer_AppendString(dbuf, "<tr>");
value=AQH_Value_List_First(valueList);
while(value) {
const char *s;
s=AQH_Value_GetName(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
GWEN_Buffer_AppendString(dbuf, "<tr>");
s=AQH_ValueType_toString(AQH_Value_GetValueType(value));
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetName(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_ValueModality_toString(AQH_Value_GetModality(value));
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_ValueType_toString(AQH_Value_GetValueType(value));
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetDriver(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_ValueModality_toString(AQH_Value_GetModality(value));
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetDeviceNameForSystem(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetDriver(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetNameForSystem(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
s=AQH_Value_GetDeviceNameForSystem(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
GWEN_Buffer_AppendArgs(dbuf, "</tr>\n");
s=AQH_Value_GetNameForSystem(value);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
value=AQH_Value_List_Next(value);
GWEN_Buffer_AppendArgs(dbuf, "</tr>\n");
value=AQH_Value_List_Next(value);
}
GWEN_Buffer_AppendString(dbuf,
"</tbody>\n"
"</table>\n");
AQH_Value_List_free(valueList);
}
GWEN_Buffer_AppendString(dbuf,
"</tbody>\n"
"</table>\n");
AQH_Value_List_free(valueList);
}
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>No permissions to read devices/values.</p>");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
}

View File

@@ -18,22 +18,21 @@
#include <gwenhywfar/buffer.h>
#define AQH_MODDEVICES_PERMS_DEVICELIST 0x001
#define AQH_MODDEVICES_PERMS_DEVICEREAD 0x002
#define AQH_MODDEVICES_PERMS_DEVICEWRITE 0x004
#define AQH_MODDEVICES_PERMS_DEVICEADD 0x008
#define AQH_MODDEVICES_PERMS_DEVICEDEL 0x010
#define AQH_MODDEVICES_PERMS_DEVICEREAD 0x001
#define AQH_MODDEVICES_PERMS_DEVICEWRITE 0x002
#define AQH_MODDEVICES_PERMS_DEVICEADD 0x004
#define AQH_MODDEVICES_PERMS_DEVICEDEL 0x008
#define AQH_MODDEVICES_PERMS_VALUELIST 0x020
#define AQH_MODDEVICES_PERMS_VALUEREAD 0x040
#define AQH_MODDEVICES_PERMS_VALUEWRITE 0x080
#define AQH_MODDEVICES_PERMS_VALUEADD 0x100
#define AQH_MODDEVICES_PERMS_VALUEDEL 0x200
#define AQH_MODDEVICES_PERMS_VALUESET 0x400
#define AQH_MODDEVICES_PERMS_VALUEREAD 0x010
#define AQH_MODDEVICES_PERMS_VALUEWRITE 0x020
#define AQH_MODDEVICES_PERMS_VALUEADD 0x040
#define AQH_MODDEVICES_PERMS_VALUEDEL 0x080
#define AQH_MODDEVICES_PERMS_VALUESET 0x100
void AQH_ModDevices_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder);
int AQH_ModDevices_Create(AQH_SERVICE *sv);

View File

@@ -163,10 +163,6 @@ int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const
_handleRqIndex(m, rq, session, dbuf);
else if (strcasecmp(sLastPathElem, "editmodule.html")==0)
_handleRqEditMod(m, rq, session, dbuf);
#if 0
else if (strcasecmp(sLastPathElem, "editperm.html")==0)
_handleRqEditPerm(m, rq, session, dbuf);
#endif
else if (strcasecmp(sLastPathElem, "addrole.html")==0)
_handleRqAddRole(m, rq, session, dbuf);
else if (strcasecmp(sLastPathElem, "editrole.html")==0)
@@ -353,19 +349,6 @@ void _writeEditModForm(const AQH_MODULE *currentMod, const char *sModName, GWEN_
GWEN_Buffer_AppendArgs(dbuf, "<input type=\"hidden\" name=\"module\" value=\"%s\">\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf, "<input type=\"submit\" value=\"Save\">\n</form>\n\n");
#if 0
/* write permission def list */
GWEN_Buffer_AppendString(dbuf, "<h2>Permission Definitions</h2>\n");
if (permDefList)
_writePermDefListToForm(permDefList, sModName, dbuf);
else
GWEN_Buffer_AppendString(dbuf, "<p>none</p>");
GWEN_Buffer_AppendArgs(dbuf,
"<a href=\"addperm.html?mod=%s\">"
"<img src=\"/pics/plus.png\">Add Permission</a>\n",
sModName?sModName:"");
#endif
/* write role list */
GWEN_Buffer_AppendString(dbuf, "<h2>User Roles</h2>\n");
if (roleList)
@@ -380,55 +363,6 @@ void _writeEditModForm(const AQH_MODULE *currentMod, const char *sModName, GWEN_
#if 0
void _writePermDefListToForm(const AQH_PERMDEF_LIST *permDefList, const char *sModName, GWEN_BUFFER *dbuf)
{
const AQH_PERMDEF *permDef;
GWEN_Buffer_AppendString(dbuf,
"<table class=\"datatable\">\n"
"<thead>"
"<tr><th>Id</th><th>Mask</th><th>Description</th><th>Actions</th></tr>\n"
"</thead>\n"
"<tbody>\n");
permDef=AQH_PermDef_List_First(permDefList);
while(permDef) {
const char *sId;
const char *s;
GWEN_Buffer_AppendString(dbuf, "<tr>");
/* id */
sId=AQH_PermDef_GetId(permDef);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", sId?sId:"");
/* mask */
GWEN_Buffer_AppendArgs(dbuf, "<td>0x%x</td>", AQH_PermDef_GetMask(permDef));
/* description */
s=AQH_PermDef_GetDescr(permDef);
GWEN_Buffer_AppendArgs(dbuf, "<td>%s</td>", s?s:"");
/* actions */
GWEN_Buffer_AppendArgs(dbuf, "<td>");
GWEN_Buffer_AppendArgs(dbuf,
"<a href=\"editperm.html?mod=%s&id=%s\">"
"<img src=\"/pics/edit.png\"></a>",
sModName?sModName:"", sId?sId:"");
GWEN_Buffer_AppendArgs(dbuf,
"<a href=\"deleteperm.html?mod=%s&id=%s\">"
"<img src=\"/pics/minus.png\"></a>",
sModName?sModName:"", sId?sId:"");
GWEN_Buffer_AppendArgs(dbuf, "</td>");
GWEN_Buffer_AppendString(dbuf, "</tr>\n");
permDef=AQH_PermDef_List_Next(permDef);
}
GWEN_Buffer_AppendString(dbuf,
"</tbody>\n"
"</table>\n");
}
#endif
void _writeRoleListToForm(const AQH_ROLE_LIST *roleList,
const char *sModName,
const AQH_PERMDEF_LIST *permDefList,
@@ -520,6 +454,7 @@ int _handleRqEditModPost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
DBG_ERROR(NULL, "Could not save module \"%s\"", sModName);
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
AQH_Module_free(currentMod);
return 0;
}
DBG_ERROR(NULL, "Module \"%s\" saved", sModName);
@@ -538,154 +473,6 @@ int _handleRqEditModPost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
}
#if 0
int _handleRqEditPerm(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf)
{
uint32_t perms;
perms=AQH_ModService_GetUserPerms(m);
DBG_ERROR(NULL, "Perms=%08x", perms);
if (perms & AQH_MODADMMODULES_PERMS_MODULESWRITE) {
if (AQCGI_Request_GetRequestMethod(rq)==AQCGI_REQUEST_METHOD_GET)
return _handleRqEditPermGet(m, rq, session, dbuf);
else if (AQCGI_Request_GetRequestMethod(rq)==AQCGI_REQUEST_METHOD_POST)
return _handleRqEditPermPost(m, rq, session, dbuf);
else {
DBG_ERROR(NULL, "Invalid request method %d", AQCGI_Request_GetRequestMethod(rq));
AQCGI_SendResponseWithStatus(rq, 405, "Method Not Allowed");
AQCGI_Request_SetResponseCode(rq, 405);
AQCGI_Request_SetResponseText(rq, "Method Not Allowed");
}
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>No permissions to edit modules.</p>");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
return 0;
}
int _handleRqEditPermGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf)
{
AQH_SERVICE *sv;
GWEN_DB_NODE *dbQuery;
const char *sModName;
const char *sId;
const char *sDescr;
uint32_t mask;
AQH_MODULE *currentMod;
const AQH_PERMDEF_LIST *permDefList;
const AQH_PERMDEF *permDef;
sv=AQH_ModService_GetService(m);
dbQuery=AQCGI_Request_GetDbQuery(rq);
sModName=dbQuery?GWEN_DB_GetCharValue(dbQuery, "mod", 0, NULL):NULL;
sId=dbQuery?GWEN_DB_GetCharValue(dbQuery, "id", 0, NULL):NULL;
currentMod=(sModName && *sModName)?AQH_Service_LoadModule(sv, sModName):NULL;
permDefList=currentMod?AQH_Module_GetPermDefList(currentMod):NULL;
permDef=(permDefList && sId && *sId)?AQH_PermDef_List_GetById(permDefList, sId):NULL;
sDescr=permDef?AQH_PermDef_GetDescr(permDef):NULL;
mask=permDef?AQH_PermDef_GetMask(permDef):0;
if (permDef) {
GWEN_Buffer_AppendArgs(dbuf, "<h2>Edit Permission Definition for Module %s</h2>\n", sModName?sModName:"");
GWEN_Buffer_AppendArgs(dbuf,
"<form action=\"editperm.html\" method=\"post\">\n"
"<table class=\"formtable\">\n"
"<tr>"
"<td><label for=\"id\">Id:</label></td>"
"<td><input type=\"text\" name=\"id\" value=\"%s\"></td>"
"</tr>\n"
"<tr>\n"
"<td><label for=\"mask\">Mask:</label></td>"
"<td><input type=\"text\" name=\"mask\" value=\"0x%lx\"></td>"
"</tr>\n"
"<tr>"
"<td><label for=\"descr\">Description:</label></td>"
"<td><input type=\"text\" name=\"descr\" value=\"%s\"></td>"
"</tr>\n",
sId, (unsigned long int) mask, sDescr?sDescr:"");
GWEN_Buffer_AppendString(dbuf, "</table>\n");
GWEN_Buffer_AppendArgs(dbuf, "<input type=\"hidden\" name=\"mod\" value=\"%s\">\n", sModName?sModName:"");
GWEN_Buffer_AppendArgs(dbuf, "<input type=\"hidden\" name=\"oldId\" value=\"%s\">\n", sId?sId:"");
GWEN_Buffer_AppendString(dbuf, "<input type=\"submit\" value=\"Save\">\n");
GWEN_Buffer_AppendString(dbuf, "</form>\n\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
return 0;
}
else {
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
return 0;
}
}
int _handleRqEditPermPost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf)
{
AQH_SERVICE *sv;
GWEN_DB_NODE *dbPost;
const char *sModName;
AQH_MODULE *currentMod;
const char *sOldId;
const char *sNewId;
const char *sDescr;
const char *sMask;
uint32_t mask=0;
AQH_PERMDEF_LIST *permDefList;
AQH_PERMDEF *permDef;
long int i;
int rv;
/* sample data */
sv=AQH_ModService_GetService(m);
dbPost=AQCGI_Request_GetDbPostBody(rq);
sModName=dbPost?GWEN_DB_GetCharValue(dbPost, "mod", 0, NULL):NULL;
currentMod=(sModName && *sModName)?AQH_Service_LoadModule(sv, sModName):NULL;
sOldId=dbPost?GWEN_DB_GetCharValue(dbPost, "oldId", 0, NULL):NULL;
sNewId=dbPost?GWEN_DB_GetCharValue(dbPost, "id", 0, NULL):NULL;
sDescr=dbPost?GWEN_DB_GetCharValue(dbPost, "descr", 0, NULL):NULL;
sMask=dbPost?GWEN_DB_GetCharValue(dbPost, "mask", 0, NULL):NULL;
if (sMask && *sMask && 1==sscanf(sMask, "%li", &i))
mask=i;
permDefList=currentMod?AQH_Module_GetPermDefList(currentMod):NULL;
permDef=(permDefList && sOldId)?AQH_PermDef_List_GetById(permDefList, sOldId):NULL;
/* validate */
if (mask==0) {
DBG_ERROR(NULL, "Invalid value for mask");
}
if (permDef==NULL) {
DBG_ERROR(NULL, "PermDef %s not found", sOldId?sOldId:NULL);
}
/* set new values */
AQH_PermDef_SetId(permDef, sNewId);
AQH_PermDef_SetMask(permDef, mask);
AQH_PermDef_SetDescr(permDef, sDescr);
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<h2>Error</h2><p>Error saving module</p>");
DBG_ERROR(NULL, "Could not save module \"%s\"", sModName);
return 0;
}
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
return 0;
}
#endif
int _handleRqAddRole(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf)
{
@@ -731,43 +518,52 @@ int _handleRqAddRoleGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
guestPerms=currentMod?AQH_Module_GetGuestPerms(currentMod):0;
permDefList=currentMod?AQH_Module_GetPermDefList(currentMod):NULL;
if (permDefList) {
GWEN_Buffer_AppendArgs(dbuf, "<h2>Add Role for Module %s</h2>\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf,
"<form action=\"addrole.html\" method=\"post\">\n"
"<table class=\"formtable\">\n"
"<tr>"
"<td><label for=\"name\">Name:</label></td>"
"<td><input type=\"text\" name=\"name\"></td>"
"<tr>"
"<td><label for=\"descr\">Description:</label></td>"
"<td><input type=\"text\" name=\"descr\"></td>"
"</tr>\n");
if (currentMod) {
if (permDefList) {
GWEN_Buffer_AppendArgs(dbuf, "<h2>Add Role for Module %s</h2>\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf,
"<form action=\"addrole.html\" method=\"post\">\n"
"<table class=\"formtable\">\n"
"<tr>"
"<td><label for=\"name\">Name:</label></td>"
"<td><input type=\"text\" name=\"name\"></td>"
"<tr>"
"<td><label for=\"descr\">Description:</label></td>"
"<td><input type=\"text\" name=\"descr\"></td>"
"</tr>\n");
GWEN_Buffer_AppendString(dbuf, "<tr><td><label>Permissions:</label></td><td>");
_writePermissionsToForm(permDefList, guestPerms, dbuf);
GWEN_Buffer_AppendString(dbuf, "</td></tr>\n");
GWEN_Buffer_AppendString(dbuf, "<tr><td><label>Permissions:</label></td><td>");
_writePermissionsToForm(permDefList, guestPerms, dbuf);
GWEN_Buffer_AppendString(dbuf, "</td></tr>\n");
#if 0
GWEN_Buffer_AppendString(dbuf, "<tr><td><label>Explicitly add permissions:</label></td><td>");
_writePermissionsToForm(permDefList, 0, dbuf);
GWEN_Buffer_AppendString(dbuf, "</td></tr>\n");
GWEN_Buffer_AppendString(dbuf, "<tr><td><label>Explicitly add permissions:</label></td><td>");
_writePermissionsToForm(permDefList, 0, dbuf);
GWEN_Buffer_AppendString(dbuf, "</td></tr>\n");
GWEN_Buffer_AppendString(dbuf, "<tr><td><label>Explicitly sub permissions:</label></td><td>");
_writePermissionsToForm(permDefList, 0, dbuf);
GWEN_Buffer_AppendString(dbuf, "</td></tr>\n");
GWEN_Buffer_AppendString(dbuf, "<tr><td><label>Explicitly sub permissions:</label></td><td>");
_writePermissionsToForm(permDefList, 0, dbuf);
GWEN_Buffer_AppendString(dbuf, "</td></tr>\n");
#endif
GWEN_Buffer_AppendString(dbuf, "</table>\n");
GWEN_Buffer_AppendArgs(dbuf, "<input type=\"hidden\" name=\"mod\" value=\"%s\">\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf, "<input type=\"submit\" value=\"Add\">\n");
GWEN_Buffer_AppendString(dbuf, "</form>\n\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
GWEN_Buffer_AppendString(dbuf, "</table>\n");
GWEN_Buffer_AppendArgs(dbuf, "<input type=\"hidden\" name=\"mod\" value=\"%s\">\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf, "<input type=\"submit\" value=\"Add\">\n");
GWEN_Buffer_AppendString(dbuf, "</form>\n\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>Please add permission definitions first.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
AQH_Module_free(currentMod);
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>Please add permission definitions first.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf, "<p>Error loading module.</p>\n");
GWEN_Buffer_AppendString(dbuf, "<p><a href=\"index.html\"> back to module list</p>\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
@@ -792,7 +588,6 @@ int _handleRqAddRolePost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
#endif
AQH_PERMDEF_LIST *permDefList;
AQH_ROLE_LIST *roleList;
AQH_ROLE *role;
int rv;
/* sample data */
@@ -809,7 +604,7 @@ int _handleRqAddRolePost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
sDescr=dbPost?GWEN_DB_GetCharValue(dbPost, "descr", 0, NULL):NULL;
perms=(dbPost && permDefList)?_readPermissionsFromForm(dbPost, permDefList):0;
/* validate */
/* validate */
if (!(sName && *sName)) {
DBG_ERROR(NULL, "Missing value for \"name\"");
GWEN_Buffer_AppendString(dbuf, "<p>Missing name.</p>\n");
@@ -819,32 +614,44 @@ int _handleRqAddRolePost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
return 0;
}
/* set new values */
role=AQH_Role_new();
AQH_Role_SetId(role, newId);
AQH_Role_SetName(role, sName);
AQH_Role_SetDescr(role, sDescr);
AQH_Role_SetPerms(role, perms);
/* add role */
if (roleList==NULL) {
roleList=AQH_Role_List_new();
AQH_Module_SetRoleList(currentMod, roleList);
}
AQH_Role_List_Add(role, roleList);
if (currentMod) {
AQH_ROLE *role;
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<p>Error saving module.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
/* set new values */
role=AQH_Role_new();
AQH_Role_SetId(role, newId);
AQH_Role_SetName(role, sName);
AQH_Role_SetDescr(role, sDescr);
AQH_Role_SetPerms(role, perms);
/* add role */
if (roleList==NULL) {
roleList=AQH_Role_List_new();
AQH_Module_SetRoleList(currentMod, roleList);
}
AQH_Role_List_Add(role, roleList);
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<p>Error saving module.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
AQH_Module_free(currentMod);
return 0;
}
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
AQH_Module_free(currentMod);
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>Error loading module.</p>\n");
GWEN_Buffer_AppendString(dbuf, "<p><a href=\"index.html\"> back to module list</p>\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
return 0;
}
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
return 0;
}
@@ -1030,24 +837,34 @@ int _handleRqEditRolePost(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session
return 0;
}
/* set new values */
AQH_Role_SetName(role, sName);
AQH_Role_SetDescr(role, sDescr);
AQH_Role_SetPerms(role, perms);
if (currentMod) {
/* set new values */
AQH_Role_SetName(role, sName);
AQH_Role_SetDescr(role, sDescr);
AQH_Role_SetPerms(role, perms);
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<p>Error saving module.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<p>Error saving module.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
AQH_Module_free(currentMod);
return 0;
}
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
AQH_Module_free(currentMod);
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>Error loading module.</p>\n");
GWEN_Buffer_AppendString(dbuf, "<p><a href=\"index.html\"> back to module list</p>\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
return 0;
}
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
return 0;
}
@@ -1073,32 +890,41 @@ int _handleRqDeleteRole(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session,
sModName=dbQuery?GWEN_DB_GetCharValue(dbQuery, "mod", 0, NULL):NULL;
id=dbQuery?GWEN_DB_GetIntValue(dbQuery, "id", 0, 0):0;
currentMod=(sModName && *sModName)?AQH_Service_LoadModule(sv, sModName):NULL;
roleList=currentMod?AQH_Module_GetRoleList(currentMod):NULL;
role=roleList?AQH_Role_List_GetById(roleList, id):NULL;
if (currentMod) {
roleList=currentMod?AQH_Module_GetRoleList(currentMod):NULL;
role=roleList?AQH_Role_List_GetById(roleList, id):NULL;
if (role) {
int rv;
if (role) {
int rv;
AQH_Role_List_Del(role);
AQH_Role_free(role);
AQH_Role_List_Del(role);
AQH_Role_free(role);
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<p>Error saving module.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
AQH_Module_free(currentMod);
return 0;
}
/* save module */
rv=AQH_Service_SaveModule(sv, currentMod);
if (rv<0) {
GWEN_Buffer_AppendString(dbuf, "<p>Error saving module.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
return 0;
}
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
_setLocationHeaderForMod(rq, "editmodule.html", sModName);
AQCGI_Request_SetResponseCode(rq, 303);
AQCGI_Request_SetResponseText(rq, "See Other");
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>Role not found.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}
AQH_Module_free(currentMod);
}
else {
GWEN_Buffer_AppendString(dbuf, "<p>Role not found.</p>\n");
GWEN_Buffer_AppendArgs(dbuf, "<p><a href=\"editmodule.html?name=\"%s\"> back to module</p>\n", sModName?sModName:"");
GWEN_Buffer_AppendString(dbuf, "<p>Error loading module.</p>\n");
GWEN_Buffer_AppendString(dbuf, "<p><a href=\"index.html\"> back to module list</p>\n");
AQCGI_Request_SetResponseCode(rq, 200);
AQCGI_Request_SetResponseText(rq, "Ok");
}