折腾了一周了,大虾帮看看看.偶要实现的功能是,如果用户没权限操作这个菜单MainMenu1里的菜单项,那这个菜单项就会暗(不可用).而数据库表userAuthor 里,保存的就是每个用户,能操作哪个菜单的记录,格式如下:
id username MenuName
---------------------------
1 小张 人员管理
2 小张 供应商管理
......
现在,问题是,为什么我的代码,除了管理员,在换用户后,菜单的权限还是一样和上一个用户一样.我的CheckMenu是放在主窗口的FormActivate里的,系统切换登陆用户时,
会把主窗口MainForm隐藏,再显示,但调试时发现是会执行到CheckMenu这个函数的,
可为什么菜单的明暗(可用/禁止)状态还是没变?
如何解决?还有,这代码效率太低..晕死..
//禁止不能用的菜单
procedure TMainForm.CheckMenu;
var
i,j,k,x:Integer;
str1,str2,Mstr:string;
begin
if LoginPower='系统管理员' then Exit; //取得所有子菜单
{for j:=0 to ComponentCount-1 do
begin
if (Components[j].classtype=TMenuItem) then
begin
TMenuItem(Components[j]).Enabled:=True;
end;
Next; end; }
try
//循环整个菜单项
for i := 0 to MainMenu1.Items.Count-1 do
begin
if i=1 then Continue;{特殊标记退出}
if i=10 then Continue; //循环子菜单
for j := 0 to MainMenu1.Items.items[i].Count-1 do
begin
if MainMenu1.Items[I].Items[J].Caption='-' THEN Continue;{特殊标记退出}
if MainMenu1.Items[I].Items[J].Tag=111 then Continue;
str1:='';
str1:=GetSql('MenuName','Select MenuName from userAuthor where UserNAme='''+LoginUser+''' and MenuName='''+StringReplace(trim(MainMenu1.Items[I].Items[J].Caption),'新增','',[rfReplaceAll,rfIgnoreCase])+''' ');//取得当前登陆用户对该菜单的操作权限是否存在
if str1<>'' then
MainMenu1.Items[I].Items[J].Enabled:=true
else
if MainMenu1.Items[I].Items[J].Count<=0 then//过滤掉有子菜单的菜单被禁止的情况
MainMenu1.Items[I].Items[J].Enabled:=False;
for k := 0 to MainMenu1.Items.Items[i].Items[j].Count-1 do
begin
str2:='';
if MainMenu1.Items[I].Items[J].Items[K].Caption='-' THEN Continue;
if MainMenu1.Items[I].Items[J].Items[K].Tag=111 then Continue;
str2:=GetSql('MenuName','Select MenuName from userAuthor where UserName='''+LoginUser+''' and MenuName='''+StringReplace(trim(MainMenu1.Items[I].Items[J].Items[K].Caption),'新增','',[rfReplaceAll,rfIgnoreCase])+''' ');
if str2<>'' then
MainMenu1.Items[I].Items[J].Items[K].Enabled:=true
else
if MainMenu1.Items[I].Items[J].Items[K].Count<=0 then
MainMenu1.Items[I].Items[J].Items[K].Enabled:=false; next;
end;
next;
end;
next;
end;
except
end;
end;
id username MenuName
---------------------------
1 小张 人员管理
2 小张 供应商管理
......
现在,问题是,为什么我的代码,除了管理员,在换用户后,菜单的权限还是一样和上一个用户一样.我的CheckMenu是放在主窗口的FormActivate里的,系统切换登陆用户时,
会把主窗口MainForm隐藏,再显示,但调试时发现是会执行到CheckMenu这个函数的,
可为什么菜单的明暗(可用/禁止)状态还是没变?
如何解决?还有,这代码效率太低..晕死..
//禁止不能用的菜单
procedure TMainForm.CheckMenu;
var
i,j,k,x:Integer;
str1,str2,Mstr:string;
begin
if LoginPower='系统管理员' then Exit; //取得所有子菜单
{for j:=0 to ComponentCount-1 do
begin
if (Components[j].classtype=TMenuItem) then
begin
TMenuItem(Components[j]).Enabled:=True;
end;
Next; end; }
try
//循环整个菜单项
for i := 0 to MainMenu1.Items.Count-1 do
begin
if i=1 then Continue;{特殊标记退出}
if i=10 then Continue; //循环子菜单
for j := 0 to MainMenu1.Items.items[i].Count-1 do
begin
if MainMenu1.Items[I].Items[J].Caption='-' THEN Continue;{特殊标记退出}
if MainMenu1.Items[I].Items[J].Tag=111 then Continue;
str1:='';
str1:=GetSql('MenuName','Select MenuName from userAuthor where UserNAme='''+LoginUser+''' and MenuName='''+StringReplace(trim(MainMenu1.Items[I].Items[J].Caption),'新增','',[rfReplaceAll,rfIgnoreCase])+''' ');//取得当前登陆用户对该菜单的操作权限是否存在
if str1<>'' then
MainMenu1.Items[I].Items[J].Enabled:=true
else
if MainMenu1.Items[I].Items[J].Count<=0 then//过滤掉有子菜单的菜单被禁止的情况
MainMenu1.Items[I].Items[J].Enabled:=False;
for k := 0 to MainMenu1.Items.Items[i].Items[j].Count-1 do
begin
str2:='';
if MainMenu1.Items[I].Items[J].Items[K].Caption='-' THEN Continue;
if MainMenu1.Items[I].Items[J].Items[K].Tag=111 then Continue;
str2:=GetSql('MenuName','Select MenuName from userAuthor where UserName='''+LoginUser+''' and MenuName='''+StringReplace(trim(MainMenu1.Items[I].Items[J].Items[K].Caption),'新增','',[rfReplaceAll,rfIgnoreCase])+''' ');
if str2<>'' then
MainMenu1.Items[I].Items[J].Items[K].Enabled:=true
else
if MainMenu1.Items[I].Items[J].Items[K].Count<=0 then
MainMenu1.Items[I].Items[J].Items[K].Enabled:=false; next;
end;
next;
end;
next;
end;
except
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货