<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python script to set privileges / add users to Enterprise Geodatabase in ArcGIS Enterprise Questions</title>
    <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574057#M41361</link>
    <description>&lt;P&gt;Ignore my question. I was able to figure it out myself.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jan 2025 18:08:36 GMT</pubDate>
    <dc:creator>HerveHabonimana</dc:creator>
    <dc:date>2025-01-09T18:08:36Z</dc:date>
    <item>
      <title>Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1536374#M40532</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;Does somebody have a sample Python script they could share that adds users to Enterprise Geodatabase tables?&amp;nbsp; I am looking for the scripted analogy of this GUI workflow...(right click on EGDB table)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_0-1725886689533.png" style="width: 278px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/114551i0B7321ABDF91292C/image-dimensions/278x177?v=v2" width="278" height="177" role="button" title="TylerT_0-1725886689533.png" alt="TylerT_0-1725886689533.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_1-1725886715872.png" style="width: 226px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/114552iB55B02AE87398E09/image-dimensions/226x227?v=v2" width="226" height="227" role="button" title="TylerT_1-1725886715872.png" alt="TylerT_1-1725886715872.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;...of all or list of tables in the Enterprise Geodatabase.&lt;BR /&gt;&lt;BR /&gt;Thank you,&lt;BR /&gt;&lt;BR /&gt;Tyler&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 13:01:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1536374#M40532</guid>
      <dc:creator>TylerT</dc:creator>
      <dc:date>2024-09-09T13:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1537885#M40557</link>
      <description>&lt;P&gt;i use this one. it is for OS authentication and using DB rules. You may need to tweek it for your need.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

def create_database_user(gdb, username, role):
    # Connect to the geodatabase as an admin
    arcpy.env.workspace = gdb

    # Check if user exists
    users = [user.Name for user in arcpy.ListUsers(gdb)]
    if username in users:
        print(f"User {username} already exists.")
        return

    # Create the user and assign to a role
    arcpy.management.CreateDatabaseUser(input_database=gdb,
                                        user_authentication_type="OPERATING_SYSTEM_USER",
                                        user_name=username,
                                        user_password='',
                                        role=role,
                                        tablespace_name="")
    
    print(f"User {username} created and assigned to role {role}.")



# inputs

geodatabase = r"&amp;lt;database&amp;gt;.sde"
username = "DOMAIN\\username"
role = "DB Writer Role"

create_database_user(geodatabase, username, role)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 17:23:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1537885#M40557</guid>
      <dc:creator>AzinSharaf</dc:creator>
      <dc:date>2024-09-12T17:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1546643#M40698</link>
      <description>&lt;P&gt;Thank you, but your script is for adding users to EGDB.&amp;nbsp; My question is about assigning User privilege's to EGDB tables.&amp;nbsp; The add user dialogue in the GUI confuses the situation a bit, but I don't want to add users to EGDB, rather add users to tables in order to set to set User permissions, which is done through the dialogues shown.&lt;BR /&gt;&lt;BR /&gt;For context, if a table is dropped and re-added, the User privilege's are wiped out.&amp;nbsp; We need to automate adding User privilege's back in to tables after dropping tables.&lt;BR /&gt;&lt;BR /&gt;Thank you,&lt;/P&gt;&lt;P&gt;Tyler&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 17:28:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1546643#M40698</guid>
      <dc:creator>TylerT</dc:creator>
      <dc:date>2024-10-08T17:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1546690#M40701</link>
      <description>&lt;P&gt;this is what i am doing. hope it gives you the idea.&lt;/P&gt;&lt;P&gt;I create two database roles (&amp;lt;name&amp;gt;_r and &amp;lt;name&amp;gt;_w) and assign the privileges of each feature dataset to it. I always add Roles in the Privilage menu, not individual users.&lt;/P&gt;&lt;P&gt;how to create SQL roles in gdb:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def create_sql_roles(sde_connections_folder):
    # This function creates Reader and Writer database roles in SQL Server.
    arcpy.AddMessage("Creating reader and writer database roles in SQL Server...")
    sde_conn_admin = find(pattern="*admin*", path=sde_connections_folder)[0]
    sde_conn_reader = find(pattern="*reader*", path=sde_connections_folder)[0]
    sde_conn_writer = find(pattern="*writer*", path=sde_connections_folder)[0]

    admin_account_name = arcpy.Describe(sde_conn_admin).connectionProperties.user
    reader_account_name = arcpy.Describe(sde_conn_reader).connectionProperties.user
    writer_account_name = arcpy.Describe(sde_conn_writer).connectionProperties.user

    db_instance = arcpy.Describe(sde_conn_admin).connectionProperties.instance.lstrip(
        "sde:sqlserver:"
    )
    db_name = arcpy.Describe(sde_conn_admin).connectionProperties.database

    # connect to database
    # the account that python is running under should be SQL Server sys admin
    conn = pyodbc.connect(
        "DRIVER={ODBC Driver 17 for SQL Server};"
        "SERVER=" + db_instance + ";"
        "DATABASE=" + db_name + ";"
        "Trusted_Connection=yes;"
    )

    conn.autocommit = True
    cursor = conn.cursor()

    # creates DOMAIN\user database user
    arcpy.AddMessage(r"Creating REDACTED database user...")
    sql_string = f"""
                     USE [{db_name}]
                     IF NOT EXISTS (SELECT name
                                    FROM [sys].[database_principals]
                                    WHERE name = N'DOMAIN\REDACTED')
                     BEGIN
                        CREATE USER [DOMAIN\\REDACTED] FOR LOGIN [DOMAIN\\REDACTED]
                     END
                  """

    cursor.execute(sql_string)

    # creating Reader Role in SQL database and add X active directory group to the role
    reader_role_name = f"{db_name}_r"
    arcpy.AddMessage(f"Creating {reader_role_name} role...")
    arcpy.management.CreateRole(
        input_database=sde_conn_admin,
        role=reader_role_name,
        grant_revoke="GRANT",
        user_name=r"DOMAIN\AD Name",
    )



    arcpy.AddMessage(f"Adding {reader_account_name} user to {reader_role_name} role...")

    sql_string = f"""
                        USE [{db_name}]
                        ALTER ROLE {reader_role_name} ADD MEMBER [{reader_account_name}]
                  """
    cursor.execute(sql_string)

    # create Writer Role in SQL database

    writer_role_name = f"{db_name}_w"
    arcpy.AddMessage(f"Creating {writer_role_name} role...")
    arcpy.management.CreateRole(
        input_database=sde_conn_admin,
        role=writer_role_name,
        grant_revoke="GRANT",
        user_name=None,
    )

    arcpy.AddMessage(f"Adding {writer_account_name} user to {writer_role_name} role...")

    sql_string = f"""
                        USE [{db_name}]
                        ALTER ROLE {writer_role_name} ADD MEMBER [{writer_account_name}]
                  """
    cursor.execute(sql_string)

    return reader_role_name, writer_role_name&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and how to change the permission on each dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def get_datasets_list(sde_conn):
    arcpy.env.workspace = sde_conn
    datasets_list = arcpy.ListDatasets(wild_card="*", feature_type="Feature")
    table_list = arcpy.ListTables(wild_card="*", table_type="ALL")

    datasets_list = datasets_list + table_list

    return datasets_list




