From 6b39e0bfb1e298dadb57cce9b8f968ef7a5b57c4 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Thu, 18 Sep 2025 23:48:47 +0200 Subject: [PATCH] aqhome-cgi: started working on users module. --- apps/aqhome-cgi/modules/common/0BUILD | 2 + apps/aqhome-cgi/modules/common/madmin.c | 19 ++ apps/aqhome-cgi/modules/common/musers.c | 286 ++++++++++++++++++++++++ apps/aqhome-cgi/modules/common/musers.h | 35 +++ 4 files changed, 342 insertions(+) create mode 100644 apps/aqhome-cgi/modules/common/musers.c create mode 100644 apps/aqhome-cgi/modules/common/musers.h diff --git a/apps/aqhome-cgi/modules/common/0BUILD b/apps/aqhome-cgi/modules/common/0BUILD index ff270a4..431bbd0 100644 --- a/apps/aqhome-cgi/modules/common/0BUILD +++ b/apps/aqhome-cgi/modules/common/0BUILD @@ -52,6 +52,7 @@ mservice.h madmin.h mmodules.h + musers.h @@ -66,6 +67,7 @@ mservice.c madmin.c mmodules.c + musers.c diff --git a/apps/aqhome-cgi/modules/common/madmin.c b/apps/aqhome-cgi/modules/common/madmin.c index e5d7d50..429ec24 100644 --- a/apps/aqhome-cgi/modules/common/madmin.c +++ b/apps/aqhome-cgi/modules/common/madmin.c @@ -15,6 +15,7 @@ #include "aqhome-cgi/service/module.h" #include "aqhome-cgi/modules/common/mmodules.h" +#include "aqhome-cgi/modules/common/musers.h" #include #include @@ -131,6 +132,24 @@ AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *sessio 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; } diff --git a/apps/aqhome-cgi/modules/common/musers.c b/apps/aqhome-cgi/modules/common/musers.c new file mode 100644 index 0000000..e94cfab --- /dev/null +++ b/apps/aqhome-cgi/modules/common/musers.c @@ -0,0 +1,286 @@ +/**************************************************************************** + * 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 +#endif + + +#include "./musers.h" + +#include "aqhome-cgi/service/module.h" + +#include +#include +#include + + + +/* ------------------------------------------------------------------------------------------------ + * defs and enums + * ------------------------------------------------------------------------------------------------ + */ + +#define GBAS GWEN_Buffer_AppendString +#define GBAA GWEN_Buffer_AppendArgs + + + +/* ------------------------------------------------------------------------------------------------ + * 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 void _handleRqIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf); + +static void _writeEditUserForm(const AQH_USER *user, const char *sAlias, GWEN_BUFFER *dbuf); +static void _addLabelAndInputToFormTableH(const char *sTitle, const char *sName, const char *sValue, GWEN_BUFFER *dbuf); + +static void _setLocationHeaderForMod(AQCGI_REQUEST *rq, const char *page, const char *sModName); + + + +/* ------------------------------------------------------------------------------------------------ + * vars + * ------------------------------------------------------------------------------------------------ + */ + +static AQH_MODSERVICE_HANDLER_ENTRY _requestTable[]={ + {"index.html", AQCGI_REQUEST_METHOD_GET, AQH_MODADMUSERS_PERMS_USERSREAD, _handleRqIndex}, + {NULL, 0, 0, NULL} +}; + + + +/* ------------------------------------------------------------------------------------------------ + * code + * ------------------------------------------------------------------------------------------------ + */ + +void AQH_ModAdmUsers_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_ModAdmUsers_Create(AQH_SERVICE *sv) +{ + AQH_MODULE *m; + int rv; + + m=AQH_Module_new(); + AQH_Module_SetName(m, "users"); + AQH_Module_SetDescr(m, "user 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, "UserRead", 0x001, "Read users"); + AQH_ModService_AddPermDef(permDefList, "UserWrite", 0x002, "Modify users"); + AQH_ModService_AddPermDef(permDefList, "UserAdd", 0x004, "Add users"); + AQH_ModService_AddPermDef(permDefList, "UserDel", 0x008, "Remove users"); + + 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++, "admin", + AQH_MODADMUSERS_PERMS_USERSREAD | + AQH_MODADMUSERS_PERMS_USERSWRITE | + AQH_MODADMUSERS_PERMS_USERSADD | + AQH_MODADMUSERS_PERMS_USERSDEL, + "Administrator Role"); + AQH_Module_SetRoleList(m, roleList); +} + + + +AQH_MODULE *_loadSubModule(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sModuleName) +{ + /* no sub-modules */ + return NULL; +} + + + +int _handleRequest(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, const char *sLastPathElem) +{ + AQH_ModService_HandleRequestWithTable(m, rq, session, sLastPathElem, _requestTable); + return AQCGI_SendResponse(rq); +} + + + +void _handleRqIndex(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf) +{ + AQH_SERVICE *sv; + GWEN_STRINGLIST *slUsers; + uint32_t perms; + + perms=AQH_ModService_GetUserPerms(m); + sv=AQH_ModService_GetService(m); + slUsers=AQH_Service_ListUsers(sv); + if (slUsers) { + GWEN_STRINGLISTENTRY *se; + + GBAS(dbuf, "

