RolesPermissions 组件 — sgcHTML | eSeGeCe

RolesPermissions

TsgcHTMLComponent_RolesPermissions — 一个角色/权限矩阵,每个单元格带复选框,支持分类分组和实时 WebSocket 授权更新,适用于 Delphi、C++ Builder 和 .NET。

TsgcHTMLComponent_RolesPermissions

添加角色和权限,设置初始授权,然后读取 HTML 属性。每次复选框切换都会发送到服务器,服务器必须先授权才能应用。

组件类

TsgcHTMLComponent_RolesPermissions

渲染为

Bootstrap 5 表格矩阵 + 内联脚本

家族

身份验证

语言

Delphi, C++ Builder, .NET

添加角色、添加权限、授权、渲染它

调用 AddRoleAddPermission 构建矩阵的两个轴,用 SetGrant 设置初始勾选状态,然后读取 HTML

uses
  sgcHTML_Enums, sgcHTML_Component_RolesPermissions;

var
  oMatrix: TsgcHTMLComponent_RolesPermissions;
begin
  oMatrix := TsgcHTMLComponent_RolesPermissions.Create(nil);
  try
    oMatrix.MatrixID := 'acl';
    oMatrix.StickyHeader := True;

    oMatrix.AddRole('admin', 'Administrator').Color := hcDanger;
    oMatrix.AddRole('editor', 'Editor');
    oMatrix.AddRole('viewer', 'Viewer');

    oMatrix.AddPermission('users.edit', 'Edit users', 'Users');
    oMatrix.AddPermission('users.delete', 'Delete users', 'Users');
    oMatrix.AddPermission('billing.view', 'View invoices', 'Billing');

    oMatrix.SetGrant('admin', 'users.edit', True);
    oMatrix.SetGrant('admin', 'users.delete', True);
    oMatrix.SetGrant('editor', 'users.edit', True);

    WebModule.Response := oMatrix.HTML;   // matrix table + inline script
  finally
    oMatrix.Free;
  end;
end;

// Server-side handler for a checkbox toggle received over WebSockets.
// ALWAYS verify the acting user is authorized before applying the change:
oMatrix.ProcessGrantChanged('editor', 'users.delete', True);
// includes: sgcHTML_Enums.hpp, sgcHTML_Component_RolesPermissions.hpp

TsgcHTMLComponent_RolesPermissions *oMatrix = new TsgcHTMLComponent_RolesPermissions(NULL);
try
{
  oMatrix->MatrixID = "acl";
  oMatrix->StickyHeader = true;

  oMatrix->AddRole("admin", "Administrator")->Color = hcDanger;
  oMatrix->AddRole("editor", "Editor");
  oMatrix->AddRole("viewer", "Viewer");

  oMatrix->AddPermission("users.edit", "Edit users", "Users");
  oMatrix->AddPermission("users.delete", "Delete users", "Users");
  oMatrix->AddPermission("billing.view", "View invoices", "Billing");

  oMatrix->SetGrant("admin", "users.edit", true);
  oMatrix->SetGrant("admin", "users.delete", true);
  oMatrix->SetGrant("editor", "users.edit", true);

  String html = oMatrix->HTML;   // matrix table + inline script

  // Server-side handler for a checkbox toggle (verify authorization first):
  oMatrix->ProcessGrantChanged("editor", "users.delete", true);
}
__finally
{
  delete oMatrix;
}
using esegece.sgcWebSockets;

var matrix = new TsgcHTMLComponent_RolesPermissions();
matrix.MatrixID = "acl";
matrix.StickyHeader = true;

matrix.AddRole("admin", "Administrator").Color = TsgcHTMLColor.hcDanger;
matrix.AddRole("editor", "Editor");
matrix.AddRole("viewer", "Viewer");

matrix.AddPermission("users.edit", "Edit users", "Users");
matrix.AddPermission("users.delete", "Delete users", "Users");
matrix.AddPermission("billing.view", "View invoices", "Billing");

matrix.SetGrant("admin", "users.edit", true);
matrix.SetGrant("admin", "users.delete", true);
matrix.SetGrant("editor", "users.edit", true);

string html = matrix.HTML;   // matrix table + inline script

// Server-side handler for a checkbox toggle (verify authorization first):
matrix.ProcessGrantChanged("editor", "users.delete", true);

关键属性与方法

您最常使用的成员。

角色与权限

AddRole(aId, aName)AddPermission(aId, aName, aCategory) 构建矩阵的两个轴;每个角色都有一个彩色徽章,每个权限都有一个可选的分类标题。

授权

SetGrant(aRoleId, aPermissionId, aGranted) / GetGrant 以角色 + 权限 id 为键,读写勾选的单元格。

数据集绑定

LoadRolesFromDataSetLoadPermissionsFromDataSetLoadGrantsFromDataSet 直接从 TDataSet 填充矩阵。

客户端变更

每次复选框切换都会通过 WebSocket 通道发送 JSON 负载,并触发 DOM 事件 sgcRolesPermissions:grantChanged

外观

ShowDescriptionsShowCategoriesStripedBorderedCompactStickyHeader(配合 MaxHeight)设置表格样式;ReadOnly 禁用所有复选框。

安全性

此组件仅渲染 UI。只有在宿主程序在服务器端验证操作用户有权更改该角色/权限组合之后,才能调用 ProcessGrantChanged

继续探索

在线帮助此组件的完整 API 参考和使用指南。
所有 sgcHTML 组件浏览 80 多个组件的完整功能矩阵。
下载免费试用版30 天试用版附带 60.HTML 演示项目。
价格Single、Team 和 Site 授权,均含完整源代码。

准备好开始了吗?

下载免费试用版,为您的 Delphi、C++ Builder 或 .NET 管理面板添加角色/权限矩阵。