def change_privileges(sde_conn_admin, role, view_access, write_access):
    arcpy.AddMessage(f"Changing privileges for {role} role...")
    datasets_list = get_datasets_list(sde_conn=sde_conn_admin)
    for dataset in datasets_list:
        arcpy.management.ChangePrivileges(
            in_dataset=dataset, user=role, View=view_access, Edit=write_access
        )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and use it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;                change_privileges(
                    sde_conn_admin=sde_conn_admin,
                    role=reader_role_name,
                    view_access="GRANT",
                    write_access="AS_IS",
                )

                change_privileges(
                    sde_conn_admin=sde_conn_admin,
                    role=writer_role_name,
                    view_access="GRANT",
                    write_access="GRANT",
                )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 18:47:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1546690#M40701</guid>
      <dc:creator>AzinSharaf</dc:creator>
      <dc:date>2024-10-08T18:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574053#M41360</link>
      <description>&lt;P&gt;Thanks a lot for sharing this.&lt;/P&gt;&lt;P&gt;Can you share details about this find function?&lt;/P&gt;&lt;PRE&gt;sde_conn_admin = find(pattern="*admin*", path=sde_connections_folder)[0]&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 17:58:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574053#M41360</guid>
      <dc:creator>HerveHabonimana</dc:creator>
      <dc:date>2025-01-09T17:58:06Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574057#M41361</link>
      <description>&lt;P&gt;Ignore my question. I was able to figure it out myself.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 18:08:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574057#M41361</guid>
      <dc:creator>HerveHabonimana</dc:creator>
      <dc:date>2025-01-09T18:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Python script to set privileges / add users to Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574159#M41363</link>
      <description>&lt;P&gt;After struggling with this issue, here is how I resolved it in Oracle Database&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# this script assign SELECT, INSERT, UPDATE, DELETE 
import arcpy, os, oracledb
dbuser    = 'dbuser' # the user who needs access
# path to sde
path = r'path to .sde'
# path to connection file
sde_path = os.path.join(path, f'file.sde')
USER = "sde level username"
PASSWORD = "psw"
DSN = "dns"
oracledb.init_oracle_client(lib_dir=None)
conn = oracledb.connect(user=USER, password=PASSWORD, dsn=DSN)
curs = conn.cursor()
writer_role_name = f"the name of the role you want to assign to user"
db_name = "the name of the schema"
# assign the writer role to the user

arcpy.CreateRole_management(sde_path, writer_role_name,
                            "GRANT", dbuser)
feature_lists = ['fc1', 'fc2']
for fc in feature_lists:
    fc_ = f"{db_name}.{fc}"
    print(fc_)
    try:
        print(f"Granting privileges to {writer_role_name} on {fc_}...")
        curs.execute(f"GRANT SELECT, INSERT, UPDATE, DELETE ON {fc_} TO {writer_role_name}")
        conn.commit()
        print(f"Privileges granted to {writer_role_name} on {fc_}")
    except Exception as e:
        print(f"Failed to grant privileges to {writer_role_name} on {fc_}")
        print(e)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 21:00:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-enterprise-questions/python-script-to-set-privileges-add-users-to/m-p/1574159#M41363</guid>
      <dc:creator>HerveHabonimana</dc:creator>
      <dc:date>2025-01-09T21:00:51Z</dc:date>
    </item>
  </channel>
</rss>

