Tips to implement permission management in DELPHI
When using DELPHI to compile some application systems, it is often necessary to grant different permissions to different users, and different systems have different permission allocation methods. If permissions are assigned to each user in the program, not only It makes the preparation of the program very troublesome, and it is not conducive to management! The author recently came up with a method that I think is better, and I would like to share it with you. I hope you can give me more advice!
In many systems, permissions are usually divided into several levels. The operations that users at each level can perform are different, and the method to achieve this function is generally to allow users with different permissions to see different menus. Simply put, this menu is invisible to users who do not have certain permissions! In order to achieve this control, programmers often have to spend a lot of time working on this module, wasting a lot of precious time!
The author's idea is: if we set up a two-dimensional array, the first dimension represents the first-level menu, and the second dimension represents the sub-menu. Each array element only stores the two numbers 0 or 1. A user with a certain authority corresponds to a A two-dimensional array, and this array represents the corresponding menu. After assigning permissions to a user of a certain level, he will have a corresponding array. When logging in, take the value of each element from the array. If it is 1, the corresponding menu will appear, if it is 0, the menu will not appear. This method can not only be used for classification (the arrays of users at the same level are the same), but also the permissions can be subdivided among different users, as long as you modify the corresponding value to 0 or 1!
The above is the result of a simple example.
The following is part of the source program (this is just an explanatory example program). For simplicity, there is only one form and one main menu in the program. Initialize the array M when creating the form, read the array value when displaying and control the display by setting the Enabled property of the menu or True or False of the Visible property! !
var
Form1: TForm1;
m:array[0..1,0..4] of integer; //Define the stored array
i,j:integer;//Define the variables of the loop
implementation
{$R *.dfm}
PRocedure TForm1.FormShow(Sender: TObject);
begin //Read the numbers and control the display of the menu
for i:=Low(m) to High(m) do
for j:=Low(m[i]) to High(m[i]) do begin
if m[i][j]=0 then MainMenu1.Items[i].Items[j].Enabled:=false
else MainMenu1.Items[i].Items[j].Enabled:=true;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin //The following is initialization
k:=0;
for i:=Low(m) to High(m) do
for j:=Low(m[i]) to High(m[i]) do
if (j mod 2)=0 then m[i][j]:=1
else m[i][j]:=0;
end;