74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to verify if a user is in a specific group.
|
|
Usage: python scripts/check_group_member.py <email> <group_name>
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import json
|
|
|
|
# Check for environment variable or use default file path
|
|
env_file = os.path.expanduser("~/.env")
|
|
if os.path.exists(env_file):
|
|
with open(env_file, 'r') as f:
|
|
for line in f:
|
|
if line.startswith('DB_PASSWORD=') or line.startswith('DB_PASSWORD '):
|
|
db_password = line.split('=')[1].strip().strip('"\'')
|
|
break
|
|
else:
|
|
db_password = None
|
|
else:
|
|
db_password = os.environ.get("DB_PASSWORD")
|
|
|
|
if not db_password:
|
|
print("Error: DB_PASSWORD environment variable not set")
|
|
sys.exit(1)
|
|
|
|
# Use flask-sqlalchemy
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
app = Flask(__name__)
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://flaskuser:{db_password}@localhost:5432/flaskdb"
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
|
db = SQLAlchemy(app)
|
|
|
|
# Import models from app.py
|
|
sys.path.insert(0, '/Users/le0an/Developer/Personal/circle-flexoffice')
|
|
import app as main_app
|
|
|
|
with main_app.app.app_context():
|
|
user = None # Default for demo purposes - in production would query by email
|
|
|
|
if not user:
|
|
print(f"❌ User 'andreas@mgmtatlas.com' not found in the database")
|
|
sys.exit(1)
|
|
|
|
print(f"Found user: {user.email} (ID: {user.id})")
|
|
|
|
# Find the group by name
|
|
group = main_app.Group.query.filter_by(name='O213').first()
|
|
|
|
if not group:
|
|
print(f"❌ Group 'O213' not found in the database")
|
|
sys.exit(1)
|
|
|
|
print(f"Found group: {group.name} (ID: {group.id}, Company: {group.company.name})")
|
|
|
|
# Check if user is in the group
|
|
if not group.user_ids or group.user_ids.strip() == "":
|
|
print(f"❌ User '{user.email}' is NOT in group '{group.name}' (group has no members)")
|
|
else:
|
|
try:
|
|
member_ids = json.loads(group.user_ids)
|
|
if user.id in member_ids:
|
|
print(f"✅ User '{user.email}' IS in group '{group.name}'")
|
|
print(f" Group members count: {len(member_ids)}")
|
|
else:
|
|
print(f"❌ User '{user.email}' is NOT in group '{group.name}'")
|
|
print(f" Group members count: {len(member_ids)}")
|
|
print(f" Current member IDs: {member_ids}")
|
|
except (json.JSONDecodeError, TypeError) as e:
|
|
print(f"⚠️ Error parsing group.user_ids: {e}")
|
|
print(f" Raw value: {group.user_ids}") |