S0t4's Blog

Hanya Catatan dan Mencoba Untuk Berbagi

Friday, May 22, 2009

Iseng-iseng Buat Script Batasi Hak User pada Program pakai Delphi

  Blogger Sejati       Friday, May 22, 2009
Komponen yang di pasang pada Form:
Dbgrid
Edit text: Edit1,edit2,edit3
Button : Bnew,Bedit,Bdelete,Bsave
Query : Zquery,Zquery1 (query-nya aku pakai zeos untuk koneksi mysql)
TCheckbox(jumlah suka2)

Berikut listing lengkapnya :

unit edit_user;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ZQuery, ZMySqlQuery, Grids, Wwdbigrd, Wwdbgrid, StdCtrls;

type
TFedituser = class(TForm)
wwDBGrid1: TwwDBGrid;
Zquery: TZMySqlQuery;
Zqueryid: TIntegerField;
Zquerynama: TStringField;
Zquerypassword: TStringField;
DataSource1: TDataSource;
Box: TGroupBox;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Bnew: TButton;
Bedit: TButton;
Bdelete: TButton;
Bsave: TButton;
Zquery1: TZMySqlQuery;
Edit3: TEdit;
Label3: TLabel;
Zquerylantai: TIntegerField;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox4: TCheckBox;
Label4: TLabel;
CheckBox3: TCheckBox;
CheckBox15: TCheckBox;
CheckBox5: TCheckBox;
CheckBox6: TCheckBox;
CheckBox7: TCheckBox;
CheckBox9: TCheckBox;
CheckBox8: TCheckBox;
CheckBox13: TCheckBox;
CheckBox14: TCheckBox;
CheckBox12: TCheckBox;
CheckBox16: TCheckBox;
CheckBox10: TCheckBox;
CheckBox11: TCheckBox;
CheckBox17: TCheckBox;
CheckBox18: TCheckBox;
CheckBox19: TCheckBox;
Label5: TLabel;
procedure wwDBGrid1DblClick(Sender: TObject);
procedure BnewClick(Sender: TObject);
procedure BsaveClick(Sender: TObject);
procedure BeditClick(Sender: TObject);
procedure BdeleteClick(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
id_update:string;
hak:string;
checkboxcount:integer;
public
{ Public declarations }
end;

var
Fedituser: TFedituser;

implementation

uses datamodule;

{$R *.DFM}
procedure TFedituser.wwDBGrid1DblClick(Sender: TObject);
begin
edit1.Text:=Zquerynama.Value;
edit3.Text:=inttostr(Zquerylantai.value);
end;

procedure TFedituser.BnewClick(Sender: TObject);
begin
id_update:='new';
box.Enabled:=true;
edit1.SetFocus;
end;

procedure TFedituser.BeditClick(Sender: TObject);
var i:integer;
begin
box.Enabled:=true;
edit2.SetFocus;
id_update:='edit';
with zquery1 do
begin
close;
sql.Clear;
sql.Add('SELECT hak,id FROM pemakai where id=:id');
params[0].AsInteger:=zqueryid.Value;
open;
end;

hak:=zquery1.fields[0].value;
zquery1.Close;
for i:=1 to checkboxcount do
begin
if (hak[i]='a') then
TCheckbox(FindComponent('Checkbox'+IntToStr(i))).Checked := true
else
TCheckbox(FindComponent('Checkbox'+IntToStr(i))).Checked :=
false;
end;
end;

procedure TFedituser.BdeleteClick(Sender: TObject);
begin
if (Application.MessageBox('Data mau dihapus?',
'Peringatan', MB_YESNO or MB_ICONQUESTION) = IDYES) then
begin
with Zquery1 do
begin
close;
sql.Clear;
sql.Add('DELETE FROM pemakai WHERE id=:id');
params[0].AsInteger:=Zqueryid.Value;
ExecSql;
end;
end;
Zquery.Refresh;
end;

procedure TFedituser.BsaveClick(Sender: TObject);
var hak : array[1..50] of char;
hakuser:string;
i:integer;
begin
hakuser:='';
if id_update='new' then
begin
for i:=1 to checkboxcount do
begin
if
(TCheckbox(FindComponent('Checkbox'+IntToStr(i))).Checked = true) then
hak[i]:='a' else hak[i]:='n';
hakuser:=hakuser+hak[i];
end;

if (edit1.text<>'') then
begin
with Zquery1 do
begin
close;
sql.Clear;
sql.Add('INSERT INTO pemakai(nama,password,lantai,hak)
VALUES (:nm,password(:pw),:lt,:hk)');
params[0].AsString:=edit1.Text;
params[1].AsString:=edit2.Text;
params[2].AsInteger:=strtoint(edit3.Text);
params[3].AsString:=hakuser;
ExecSql;
end;
end;
end else
if id_update='edit' then
begin
for i:=1 to checkboxcount do
begin
if
(TCheckbox(FindComponent('Checkbox'+IntToStr(i))).Checked = true) then
hak[i]:='a' else hak[i]:='n';
hakuser:=hakuser+hak[i];
end;

if (edit2.text<>'') then
begin
with Zquery1 do
begin
close;
sql.Clear;
sql.Add('UPDATE pemakai SET
nama=:nm,password=password(:pw),lantai=:lt,hak=:hk WHERE id=:id');
params[0].AsString:=edit1.Text;
params[1].AsString:=edit2.Text;
params[2].AsInteger:=strtoint(edit3.Text);
params[3].AsString:=hakuser;
params[4].AsInteger:=Zqueryid.Value;
ExecSql;
end;
end else
begin
with Zquery1 do
begin
close;
sql.Clear;
sql.Add('UPDATE pemakai SET nama=:nm,lantai=:lt,hak=:hk
WHERE id=:id');
params[0].AsString:=edit1.Text;
params[1].AsInteger:=strtoint(edit3.Text);
params[2].AsString:=hakuser;
params[3].AsInteger:=Zqueryid.Value;
ExecSql;
end;
end;
end;
Edit1.Text:='';
Edit2.Text:='';
Edit3.Text:='';
box.Enabled:=false;
Zquery.Refresh;
end;

procedure TFedituser.FormActivate(Sender: TObject);
var i: Integer;
begin
checkboxcount:=0;
for i := 0 to ComponentCount - 1 do
begin
if Components[i] is TCheckbox then
begin
checkboxcount:=checkboxcount+1;
end;
end;
end;

end.

logoblog

Thanks for reading Iseng-iseng Buat Script Batasi Hak User pada Program pakai Delphi

Previous
« Prev Post

No comments: