Aracılığıyla paylaş


XML şema koleksiyonu üzerinde izinler verme

Şunlar için geçerlidir:SQL ServerAzure SQL VeritabanıAzure SQL Yönetilen ÖrneğiMicrosoft Fabric'te SQL veritabanı

XML şema koleksiyonu oluşturma izinleri verebilir ve ayrıca bir XML şema koleksiyonu nesnesi üzerinde izinler vekleyebilirsiniz.

XML şema koleksiyonu oluşturma izni verme

XML şema koleksiyonu oluşturmak için aşağıdaki izinler gereklidir:

  • Sorumlu, veritabanı düzeyinde CREATE XML SCHEMA COLLECTION izni gerektirir.

  • XML şema koleksiyonları ilişkisel şema kapsamına sahip olduğundan, sorumlunun ilişkisel şema üzerinde ALTER iznine de sahip olması gerekir.

Aşağıdaki izinler, bir sorumlunun sunucudaki veritabanındaki ilişkisel şemada xml şeması koleksiyonu oluşturmasına olanak sağlar:

  • Sunucuda KONTROL izni

  • Sunucudaki HERHANGİ Bİr VERITABANı iznini DEĞIŞTIRME

  • Veritabanında ALTER izni

  • Veritabanında KONTROL izni

  • Veritabanında Herhangi Bir Şema İznini ve Create XML Schema Collection İznini Değiştirme

  • İlişkisel şema üzerinde ALTER veya CONTROL izni ve veritabanında XML ŞEMA KOLEKSİYONU OLUŞTURMA izni

Bu son izin yöntemi aşağıdaki örnekte kullanılmıştır.

İlişkisel şemanın sahibi, bu şemada oluşturulan XML şema koleksiyonunun sahibi olur. Bu sahip daha sonra XML şema koleksiyonu üzerinde tam denetime sahiptir. Bu nedenle, bu sahip XML şema koleksiyonunu değiştirebilir, bir xml sütunu yazabilir veya XML şema koleksiyonunu bırakabilir.

XML şema koleksiyonu nesnesi üzerinde izin verme

XML şema koleksiyonunda aşağıdaki izinlere izin verilir:

  • ALTER XML SCHEMA COLLECTION deyimi kullanılarak var olan bir XML şema koleksiyonunun içeriği değiştirilirken ALTER izni gereklidir.

  • CONTROL izni, kullanıcının XML şema koleksiyonu üzerinde herhangi bir işlem gerçekleştirmesine olanak tanır.

  • XML şema koleksiyonunun sahipliğini bir yetkiliden diğerine aktarmak için Sahiplik Alma izni gereklidir.

  • REFERANS izni, bir kişiye XML şema koleksiyonunu tablo, görünüm ve parametrelerde xml türündeki sütunları yazmak veya kısıtlamak amacıyla kullanma yetkisi verir. BIR XML şema koleksiyonu başka bir XML şemasına başvurduğunda BAŞVURULAR izni de gereklidir.

  • VIEW DEFINITION izni, bu sorumlunun koleksiyondaki ALTER, REFERENCES veya CONTROL izinlerinden birine de sahip olması koşuluyla, sorumlunun XML_SCHEMA_NAMESPACE veya katalog görünümleri aracılığıyla XML şema koleksiyonunun içeriğini sorgulamasına olanak tanır.

  • xml türü sütunlarını, değişkenlerini ve parametrelerini yazan veya kısıtlayan XML şema koleksiyonuna karşı sorumlu tarafından eklenen veya güncelleştirilen değerleri doğrulamak için EXECUTE izni gereklidir. Bu sütunlarda ve değişkenlerde depolanan XML'yi sorgularken de bu izne ihtiyacınız vardır.

Örnekler

Aşağıdaki örneklerde yer alan senaryolarda XML şema izinlerinin nasıl çalıştığı gösterilmektedir. Her örnek gerekli test veritabanını, ilişkisel şemaları ve oturum açma bilgilerini oluşturur. Bu oturum açma işlemlerine gerekli XML şema koleksiyonu izinleri verilir. Her örnek, sonunda gerekli temizlemeyi yapar.

A. XML şema koleksiyonu oluşturma izinleri verme

