﻿
{"id":5009,"date":"2016-10-03T05:31:32","date_gmt":"2016-10-03T05:31:32","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=5009"},"modified":"2016-10-03T05:32:41","modified_gmt":"2016-10-03T05:32:41","slug":"sql-server-2016-row-level-security","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/sql-server-2016-row-level-security\/","title":{"rendered":"SQL Server 2016 &#8211; Row Level Security"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Veritaban\u0131 y\u00f6netim sistemlerinde amac\u0131m\u0131z; d\u00fczenli, organize edilmi\u015f ve ili\u015fkisel bir \u015fekilde verilerimizi modifiye etmektir. Bu amac\u0131 icra ederken g\u00fcvenlik birinci dereceden \u00f6nem te\u015fkil etmekte ve \u00e7e\u015fitli y\u00f6ntemlerle g\u00fcvenlik mekanizmas\u0131 sa\u011flanmaktad\u0131r. Bu y\u00f6ntemler genellikle kullan\u0131c\u0131 rol ve yetkilendirmeleriyle sa\u011flanmaktayken, verilere d\u00f6n\u00fck olarakta view gibi yap\u0131larla ger\u00e7ekle\u015ftirilmektedir.<\/p>\n<p>SQL Server&#8217;da hangi kullan\u0131c\u0131n\u0131n hangi tablo \u00fczerinde hangi i\u015flemi yapabilece\u011fini belirleyebiliyorduk. Lakin kullan\u0131c\u0131lar\u0131n kendilerini ilgilendiren verileri g\u00f6rmesini belirleyemiyorduk. Ta ki, SQL Server 2016 ile gelen Row Level Security \u00f6zelli\u011fi gelene kadar&#8230;<\/p>\n<p>\u015e\u00f6yle bir \u00f6rnek \u00fczerinden anlat\u0131m yaparsak konuyu olduk\u00e7a k\u0131sa tutaca\u011f\u0131m\u0131z kanaatindeyim. Bir tablo ve bu tablo \u00fczerinde yetkilendirilmi\u015f birden fazla kullan\u0131c\u0131 d\u00fc\u015f\u00fcn\u00fcn. Row Level Security \u00f6zelli\u011fi ile her yetkili kullan\u0131c\u0131 sadece kendisine ait kay\u0131tlar\u0131 sorgulayabilecektir. Biz genellikle bu i\u015flem i\u00e7in bir Stored Procedure yahut Function ile parametre olarak al\u0131nan kullan\u0131c\u0131 ad\u0131na \u00f6zel olarak verileri se\u00e7iyorduk. Art\u0131k bu i\u015flemi daha teknik boyutta ger\u00e7ekle\u015ftirece\u011fiz&#8230;<\/p>\n<p>Yani anlayaca\u011f\u0131n\u0131z Row Level Security \u00f6zelli\u011fi ile kullan\u0131c\u0131lara tablo \u00fczerinde yetki verirken t\u00fcm kay\u0131tlara de\u011fil sadece kendisini ilgilendiren kay\u0131tlara \u00f6zel bir yetkilendirme yap\u0131yor olaca\u011f\u0131z.<\/p>\n<p>\u015eimdi a\u015fa\u011f\u0131daki gibi veritaban\u0131m\u0131z\u0131, tablomuzu ve verilerimizi olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCREATE DATABASE Yenilikler\r\n\r\nUSE Yenilikler\r\n\r\nCREATE TABLE Satislar\r\n(\r\n\tSatisID INT PRIMARY KEY IDENTITY,\r\n\tUrun NVARCHAR(MAX),\r\n\tAdet INT,\r\n\tKullanici NVARCHAR(MAX)\r\n)\r\n\r\nINSERT Satislar VALUES\r\n('AUrun', 3, 'Gencay'),\r\n('BUrun', 5, 'Mehmet'),\r\n('CUrun', 13, 'Ali'),\r\n('DUrun', 23, 'Gencay'),\r\n('EUrun', 33, 'Mehmet'),\r\n('FUrun', 43, 'Ali'),\r\n('GUrun', 53, 'Gencay'),\r\n('HUrun', 63, 'Mehmet'),\r\n('IUrun', 73, 'Ali'),\r\n('OUrun', 83, 'Gencay'),\r\n('PUrun', 93, 'Mehmet'),\r\n('RUrun', 133, 'Ali')\r\n<\/pre>\n<p>Dikkatinizi \u00e7ekerim ki, &#8220;Satislar&#8221; tablosundaki &#8220;Kullanici&#8221; kolonuna &#8220;Gencay&#8221;, &#8220;Mehmet&#8221; ve &#8220;Ali&#8221; de\u011ferlerini ekliyorum. \u015eimdi bu isimlerde kullan\u0131c\u0131lar\u0131 fiziksel olarak olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCREATE USER Gencay WITHOUT LOGIN\r\nCREATE USER Mehmet WITHOUT LOGIN\r\nCREATE USER Ali WITHOUT LOGIN\r\n<\/pre>\n<p>\u015eimdi bu kullan\u0131c\u0131lar\u0131m\u0131za &#8220;Satislar&#8221; tablosuna \u00f6zel &#8220;Select&#8221; yetkisi verelim.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nGRANT SELECT ON Satislar TO Gencay\r\nGRANT SELECT ON Satislar TO Mehmet\r\nGRANT SELECT ON Satislar TO Ali\r\n<\/pre>\n<p>Bu i\u015flemlerden sonra ilgili kullan\u0131c\u0131lar\u0131m\u0131z\u0131n herhangi biriyle tablomuzda select i\u015flemi yapt\u0131\u011f\u0131m\u0131zda a\u015fa\u011f\u0131daki gibi t\u00fcm verilerin geldi\u011fini g\u00f6rece\u011fiz.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-228x300.png\" alt=\"SQL Server 2016 - Row Level Security\" width=\"228\" height=\"300\" class=\"aligncenter size-medium wp-image-5018\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-228x300.png 228w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security.png 267w\" sizes=\"auto, (max-width: 228px) 100vw, 228px\" \/><\/a><\/p>\n<p>Bunun sebebi do\u011fal olarak daha Row Level Security \u00f6zelli\u011fini kullanmad\u0131\u011f\u0131m\u0131zdan kaynaklanmaktad\u0131r.<\/p>\n<p>\u015eimdi Row Level Security \u00f6zelli\u011fini kullanabilmemiz i\u00e7in <a href=\"http:\/\/www.gencayyildiz.com\/blog\/transact-sqlfunctions-scalar-function-inline-function\/\" target=\"_blank\">Inline Table Value Funtion<\/a> olu\u015fturmal\u0131y\u0131z. Bu fonksiyon, kullan\u0131c\u0131 sorguyu \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131nda geriye 1 yani true de\u011ferini d\u00f6necek bir i\u00e7erikte olmal\u0131d\u0131r.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCREATE FUNCTION RowLevelSecurityFunction (@KullaniciAdi as sysname)\r\nRETURNS TABLE\r\nWITH SCHEMABINDING\r\nAS\r\nRETURN SELECT 1 AS rowLevelResult\r\nWHERE @KullaniciAdi = USER_NAME()\r\n<\/pre>\n<p>\u015eimdi bu fonksiyonu, birazdan olu\u015fturaca\u011f\u0131m\u0131z Security Policy(G\u00fcvenlik Politikas\u0131) i\u00e7in Filter Predicate olarak ekliyoruz. Yani uzun laf\u0131n k\u0131sas\u0131 filtre olarak ayarl\u0131yoruz.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCREATE SECURITY POLICY GuvenlikFiltresi\r\nADD FILTER PREDICATE dbo.RowLevelSecurityFunction(Kullanici)\r\nON dbo.Satislar\r\nWITH (STATE = ON);\r\n<\/pre>\n<p>Burada dikkat etmemiz gereken bir ka\u00e7 husus var. Yukar\u0131da olu\u015fturdu\u011fumuz &#8220;RowLevelSecurityFunction&#8221; isimli fonksiyon arac\u0131l\u0131\u011f\u0131yla &#8220;Satislar&#8221; tablosundaki &#8220;Kullanici&#8221; kolonunu filtre olarak ayarl\u0131yoruz. Bir di\u011fer nokta ise burada kullan\u0131lan t\u00fcm yap\u0131lar\u0131n \u015fema adlar\u0131n\u0131(.dbo) girerek \u00e7al\u0131\u015fman\u0131z\u0131 tavsiye ediyorum.<\/p>\n<p>\u015eimdi bu i\u015flemlerden sonra s\u0131ra sorgulama ad\u0131m\u0131na geldi.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-1.png\" alt=\"SQL Server 2016 - Row Level Security\" width=\"257\" height=\"189\" class=\"aligncenter size-full wp-image-5026\" \/><\/a><br \/>\nG\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere &#8220;Gencay&#8221; kullan\u0131c\u0131s\u0131yla bir select sorgusu \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131mda gelen tablo ilgili kullan\u0131c\u0131ya \u00f6zel verileri bar\u0131nd\u0131ran tablo olmaktad\u0131r.<\/p>\n<p>Keza di\u011fer kullan\u0131c\u0131larda da benzer \u015fekilde sonu\u00e7lar al\u0131nacakt\u0131r.<\/p>\n<table style=\"with:100%;\">\n<tr>\n<td>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-2.png\" alt=\"SQL Server 2016 \u2013 Row Level Security\" width=\"235\" height=\"198\" class=\"aligncenter size-full wp-image-5028\" \/><\/a>\n<\/td>\n<td>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/10\/SQL-Server-2016-Row-Level-Security-3.png\" alt=\"SQL Server 2016 \u2013 Row Level Security\" width=\"259\" height=\"193\" class=\"aligncenter size-full wp-image-5029\" \/><\/a>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u015eimdi g\u00f6r\u00fcld\u00fc\u011f\u00fc gibi select sorgumuza &#8220;<em>where Kullanici = &#8216;&#8230;&#8217;<\/em>&#8221; parametresini eklemedi\u011fimiz halde Row Level Security \u00f6zelli\u011fi arkaplanda ilgili parametreyi eklemektedir. Haliyle bu i\u015flemi olu\u015fturmu\u015f oldu\u011fumuz Security Policy nesnesi ger\u00e7ekle\u015ftirmektedir. <\/p>\n<p>Evet, bir yaz\u0131m\u0131z\u0131n daha sonuna gelmi\u015f bulunmaktay\u0131z. Umar\u0131m bol bol faydalan\u0131rs\u0131n\u0131z.<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Veritaban\u0131 y\u00f6netim sistemlerinde amac\u0131m\u0131z; d\u00fczenli, organize edilmi\u015f ve ili\u015fkisel bir \u015fekilde verilerimizi modifiye etmektir. Bu amac\u0131 icra ederken g\u00fcvenlik birinci dereceden \u00f6nem te\u015fkil etmekte ve \u00e7e\u015fitli y\u00f6ntemlerle g\u00fcvenlik mekanizmas\u0131 sa\u011flanmaktad\u0131r. Bu y\u00f6ntemler genellikle&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1412],"tags":[1449,1451,1448,1450,245,1413],"class_list":["post-5009","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server-2016","tag-filter-predicate","tag-guvenlik-politikasi","tag-row-level-security","tag-security-policy","tag-sql-server","tag-sql-server-2016"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=5009"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5009\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3415"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=5009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=5009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=5009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}