-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsp_tool_induct.sql
More file actions
164 lines (137 loc) · 4.78 KB
/
sp_tool_induct.sql
File metadata and controls
164 lines (137 loc) · 4.78 KB
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
drop procedure if exists sp_tool_induct;
/* Recorded member with card <p_card_inductee> as having been inducted on <p_tool_name> by member with card <p_card_inductee>.
* p_ret = 0 on success, non-zero on failure
* p_msg = mesage to show on LCD (only first 16 characters appear on display) */
DELIMITER //
CREATE PROCEDURE sp_tool_induct
(
IN p_tool_name varchar( 20),
IN p_card_inductor varchar( 50),
IN p_card_inductee varchar( 50),
OUT p_ret int,
OUT p_msg varchar(2000)
)
SQL SECURITY DEFINER
BEGIN
declare cnt int;
declare l_inductor_id int;
declare l_inductee_id int;
declare l_tool_pph int;
declare tool_id int;
declare EXIT HANDLER for SQLEXCEPTION, SQLWARNING
begin
GET DIAGNOSTICS CONDITION 1 @text = MESSAGE_TEXT;
-- note that only the first 16 characters of the error appears in the tool LCD. But all of it ends up in the log.
set p_msg = concat('Failed: int err. Error - transaction rollback!: ', @text);
rollback;
end;
set p_msg = '';
set p_ret = -1;
main: begin
-- Check tool name is actaully known
select count(*)
into cnt
from tools t
where t.name = p_tool_name;
if (cnt = 0) then
set p_msg = 'Tool not configured';
leave main;
elseif (cnt > 1) then
set p_msg = 'Tool config error';
leave main;
end if;
-- Get tool id
select t.id, t.pph
into tool_id, l_tool_pph
from tools t
where t.name = p_tool_name;
-- Check <p_card_inductor> is actaully listed as being able to give inductions, and get details if so
select count(*)
into cnt
from rfid_tags r
where r.rfid_serial = p_card_inductor
and r.state = 10
and (fn_check_permission(r.user_id, concat('tools.', replace(p_tool_name, ' ', ''), '.induct')) = 1);
if (cnt <= 0) then
set p_msg = 'Access denied (NI)';
leave main;
end if;
-- Get member id of inductor
select r.user_id
into l_inductor_id
from rfid_tags r
where r.rfid_serial = p_card_inductor
and r.state = 10;
-- check <p_card_inductee> relates to a member with the generic "tools.use" permission
select count(*)
into cnt
from user u
inner join rfid_tags r on u.id = r.user_id
where r.rfid_serial = p_card_inductee
and r.state = 10
and (fn_check_permission(u.id, 'tools.use') = 1);
if (cnt <= 0) then
set p_msg = 'Bad card/No perm.';
leave main;
end if;
-- Check if inductee has already been inducted.. and just return success if so
select count(*)
into cnt
from user u
inner join rfid_tags r on u.id = r.user_id
where r.rfid_serial = p_card_inductee
and r.state = 10
and (fn_check_permission(r.user_id, concat('tools.', replace(p_tool_name, ' ', ''), '.use')) = 1);
if (cnt > 0) then
set p_msg = 'Already inducted';
set p_ret = 0;
leave main;
end if;
-- Get member id of inductee
select u.id
into l_inductee_id
from user u
inner join rfid_tags r on r.user_id = u.id
where r.rfid_serial = p_card_inductee
and r.state = 10;
start transaction;
-- Give user being inducted the "user" role for the tool
insert into role_user (user_id, role_id)
select l_inductee_id, r.id
from roles r
where r. name = concat('tools.', replace(p_tool_name, ' ', ''), '.user');
-- expecting exactly 1 row inserted
set cnt = ROW_COUNT();
if (cnt != 1) then
set p_msg = concat('Failed: int err. Error - unexpected number of rows inserted into role_user: ', convert(cnt,char));
rollback;
leave main;
end if;
-- Log who inducted the user
insert into role_updates (user_id, added_role_id, created_at, update_by_user_id)
select l_inductee_id, r.id, UTC_TIMESTAMP(), l_inductor_id
from roles r
where r. name = concat('tools.', replace(p_tool_name, ' ', ''), '.user');
-- expecting exactly 1 row inserted
set cnt = ROW_COUNT();
if (cnt != 1) then
set p_msg = concat('Failed: int err. Error - unexpected number of rows inserted into role_updates: ', convert(cnt,char));
rollback;
leave main;
end if;
-- If this is a tool that changes, give 60minutes of time after induction
if (l_tool_pph > 0) then
insert into tool_usages (user_id , tool_id, start , status , duration, active_time)
values (l_inductee_id , tool_id, UTC_TIMESTAMP(), 'COMPLETE', -(60*60), -0); -- 60s * 60m = 1 hour credit
set cnt = ROW_COUNT();
if (cnt != 1) then
set p_msg = concat('Failed: int err. Error - unexpected number of rows inserted into tool_usages: ', convert(cnt,char));
rollback;
leave main;
end if;
end if;
commit;
set p_ret = 0;
end main;
END //
DELIMITER ;