-
Notifications
You must be signed in to change notification settings - Fork 2
/
us3_people_procs.sql
758 lines (558 loc) · 19 KB
/
us3_people_procs.sql
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
--
-- us3_people_procs.sql
--
-- Script to set up the MySQL stored procedures for the US3 system
-- These are procedures related to information about people
-- Run as us3admin
--
DELIMITER $$
-- Finds the user with the passed email and password,
-- returns ID, firstName, lastName, phone, and userLevel
DROP PROCEDURE IF EXISTS get_user_info$$
CREATE PROCEDURE get_user_info( p_personGUID CHAR(36),
p_password VARCHAR(80) )
READS SQL DATA
BEGIN
CALL config();
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
SELECT @OK AS status;
SELECT @US3_ID AS ID,
@FNAME AS firstName,
@LNAME AS lastName,
@PHONE AS phone,
@EMAIL AS email,
@USERLEVEL AS userLevel;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- Returns of count of all records in the database, optionally
-- matching some text in the last name field
DROP FUNCTION IF EXISTS count_people$$
CREATE FUNCTION count_people( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_template VARCHAR(30) )
RETURNS INT
READS SQL DATA
BEGIN
DECLARE template VARCHAR(40);
DECLARE count_names INT;
CALL config();
SET p_template = TRIM( p_template );
IF ( LENGTH(p_template) = 0 ) THEN
SELECT COUNT(*)
INTO count_names
FROM people;
ELSE
SET template = CONCAT('%', p_template, '%');
SELECT COUNT(*)
INTO count_names
FROM people
WHERE lname LIKE template
OR fname LIKE template;
END IF;
RETURN( count_names );
END$$
-- Lists all ID's and names in the database, optionally matching
-- some text in the last name field
DROP PROCEDURE IF EXISTS get_people$$
CREATE PROCEDURE get_people( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_template VARCHAR(30) )
READS SQL DATA
BEGIN
DECLARE template VARCHAR(40);
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET p_template = TRIM( p_template );
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_people( p_personGUID, p_password, p_template ) < 1 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( LENGTH(p_template) = 0 ) THEN
SELECT @OK AS status;
SELECT personID,
lname AS lastName,
fname AS firstName,
organization
FROM people
ORDER BY lname;
ELSE
SELECT @OK AS status;
SET template = CONCAT('%', p_template, '%');
SELECT personID,
lname AS lastName,
fname AS firstName,
organization
FROM people
WHERE lname LIKE template
OR fname LIKE template
ORDER BY lname, fname;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- Lists all ID's and names in the database [global reviewers] optionally matching
-- some text in the last name field
DROP PROCEDURE IF EXISTS get_people_grev$$
CREATE PROCEDURE get_people_grev( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_template VARCHAR(30) )
READS SQL DATA
BEGIN
DECLARE template VARCHAR(40);
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET p_template = TRIM( p_template );
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_people( p_personGUID, p_password, p_template ) < 1 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( LENGTH(p_template) = 0 ) THEN
SELECT @OK AS status;
SELECT personID,
lname AS lastName,
fname AS firstName,
organization
FROM people
WHERE gmpReviewerRole = 'REVIEWER'
ORDER BY lname;
ELSE
SELECT @OK AS status;
SET template = CONCAT('%', p_template, '%');
SELECT personID,
lname AS lastName,
fname AS firstName,
organization
FROM people
WHERE (gmpReviewerRole = 'REVIEWER')
AND (lname LIKE template OR fname LIKE template)
ORDER BY lname, fname;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- Lists all ID's and names in the database [global approver] optionally matching
-- some text in the last name field
DROP PROCEDURE IF EXISTS get_people_gappr$$
CREATE PROCEDURE get_people_gappr( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_template VARCHAR(30) )
READS SQL DATA
BEGIN
DECLARE template VARCHAR(40);
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET p_template = TRIM( p_template );
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_people( p_personGUID, p_password, p_template ) < 1 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( LENGTH(p_template) = 0 ) THEN
SELECT @OK AS status;
SELECT personID,
lname AS lastName,
fname AS firstName,
organization
FROM people
WHERE gmpReviewerRole = 'APPROVER'
ORDER BY lname;
ELSE
SELECT @OK AS status;
SET template = CONCAT('%', p_template, '%');
SELECT personID,
lname AS lastName,
fname AS firstName,
organization
FROM people
WHERE (gmpReviewerRole = 'APPROVER')
AND (lname LIKE template OR fname LIKE template)
ORDER BY lname, fname;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- Lists all ID's and names in the database [ ![NOT] global reviewers] optionally matching
-- some text in the last name field
DROP PROCEDURE IF EXISTS get_people_inv$$
CREATE PROCEDURE get_people_inv( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_template VARCHAR(30) )
READS SQL DATA
BEGIN
DECLARE template VARCHAR(40);
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET p_template = TRIM( p_template );
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_people( p_personGUID, p_password, p_template ) < 1 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSEIF ( LENGTH(p_template) = 0 ) THEN
SELECT @OK AS status;
SELECT personID,
lname AS lastName,
fname AS firstName,
organization,
userlevel
FROM people
WHERE gmpReviewerRole = 'NONE'
ORDER BY lname;
ELSE
SELECT @OK AS status;
SET template = CONCAT('%', p_template, '%');
SELECT personID,
lname AS lastName,
fname AS firstName,
organization,
userlevel
FROM people
WHERE (gmpReviewerRole = 'NONE')
AND (lname LIKE template OR fname LIKE template)
ORDER BY lname, fname;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- Returns a more complete list of information about one user
DROP PROCEDURE IF EXISTS get_person_info$$
CREATE PROCEDURE get_person_info( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
READS SQL DATA
BEGIN
DECLARE count_person INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_person
FROM people
WHERE personID = p_ID;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_person = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
SELECT fname AS firstName,
lname AS lastName,
address,
city,
state,
zip,
phone,
organization,
email,
personGUID,
userlevel,
gmpReviewerRole
FROM people
WHERE personID = p_ID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- set GReviewer based in ID ------------------------------------
DROP PROCEDURE IF EXISTS set_person_grev_status$$
CREATE PROCEDURE set_person_grev_status( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
MODIFIES SQL DATA
BEGIN
DECLARE count_person INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_person
FROM people
WHERE personID = p_ID;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_person = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
UPDATE people
SET gmpReviewerRole = 'REVIEWER'
WHERE personID = p_ID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- [UN]set GReviewer based in ID ------------------------------------
DROP PROCEDURE IF EXISTS unset_person_grev_status$$
CREATE PROCEDURE unset_person_grev_status( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
MODIFIES SQL DATA
BEGIN
DECLARE count_person INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_person
FROM people
WHERE personID = p_ID;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_person = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
UPDATE people
SET gmpReviewerRole = 'NONE'
WHERE personID = p_ID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- set GApprover based in ID ------------------------------------
DROP PROCEDURE IF EXISTS set_person_gappr_status$$
CREATE PROCEDURE set_person_gappr_status( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
MODIFIES SQL DATA
BEGIN
DECLARE count_person INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_person
FROM people
WHERE personID = p_ID;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_person = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
UPDATE people
SET gmpReviewerRole = 'APPROVER'
WHERE personID = p_ID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- [UN]set GReviewer based in ID ------------------------------------
DROP PROCEDURE IF EXISTS unset_person_gappr_status$$
CREATE PROCEDURE unset_person_gappr_status( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
MODIFIES SQL DATA
BEGIN
DECLARE count_person INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_person
FROM people
WHERE personID = p_ID;
IF ( verify_user( p_personGUID, p_password ) = @OK ) THEN
IF ( count_person = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT @OK AS status;
UPDATE people
SET gmpReviewerRole = 'NONE'
WHERE personID = p_ID;
END IF;
ELSE
SELECT @US3_LAST_ERRNO AS status;
END IF;
END$$
-- INSERTs a new person with the specified information
DROP PROCEDURE IF EXISTS new_person$$
CREATE PROCEDURE new_person ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_fname VARCHAR(30),
p_lname VARCHAR(30),
p_address VARCHAR(255),
p_city VARCHAR(30),
p_state CHAR(2),
p_zip VARCHAR(10),
p_phone VARCHAR(24),
p_new_email VARCHAR(63),
p_new_guid CHAR(36),
p_organization VARCHAR(45),
p_new_password VARCHAR(80) )
MODIFIES SQL DATA
BEGIN
DECLARE duplicate_key TINYINT DEFAULT 0;
DECLARE null_field TINYINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR 1062
SET duplicate_key = 1;
DECLARE CONTINUE HANDLER FOR 1048
SET null_field = 1;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET @LAST_INSERT_ID = 0;
IF ( ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) &&
( check_GUID ( p_personGUID, p_password, p_new_guid ) = @OK ) ) THEN
INSERT INTO people SET
fname = p_fname,
lname = p_lname,
address = p_address,
city = p_city,
state = p_state,
zip = p_zip,
phone = p_phone,
email = p_new_email,
personGUID = p_new_guid,
organization = p_organization,
password = MD5(p_new_password),
activated = true,
signup = NOW(),
lastLogin = NOW(),
clusterAuthorizations = 'lonestar5:stampede2:comet:alamo:jetstream',
userlevel = 0;
IF ( duplicate_key = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTDUP;
SET @US3_LAST_ERROR = "MySQL: Duplicate entry for email or personGUID field";
ELSEIF ( null_field = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTNULL;
SET @US3_LAST_ERROR = "MySQL: NULL value for a field that cannot be NULL";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- UPDATEs a person with the specified information
DROP PROCEDURE IF EXISTS update_person$$
CREATE PROCEDURE update_person ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT,
p_fname VARCHAR(30),
p_lname VARCHAR(30),
p_address VARCHAR(255),
p_city VARCHAR(30),
p_state CHAR(2),
p_zip VARCHAR(10),
p_phone VARCHAR(24),
p_new_email VARCHAR(63),
p_organization VARCHAR(45),
p_new_password VARCHAR(80) )
MODIFIES SQL DATA
BEGIN
DECLARE duplicate_key TINYINT DEFAULT 0;
DECLARE null_field TINYINT DEFAULT 0;
DECLARE not_found TINYINT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR 1062
SET duplicate_key = 1;
DECLARE CONTINUE HANDLER FOR 1048
SET null_field = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET not_found = 1;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SET @LAST_INSERT_ID = 0;
-- Legitimate users include people updating their own info, or admins
IF ( ( ( verify_user( p_personGUID, p_password ) = @OK ) &&
( p_ID = @US3_ID ) ) ||
( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) ) THEN
UPDATE people SET
fname = p_fname,
lname = p_lname,
address = p_address,
city = p_city,
state = p_state,
zip = p_zip,
phone = p_phone,
email = p_new_email,
organization = p_organization,
password = MD5(p_new_password),
activated = true,
lastLogin = NOW()
WHERE personID = p_ID;
IF ( duplicate_key = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTDUP;
SET @US3_LAST_ERROR = "MySQL: Duplicate entry for email field";
ELSEIF ( null_field = 1 ) THEN
SET @US3_LAST_ERRNO = @INSERTNULL;
SET @US3_LAST_ERROR = "MySQL: NULL value for email field is not allowed";
ELSEIF ( not_found = 1 ) THEN
SET @US3_LAST_ERRNO = @NO_PERSON;
SET @US3_LAST_ERROR = "MySQL: No person with that ID exists";
ELSE
SET @LAST_INSERT_ID = LAST_INSERT_ID();
END IF;
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- DELETEs a person (marks inactive)
DROP PROCEDURE IF EXISTS delete_person$$
CREATE PROCEDURE delete_person ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_ID INT )
MODIFIES SQL DATA
BEGIN
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
IF ( verify_userlevel( p_personGUID, p_password, @US3_ADMIN ) = @OK ) THEN
UPDATE people SET
activated = false,
lastLogin = NOW()
WHERE personID = p_ID;
SET @LAST_INSERT_ID = LAST_INSERT_ID();
END IF;
SELECT @US3_LAST_ERRNO AS status;
END$$
-- Translate a personGUID into a personID
DROP PROCEDURE IF EXISTS get_personID_from_GUID$$
CREATE PROCEDURE get_personID_from_GUID ( p_personGUID CHAR(36),
p_password VARCHAR(80),
p_lookupGUID CHAR(36) )
READS SQL DATA
BEGIN
DECLARE count_person INT;
DECLARE l_personID INT;
CALL config();
SET @US3_LAST_ERRNO = @OK;
SET @US3_LAST_ERROR = '';
SELECT COUNT(*)
INTO count_person
FROM people
WHERE personGUID = p_lookupGUID;
IF ( count_person = 0 ) THEN
SET @US3_LAST_ERRNO = @NOROWS;
SET @US3_LAST_ERROR = 'MySQL: no rows returned';
SELECT @US3_LAST_ERRNO AS status;
ELSE
SELECT personID
INTO l_personID
FROM people
WHERE personGUID = p_lookupGUID
LIMIT 1; -- should be only 1
SELECT @OK AS status;
SELECT l_personID AS personID;
END IF;
END$$