-
Notifications
You must be signed in to change notification settings - Fork 12
/
cAcaoAcesso.pas
350 lines (302 loc) · 8.53 KB
/
cAcaoAcesso.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
unit cAcaoAcesso;
interface
uses System.Classes,
Vcl.Controls,
Vcl.ExtCtrls,
Vcl.Dialogs,
ZAbstractConnection,
ZConnection,
ZAbstractRODataset,
ZAbstractDataset,
ZDataset,
System.SysUtils,
Vcl.Forms,
Vcl.Buttons;
type
TAcaoAcesso = class
private
ConexaoDB:TZConnection;
F_acaoAcessoId:Integer;
F_descricao:String;
F_chave: string;
class procedure PreencherAcoes(aForm: TForm; aConexao:TZConnection); static;
class procedure VerificarUsuarioAcao(aUsuarioId, aAcaoAcessoId: Integer;
aConexao: TZConnection); static;
public
constructor Create(aConexao:TZConnection);
destructor Destroy; override;
function Inserir:Boolean;
function Atualizar:Boolean;
function Apagar:Boolean;
function Selecionar(id:Integer):Boolean;
function ChaveExiste(aChave: String; aId:Integer=0): Boolean;
class procedure CriarAcoes(aNomeForm: TFormClass; aConexao: TZConnection); static;
class procedure PreencherUsuariosVsAcoes(aConexao: TZConnection); static;
published
property codigo :Integer read F_acaoAcessoId write F_acaoAcessoId;
property descricao :string read F_descricao write F_descricao;
property chave :string read F_chave write F_chave;
end;
implementation
{ TCategoria }
{$region 'Constructor and Destructor'}
constructor TAcaoAcesso.Create(aConexao:TZConnection);
begin
ConexaoDB:=aConexao;
end;
destructor TAcaoAcesso.Destroy;
begin
inherited;
end;
{$endRegion}
{$region 'CRUD'}
function TAcaoAcesso.Apagar: Boolean;
var Qry:TZQuery;
begin
if MessageDlg('Apagar o Registro: '+#13+#13+
'Código: '+IntToStr(F_acaoAcessoId)+#13+
'Nome: ' +F_descricao, mtConfirmation,[mbYes, mbNo],0)=mrNo then begin
Result:=false;
abort;
end;
try
Result:=true;
Qry:=TZQuery.Create(nil);
Qry.Connection:=ConexaoDB;
Qry.SQL.Clear;
Qry.SQL.Add('DELETE FROM acaoAcesso '+
' WHERE acaoAcessoId=:acaoAcessoId ');
Qry.ParamByName('acaoAcessoId').AsInteger :=F_acaoAcessoId;
Try
ConexaoDB.StartTransaction;
Qry.ExecSQL;
ConexaoDB.Commit;
Except
ConexaoDB.Rollback;
Result:=false;
End;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
function TAcaoAcesso.Atualizar: Boolean;
var Qry:TZQuery;
begin
try
Result:=true;
Qry:=TZQuery.Create(nil);
Qry.Connection:=ConexaoDB;
Qry.SQL.Clear;
Qry.SQL.Add('UPDATE acaoAcesso '+
' SET descricao =:descricao '+
' ,chave =:chave '+
' WHERE acaoAcessoId=:acaoAcessoId ');
Qry.ParamByName('acaoAcessoId').AsInteger :=Self.F_acaoAcessoId;
Qry.ParamByName('descricao').AsString :=Self.F_descricao;
Qry.ParamByName('chave').AsString :=Self.F_chave;
Try
ConexaoDB.StartTransaction;
Qry.ExecSQL;
ConexaoDB.Commit;
Except
ConexaoDB.Rollback;
Result:=false;
End;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
function TAcaoAcesso.Inserir: Boolean;
var Qry:TZQuery;
begin
try
Result:=true;
Qry:=TZQuery.Create(nil);
Qry.Connection:=ConexaoDB;
Qry.SQL.Clear;
Qry.SQL.Add('INSERT INTO acaoAcesso (descricao, '+
' chave )'+
' VALUES (:descricao, '+
' :chave )' );
Qry.ParamByName('descricao').AsString :=Self.F_descricao;
Qry.ParamByName('chave').AsString :=Self.F_chave;
Try
ConexaoDB.StartTransaction;
Qry.ExecSQL;
ConexaoDB.Commit;
Except
ConexaoDB.Rollback;
Result:=false;
End;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
function TAcaoAcesso.Selecionar(id: Integer): Boolean;
var Qry:TZQuery;
begin
try
Result:=true;
Qry:=TZQuery.Create(nil);
Qry.Connection:=ConexaoDB;
Qry.SQL.Clear;
Qry.SQL.Add('SELECT acaoAcessoId,'+
' descricao, '+
' chave '+
' FROM acaoAcesso '+
' WHERE acaoAcessoId=:acaoAcessoId');
Qry.ParamByName('acaoAcessoId').AsInteger:=id;
Try
Qry.Open;
Self.F_acaoAcessoId := Qry.FieldByName('acaoAcessoId').AsInteger;
Self.F_descricao := Qry.FieldByName('descricao').AsString;
Self.F_chave := Qry.FieldByName('chave').AsString;;
Except
Result:=false;
End;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
function TAcaoAcesso.ChaveExiste(aChave: String; aId:Integer):Boolean;
var Qry:TZQuery;
begin
try
Qry:=TZQuery.Create(nil);
Qry.Connection:=ConexaoDB;
Qry.SQL.Clear;
Qry.SQL.Add('SELECT COUNT(acaoAcessoId) AS Qtde '+
' FROM acaoAcesso '+
' WHERE chave =:chave ');
if aId > 0 then
begin
Qry.SQL.Add(' AND acaoAcessoId<>:acaoAcessoId');
Qry.ParamByName('acaoAcessoId').AsInteger :=aId;
end;
Qry.ParamByName('chave').AsString :=aChave;
Try
Qry.Open;
if Qry.FieldByName('Qtde').AsInteger>0 then
result := true
else
result := false;
Except
Result:=false;
End;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
class procedure TAcaoAcesso.PreencherAcoes(aForm: TForm; aConexao:TZConnection);
var i:Integer;
oAcaoAcesso:TAcaoAcesso;
begin
try
oAcaoAcesso:=TAcaoAcesso.Create(aConexao);
oAcaoAcesso.descricao := aForm.Caption;
oAcaoAcesso.Chave := aForm.Name;
if not oAcaoAcesso.ChaveExiste(oAcaoAcesso.Chave) then
oAcaoAcesso.Inserir;
for I := 0 to aForm.ComponentCount -1 do
begin
if (aForm.Components[i] is TBitBtn) then
begin
if TBitBtn(aForm.Components[i]).Tag=99 then
begin
oAcaoAcesso.descricao := ' - BOTÃO '+ StringReplace(TBitBtn(aForm.Components[i]).Caption, '&','',[rfReplaceAll]);
oAcaoAcesso.Chave := aForm.Name+'_'+TBitBtn(aForm.Components[i]).Name;
if not oAcaoAcesso.ChaveExiste(oAcaoAcesso.Chave) then
oAcaoAcesso.Inserir;
end;
end;
end;
finally
if Assigned(oAcaoAcesso) then
FreeAndNil(oAcaoAcesso);
end;
end;
class procedure TAcaoAcesso.CriarAcoes(aNomeForm: TFormClass; aConexao:TZConnection);
var
form: TForm;
begin
try
form := aNomeForm.Create(Application);
PreencherAcoes(form,aConexao);
finally
if Assigned(form) then
form.Release;
end;
end;
class procedure TAcaoAcesso.VerificarUsuarioAcao(aUsuarioId:Integer; aAcaoAcessoId:Integer; aConexao:TZConnection);
var Qry:TZQuery;
begin
try
Qry:=TZQuery.Create(nil);
Qry.Connection:=aConexao;
Qry.SQL.Clear;
Qry.SQL.Add('SELECT usuarioId '+
' FROM usuariosAcaoAcesso '+
' WHERE usuarioId=:usuarioId '+
' AND acaoAcessoId=:acaoAcessoId ');
Qry.ParamByName('usuarioId').AsInteger:=aUsuarioId;
Qry.ParamByName('acaoAcessoId').AsInteger:=aAcaoAcessoId;
Qry.Open;
if Qry.IsEmpty then
begin
Qry.Close;
Qry.SQL.Clear;
Qry.SQL.Add('INSERT INTO usuariosAcaoAcesso (usuarioId, acaoAcessoId, ativo) '+
' VALUES (:usuarioId, :acaoAcessoId, :ativo) ');
Qry.ParamByName('usuarioId').AsInteger:=aUsuarioId;
Qry.ParamByName('acaoAcessoId').AsInteger:=aAcaoAcessoId;
Qry.ParamByName('ativo').AsBoolean:=true;
Try
aConexao.StartTransaction;
Qry.ExecSQL;
aConexao.Commit;
Except
aConexao.Rollback;
End;
end;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
class procedure TAcaoAcesso.PreencherUsuariosVsAcoes(aConexao:TZConnection);
var Qry:TZQuery;
QryAcaoAcesso:TZQuery;
begin
try
Qry:=TZQuery.Create(nil);
Qry.Connection:=aConexao;
Qry.SQL.Clear;
QryAcaoAcesso:=TZQuery.Create(nil);
QryAcaoAcesso.Connection:=aConexao;
QryAcaoAcesso.SQL.Clear;
Qry.SQL.Add('SELECT usuarioId FROM usuarios ');
Qry.Open;
QryAcaoAcesso.SQL.Add('SELECT acaoAcessoId FROM acaoAcesso ');
QryAcaoAcesso.Open;
while not Qry.Eof do
begin
QryAcaoAcesso.First;
while not QryAcaoAcesso.Eof do
begin
VerificarUsuarioAcao(Qry.FieldByName('usuarioId').AsInteger, QryAcaoAcesso.FieldByName('acaoAcessoId').AsInteger, aConexao);
QryAcaoAcesso.Next;
end;
Qry.Next;
end;
finally
if Assigned(Qry) then
FreeAndNil(Qry);
end;
end;
{$endregion}
end.