Users

\n"); + GBAS(dbuf, + "\n" + "" + "\n" + "\n" + "\n"); + se=GWEN_StringList_FirstEntry(slUsers); + while(se) { + const char *sUserAlias; + + sUserAlias=GWEN_StringListEntry_Data(se); + if (sUserAlias && *sUserAlias) { + AQH_USER *currentUser; + + currentUser=AQH_Service_LoadUser(sv, sUserAlias); + if (currentUser) { + uint32_t id; + const char *s; + const char *sAlias; + + id=AQH_User_GetId(currentUser); + sAlias=AQH_User_GetAlias(currentUser); + GBAS(dbuf, ""); + /* id */ + GBAA(dbuf, "", (unsigned long int) id); + /* alias */ + GBAA(dbuf, "", sAlias?sAlias:""); + /* name */ + s=AQH_User_GetName(currentUser); + GBAA(dbuf, "", s?s:""); + /* status */ + s=AQH_UserState_toString(AQH_User_GetState(currentUser)); + GBAA(dbuf, "", s?s:""); + /* email */ + s=AQH_User_GetEmail(currentUser); + GBAA(dbuf, "", s?s:""); + /* notes */ + s=AQH_User_GetNotes(currentUser); + GBAA(dbuf, "", s?s:""); + + GBAS(dbuf, "\n"); + GBAA(dbuf, "\n"); + AQH_User_free(currentUser); + } + } + se=GWEN_StringListEntry_Next(se); + } + GBAS(dbuf, + "\n" + "
IdAliasNameStatusEmailNotesActions
%lu%s%s%s%s%s"); + if (perms & AQH_MODADMUSERS_PERMS_USERSWRITE) { + GBAS(dbuf, ""); + } + GBAA(dbuf, "
\n"); + GWEN_StringList_free(slUsers); + } + if (perms & AQH_MODADMUSERS_PERMS_USERSADD) + GBAS(dbuf, "
Add User"); +} + + + +void _handleRqEditUserGet(AQH_MODULE *m, AQCGI_REQUEST *rq, AQH_SESSION *session, GWEN_BUFFER *dbuf) +{ + AQH_SERVICE *sv; + GWEN_DB_NODE *dbQuery; + const char *sAlias; + AQH_USER *user; + + sv=AQH_ModService_GetService(m); + dbQuery=AQCGI_Request_GetDbQuery(rq); + sAlias=dbQuery?GWEN_DB_GetCharValue(dbQuery, "alias", 0, NULL):NULL; + user=(sAlias && *sAlias)?AQH_Service_LoadUser(sv, sAlias):NULL; + if (user) { + _writeEditUserForm(user, sAlias, dbuf); + AQH_User_free(user); + } + else { + AQCGI_Request_AddResponseHeaderData(rq, "Location: index.html"); + AQCGI_Request_SetResponseCode(rq, 303); + AQCGI_Request_SetResponseText(rq, "See other"); + } +} + + + +void _writeEditUserForm(const AQH_USER *user, const char *sAlias, GWEN_BUFFER *dbuf) +{ + + + /* write module info */ + GBAS(dbuf, "

Module Info

\n"); + GBAS(dbuf, + "
\n" + "\n"); + _addLabelAndInputToFormTableH("Alias", "alias", AQH_User_GetAlias(user), dbuf); + _addLabelAndInputToFormTableH("Name", "name", AQH_User_GetName(user), dbuf); + _addLabelAndInputToFormTableH("Email", "email", AQH_User_GetEmail(user), dbuf); + _addLabelAndInputToFormTableH("Notes", "notes", AQH_User_GetNotes(user), dbuf); + GBAS(dbuf, "
\n"); + GBAA(dbuf, "\n", sAlias); + GBAS(dbuf, "\n
\n\n"); + + +} + + + +void _addLabelAndInputToFormTableH(const char *sTitle, const char *sName, const char *sValue, GWEN_BUFFER *dbuf) +{ + GBAS(dbuf, ""); + GBAA(dbuf, "", sName?sName:"", sTitle?sTitle:""); + GBAA(dbuf, ""); + GBAS(dbuf, "\n"); +} + + + diff --git a/apps/aqhome-cgi/modules/common/musers.h b/apps/aqhome-cgi/modules/common/musers.h new file mode 100644 index 0000000..cfa7d74 --- /dev/null +++ b/apps/aqhome-cgi/modules/common/musers.h @@ -0,0 +1,35 @@ +/**************************************************************************** + * 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. + ****************************************************************************/ + +#ifndef AQHOME_CGI_MUSERS_H +#define AQHOME_CGI_MUSERS_H + +#include + +#include + +#include + + + +#define AQH_MODADMUSERS_PERMS_USERSREAD 0x001 +#define AQH_MODADMUSERS_PERMS_USERSWRITE 0x002 +#define AQH_MODADMUSERS_PERMS_USERSADD 0x004 +#define AQH_MODADMUSERS_PERMS_USERSDEL 0x008 + + + +void AQH_ModAdmUsers_Extend(AQH_MODULE *m, AQH_SERVICE *sv, const char *baseFolder); + +int AQH_ModAdmUsers_Create(AQH_SERVICE *sv); + + + + +#endif +