Delete gradio_app.py
Browse files- gradio_app.py +0 -69
gradio_app.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from ldap3 import Server, Connection, ALL, MODIFY_ADD
|
| 3 |
-
|
| 4 |
-
# Connect to Samba AD via LDAP
|
| 5 |
-
def ldap_connect():
|
| 6 |
-
# Replace with your Samba AD server details
|
| 7 |
-
server = Server('ldap://localhost', get_info=ALL)
|
| 8 |
-
conn = Connection(server, user='administrator@EXAMPLE.COM', password='adminpassword')
|
| 9 |
-
if not conn.bind():
|
| 10 |
-
return None
|
| 11 |
-
return conn
|
| 12 |
-
|
| 13 |
-
# Fetch all users from the LDAP server
|
| 14 |
-
def get_users():
|
| 15 |
-
conn = ldap_connect()
|
| 16 |
-
if conn:
|
| 17 |
-
conn.search('dc=example,dc=com', '(objectClass=user)', attributes=['cn', 'mail'])
|
| 18 |
-
users = []
|
| 19 |
-
for entry in conn.entries:
|
| 20 |
-
users.append(f"Name: {entry['cn'][0]}, Email: {entry.get('mail', ['No email'])[0]}")
|
| 21 |
-
conn.unbind()
|
| 22 |
-
return "\n".join(users) if users else "No users found."
|
| 23 |
-
return "Failed to connect to LDAP."
|
| 24 |
-
|
| 25 |
-
# Add a new user to Samba AD
|
| 26 |
-
def add_user(username, email):
|
| 27 |
-
conn = ldap_connect()
|
| 28 |
-
if conn:
|
| 29 |
-
# Construct the DN for the new user
|
| 30 |
-
dn = f"CN={username},CN=Users,DC=example,DC=com"
|
| 31 |
-
user_attributes = {
|
| 32 |
-
'objectClass': ['top', 'person', 'organizationalPerson', 'user'],
|
| 33 |
-
'sAMAccountName': username,
|
| 34 |
-
'userPrincipalName': f'{username}@example.com',
|
| 35 |
-
'mail': email,
|
| 36 |
-
'givenName': username,
|
| 37 |
-
'sn': username,
|
| 38 |
-
'displayName': username
|
| 39 |
-
}
|
| 40 |
-
conn.add(dn, attributes=user_attributes)
|
| 41 |
-
if conn.result['result'] == 0:
|
| 42 |
-
conn.unbind()
|
| 43 |
-
return f"User {username} added successfully!"
|
| 44 |
-
conn.unbind()
|
| 45 |
-
return "Failed to add user."
|
| 46 |
-
return "Failed to connect to LDAP."
|
| 47 |
-
|
| 48 |
-
# Gradio Interface
|
| 49 |
-
def display_users():
|
| 50 |
-
return get_users()
|
| 51 |
-
|
| 52 |
-
def add_new_user(username, email):
|
| 53 |
-
return add_user(username, email)
|
| 54 |
-
|
| 55 |
-
# Create Gradio Interface
|
| 56 |
-
with gr.Blocks() as demo:
|
| 57 |
-
gr.Markdown("### Samba AD User Management")
|
| 58 |
-
|
| 59 |
-
# Display users
|
| 60 |
-
with gr.Row():
|
| 61 |
-
gr.Button("Fetch Users").click(display_users, outputs=gr.Textbox())
|
| 62 |
-
|
| 63 |
-
# Add new user
|
| 64 |
-
with gr.Row():
|
| 65 |
-
username_input = gr.Textbox(label="Username")
|
| 66 |
-
email_input = gr.Textbox(label="Email")
|
| 67 |
-
gr.Button("Add User").click(add_new_user, inputs=[username_input, email_input], outputs=gr.Textbox())
|
| 68 |
-
|
| 69 |
-
gr.Interface(fn=display_users, inputs=[], outputs="text").launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|