Aşağıdaki örnekte, bir sorumlunun XML şema koleksiyonu oluşturabilmesi için izinlerin nasıl verileceğini gösterilmektedir. Örnek, TestLogin1bir örnek veritabanı ve bir test kullanıcısı oluşturur. Ardından TestLogin1, ilişkisel şemada ALTER izni ve veritabanında CREATE XML SCHEMA COLLECTION izni verilir. Bu izinlerle TestLogin1 örnek XML şema koleksiyonu oluşturmada başarılı olur.

SETUSER;
GO
USE master;
GO
CREATE LOGIN TestLogin1 WITH password='SQLSvrPwd1';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
CREATE USER TestLogin1;
GO
-- User must have ALTER permission on the relational schema in the database.
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- User also must have permission to create XML schema collections in the database.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
-- Execute CREATE XML SCHEMA COLLECTION.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="AdditionalContactInfo" >
  <xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="root" type="xsd:byte"/>
</xsd:schema>';
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
GO

B. Var olan bir XML şema koleksiyonunu kullanma izni verme

Aşağıdaki örnekte, XML şema koleksiyonu için izin modeli gösterilmektedir. Örnek, XML şema koleksiyonunu oluşturmak ve kullanmak için farklı izinlerin nasıl gerekli olduğunu gösterir.

Örnek, TestLogin1bir test veritabanı ve bir giriş oluşturur. TestLogin1 veritabanında bir XML şema koleksiyonu oluşturur. Daha sonra oturum açma işlemi bir tablo oluşturur ve XML şema koleksiyonunu kullanarak yazılan xml sütununu oluşturur. Kullanıcı daha sonra verileri ekler ve sorgular. Tüm bu adımlar, kodda gösterildiği gibi gerekli şema izinlerini gerektirir.

SETUSER
GO
USE master;
GO
CREATE LOGIN TestLogin1 WITH password='SQLSvrPwd1';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
CREATE USER TestLogin1;
GO
-- Grant permission to the user.
SETUSER;
GO
-- User must have ALTER permission on the relational schema in the database.
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- User also must have permission to create XML schema collections in the database.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
-- Now user can execute the previous CREATE XML SCHEMA COLLECTION statement.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="AdditionalContactInfo" >
  <xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="telephone" type="xsd:string" />
</xsd:schema>';
GO

