{"id":92107,"date":"2020-08-23T07:42:16","date_gmt":"2020-08-23T05:42:16","guid":{"rendered":"https:\/\/prohoster.info\/blog\/administrirovanie\/etyud-po-realizaczii-row-level-secutity-v-postgresql"},"modified":"2020-08-23T07:42:16","modified_gmt":"2020-08-23T05:42:16","slug":"etyud-po-realizaczii-row-level-secutity-v-postgresql","status":"publish","type":"post","link":"https:\/\/prohoster.info\/sk\/blog\/administrirovanie\/etyud-po-realizaczii-row-level-secutity-v-postgresql","title":{"rendered":"\u0160t\u00fadia o implement\u00e1cii Row Level Security v PostgreSQL","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Ako doplnok k <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/515628\/\"> Et\u00fada o realiz\u00e1cii obchodnej logiky na \u00farovni ulo\u017een\u00fdch funkci\u00ed PostgreSQL<\/a><\/noindex> a <b>hlavne pre rozvinut\u00fa odpove\u010f<\/b> na <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/515628\/#comment_21973176\">koment\u00e1r<\/a><\/noindex>.<\/p>\n<p>Teoretick\u00e1 \u010das\u0165 je skvele op\u00edsan\u00e1 v dokument\u00e1cii <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/\">Postgres Pro<\/a><\/noindex> \u2014 <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgrespro\/11\/ddl-rowsecurity\">Politiky ochrany riadkov<\/a><\/noindex>. Ni\u017e\u0161ie je op\u00edsan\u00e1 praktick\u00e1 realiz\u00e1cia malej <b>konkr\u00e9tnej podnikovej \u00falohy \u2014 skrytie odstr\u00e1nen\u00fdch \u00fadajov.<\/b> Etyd venovan\u00fd realiz\u00e1cii <b><noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/516040\/\">Rolov\u00e9ho modelu s pou\u017eit\u00edm RLS<\/a><\/noindex><\/b> je prezentovan\u00fd samostatne.<\/p>\n<p><img decoding=\"async\" alt=\"\u0160t\u00fadia o implement\u00e1cii Row Level Security v PostgreSQL\" src=\"\/wp-content\/uploads\/2020\/08\/6dbc1bcef27f246172192a4417dbdaf3.png\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<blockquote><p>V \u010dl\u00e1nku nie je ni\u010d nov\u00e9, \u017eiadny skryt\u00fd zmysel a tajn\u00e9 poznatky. Iba skica o praktickej realiz\u00e1cii teoretickej my\u0161lienky. Ak v\u00e1s to zauj\u00edma \u2014 \u010d\u00edtajte. Kto nie, nech nestr\u00e1ca \u010das zbyto\u010dne.<\/p><\/blockquote>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h3>Stanovenie \u00falohy<\/h3>\n<p>\nBez hlbok\u00e9ho ponorenia sa do predmetnej oblasti, stru\u010dne mo\u017eno \u00falohu formulova\u0165 nasledovne: <i>existuje tabu\u013eka implementuj\u00faca ur\u010dit\u00fa podnikate\u013esk\u00fa entitu. Riadky v tabu\u013eke m\u00f4\u017eu by\u0165 odstra\u0148ovan\u00e9, av\u0161ak fyzicky ich odstr\u00e1ni\u0165 nemo\u017eno, je potrebn\u00e9 ich skr\u00fdva\u0165. <\/p>\n<p><b>Lebo je povedan\u00e9 \u2014 \u201eNi\u010d neodstra\u0148uj, iba premen\u00favaj. Internet uchov\u00e1va V\u0160ETKO\u201c <\/b><\/p>\n<p><\/i> Popri tom, je \u017eiad\u00face neprepisova\u0165 u\u017e existuj\u00face ulo\u017een\u00e9 funkcie pracuj\u00face s danou entitou.<\/p>\n<p>Pre realiz\u00e1ciu tejto koncepcie m\u00e1 tabu\u013eka atrib\u00fat <i>is_deleted<\/i>. \u010ealej je v\u0161etko jednoduch\u00e9 \u2014 je potrebn\u00e9 zabezpe\u010di\u0165, aby klient mohol vidie\u0165 iba riadky, v ktor\u00fdch je atrib\u00fat <i>is_deleted<\/i> falo\u0161n\u00fd. Na \u010do sa pou\u017e\u00edva mechanizmus <b>Row Level Security.<\/b><\/p>\n<h3>Implement\u00e1cia<\/h3>\n<p>\nVytv\u00e1rame samostatn\u00fa rolu a sch\u00e9mu<\/p>\n<pre><code class=\"pgsql\">CREATE ROLE repos;\nCREATE SCHEMA repos;<\/code><\/pre>\n<p>\nVytv\u00e1rame cie\u013eov\u00fa tabu\u013eku<\/p>\n<pre><code class=\"pgsql\">CREATE TABLE repos.file\n(\n...\nis_del BOOLEAN DEFAULT FALSE\n);\nCREATE SCHEMA repos<\/code><\/pre>\n<p>\nAktivujte <i>Row Level Security<\/i><\/p>\n<pre><code class=\"pgsql\">ALTER TABLE repos.file  ENABLE ROW LEVEL SECURITY ;\nCREATE POLICY file_invisible_deleted  ON repos.file FOR ALL TO dba_role USING ( NOT is_deleted );\nGRANT ALL ON TABLE repos.file to dba_role ;\nGRANT USAGE ON SCHEMA repos TO dba_role ;<\/code><\/pre>\n<p>\n<b>Slu\u017eobn\u00e1 funkcia<\/b> \u2014 odstr\u00e1nenie riadku v tabu\u013eke<\/p>\n<pre><code class=\"pgsql\">CREATE OR REPLACE repos.delete( curr_id repos.file.id%TYPE)\nRETURNS integer AS $$\nBEGIN\n...\nUPDATE repos.file\nSET is_del = TRUE \nWHERE id = curr_id ; \n...\nEND\n$$ LANGUAGE plpgsql SECURITY DEFINER;<\/code><\/pre>\n<p>\n<b>Podnikov\u00e1 funkcia<\/b> \u2014 odstr\u00e1nenie dokumentu<\/p>\n<pre><code class=\"pgsql\">CREATE OR REPLACE business_functions.deleteDoc( doc_for_delete JSON )\nRETURNS JSON AS $$\nBEGIN\n...\nPERFORM  repos.delete( doc_id ) ;\n...\nEND\n$$ LANGUAGE plpgsql SECURITY DEFINER;<\/code><\/pre>\n<p><\/p>\n<h3>V\u00fdsledky<\/h3>\n<p>\nKlient odstr\u00e1ni dokument<\/p>\n<pre><code class=\"pgsql\">SELECT business_functions.delCFile( (SELECT json_build_object( 'CId', 3 )) );<\/code><\/pre>\n<p>\nPo odstr\u00e1nen\u00ed, klient dokumentu nevid\u00ed<\/p>\n<pre><code class=\"pgsql\">SELECT business_functions.getCFile( (SELECT json_build_object( 'CId', 3 )) );\n-----------------\n(0 riadkov)<\/code><\/pre>\n<p>\nAle v DB dokument nebol odstr\u00e1nen\u00fd, iba bol zmenen\u00fd atrib\u00fat <i>is_del<\/i><\/p>\n<pre><code class=\"pgsql\">psql -d my_db\nSELECT  id, name , is_del FROM repos.file ;\nid |  name  | is_del\n--+---------+------------\n 1 |  test_1 | t\n(1 row)<\/code><\/pre>\n<p>\n\u010co bolo po\u017eadovan\u00e9 v zadanej \u00falohe. <\/p>\n<h2>Z\u00e1ver<\/h2>\n<p>\nAk bude t\u00e9ma zauj\u00edmav\u00e1, v \u010fal\u0161om etyde m\u00f4\u017eem uk\u00e1za\u0165 pr\u00edklad realiz\u00e1cie rolov\u00e9ho modelu delenia pr\u00edstupu k \u00fadajom s pou\u017eit\u00edm Row Level Security.<br \/>\n<br \/>Zdroj: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/515896\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a \u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 PostgreSQL \u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u043d\u0430 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439. \u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Postgres Pro \u2014 \u041f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0437\u0430\u0449\u0438\u0442\u044b \u0441\u0442\u0440\u043e\u043a. \u041d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0439 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0431\u0438\u0437\u043d\u0435\u0441 \u0437\u0430\u0434\u0430\u0447\u0438 \u2014 \u0441\u043a\u0440\u044b\u0442\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 . \u042d\u0442\u044e\u0434 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0420\u043e\u043b\u0435\u0432\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c RLS \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":92108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-92107","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.5.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a \u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 PostgreSQL \u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u043e\u0433\u043e.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/sk\/blog\/administrirovanie\/etyud-po-realizaczii-row-level-secutity-v-postgresql\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.5.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"sk_SK\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Row Level Secutity \u0432 PostgreSQL | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a \u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 PostgreSQL \u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u043e\u0433\u043e.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/sk\/blog\/administrirovanie\/etyud-po-realizaczii-row-level-secutity-v-postgresql\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-08-23T05:42:16+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-08-23T05:42:16+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Etyd o realiz\u00e1cii Row Level Security v PostgreSQL | ProHoster","description":"Ako doplnok k Etyd o realiz\u00e1cii podnikovej logiky na \u00farovni ulo\u017een\u00fdch funkci\u00ed PostgreSQL a hlavne pre rozvinut\u00fa.","canonical_url":"https:\/\/prohoster.info\/sk\/blog\/administrirovanie\/etyud-po-realizaczii-row-level-secutity-v-postgresql","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"sk_SK","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Row Level Secutity \u0432 PostgreSQL | ProHoster","og:description":"\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a \u042d\u0442\u044e\u0434 \u043f\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0438 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u043c\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 PostgreSQL \u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u043e\u0433\u043e.","og:url":"https:\/\/prohoster.info\/sk\/blog\/administrirovanie\/etyud-po-realizaczii-row-level-secutity-v-postgresql","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-08-23T05:42:16+00:00","article:modified_time":"2020-08-23T05:42:16+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"92107","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 12:14:24","updated":"2022-09-28 09:33:44","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/posts\/92107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/comments?post=92107"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/posts\/92107\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/media\/92108"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/media?parent=92107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/categories?post=92107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/sk\/wp-json\/wp\/v2\/tags?post=92107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}