|
| 1 | +""" |
| 2 | +Create the initial admin user and organization for ExaFS. |
| 3 | +
|
| 4 | +Run this once after 'exafs-db-init' to set up the first administrator |
| 5 | +and their organization. Without at least one admin user, the application |
| 6 | +cannot be managed through the web interface. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + exafs-create-admin |
| 10 | +
|
| 11 | +Requires config.py to be present in the current working directory. |
| 12 | +""" |
| 13 | + |
| 14 | +import sys |
| 15 | + |
| 16 | +from flowapp.scripts import _create_app |
| 17 | + |
| 18 | + |
| 19 | +def prompt(label, required=True, default=None): |
| 20 | + """Prompt for input, optionally with a default value.""" |
| 21 | + display = f"{label} [{default}]: " if default else f"{label}: " |
| 22 | + while True: |
| 23 | + value = input(display).strip() |
| 24 | + if not value and default: |
| 25 | + return default |
| 26 | + if value: |
| 27 | + return value |
| 28 | + if not required: |
| 29 | + return "" |
| 30 | + print(f" {label} is required.") |
| 31 | + |
| 32 | + |
| 33 | +def create_admin(): |
| 34 | + app = _create_app() |
| 35 | + |
| 36 | + from flowapp import db |
| 37 | + from flowapp.models import Organization, Role, User |
| 38 | + |
| 39 | + with app.app_context(): |
| 40 | + # Verify migrations have been run |
| 41 | + admin_role = Role.query.filter_by(name="admin").first() |
| 42 | + if not admin_role: |
| 43 | + print("Error: roles not found in database.") |
| 44 | + print("Please run 'exafs-db-init' first.") |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + print() |
| 48 | + print("ExaFS initial admin setup") |
| 49 | + print("=" * 40) |
| 50 | + |
| 51 | + # --- User --- |
| 52 | + print() |
| 53 | + print("Admin user") |
| 54 | + print("-" * 20) |
| 55 | + print("UUID is the unique identifier used for authentication.") |
| 56 | + print("For SSO (Shibboleth), this is typically the eppn attribute.") |
| 57 | + print("For local auth, use any unique string (e.g. email address).") |
| 58 | + print() |
| 59 | + |
| 60 | + while True: |
| 61 | + uuid = prompt("UUID (e.g. user@example.edu)") |
| 62 | + existing = User.query.filter_by(uuid=uuid).first() |
| 63 | + if existing: |
| 64 | + print(f" A user with UUID '{uuid}' already exists.") |
| 65 | + overwrite = input(" Update this user's roles and org? (yes/no): ").strip().lower() |
| 66 | + if overwrite == "yes": |
| 67 | + user = existing |
| 68 | + break |
| 69 | + continue |
| 70 | + user = None |
| 71 | + break |
| 72 | + |
| 73 | + name = prompt("Full name", required=False) |
| 74 | + email = prompt("Email", default=uuid if "@" in uuid else None) |
| 75 | + phone = prompt("Phone", required=False) |
| 76 | + |
| 77 | + # --- Organization --- |
| 78 | + print() |
| 79 | + print("Organization") |
| 80 | + print("-" * 20) |
| 81 | + print("Address ranges (arange) are whitespace-separated CIDR prefixes.") |
| 82 | + print("Example: 192.0.2.0/24 2001:db8::/32") |
| 83 | + print() |
| 84 | + |
| 85 | + orgs = Organization.query.all() |
| 86 | + if orgs: |
| 87 | + print("Existing organizations:") |
| 88 | + for org in orgs: |
| 89 | + print(f" [{org.id}] {org.name}") |
| 90 | + print() |
| 91 | + choice = input("Use existing organization ID, or press Enter to create new: ").strip() |
| 92 | + if choice.isdigit(): |
| 93 | + org = Organization.query.get(int(choice)) |
| 94 | + if not org: |
| 95 | + print(f" Organization {choice} not found, creating new.") |
| 96 | + org = None |
| 97 | + else: |
| 98 | + org = None |
| 99 | + else: |
| 100 | + org = None |
| 101 | + |
| 102 | + if org is None: |
| 103 | + org_name = prompt("Organization name") |
| 104 | + org_arange = prompt("Address ranges (CIDR, space-separated)") |
| 105 | + org = Organization(name=org_name, arange=org_arange) |
| 106 | + db.session.add(org) |
| 107 | + db.session.flush() # get org.id before commit |
| 108 | + print(f" Created organization: {org.name}") |
| 109 | + |
| 110 | + # --- Confirm --- |
| 111 | + print() |
| 112 | + print("Summary") |
| 113 | + print("=" * 40) |
| 114 | + print(f" UUID: {uuid}") |
| 115 | + print(f" Name: {name or '(not set)'}") |
| 116 | + print(f" Email: {email or '(not set)'}") |
| 117 | + print(f" Phone: {phone or '(not set)'}") |
| 118 | + print(f" Role: admin") |
| 119 | + print(f" Organization: {org.name}") |
| 120 | + print() |
| 121 | + |
| 122 | + confirm = input("Create admin user? (yes/no): ").strip().lower() |
| 123 | + if confirm != "yes": |
| 124 | + print("Aborted.") |
| 125 | + db.session.rollback() |
| 126 | + sys.exit(0) |
| 127 | + |
| 128 | + # --- Create or update user --- |
| 129 | + if user is None: |
| 130 | + user = User(uuid=uuid, name=name or None, email=email or None, phone=phone or None) |
| 131 | + db.session.add(user) |
| 132 | + else: |
| 133 | + if name: |
| 134 | + user.name = name |
| 135 | + if email: |
| 136 | + user.email = email |
| 137 | + if phone: |
| 138 | + user.phone = phone |
| 139 | + |
| 140 | + # Assign admin role (avoid duplicates) |
| 141 | + if not user.role.filter_by(name="admin").first(): |
| 142 | + user.role.append(admin_role) |
| 143 | + |
| 144 | + # Assign organization (avoid duplicates) |
| 145 | + if not user.organization.filter_by(id=org.id).first(): |
| 146 | + user.organization.append(org) |
| 147 | + |
| 148 | + db.session.commit() |
| 149 | + |
| 150 | + print() |
| 151 | + print(f"Admin user '{uuid}' created successfully.") |
| 152 | + print(f"Organization: {org.name}") |
| 153 | + print() |
| 154 | + print("You can now log in and manage ExaFS through the web interface.") |
| 155 | + |
| 156 | + |
| 157 | +def main(): |
| 158 | + create_admin() |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments