1+ -- Licensed to the Apache Software Foundation (ASF) under one
2+ -- or more contributor license agreements. See the NOTICE file
3+ -- distributed with this work for additional information
4+ -- regarding copyright ownership. The ASF licenses this file
5+ -- to you under the Apache License, Version 2.0 (the
6+ -- "License"); you may not use this file except in compliance
7+ -- with the License. You may obtain a copy of the License at
8+ --
9+ -- http://www.apache.org/licenses/LICENSE-2.0
10+ --
11+ -- Unless required by applicable law or agreed to in writing,
12+ -- software distributed under the License is distributed on an
13+ -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ -- KIND, either express or implied. See the License for the
15+ -- specific language governing permissions and limitations
16+ -- under the License.
17+
18+ DROP PROCEDURE IF EXISTS ` cloud` .` IDEMPOTENT_UPDATE_ROLE_PERMISSION` ;
19+
20+ CREATE PROCEDURE ` cloud` .` IDEMPOTENT_UPDATE_ROLE_PERMISSION` (
21+ IN role VARCHAR (255 ),
22+ IN rule VARCHAR (255 ),
23+ IN permission VARCHAR (255 )
24+ )
25+ BEGIN
26+ DECLARE role_id BIGINT (20 ) UNSIGNED;
27+ DECLARE max_sort_order BIGINT (20 ) UNSIGNED;
28+
29+ SELECT ` r` .` id` INTO role_id
30+ FROM ` cloud` .` roles` ` r`
31+ WHERE ` r` .` name` = role
32+ AND ` r` .` is_default` = 1 ;
33+
34+ SELECT MAX (` rp` .` sort_order` ) INTO max_sort_order
35+ FROM ` cloud` .` role_permissions` ` rp`
36+ WHERE ` rp` .` role_id` = role_id;
37+
38+ IF NOT EXISTS (
39+ SELECT * FROM ` cloud` .` role_permissions` ` rp`
40+ WHERE ` rp` .` role_id` = role_id
41+ AND ` rp` .` rule` = rule
42+ ) THEN
43+ UPDATE ` cloud` .` role_permissions` ` rp`
44+ SET ` rp` .` sort_order` = max_sort_order + 1
45+ WHERE ` rp` .` sort_order` = max_sort_order
46+ AND ` rp` .` role_id` = role_id;
47+
48+ INSERT INTO ` cloud` .` role_permissions`
49+ (uuid, role_id, rule, permission, sort_order)
50+ VALUES (uuid(), role_id, rule, permission, max_sort_order - 1 );
51+ END IF;
52+ END;
0 commit comments