· 5 years ago · Mar 27, 2020, 07:06 PM
1delimiter ;
2
3use empresa;
4
5-- eliminar procedimientos previos
6drop procedure if exists grant_delete_departamento;
7drop procedure if exists grant_insert_departamento;
8drop procedure if exists grant_update_departamento;
9
10drop procedure if exists grant_delete_detalle;
11drop procedure if exists grant_insert_detalle;
12drop procedure if exists grant_update_detalle;
13
14drop procedure if exists grant_delete_comercial;
15drop procedure if exists grant_insert_comercial;
16drop procedure if exists grant_update_comercial;
17
18drop procedure if exists grant_delete_empresa;
19drop procedure if exists grant_insert_empresa;
20drop procedure if exists grant_update_empresa;
21
22drop procedure if exists grant_delete_producto;
23drop procedure if exists grant_insert_producto;
24drop procedure if exists grant_update_producto;
25
26drop procedure if exists grant_delete_cliente;
27drop procedure if exists grant_insert_cliente;
28drop procedure if exists grant_update_cliente;
29
30-- eliminar vistas previas
31drop view if exists select_detalle;
32drop view if exists select_comanda;
33drop view if exists select_cliente;
34drop view if exists select_producto;
35drop view if exists select_empleado;
36
37-- eliminar tablas previas
38drop table if exists logs;
39
40drop procedure if exists empleats;
41drop procedure if exists permisos;
42drop procedure if exists duplicate_User;
43drop procedure if exists add_deleted_columns;
44drop procedure if exists add_User;
45
46
47-- crear tabla logs
48create table if not exists logs (
49 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
50 apellido VARCHAR(30) NOT NULL,
51 accion VARCHAR(50),
52 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
53);
54
55delimiter //
56
57-- crear procedimiento añadir soft deleted
58create procedure add_deleted_columns ()
59 begin
60 declare continue handler for 1060
61 select "No insertamos la columna deleted por que ya existe.";
62
63 ALTER TABLE empresa.EMP ADD COLUMN deleted boolean default 0;
64 ALTER TABLE empresa.PRODUCTO ADD COLUMN deleted boolean default 0;
65 ALTER TABLE empresa.DEPT ADD COLUMN deleted boolean default 0;
66 ALTER TABLE empresa.DETALLE ADD COLUMN deleted boolean default 0;
67 ALTER TABLE empresa.CLIENTE ADD COLUMN deleted boolean default 0;
68 ALTER TABLE empresa.COMANDA ADD COLUMN deleted boolean default 0;
69 end//
70
71delimiter ;
72
73call add_deleted_columns();
74
75-- crear vistas select
76create or replace view select_detalle as
77 select COM_NUM,DETALLE_NUM,PROD_NUM,PRECIO_VENTA,CANTIDAD,IMPORTE
78 from DETALLE
79 where deleted = false;
80
81create or replace view select_comanda as
82 select COM_NUM,COM_FECHA,COM_TIPO,CLIENTE_COD,FECHA_TRAM,TOTAL
83 from COMANDA
84 where deleted = false;
85
86create or replace view select_cliente as
87 select CLIENTE_COD,NOMBRE,DIRECCIÓN,CIUDAD,ESTADO,CODI_POSTAL,AREA,TELEFONO,REPR_COD,LIMIT_CREDITO,OBSERVACIONES
88 from CLIENTE
89 where deleted = false;
90
91create or replace view select_producto as
92 select PROD_NUM,DESCRIPCIÓN
93 from PRODUCTO
94 where deleted = false;
95
96create or replace view select_empleado as
97 select EMP_NO,APELLIDO,OFICIO,JEFE,FECHA_ALTA,DEPT_NO
98 from EMP
99 where deleted = false;
100
101delimiter //
102
103-- crear procedimientos permisos (insert, update, delete)
104create procedure grant_delete_departamento (in COM_NUM int)
105 begin
106 declare continue handler for 1406
107 select "El nombre que has introducido tiene mas carácteres de los permitidos";
108 declare continue handler for 1264
109 select "El número que has introducido tiene mas carácteres de los permitidos";
110 update DEPT set deleted = 1 where DEPT_NO=DEPT_NO;
111 insert into logs (apellido, accion) values (USER(), 'delete_departamento');
112 end//
113
114create procedure grant_insert_departamento (in DEPT_NO int,in DNOM varchar(30),in LOC varchar(30))
115 begin
116 declare continue handler for 1406
117 select "El nombre que has introducido tiene mas carácteres de los permitidos";
118 declare continue handler for 1264
119 select "El número que has introducido tiene mas carácteres de los permitidos";
120 insert into DEPT values (DEPT_NO,DNOM,LOC, 0);
121 insert into logs (apellido, accion) values (USER(), 'insert_departamento');
122 end//
123
124create procedure grant_update_departamento (in DEPT_NO2 smallint,in DNOM2 DATE,in LOC2 CHAR(1))
125 begin
126 declare continue handler for 1406
127 select "El nombre que has introducido tiene mas carácteres de los permitidos";
128 declare continue handler for 1264
129 select "El número que has introducido tiene mas carácteres de los permitidos";
130 update DEPT set DEPT_NO=DEPT_NO,DNOM=DNOM2,LOC=LOC2 where DEPT_NO=DEPT_NO2;
131 insert into logs (apellido, accion) values (USER(), 'update_departamento');
132 end//
133
134create procedure grant_delete_detalle (in COM_NUM int)
135 begin
136 declare continue handler for 1406
137 select "El nombre que has introducido tiene mas carácteres de los permitidos";
138 declare continue handler for 1264
139 select "El número que has introducido tiene mas carácteres de los permitidos";
140 update DETALLE set deleted = 1 where COM_NUM=COM_NUM;
141 insert into logs (apellido, accion) values (USER(), 'delete_detalle');
142 end//
143
144create procedure grant_insert_detalle (in COM_NUM smallint(4),in DETALLE_NUM smallint(4),in PROD_NUM int(6),in PRECIO_VENTA decimal(8,2),in CANTIDAD int(8),in IMPORTE decimal(8,2))
145 begin
146 declare continue handler for 1406
147 select "El nombre que has introducido tiene mas carácteres de los permitidos";
148 declare continue handler for 1264
149 select "El número que has introducido tiene mas carácteres de los permitidos";
150 insert into DETALLE values (DEPT_NO,DNOM,LOC, 0);
151 insert into logs (apellido, accion) values (USER(), 'insert_detalle');
152 end//
153
154create procedure grant_update_detalle (in COM_NUM2 smallint(4),in DETALLE_NUM2 smallint(4),in PROD_NUM2 int(6),in PRECIO_VENTA2 decimal(8,2),in CANTIDAD2 int(8),in IMPORTE2 decimal(8,2))
155 begin
156 declare continue handler for 1406
157 select "El nombre que has introducido tiene mas carácteres de los permitidos";
158 declare continue handler for 1264
159 select "El número que has introducido tiene mas carácteres de los permitidos";
160 update DETALLE set COM_NUM=COM_NUM,DETALLE_NUM=DETALLE_NUM2,PROD_NUM=PROD_NUM2,PRECIO_VENTA=PRECIO_VENTA2,CANTIDAD=CANTIDAD2,IMPORTE=IMPORTE2 where COM_NUM=COM_NUM2;
161 insert into logs (apellido, accion) values (USER(), 'update_detalle');
162 end//
163
164create procedure grant_delete_comercial (in COM_NUM int)
165 begin
166 declare continue handler for 1406
167 select "El nombre que has introducido tiene mas carácteres de los permitidos";
168 declare continue handler for 1264
169 select "El número que has introducido tiene mas carácteres de los permitidos";
170 update COMANDA set deleted = 1 where COM_NUM=COM_NUM;
171 insert into logs (apellido, accion) values (USER(), 'delete_comercial');
172 end//
173
174create procedure grant_insert_comercial (in COM_NUM smallint,in COM_FECHA DATE,in COM_TIPO CHAR(1),
175 in CLIENTE_COD INT,in FECHA_TRAM DATE,in TOTAL FLOAT (8,2))
176 begin
177 declare continue handler for 1406
178 select "El nombre que has introducido tiene mas carácteres de los permitidos";
179 declare continue handler for 1264
180 select "El número que has introducido tiene mas carácteres de los permitidos";
181 insert into COMANDA values (COM_NUM,COM_FECHA,COM_TIPO,CLIENTE_COD,FECHA_TRAM,TOTAL, 0);
182 insert into logs (apellido, accion) values (USER(), 'insert_comercial');
183 end//
184
185create procedure grant_update_comercial (in COM_NUM2 smallint,in COM_FECHA2 DATE,in COM_TIPO2 CHAR(1),
186 in CLIENTE_COD2 INT,in FECHA_TRAM2 DATE,in TOTAL2 FLOAT (8,2))
187 begin
188 declare continue handler for 1406
189 select "El nombre que has introducido tiene mas carácteres de los permitidos";
190 declare continue handler for 1264
191 select "El número que has introducido tiene mas carácteres de los permitidos";
192 update CLIENTE set COM_NUM=COM_NUM,COM_FECHA=COM_NUM2,COM_TIPO=COM_TIPO2,CLIENTE_COD=CLIENTE_COD2,FECHA_TRAM=FECHA_TRAM2,TOTAL=TOTAL2 where COM_NUM=COM_NUM2;
193 insert into logs (apellido, accion) values (USER(), 'update_comercial');
194 end//
195
196create procedure grant_delete_empresa (in EMP_NO2 int)
197 begin
198 declare continue handler for 1406
199 select "El nombre que has introducido tiene mas carácteres de los permitidos";
200 declare continue handler for 1264
201 select "El número que has introducido tiene mas carácteres de los permitidos";
202 update EMP set deleted = 1 where EMP_NO=EMP_NO2;
203 insert into logs (apellido, accion) values (USER(), 'delete_empresa');
204 end//
205
206create procedure grant_insert_empresa (in EMP_NO smallint(4),in APELLIDO varchar(10),in OFICIO varchar(10),in JEFE smallint(4),in FECHA_ALTA date,in SALARIO int(10),in COMISIÓN int(10),in DEPT_NO tinyint(2))
207 begin
208 declare continue handler for 1406
209 select "El nombre que has introducido tiene mas carácteres de los permitidos";
210 declare continue handler for 1264
211 select "El número que has introducido tiene mas carácteres de los permitidos";
212 insert into EMP values (EMP_NO,APELLIDO,OFICIO,JEFE,FECHA_ALTA,SALARIO,COMISIÓN,DEPT_NO, 0);
213 insert into logs (apellido, accion) values (USER(), 'insert_empresa');
214 end//
215
216create procedure grant_update_empresa (in EMP_NO2 smallint(4),in APELLIDO2 varchar(10),in OFICIO2 varchar(10),in JEFE2 smallint(4),in FECHA_ALTA2 date,in SALARIO2 int(10),in COMISIÓN2 int(10),in DEPT_NO2 tinyint(2))
217 begin
218 update EMP set EMP_NO=EMP_NO,APELLIDO=APELLIDO2,OFICIO=OFICIO2,JEFE=JEFE2,FECHA_ALTA=FECHA_ALTA2,SALARIO=SALARIO2,COMISIÓN=COMISIÓN2,DEPT_NO=DEPT_NO2
219 where EMP_NO=EMP_NO2;
220 insert into logs (apellido, accion) values (USER(), 'update_empresa');
221 end//
222
223create procedure grant_delete_producto (in PROD_NUM2 int)
224 begin
225 declare continue handler for 1406
226 select "El nombre que has introducido tiene mas carácteres de los permitidos";
227 declare continue handler for 1264
228 select "El número que has introducido tiene mas carácteres de los permitidos";
229 update PRODUCTO set deleted = 1 where PROD_NUM=PROD_NUM2;
230 insert into logs (apellido, accion) values (USER(), 'delete_producto');
231 end//
232
233create procedure grant_insert_producto (in PROD_NUM int(6),in DESCRIPCIÓN varchar(30))
234 begin
235 declare continue handler for 1406
236 select "El nombre que has introducido tiene mas carácteres de los permitidos";
237 declare continue handler for 1264
238 select "El número que has introducido tiene mas carácteres de los permitidos";
239 insert into PRODUCTO values (PROD_NUM,DESCRIPCIÓN, 0);
240 insert into logs (apellido, accion) values (USER(), 'insert_producto');
241 end//
242
243create procedure grant_update_producto (in PROD_NUM2 int(6),in DESCRIPCIÓN2 varchar(30))
244 begin
245 declare continue handler for 1406
246 select "El nombre que has introducido tiene mas carácteres de los permitidos";
247 declare continue handler for 1264
248 select "El número que has introducido tiene mas carácteres de los permitidos";
249 update PRODUCTO set PROD_NUM=PROD_NUM,DESCRIPCIÓN=DESCRIPCIÓN2 where PROD_NUM=PROD_NUM2;
250 insert into logs (apellido, accion) values (USER(), 'update_producto');
251 end//
252
253create procedure grant_delete_cliente (in codigo int)
254 begin
255 declare continue handler for 1406
256 select "El nombre que has introducido tiene mas carácteres de los permitidos";
257 declare continue handler for 1264
258 select "El número que has introducido tiene mas carácteres de los permitidos";
259 update CLIENTE set deleted = 1 where CLIENTE_COD=codigo;
260 insert into logs (apellido, accion) values (USER(), 'delete_cliente');
261 end//
262
263create procedure grant_insert_cliente (in CLIENTE_COD2 int,in NOMBRE2 VARCHAR(45),in DIRECCIÓN2 VARCHAR(40),
264 in CIUDAD2 VARCHAR(30),in ESTADO2 VARCHAR(2),in CODI_POSTAL2 VARCHAR (9),
265 in AREA2 smallint(3),in TELEFONO2 VARCHAR (9),in REPR_COD2 smallint (4),
266 in LIMIT_CREDITO2 decimal(9,2),OBSERVACIONES2 text)
267 begin
268 declare continue handler for 1406
269 select "El nombre que has introducido tiene mas carácteres de los permitidos";
270 declare continue handler for 1264
271 select "El número que has introducido tiene mas carácteres de los permitidos";
272 insert into CLIENTE values (CLIENTE_COD2,NOMBRE2,DIRECCIÓN2,CIUDAD2,ESTADO2,CODI_POSTAL2,AREA2,TELEFONO2,REPR_COD2,LIMIT_CREDITO2,OBSERVACIONES2, 0);
273 insert into logs (apellido, accion) values (USER(), 'insert_cliente');
274 end//
275
276create procedure grant_update_cliente (in CLIENTE_COD2 int,in NOMBRE2 VARCHAR(45),in DIRECCIÓN2 VARCHAR(40),in CIUDAD2 VARCHAR(30),in ESTADO2 VARCHAR(2),in CODI_POSTAL2 VARCHAR (9),in AREA2 smallint(3),in TELEFONO2 VARCHAR (9),in REPR_COD2 smallint (4),in LIMIT_CREDITO2 decimal(9,2),OBSERVACIONES2 text)
277 begin
278 declare continue handler for 1406
279 select "El nombre que has introducido tiene mas carácteres de los permitidos";
280 declare continue handler for 1264
281 select "El número que has introducido tiene mas carácteres de los permitidos";
282 update CLIENTE set CLIENTE_COD=CLIENTE_COD,NOMBRE=NOMBRE2,DIRECCIÓN=DIRECCIÓN2,CIUDAD=CIUDAD2,ESTADO=ESTADO2,CODI_POSTAL=CODI_POSTAL2,AREA=AREA2,TELEFONO=TELEFONO2,REPR_COD=REPR_COD2,LIMIT_CREDITO=LIMIT_CREDITO2,OBSERVACIONES=OBSERVACIONES2
283 where CLIENTE_COD=CLIENTE_COD2;
284 insert into logs (apellido, accion) values (USER(), 'update_cliente');
285 end//
286
287 -- crear procedimientos asignación permisos
288CREATE PROCEDURE permisos(IN var1 VARCHAR(45))
289BEGIN
290DECLARE num_departamento int(2);
291 SELECT DEPT_NO INTO num_departamento FROM EMP WHERE APELLIDO = var1;
292
293 if (num_departamento = 10) then
294 set @select_empresa := concat('grant select on empresa.select_empleado to ', var1, '@localhost');
295 set @select_detalle := concat('grant select on empresa.select_detalle to ', var1, '@localhost');
296 set @select_comanda := concat('grant select on empresa.select_comanda to ', var1, '@localhost');
297 set @update_detalle := concat('grant execute on procedure empresa.grant_update_detalle to ', var1, '@localhost');
298 set @delete_detalle := concat('grant execute on procedure empresa.grant_delete_detalle to ', var1, '@localhost');
299 set @insert_detalle := concat('grant execute on procedure empresa.grant_insert_detalle to ', var1, '@localhost');
300 set @update_comercial := concat('grant execute on procedure empresa.grant_update_comercial to ', var1, '@localhost');
301 set @delete_comercial := concat('grant execute on procedure empresa.grant_delete_comercial to ', var1, '@localhost');
302 set @insert_comercial := concat('grant execute on procedure empresa.grant_insert_comercial to ', var1, '@localhost');
303
304 prepare select_empresa_statement from @select_empresa;
305 execute select_empresa_statement;
306 deallocate prepare select_empresa_statement;
307
308 prepare select_detalle_statement from @select_detalle;
309 execute select_detalle_statement;
310 deallocate prepare select_detalle_statement;
311
312 prepare select_comanda_statement from @select_comanda;
313 execute select_comanda_statement;
314 deallocate prepare select_comanda_statement;
315
316 prepare update_detalle_statement from @update_detalle;
317 execute update_detalle_statement;
318 deallocate prepare update_detalle_statement;
319
320 prepare delete_detalle_statement from @delete_detalle;
321 execute delete_detalle_statement;
322 deallocate prepare delete_detalle_statement;
323
324 prepare insert_detalle_statement from @insert_detalle;
325 execute insert_detalle_statement;
326 deallocate prepare insert_detalle_statement;
327
328 prepare update_comercial_statement from @update_comercial;
329 execute update_comercial_statement;
330 deallocate prepare update_comercial_statement;
331
332 prepare delete_comercial_statement from @delete_comercial;
333 execute delete_comercial_statement;
334 deallocate prepare delete_comercial_statement;
335
336 prepare insert_comercial_statement from @insert_comercial;
337 execute insert_comercial_statement;
338 deallocate prepare insert_comercial_statement;
339 END IF;
340
341 if (num_departamento = 20) then
342 set @select_producto := concat('grant select on empresa.select_producto to ', var1, '@localhost');
343 set @select_comanda := concat('grant select on empresa.select_comanda to ', var1, '@localhost');
344 set @select_detalle := concat('grant select on empresa.select_detalle to ', var1, '@localhost');
345 set @select_cliente := concat('grant select on empresa.select_cliente to ',var1, '@localhost');
346 set @select_empresa := concat('grant select on empresa.select_empleado to ', var1, '@localhost');
347
348 prepare select_producto_statement from @select_producto;
349 execute select_producto_statement;
350 deallocate prepare select_producto_statement;
351
352 prepare select_comanda_statement from @select_comanda;
353 execute select_comanda_statement;
354 deallocate prepare select_comanda_statement;
355
356 prepare select_detalle_statement from @select_detalle;
357 execute select_detalle_statement;
358 deallocate prepare select_detalle_statement;
359
360 prepare select_cliente_statement from @select_cliente;
361 execute select_cliente_statement;
362 deallocate prepare select_cliente_statement;
363
364 prepare select_empresa_statement from @select_empresa;
365 execute select_empresa_statement;
366 deallocate prepare select_empresa_statement;
367 END IF;
368
369 if (num_departamento = 30) then
370 set @select_comanda := concat('grant select on empresa.select_comanda to ', var1, '@localhost');
371 set @select_detalle := concat('grant select on empresa.select_detalle to ', var1, '@localhost');
372 set @select_cliente := concat('grant select on empresa.select_cliente to ',var1, '@localhost');
373 set @update_cliente := concat('grant execute on procedure empresa.grant_update_cliente to ', var1, '@localhost');
374 set @delete_cliente := concat('grant execute on procedure empresa.grant_delete_cliente to ', var1, '@localhost');
375 set @insert_cliente := concat('grant execute on procedure empresa.grant_insert_cliente to ', var1, '@localhost');
376
377 prepare select_comanda_statement from @select_comanda;
378 execute select_comanda_statement;
379 deallocate prepare select_comanda_statement;
380
381 prepare select_detalle_statement from @select_detalle;
382 execute select_detalle_statement;
383 deallocate prepare select_detalle_statement;
384
385 prepare select_cliente_statement from @select_cliente;
386 execute select_cliente_statement;
387 deallocate prepare select_cliente_statement;
388
389 prepare update_cliente_statement from @update_cliente;
390 execute update_cliente_statement;
391 deallocate prepare update_cliente_statement;
392
393 prepare delete_cliente_statement from @delete_cliente;
394 execute delete_cliente_statement;
395 deallocate prepare delete_cliente_statement;
396
397 prepare insert_cliente_statement from @insert_cliente;
398 execute insert_cliente_statement;
399 deallocate prepare insert_cliente_statement;
400 END IF;
401
402 if (num_departamento = 40) then
403 set @select_producto := concat('grant select on empresa.select_producto to ', var1, '@localhost');
404 set @update_producto := concat('grant execute on procedure empresa.grant_update_producto to ', var1, '@localhost');
405 set @delete_producto := concat('grant execute on procedure empresa.grant_delete_producto to ', var1, '@localhost');
406 set @insert_producto := concat('grant execute on procedure empresa.grant_insert_producto to ', var1, '@localhost');
407
408 prepare select_producto_statement from @select_producto;
409 execute select_producto_statement;
410 deallocate prepare select_producto_statement;
411
412 prepare update_producto_statement from @update_producto;
413 execute update_producto_statement;
414 deallocate prepare update_producto_statement;
415
416 prepare delete_producto_statement from @delete_producto;
417 execute delete_producto_statement;
418 deallocate prepare delete_producto_statement;
419
420 prepare insert_producto_statement from @insert_producto;
421 execute insert_producto_statement;
422 deallocate prepare insert_producto_statement;
423 END IF;
424
425 SET @flush:=('flush privileges');
426 PREPARE flush FROM @flush;
427 EXECUTE flush;
428 DEALLOCATE PREPARE flush;
429END//
430
431-- crear procedimiento duplicado.
432
433create procedure duplicate_User(IN userOrigen VARCHAR(45), IN userDesti VARCHAR(45), IN idUser smallint)
434begin
435 SET @sql:=CONCAT('insert into EMP (EMP_NO, APELLIDO, OFICIO, JEFE, FECHA_ALTA, SALARIO, COMISIÓN, DEPT_NO)
436 select ', idUser, ', "', userDesti, '", OFICIO, JEFE, FECHA_ALTA, SALARIO, COMISIÓN, DEPT_NO
437 from EMP
438 where APELLIDO = "', userOrigen, '" limit 1');
439 PREPARE stmt FROM @sql;
440 EXECUTE stmt;
441 DEALLOCATE PREPARE stmt;
442end//
443
444-- crear procedimiento añadir usuario
445create procedure add_User(IN var1 VARCHAR(45))
446begin
447 SET @sql:=CONCAT('CREATE USER ',var1,'@localhost IDENTIFIED BY "Usu@rio123"');
448 PREPARE stmt FROM @sql;
449 EXECUTE stmt;
450 DEALLOCATE PREPARE stmt;
451end//
452
453
454-- Crear procedimiento crear usuarios caps y damos permisos a los caps
455
456DROP PROCEDURE IF EXISTS add_UserCaps;
457CREATE PROCEDURE add_UserCaps (in EMP_NO2 smallint(4),in APELLIDO2 varchar(10),in OFICIO2 varchar(10),in JEFE2 smallint(4),in FECHA_ALTA2 date,in SALARIO2 int(10),in COMISIÓN2 int(10),in DEPT_NO2 tinyint(2))
458BEGIN
459 insert into EMP (EMP_NO,APELLIDO,OFICIO,JEFE,FECHA_ALTA,SALARIO,COMISIÓN,DEPT_NO) values (EMP_NO2,APELLIDO2,OFICIO2,JEFE2,FECHA_ALTA2,SALARIO2,COMISIÓN2,DEPT_NO2);
460 call empleats();
461END//
462
463delimiter ;
464
465DROP USER if exists CAP_CONTABILIDAD@localhost;
466DROP USER if exists CAP_VENTAS@localhost;
467DROP USER if exists CAP_INVESTIGACION@localhost;
468DROP USER if exists CAP_PRODUCCION@localhost;
469
470CREATE USER 'CAP_CONTABILIDAD'@localhost IDENTIFIED BY "Usu@rio123";
471CREATE USER 'CAP_VENTAS'@localhost IDENTIFIED BY "Usu@rio123";
472CREATE USER 'CAP_INVESTIGACION'@localhost IDENTIFIED BY "Usu@rio123";
473CREATE USER 'CAP_PRODUCCION'@localhost IDENTIFIED BY "Usu@rio123";
474
475grant all on empresa.* to CAP_CONTABILIDAD;
476grant all on empresa.* to CAP_VENTAS;
477grant all on empresa.* to CAP_INVESTIGACION;
478grant all on empresa.* to CAP_PRODUCCION;
479
480set @addusercaps := concat('grant execute on procedure empresa.add_UserCaps to ', 'CAP_CONTABILIDAD', '@localhost');
481set @addusercaps2 := concat('grant execute on procedure empresa.add_UserCaps to ', 'CAP_VENTAS', '@localhost');
482set @addusercaps3 := concat('grant execute on procedure empresa.add_UserCaps to ', 'CAP_INVESTIGACION', '@localhost');
483set @addusercaps4 := concat('grant execute on procedure empresa.add_UserCaps to ', 'CAP_PRODUCCION', '@localhost');
484
485prepare adduser_statement from @addusercaps;
486execute adduser_statement;
487deallocate prepare adduser_statement;
488
489prepare adduser2_statement from @addusercaps2;
490execute adduser2_statement;
491deallocate prepare adduser2_statement;
492
493prepare adduser3_statement from @addusercaps3;
494execute adduser3_statement;
495deallocate prepare adduser3_statement;
496
497prepare adduser4_statement from @addusercaps4;
498execute adduser4_statement;
499deallocate prepare adduser4_statement;
500
501delimiter //
502
503-- crear procedimiento empleados
504create procedure empleats()
505begin
506 DECLARE var1 varchar (20);
507 DECLARE done INT DEFAULT FALSE;
508 DECLARE cur1 CURSOR FOR SELECT APELLIDO FROM EMP;
509 DECLARE continue HANDLER FOR NOT FOUND SET DONE=TRUE;
510
511 OPEN cur1;
512
513 read_loop: LOOP
514 FETCH cur1 INTO var1;
515 IF done then
516 LEAVE read_loop;
517 end IF;
518
519 set @drop_user := concat('drop user if exists ', var1, '@localhost');
520 prepare drop_user_statement from @drop_user;
521 execute drop_user_statement;
522 deallocate prepare drop_user_statement;
523
524 call add_User(var1);
525 call permisos(var1);
526 end LOOP read_loop;
527
528 close cur1;
529end //
530
531delimiter ;
532
533call empleats();