📄 aclintf.pas
字号:
function TCreatePermissionXML.GetWritePermission(FileName: String;RoleId: String): TStrings;
var
a: TStrings;
rolelist: IXMLDocument;
rolelistnode,subnode,grandnode: IXMLNode;
i: integer;
begin
// a := TStrings.Create; 此句有问题
a := TStringList.Create;
rolelist := TXMLDocument.Create(nil);
rolelist.LoadFromFile(Filename + '\rolelist.xml');
rolelist.active := true;
rolelistnode := rolelist.DocumentElement;
i := 0;
while i<rolelistnode.ChildNodes.Count do
begin
subnode := rolelistnode.ChildNodes.Nodes[i];
if subnode.Attributes['id'] = RoleId then
break;
Inc(i);
end;
i := 0;
while i<subnode.ChildNodes.Count do
begin
grandnode := subnode.ChildNodes.Nodes[i];
if grandnode.ChildNodes.FindNode('write').Text = 'True' then
a.Add(grandnode.ChildNodes.FindNode('code').Text);
Inc(i);
end;
{ if a = nil then
begin
result := nil;
exit;
end;}
rolelist.active := false;
result := a;
//FreeAndNil(a);
end;
{procedure TForm1.BuildPermission(Node:IXMLNode);//递归方法遍历
var
i:integer;
a,b:string;
subnode: IXMLNode;
exist: boolean;
begin
if Node = Form1.FNode then
begin
if not Form1.FNode.HasChildNodes then
Exit;
for i:= 0 to Node.ChildNodes.Count-1 do
begin
BuildPermission(Node.ChildNodes.Nodes[i]);
end;
i := 0;
while i<Node.ChildNodes.Count do
begin
if i=Node.ChildNodes.Count then
i := i-1;
BuildPermission(Node.ChildNodes.Nodes[i]);
Inc(i);
end;
end
else
if Node.ChildNodes.Count = 0 then
begin
//showmessage(vartostr(node.Attributes['Caption'])+'是叶子节点');
a := vartostr(node.Attributes['Caption']);
showmessage(a);
b := vartostr(node.Attributes['Tag']);
//如果该叶子节点或者它的兄弟叶子节点都不存在于用户的权限表中
exist := false;
for i:=0 to Node.ParentNode.ChildNodes.Count-1 do
begin
subnode := Node.ParentNode.ChildNodes.Nodes[i];
if readexist(subnode) or writeexist(subnode) then
begin
exist := true;
break;
end;
end;
if not exist then
begin
//删除没有叶子节点的节点,根节点除外
Node := DelPermission(Node.ParentNode);
exit;
{ showmessage(Node.Attributes['Caption']);
BuildPermission(Node);
end;
//如果该叶子节点不存在于用户的读或者写权限表中
if (not (readexist(Node))) and (not (writeexist(Node))) then
begin
DelPermission(Node);
exit;
end
else exit;
end
else
if vartostr(node.Attributes['Caption'])='A1' then
showmessage(vartostr(node.Attributes['Caption'])+'&'+inttostr(Node.ChildNodes.Count));
begin
i := 0;
while i<Node.ChildNodes.Count do
begin
if i=Node.ChildNodes.Count then
i := i-1;
showmessage(Node.ChildNodes.Nodes[i].Attributes['Caption']);
if Node.ChildNodes.Nodes[i].ChildNodes.Count = 0 then
BuildPermission(Node.ChildNodes.Nodes[i]);
Inc(i);
end;
for i:= 0 to Node.ChildNodes.Count-1 do
begin
if i>Node.ChildNodes.Count-1 then
i := i-1;
BuildPermission(Node.ChildNodes.Nodes[i])
end;
end;
end; }
procedure TCreatePermissionXML.BuildPermission(Node:IXMLNode);//删除不属于该用户权限的叶子节点
var
i,count:integer;
a,b:string;
subnode: IXMLNode;
begin
del := false;
if Node =FNode then
begin
if not FNode.HasChildNodes then
Exit;
i := 0;
count := node.ChildNodes.Count;
while i<count do
begin
// showmessage(node.ChildNodes.Nodes[i].Attributes['Caption']);
//==============//
subnode := node.ChildNodes.Nodes[i];
if subnode.ChildNodes.Count = 0 then
if (not readexist(subnode)) and (not writeexist(subnode)) then
begin
node.ChildNodes.Delete(i);
i := i-1;
count := count-1;
end;
//==============//
BuildPermission(Node.ChildNodes.Nodes[i]);
Inc(i);
end;
end
else
if Node.ChildNodes.Count = 0 then
begin
//showmessage(vartostr(node.Attributes['Caption'])+'是叶子节点');
a := vartostr(node.Attributes['Caption']);
// showmessage(a);
b := vartostr(node.Attributes['Tag']);
if (not readexist(Node)) and (not writeexist(Node)) then
begin
DelPermission(Node);
del := true;
exit;
end;
end
else
{ if vartostr(node.Attributes['Caption'])='A1' then
showmessage(vartostr(node.Attributes['Caption'])+'&'+inttostr(Node.ChildNodes.Count)); }
i := 0;
count := node.ChildNodes.Count;
while i<count do
begin
// showmessage(node.Attributes['Caption']);
if del then
begin
i := i-1;
count := count-1;
end;
BuildPermission(Node.ChildNodes.Nodes[i]);
Inc(i);
end;
end;
function TCreatePermissionXML.DelPermission(Node:IXMLNode): IXMLNode;//删除叶子节点
var
i: integer;
ParentNode: IXMLNode;
begin
if Node.ParentNode <> FNode then
begin
{if (Node.ParentNode.ChildNodes.Count = 1) and (Node.ParentNode<>Form1.FNode) then
DelPermission(Node.ParentNode)
else }
ParentNode := Node.ParentNode;
begin
for i:=0 to ParentNode.ChildNodes.Count-1 do
begin
// showmessage(Node.Attributes['Caption']);
// showmessage(ParentNode.ChildNodes.Nodes[i].Attributes['Caption']);
if ParentNode.ChildNodes.Nodes[i].Attributes['Caption']=Node.Attributes['Caption'] then
break;
end;
ParentNode.ChildNodes.Delete(i);
// Node.ParentNode.ChildNodes.Delete(0); // 删除Node节点
{ if i<ParentNode.ChildNodes.Count-1 then
result := ParentNode.ChildNodes.Nodes[i]
else result := ParentNod }
end;
end;
// else exit;
end;
function TCreatePermissionXML.CreatePermission(Node: IXMLNode): IXMLNode;//删除没有叶子节点(Tag=1为叶子节点)的节点,根节点除外
var
i,count: integer;
subnode: IXMLNode;
begin
if Node = FNode then
begin
count := Node.ChildNodes.Count;
i := 0;
while i<count do
begin
subnode := Node.ChildNodes.Nodes[i];
if strtoint(subnode.Attributes['Tag']) = 0 then
begin
CreatePermission(subnode);
if subnode.ChildNodes.Count = 0 then
begin
FNode.ChildNodes.Delete(i);
i := i-1;
count := count-1;
end;
end;
Inc(i);
end;
end
else
if node.ChildNodes.Count = 0 then
begin
if node.Attributes['Tag'] = 0 then
begin
del := true;
exit;
end;
end
else
begin
count := node.ChildNodes.Count;
i := 0;
// del := false;
while i<count do
begin
subnode := node.ChildNodes.Nodes[i];
if subnode.Attributes['Tag'] = 0 then
begin
CreatePermission(subnode);
end;
if del then
begin
node.ChildNodes.Delete(i);
i := i-1;
count := count-1;
del := false;
end;
if node.ChildNodes.Count = 0 then
if node.ParentNode <> FNode then
node.ParentNode.ChildNodes.Delete(0);
Inc(i);
end;
end;
end;
function TCreatePermissionXML.readexist(Node: IXMLNode): boolean;
var
i: integer;
begin
for i:=0 to read_result.Count-1 do
begin
if read_result.Strings[i] = Node.Attributes['Caption'] then
begin
result := true;
exit;
end;
end;
result := false;
end;
function TCreatePermissionXML.writeexist(Node: IXMLNode): boolean;
var
i: integer;
begin
for i:=0 to write_result.Count-1 do
begin
if write_result.Strings[i] = Node.Attributes['Caption'] then
begin
result := true;
exit;
end;
end;
result := false;
end;
{procedure TForm1.Button1Click(Sender: TObject);
var
aa: IXMLDocument;
bb: string;
begin
aa := TXMLDocument.Create(nil);
aa.LoadFromFile('c:\aaa.xml');
aa.Active := true;
FNode := aa.DocumentElement;
bb := vartostr(execute('Login',[inttostr(1),'']));
showmessage(bb);
// bb := vartostr(execute('Login',['之',inttostr(1)]));
// showmessage(bb);
BuildPermission(Form1.FNode);
aa.SaveToFile('c:\ccc.xml');
//==================//
aa := TXMLDocument.Create(nil);
aa.LoadFromFile('c:\ccc.xml');
aa.Active := true;
FNode := aa.DocumentElement;
CreatePermission(Form1.FNode);
aa.SaveToFile('c:\ccc.xml');
//==================//
aa.Active := false;
showmessage('生成成功!');
end;}
procedure TCreatePermissionXML.CreatePermissionXML(cmd: String; params: array of Const);
var
OriXML: IXMLDocument;
begin
OriXML := TXMLDocument.Create(nil);
OriXML.LoadFromFile(FileName + '\aaa.xml');
OriXML.Active := true;
FNode := OriXML.DocumentElement;
showmessage(vartostr(execute(cmd,[String(params[0].VAnsiString),String(params[1].VAnsiString)])));
BuildPermission(FNode);
OriXML.SaveToFile(FileName + '\ccc.xml');
OriXML := TXMLDocument.Create(nil);
OriXML.LoadFromFile(FileName + '\ccc.xml');
OriXML.Active := true;
FNode := OriXML.DocumentElement;
CreatePermission(FNode);
OriXML.SaveToFile(FileName + '\ccc.xml');
OriXML.Active := false;
// sleep(3000);
showmessage('生成成功!');
end;
constructor TCreatePermissionXML.Create(const AFileName: String);
begin
FileName := AFileName;
end;
destructor TCreatePermissionXML.Destroy;
begin
inherited;
end;
//=============================================//
initialization
finalization
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -