218 lines
6.4 KiB
C
218 lines
6.4 KiB
C
/****************************************************************************
|
|
* This file is part of the project AqHome.
|
|
* AqHome (c) by 2025 Martin Preuss, all rights reserved.
|
|
*
|
|
* The license for this file can be found in the file COPYING which you
|
|
* should have received along with this file.
|
|
****************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <config.h>
|
|
#endif
|
|
|
|
|
|
#include "./madmin.h"
|
|
|
|
#include "aqhome-cgi/service/module.h"
|
|
#include "aqhome-cgi/modules/common/mmodules.h"
|
|
#include "aqhome-cgi/modules/common/musers.h"
|
|
|
|
#include <gwenhywfar/debug.h>
|
|
#include <gwenhywfar/timestamp.h>
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* defs and enums
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* global vars
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* forward declarations
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
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 int _handleRqIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, GWEN_BUFFER *dbuf);
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
* code
|
|
* ------------------------------------------------------------------------------------------------
|
|
*/
|
|
|
|
void AQH_ModAdmin_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder)
|
|
{
|
|
AQH_ModService_Extend(m, sv, baseFolder);
|
|
AQH_ModService_SetHandleRequestFn(m, _handleRequest);
|
|
AQH_ModService_SetLoadSubModuleFn(m, _loadSubModule);
|
|
}
|
|
|
|
|
|
|
|
int AQH_ModAdmin_Create(AQH_SERVICE *sv)
|
|
{
|
|
AQH_MODULE *m;
|
|
int rv;
|
|
|
|
m=AQH_Module_new();
|
|
AQH_Module_SetName(m, "admin");
|
|
AQH_Module_SetDescr(m, "administration 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, "AdminUsers", 0x001, "User Administration");
|
|
AQH_ModService_AddPermDef(permDefList, "AdminModules", 0x002, "Module Administration");
|
|
|
|
AQH_Module_SetPermDefList(m, permDefList);
|
|
}
|
|
|
|
|
|
|
|
void _createRoleList(AQH_MODULE *m)
|
|
{
|
|
AQH_ROLE_LIST *roleList;
|
|
int id=1;
|
|
|
|
roleList=AQH_Role_List_new();
|
|
AQH_ModService_AddRole(roleList, id++, "userAdmin", AQH_MODADM_PERMS_ADMINUSERS, "User administrator");
|
|
AQH_ModService_AddRole(roleList, id++, "moduleAdmin", AQH_MODADM_PERMS_ADMINMODULES, "Module administrator");
|
|
AQH_Module_SetRoleList(m, roleList);
|
|
}
|
|
|
|
|
|
|
|
AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName)
|
|
{
|
|
AQH_SERVICE *sv;
|
|
|
|
sv=AQH_ModService_GetService(m);
|
|
if (strcasecmp(sModuleName, "modules")==0) {
|
|
AQH_MODULE *mSub;
|
|
|
|
mSub=AQH_Service_LoadModule(sv, sModuleName);
|
|
if (mSub) {
|
|
const char *s;
|
|
GWEN_BUFFER *nbuf;
|
|
|
|
nbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
|
s=AQH_ModService_GetBaseFolder(m);
|
|
GWEN_Buffer_AppendArgs(nbuf, "%s/modules", s?s:".");
|
|
|
|
AQH_ModAdmModules_Extend(mSub, AQH_ModService_GetService(m), GWEN_Buffer_GetStart(nbuf));
|
|
AQH_Module_Tree2_AddChild(m, mSub);
|
|
GWEN_Buffer_free(nbuf);
|
|
return mSub;
|
|
}
|
|
}
|
|
else if (strcasecmp(sModuleName, "users")==0) {
|
|
AQH_MODULE *mSub;
|
|
|
|
mSub=AQH_Service_LoadModule(sv, sModuleName);
|
|
if (mSub) {
|
|
const char *s;
|
|
GWEN_BUFFER *nbuf;
|
|
|
|
nbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
|
s=AQH_ModService_GetBaseFolder(m);
|
|
GWEN_Buffer_AppendArgs(nbuf, "%s/modules", s?s:".");
|
|
|
|
AQH_ModAdmUsers_Extend(mSub, AQH_ModService_GetService(m), GWEN_Buffer_GetStart(nbuf));
|
|
AQH_Module_Tree2_AddChild(m, mSub);
|
|
GWEN_Buffer_free(nbuf);
|
|
return mSub;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
|
|
|
|
int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem)
|
|
{
|
|
GWEN_BUFFER *dbuf;
|
|
int rv=0;
|
|
|
|
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
|
AQH_ModService_AddHeader(m, "en", dbuf);
|
|
|
|
if (strcasecmp(sLastPathElem, "index.html")==0) {
|
|
if (AQH_ModService_GetUserPerms(m) & (AQH_MODADM_PERMS_ADMINUSERS | AQH_MODADM_PERMS_ADMINMODULES))
|
|
rv=_handleRqIndex(m, rq, dbuf);
|
|
else {
|
|
AQCGI_Request_SetResponseCode(rq, 403);
|
|
AQCGI_Request_SetResponseText(rq, "Forbidden");
|
|
}
|
|
}
|
|
else {
|
|
AQCGI_Request_SetResponseCode(rq, 404);
|
|
AQCGI_Request_SetResponseText(rq, "Not Found");
|
|
}
|
|
AQH_ModService_AddFooter(m, "en", dbuf);
|
|
AQCGI_Request_SetBufferResponseBody(rq, dbuf);
|
|
AQCGI_Request_AddResponseHeaderData(rq, "Content-type: text/html");
|
|
|
|
return AQCGI_SendResponse(rq);
|
|
}
|
|
|
|
|
|
|
|
int _handleRqIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, GWEN_BUFFER *dbuf)
|
|
{
|
|
if (AQCGI_Request_GetRequestMethod(rq)==AQCGI_REQUEST_METHOD_GET) {
|
|
uint32_t userPerms;
|
|
|
|
GWEN_Buffer_AppendString(dbuf, "<table>");
|
|
userPerms=AQH_ModService_GetUserPerms(m);
|
|
if (userPerms & AQH_MODADM_PERMS_ADMINUSERS)
|
|
GWEN_Buffer_AppendString(dbuf,
|
|
"<tr>"
|
|
"<td><a href=\"users/index.html\" >User administration</a></td>"
|
|
"<td>Add, remove or modify users</td>"
|
|
"</tr>\n");
|
|
if (userPerms & AQH_MODADM_PERMS_ADMINMODULES)
|
|
GWEN_Buffer_AppendString(dbuf,
|
|
"<tr>"
|
|
"<td><a href=\"modules/index.html\" >Module administration</a></td>"
|
|
"<td>Add, remove or modify modules</td>"
|
|
"</tr>\n");
|
|
GWEN_Buffer_AppendString(dbuf, "</table>\n");
|
|
AQCGI_Request_SetResponseCode(rq, 200);
|
|
AQCGI_Request_SetResponseText(rq, "Ok");
|
|
return 0;
|
|
}
|
|
AQCGI_Request_SetResponseCode(rq, 405);
|
|
AQCGI_Request_SetResponseText(rq, "Method Not Allowed");
|
|
return 0;
|
|
}
|
|
|
|
|
|
|