-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmytap-user.sql
207 lines (152 loc) · 5.06 KB
/
mytap-user.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
-- USER
-- ====
USE tap;
DELIMITER //
/****************************************************************************/
DROP FUNCTION IF EXISTS _has_user //
CREATE FUNCTION _has_user(hname CHAR(60), uname CHAR(32))
RETURNS BOOLEAN
DETERMINISTIC
BEGIN
DECLARE ret BOOLEAN;
SELECT 1 INTO ret
FROM `mysql`.`user`
WHERE `host` = hname
AND `user` = uname;
RETURN COALESCE(ret, 0);
END //
-- has_user( host, user, description )
DROP FUNCTION IF EXISTS has_user //
CREATE FUNCTION has_user(hname CHAR(60), uname CHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('User \'', uname, '\'@\'', quote_ident(hname), '\' should exist');
END IF;
RETURN ok(_has_user (hname, uname), description);
END //
-- hasnt_user(host, user, description)
DROP FUNCTION IF EXISTS hasnt_user //
CREATE FUNCTION hasnt_user(hname CHAR(60), uname CHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
IF description = '' THEN
SET description = CONCAT('User \'', uname, '\'@\'', hname, '\' should not exist');
END IF;
RETURN ok(NOT _has_user(hname, uname), description);
END //
/****************************************************************************/
-- long-form user definition
-- _format_user
-- This function is intended to make sure that the user/role name conforms
-- to the expected form of 'user'@'host' to test with.
-- That this should be remotely difficult is down to the fact that mysql
-- allows users to be created with no quoting, single-quoting (literals),
-- back-ticking (identifiers), double-quoting (ANSI STYLE) and reports in
-- multiple formats and then also allows for users and roles to be defined
-- with no host part whatsoever but then defaults both to @'%' despite the host
-- having no relevance to roles whatsoever.
-- Note MySQL will return user@host as VARCHAR(81) and VARCHAR(93),
-- sometimes quoted, sometimes not, where mysql.user has a CHAR(32) user
-- and a CHAR(60) host which would be 97 characters once all quotes
-- and the @ are added. It's a mess.
-- See https://bugs.mysql.com/bug.php?id=91981
DROP FUNCTION IF EXISTS _format_user //
CREATE FUNCTION _format_user(uname CHAR(97))
RETURNS CHAR(97)
DETERMINISTIC
BEGIN
SET @uname = uname;
SET @uname = REPLACE(@uname, '"','''');
SET @uname = REPLACE(@uname, '`','''');
IF @uname REGEXP '@' = 0 THEN
SET @uname = CONCAT(@uname, '@\'%\'');
END IF;
IF LEFT(@uname,1) != '''' THEN
SET @uname = CONCAT('''', @uname);
END IF;
IF LOCATE('''@', @uname) = 0 THEN
SET @uname = REPLACE(@uname, '@', '''@');
END IF;
IF LOCATE('@''', @uname) = 0 THEN
SET @uname = REPLACE(@uname, '@', '@''');
END IF;
IF RIGHT(@uname,1) != '''' THEN
SET @uname = CONCAT(@uname,'''');
END IF;
RETURN @uname;
END //
DROP FUNCTION IF EXISTS _has_user_at_host //
CREATE FUNCTION _has_user_at_host(uname CHAR(97))
RETURNS BOOLEAN
DETERMINISTIC
BEGIN
DECLARE ret BOOLEAN;
SELECT 1 INTO ret
FROM `mysql`.`user`
WHERE CONCAT('\'',`user`, '\'@\'', `host`, '\'') = uname;
RETURN COALESCE(ret, 0);
END //
-- has_user@host(userdef, description )
DROP FUNCTION IF EXISTS has_user_at_host //
CREATE FUNCTION has_user_at_host(uname CHAR(97), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
SET @uname = _format_user(uname);
IF description = '' THEN
SET description = CONCAT('User ', uname, ' should exist');
END IF;
RETURN ok(_has_user_at_host(@uname), description);
END //
-- hasnt_user_at_host(userdef, description)
DROP FUNCTION IF EXISTS hasnt_user_at_host //
CREATE FUNCTION hasnt_user_at_host(uname CHAR(97), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
SET @uname = _format_user(uname);
IF description = '' THEN
SET description = CONCAT('User ', uname, ' should not exist');
END IF;
RETURN ok(NOT _has_user_at_host(@uname), description);
END //
/****************************************************************************/
-- function prototypes for features in 5.7.6
-- user_ok(host, user, description )
DROP FUNCTION IF EXISTS user_ok //
CREATE FUNCTION user_ok(hname CHAR(60), uname CHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
RETURN 'Requires MySQL version >= 5.7.6';
END //
DROP FUNCTION IF EXISTS user_not_ok //
CREATE FUNCTION user_not_ok(hname CHAR(60), uname CHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
RETURN 'Requires MySQL version >= 5.7.6';
END //
/****************************************************************************/
-- PASSWORD LIFETIME
-- user_has_lifetime( host, user, description )
DROP FUNCTION IF EXISTS user_has_lifetime//
CREATE FUNCTION user_has_lifetime(hname CHAR(60), uname CHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
RETURN 'Requires MySQL version >= 5.7.6';
END //
-- user_hasnt_lifetime( host, user, description )
DROP FUNCTION IF EXISTS user_hasnt_lifetime //
CREATE FUNCTION user_hasnt_lifetime(hname CHAR(60), uname CHAR(32), description TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
RETURN 'Requires MySQL version >= 5.7.6';
END //
/****************************************************************************/
DELIMITER ;