add user and permission
This commit is contained in:
@@ -1,24 +1,63 @@
|
||||
import os
|
||||
import subprocess
|
||||
from flask import Blueprint, request, jsonify, current_app, render_template, session
|
||||
from ..services.file_service import get_file_tree, load_config
|
||||
|
||||
main_bp = Blueprint('main', __name__, url_prefix='/vsc-like')
|
||||
|
||||
@main_bp.route('/', methods=['GET'])
|
||||
def vsc_like():
|
||||
uuid = request.args.get('uuid')
|
||||
path = request.args.get('path')
|
||||
session['uuid'] = uuid
|
||||
session['path'] = path
|
||||
# 加载 .config 文件并验证 uuid
|
||||
@main_bp.route('/<user_uuid>/<user_id>/<course_id>/<chapter_name>/<lesson_name>', methods=['GET'])
|
||||
def vsc_like(user_uuid, user_id, course_id, chapter_name, lesson_name):
|
||||
uuid = user_uuid
|
||||
base_path = current_app.config['ROOT_WORKSPACE_PATH']
|
||||
path = f'{base_path}/{path}'
|
||||
path = os.path.join(base_path, user_id, course_id, chapter_name, lesson_name)
|
||||
session['uuid'] = uuid
|
||||
session['path'] = path #记录本节课的根目录
|
||||
session['user_id'] = user_id
|
||||
session['course_id'] = course_id
|
||||
session['chapter_name'] = chapter_name
|
||||
session['lesson_name'] = lesson_name
|
||||
config = load_config(path)
|
||||
if not config:
|
||||
return jsonify({"error": "Configuration file not found or invalid"}), 404
|
||||
if config.get('uuid') != uuid:
|
||||
return jsonify({"error": "UUID does not match"}), 403
|
||||
|
||||
user_root = os.path.join(base_path, user_id)
|
||||
try:
|
||||
subprocess.run(["sudo", "useradd", "-m", "-d", user_root, user_id]) # 创建用户
|
||||
subprocess.run(["sudo", "chown", "-R", user_id, user_root]) # 设置目录权限为新用户
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error creating user {user_id}: {e}")
|
||||
try:
|
||||
# 使用 sudo 执行 mkdir 命令来创建目录
|
||||
subprocess.run(["sudo", "-u", user_id, "mkdir", "-p", path], check=True)
|
||||
print(f"Directory {path} created successfully for user {user_id}")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error creating directory {path} details {str(e)}")
|
||||
|
||||
try:#使用sudo创建shared_group
|
||||
subprocess.run(["sudo", "groupadd", f"shared_group_{user_id}"], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error creating shared_group: {e}")
|
||||
try:#使用sudo将user_id加入shared_group
|
||||
subprocess.run(["sudo", "usermod", "-a", "-G", f"shared_group_{user_id}", user_id], check=True)
|
||||
# 自己也加入
|
||||
subprocess.run(["sudo", "usermod", "-a", "-G", f"shared_group_{user_id}", 'flask'], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error adding user {user_id} to shared_group: {e}")
|
||||
|
||||
try:#将文件
|
||||
subprocess.run(["sudo", "chown", "-R", f"shared_group_{user_id}", path], check=True)
|
||||
subprocess.run(["sudo", "chmod", "-R", "770", path], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error changing directory {path} to shared_group_{user_id}: {e}")
|
||||
|
||||
try:#继承目录
|
||||
subprocess.run(["sudo", "chmod", "g+s", path], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error changing directory {path} to shared_group_{user_id}: {e}")
|
||||
|
||||
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user