aqhome-cgi: started working on users module.
This commit is contained in:
@@ -52,6 +52,7 @@
|
||||
mservice.h
|
||||
madmin.h
|
||||
mmodules.h
|
||||
musers.h
|
||||
</headers>
|
||||
|
||||
|
||||
@@ -66,6 +67,7 @@
|
||||
mservice.c
|
||||
madmin.c
|
||||
mmodules.c
|
||||
musers.c
|
||||
</sources>
|
||||
|
||||
|
||||
|
||||
@@ -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 <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/timestamp.h>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
286
apps/aqhome-cgi/modules/common/musers.c
Normal file
286
apps/aqhome-cgi/modules/common/musers.c
Normal file
@@ -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 <config.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "./musers.h"
|
||||
|
||||
#include "aqhome-cgi/service/module.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/timestamp.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* 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, "<h1>Users</h1>\n");
|
||||
GBAS(dbuf,
|
||||
"<table class=\"datatable\">\n"
|
||||
"<thead>"
|
||||
"<tr><th>Id</th><th>Alias</th><th>Name</th><th>Status</th><th>Email</th><th>Notes</th><th>Actions</th></tr>\n"
|
||||
"</thead>\n"
|
||||
"<tbody>\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, "<tr>");
|
||||
/* id */
|
||||
GBAA(dbuf, "<td>%lu</td>", (unsigned long int) id);
|
||||
/* alias */
|
||||
GBAA(dbuf, "<td>%s</td>", sAlias?sAlias:"");
|
||||
/* name */
|
||||
s=AQH_User_GetName(currentUser);
|
||||
GBAA(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* status */
|
||||
s=AQH_UserState_toString(AQH_User_GetState(currentUser));
|
||||
GBAA(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* email */
|
||||
s=AQH_User_GetEmail(currentUser);
|
||||
GBAA(dbuf, "<td>%s</td>", s?s:"");
|
||||
/* notes */
|
||||
s=AQH_User_GetNotes(currentUser);
|
||||
GBAA(dbuf, "<td>%s</td>", s?s:"");
|
||||
|
||||
GBAS(dbuf, "<td>");
|
||||
if (perms & AQH_MODADMUSERS_PERMS_USERSWRITE) {
|
||||
GBAS(dbuf, "<a href=\"edituser.html?alias=\"");
|
||||
GWEN_Text_EscapeToBufferTolerant(sAlias?sAlias:"", dbuf);
|
||||
GBAS(dbuf, "\"><img src=\"/pics/edit.png\"></a>");
|
||||
}
|
||||
GBAA(dbuf, "</td>\n");
|
||||
GBAA(dbuf, "</tr>\n");
|
||||
AQH_User_free(currentUser);
|
||||
}
|
||||
}
|
||||
se=GWEN_StringListEntry_Next(se);
|
||||
}
|
||||
GBAS(dbuf,
|
||||
"</tbody>\n"
|
||||
"</table>\n");
|
||||
GWEN_StringList_free(slUsers);
|
||||
}
|
||||
if (perms & AQH_MODADMUSERS_PERMS_USERSADD)
|
||||
GBAS(dbuf, "<hr><a href=\"adduser.html\">Add User</a>");
|
||||
}
|
||||
|
||||
|
||||
|
||||
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, "<h2>Module Info</h2>\n");
|
||||
GBAS(dbuf,
|
||||
"<form action=\"edituser.html\" method=\"post\">\n"
|
||||
"<table class=\"formtable\">\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, "</table>\n");
|
||||
GBAA(dbuf, "<input type=\"hidden\" name=\"alias\" value=\"%s\">\n", sAlias);
|
||||
GBAS(dbuf, "<input type=\"submit\" value=\"Save\">\n</form>\n\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _addLabelAndInputToFormTableH(const char *sTitle, const char *sName, const char *sValue, GWEN_BUFFER *dbuf)
|
||||
{
|
||||
GBAS(dbuf, "<tr>");
|
||||
GBAA(dbuf, "<td><label for=\"%s:\">%s</label></td>", sName?sName:"", sTitle?sTitle:"");
|
||||
GBAA(dbuf, "<td><input type=\"text\" name=\"%s\"", sName?sName:"");
|
||||
if (sValue && *sValue)
|
||||
GBAA(dbuf, " value=\"%s\"", sValue);
|
||||
GBAS(dbuf, "></td>");
|
||||
GBAS(dbuf, "</tr>\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
35
apps/aqhome-cgi/modules/common/musers.h
Normal file
35
apps/aqhome-cgi/modules/common/musers.h
Normal file
@@ -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 <aqhome-cgi/modules/common/mservice.h>
|
||||
|
||||
#include <aqcgi/request.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user