-- Create a table by using the collection to type an XML column.
--TestLogin1 must have permission to create a table.
SETUSER;
GO
GRANT CREATE TABLE TO TestLogin1;
GO
-- The user also must have REFERENCES permission to use the XML schema collection
-- to create a typed XML column (REFERENCES permission on schema
-- collection is not needed).
GRANT REFERENCES ON XML SCHEMA COLLECTION::myTestSchemaCollection
TO TestLogin1;
GO
-- Now user can create a table and use the XML schema collection to create
-- a typed XML column.
SETUSER 'TestLogin1';
GO
CREATE TABLE MyTestTable (xmlCol xml (dbo.myTestSchemaCollection));
GO
-- To insert data in the table, the user needs EXECUTE permission on the XML schema collection.
-- GRANT EXECUTE permission to TestLogin2 on the xml schema collection.
SETUSER;
GO
GRANT EXECUTE ON XML SCHEMA COLLECTION::myTestSchemaCollection
TO TestLogin1;
GO
-- TestLogin1 does not own the dbo schema. This user must have INSERT permission.
GRANT INSERT TO TestLogin1;
GO
-- Now the user can insert data into the table.
SETUSER 'TestLogin1';
GO
INSERT INTO MyTestTable VALUES('
<telephone xmlns="http://schemas.adventure-works.com/Additional/ContactInfo">111-1111</telephone>
');
GO
-- To query the table, TestLogin1 must have permissions: SELECT on the table and EXECUTE on the XML schema collection.
SETUSER
GO
GRANT SELECT TO TestLogin1;
GO
-- TestLogin1 already has EXECUTE permission on the schema (granted before inserting a record in the table).
SELECT xmlCol.query('declare default element namespace "https://schemas.adventure-works.com/Additional/ContactInfo"; /telephone[1]')
FROM MyTestTable;
GO
-- To show that the user must have EXECUTE permission to query, revoke the
-- previously granted permission and return the query.
SETUSER;
GO
REVOKE EXECUTE ON XML SCHEMA COLLECTION::myTestSchemaCollection to TestLogin1;
GO
-- Now TestLogin1 cannot execute the query.
SETUSER 'TestLogin1';
GO
SELECT xmlCol.query('declare default element namespace "https://schemas.adventure-works.com/Additional/ContactInfo"; /telephone[1]')
FROM MyTestTable;
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
GO

C. XML şema koleksiyonunda ALTER izni verme

Kullanıcının veritabanındaki mevcut bir XML şema koleksiyonunu değiştirmek için ALTER iznine sahip olması gerekir. Aşağıdaki örnek, ALTER iznini nasıl vereceğinizi gösteriyor.

SETUSER;
GO
USE master;
GO
CREATE LOGIN TestLogin1 WITH password='SQLSvrPwd1';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
CREATE USER TestLogin1;
GO
-- Grant permission to the user.
SETUSER;
GO
-- User must have ALTER permission on the relational schema in the database.
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- User also must have permission to create XML schema collections in the database.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
-- Now user can execute the previous CREATE XML SCHEMA COLLECTION statement.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="AdditionalContactInfo" >
  <xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="telephone" type="xsd:string" />
</xsd:schema>';
GO
-- Grant ALTER permission to TestLogin1.
SETUSER;
GO
GRANT ALTER ON XML SCHEMA COLLECTION::myTestSchemaCollection TO TestLogin1;
GO
-- TestLogin1 should be able to add components to the collection.
SETUSER 'TestLogin1';
GO
ALTER XML SCHEMA COLLECTION myTestSchemaCollection ADD '
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://schemas.adventure-works.com/Additional/ContactInfo"
elementFormDefault="qualified">
<xsd:element name="pager" type="xsd:string"/>
</xsd:schema>';
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
GO

D. XML şema koleksiyonunda SAHIPLIK ALMA izni verme

Aşağıdaki örnekte XML şeması sahipliğini bir kullanıcıdan diğerine aktarma işlemi gösterilmektedir. Örneği daha ilginç hale getirmek için, bu örnekteki kullanıcılar farklı varsayılan ilişkisel şemalarda çalışır.

Bu örnek aşağıdakileri yapar:

  • dbo ve myOtherDBSchema) olmak üzere iki ilişkisel şemaya sahip bir veritabanı oluşturur.

  • TestLogin1 ve TestLogin2adlı iki kullanıcı oluşturur. TestLogin2, myOtherDBSchema ilişkisel şemasının sahibi yapılır.

  • TestLogin1, dbo ilişkisel şemada bir XML şeması koleksiyonu oluşturur.

  • TestLogin1, XML şema koleksiyonu üzerinde TAKE OWNERSHIP'ye TestLogin2 izni verir.

  • TestLogin2, XML şema koleksiyonunun ilişkisel şemasını değiştirmeden myOtherDBSchemaxml şema koleksiyonunun sahibi olur.

CREATE LOGIN TestLogin1 with password='SQLSvrPwd1';
GO
CREATE LOGIN TestLogin2 with password='SQLSvrPwd2';
GO
CREATE DATABASE SampleDBForSchemaPermissions;
GO
USE SampleDBForSchemaPermissions;
GO
-- Create another relational schema in the database.
CREATE SCHEMA myOtherDBSchema;
GO
-- Create users in the database. Note TestLogin2's default schema is
-- myOtherDBSchema.
CREATE USER TestLogin1;
GO
CREATE USER TestLogin2 WITH DEFAULT_SCHEMA=myOtherDBSchema;
GO
-- TestLogin2 will own myOtherDBSchema relational schema.
ALTER AUTHORIZATION ON SCHEMA::myOtherDBSchema TO TestLogin2;
GO

-- For TestLogin1 to create XML schema collection, the following
-- permission is required.
GRANT CREATE XML SCHEMA COLLECTION
TO TestLogin1;
GO
GRANT ALTER ON SCHEMA::dbo TO TestLogin1;
GO
-- Now TestLogin1 can create an XML schema collection.
SETUSER 'TestLogin1';
GO
CREATE XML SCHEMA COLLECTION myTestSchemaCollection AS '<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema targetNamespace="https://schemas.adventure-works.com/Additional/ContactInfo"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="AdditionalContactInfo" >
<xsd:complexType mixed="true" >
    <xsd:sequence>
      <xsd:any processContents="strict"
               namespace="https://schemas.adventure-works.com/Contact/Record
                          https://schemas.adventure-works.com/AdditionalContactTypes"
               minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="telephone" type="xsd:string" />
</xsd:schema>';
GO

-- Grant TAKE OWNERSHIP to TestLogin2.
SETUSER;
GO
GRANT TAKE OWNERSHIP ON XML SCHEMA COLLECTION::dbo.myTestSchemaCollection
TO TestLogin2;
GO
-- Verify the owner. Note the UserName and Principal_id is null.
SELECT user_name(sys.xml_schema_collections.principal_id) as UserName,
       sys.schemas.name as RelSchemaName,*
