from flask import Blueprint, request, jsonify from app.models import Role, User, db, RoleTemplate, Permission from sqlalchemy.exc import IntegrityError from datetime import datetime bp = Blueprint('roles', __name__) @bp.route('/add_user', methods=['POST']) def add_user_to_role(role_id): data = request.get_json() if not data or 'user_id' not in data: return jsonify({'message': '用户ID是必需的'}), 400 user_id = data['user_id'] role = Role.query.get(role_id) if not role: return jsonify({'message': '角色不存在'}), 404 user = User.query.get(user_id) if not user: return jsonify({'message': '用户不存在'}), 404 if role not in user.roles: user.roles.append(role) else: return jsonify({'message': '用户已经在该角色中'}), 400 try: db.session.commit() return jsonify({'message': '用户成功添加到角色'}), 200 except IntegrityError: db.session.rollback() return jsonify({'message': '添加用户到角色失败,可能是由于数据完整性问题'}), 500 # 你可以在这里添加更多的角色相关路由... @bp.route('', methods=['POST']) def create_role(): data = request.get_json() if not data or 'name' not in data: return jsonify({'message': '角色名称是必需的'}), 400 name = data['name'] # 检查角色名称是否已存在 if Role.query.filter_by(name=name).first(): return jsonify({'message': '角色名称已存在'}), 400 new_role = Role(name=name) db.session.add(new_role) # 如果提供了描述,设置它 if 'description' in data: new_role.set_detail('description', data['description']) try: db.session.commit() except IntegrityError: db.session.rollback() return jsonify({'message': '创建角色失败,可能是由于数据完整性问题'}), 500 return jsonify({'message': '角色创建成功', 'id': new_role.id}), 201 @bp.route('/', methods=['GET']) def get_role(role_id): role = Role.query.get(role_id) if not role: return jsonify({'message': '角色不存在'}), 404 return jsonify({ 'id': role.id, 'name': role.name, 'description': role.get_detail('description'), 'users': [user.id for user in role.users], 'parent_id': role.parent_id }), 200 @bp.route('/templates', methods=['POST']) def create_role_template(): data = request.get_json() if not data or 'name' not in data or 'permissions' not in data: return jsonify({'message': '缺少必要的字段'}), 400 template = RoleTemplate.create_template( name=data['name'], description=data.get('description'), permissions=[Permission.query.get(p_id) for p_id in data['permissions']] ) return jsonify({'message': '角色模板创建成功', 'id': template.id}), 201 @bp.route('/from_template', methods=['POST']) def create_role_from_template(): data = request.get_json() if not data or 'template_id' not in data: return jsonify({'message': '缺少必要的字段'}), 400 try: role = Role.create_from_template( template_id=data['template_id'], name=data.get('name') ) return jsonify({'message': '角色创建成功', 'id': role.id}), 201 except ValueError as e: return jsonify({'message': str(e)}), 400 @bp.route('', methods=['GET']) def get_roles(): roles = Role.query.all() roles_list = [{ 'id': role.id, 'name': role.name, 'description': role.get_detail('description'), 'created_at': role.created_at.isoformat() if role.created_at else None } for role in roles] return jsonify(roles_list), 200 @bp.route('/', methods=['GET']) def get_role_details(role_id): role = Role.query.get(role_id) if not role: return jsonify({'message': '角色不存在'}), 404 permissions = [{'id': p.id, 'name': p.name, 'description': p.get_detail('description')} for p in role.permissions] users = [{'id': u.id, 'username': u.username} for u in role.users] role_details = { 'id': role.id, 'name': role.name, 'description': role.get_detail('description'), 'created_at': role.created_at.isoformat() if role.created_at else None, 'permissions': permissions, 'users': users } return jsonify(role_details), 200