INSERT INTO permission_templates (name, uuid, description, key_pattern, created_at, updated_at)
VALUES (
#{name,jdbcType=VARCHAR},
#{uuid,jdbcType=VARCHAR},
#{description,jdbcType=VARCHAR},
#{keyPattern,jdbcType=VARCHAR},
#{createdAt},
#{updatedAt})
UPDATE permission_templates
SET name = #{name}, description = #{description}, key_pattern = #{keyPattern}, updated_at = #{updatedAt}
WHERE uuid = #{uuid}
DELETE FROM permission_templates
WHERE uuid = #{uuid}
delete from
perm_templates_users
where
template_uuid = #{templateUuid,jdbcType=VARCHAR}
DELETE FROM perm_templates_users
WHERE template_uuid = #{templateUuid}
AND user_uuid = #{userUuid}
AND permission_reference = #{permission}
delete from perm_templates_users
where
user_uuid = #{userUuid,jdbcType=VARCHAR}
delete from
perm_templates_groups
where
template_uuid = #{templateUuid,jdbcType=VARCHAR}
DELETE FROM perm_templates_groups
WHERE template_uuid = #{templateUuid}
AND permission_reference = #{permission}
AND
group_uuid = #{groupUuid}
group_uuid IS NULL
INSERT INTO perm_templates_users (uuid, template_uuid, user_uuid, permission_reference, created_at, updated_at)
VALUES (#{uuid}, #{templateUuid}, #{userUuid}, #{permission}, #{createdAt}, #{updatedAt})
INSERT INTO perm_templates_groups (uuid, template_uuid, group_uuid, permission_reference, created_at, updated_at)
VALUES (
#{uuid,jdbcType=VARCHAR},
#{templateUuid,jdbcType=VARCHAR},
#{groupUuid,jdbcType=VARCHAR},
#{permission,jdbcType=VARCHAR},
#{createdAt,jdbcType=TIMESTAMP},
#{updatedAt,jdbcType=TIMESTAMP}
)
delete from perm_templates_groups
where group_uuid = #{groupUuid,jdbcType=VARCHAR}
FROM users u
LEFT JOIN perm_templates_users ptu ON ptu.user_uuid=u.uuid AND ptu.template_uuid=#{templateUuid}
u.active = ${_true}
AND (lower(u.name) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
OR lower(u.login) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
OR lower(u.email) like #{query.searchQueryToSqlLowercase} ESCAPE '/')
and ptu.permission_reference is not null
and ptu.permission_reference=#{query.permission}
FROM
(SELECT
g.uuid AS group_uuid,
g.name AS name,
ptg.permission_reference AS permission,
ptg.template_uuid AS templateUuid
FROM groups g
LEFT JOIN perm_templates_groups ptg ON
ptg.group_uuid=g.uuid
AND ptg.template_uuid=#{templateUuid}
UNION ALL
SELECT
'Anyone' AS group_uuid,
'Anyone' AS name,
ptg.permission_reference AS permission,
ptg.template_uuid AS templateUuid
FROM groups g
LEFT JOIN perm_templates_groups ptg ON
ptg.template_uuid=#{templateUuid}
ptg.group_uuid IS NULL
) groups
AND LOWER(groups.name) LIKE #{query.searchQueryToSqlLowercase} ESCAPE '/'
AND groups.permission IS NOT NULL
AND groups.templateUuid=#{templateUuid}
AND groups.permission=#{query.permission}
name, uuid, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
ptu.uuid,
ptu.template_uuid as templateUuid,
ptu.permission_reference AS permission,
ptu.user_uuid AS userUuid,
u.name AS userName,
u.login AS userLogin,
ptu.created_at AS createdAt,
ptu.updated_at AS updatedAt