FROM   sys.xml_schema_collections
      JOIN sys.schemas
      ON sys.schemas.schema_id=sys.xml_schema_collections.schema_id;
GO
-- TestLogin2 can take ownership now.
SETUSER 'TestLogin2';
GO
ALTER AUTHORIZATION ON XML SCHEMA COLLECTION::dbo.myTestSchemaCollection
TO TestLogin2;
GO
-- Note that although TestLogin2 is the owner,the XML schema collection
-- is still in dbo.
SELECT user_name(sys.xml_schema_collections.principal_id) as UserName,
      sys.schemas.name as RelSchemaName,*
FROM sys.xml_schema_collections JOIN sys.schemas
     ON sys.schemas.schema_id=sys.xml_schema_collections.schema_id;
GO

-- TestLogin2 moves the collection from dbo to myOtherDBSchema relational schema.
-- TestLogin2 already has all necessary permissions.
-- 1) TestLogin2 owns the destination relational schema so they can alter it.
-- 2) TestLogin2 owns the XML schema collection (therefore, has CONTROL permission).
ALTER SCHEMA myOtherDBSchema
TRANSFER XML SCHEMA COLLECTION::dbo.myTestSchemaCollection;
GO

SELECT user_name(sys.xml_schema_collections.principal_id) as UserName,
       sys.schemas.name as RelSchemaName,*
FROM   sys.xml_schema_collections JOIN sys.schemas
       ON sys.schemas.schema_id=sys.xml_schema_collections.schema_id;
GO
-- Final cleanup
SETUSER;
GO
USE master;
GO
DROP DATABASE SampleDBForSchemaPermissions;
GO
DROP LOGIN TestLogin1;
DROP LOGIN TestLogin2;
GO

E. XML şema koleksiyonunda VIEW DEFINITION izni verme

Aşağıdaki örnekte, XML şema koleksiyonu için VIEW DEFINITION izinlerinin nasıl verileceğini gösterilmektedir.

SETUSER;
GO
USE master;
GO
IF EXISTS( SELECT * FROM sysdatabases WHERE name='permissionsDB' )
   DROP DATABASE permissionsDB;
GO
IF EXISTS( SELECT * FROM sys.sql_logins WHERE name='schemaUser' )
   DROP LOGIN schemaUser;
GO
CREATE DATABASE permissionsDB;
GO
CREATE LOGIN schemaUser WITH PASSWORD='Pass#123',DEFAULT_DATABASE=permissionsDB;
GO
GRANT CONNECT SQL TO schemaUser;
GO
USE permissionsDB;
GO
CREATE USER schemaUser WITH DEFAULT_SCHEMA=dbo;
GO
CREATE XML SCHEMA COLLECTION MySC AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ns"
xmlns:ns="http://ns">
   <simpleType name="ListOfIntegers">
      <list itemType="integer"/>
   </simpleType>
   <element name="root" type="ns:ListOfIntegers"/>
   <element name="gRoot" type="gMonth"/>
</schema>';
GO
-- schemaUser cannot see the contents of the collection.
SETUSER 'schemaUser';
GO
SELECT XML_SCHEMA_NAMESPACE(N'dbo',N'MySC');
GO

-- Grant schemaUser VIEW DEFINITION and REFERENCES permissions
-- on the XML schema collection.
SETUSER;
GO
GRANT VIEW DEFINITION ON XML SCHEMA COLLECTION::dbo.MySC TO schemaUser;
GO
GRANT REFERENCES ON XML SCHEMA COLLECTION::dbo.MySC TO schemaUser;
GO
-- Now schemaUser can see the content of the collection.
SETUSER 'schemaUser';
GO
SELECT XML_SCHEMA_NAMESPACE(N'dbo',N'MySC');
GO
-- Revoke schemaUser VIEW DEFINITION permissions
-- on the XML schema collection.
SETUSER;
GO
REVOKE VIEW DEFINITION ON XML SCHEMA COLLECTION::dbo.MySC FROM schemaUser;
GO
-- Now schemaUser cannot see the contents of
-- the collection.
SETUSER 'schemaUser';
GO
SELECT XML_SCHEMA_NAMESPACE(N'dbo',N'MySC');
GO

Ayrıca bkz.