diff --git a/backend/app/controllers/app.py b/backend/app/controllers/app.py index cc8e133a..c32dad83 100644 --- a/backend/app/controllers/app.py +++ b/backend/app/controllers/app.py @@ -15,7 +15,7 @@ def add(): _ = getI18n("controllers") name = request.json.get('app_name') - tenant_id = request.json.get('app_tenant_id') + tenant_id = session['tenant_id'] app_id = request.json.get('app_id') default_source_branch = request.json.get('app_default_source_branch') default_target_branch = request.json.get('app_default_target_branch') @@ -50,11 +50,11 @@ def add(): @json_response def getAll(): _ = getI18n("controllers") - owner = session['username'] + tenantID = session['tenant_id'] appID = request.args.get('app_id') try: - apps = Application.get_all_application(owner, appID) + apps = Application.get_all_application(tenantID, appID) return {'apps': apps} except Exception as e: diff --git a/backend/app/controllers/requirement.py b/backend/app/controllers/requirement.py index 8305799c..fe8e10a8 100644 --- a/backend/app/controllers/requirement.py +++ b/backend/app/controllers/requirement.py @@ -1,4 +1,4 @@ -from flask import Blueprint, request, session +from flask import Blueprint, request, session, json from app.controllers.common import json_response from app.models.task import getEmptyTaskInfo from app.pkgs.tools.i18b import getI18n @@ -18,10 +18,11 @@ def clear_up(): print("clear_up failed:"+str(e)) session[session["username"]] = getEmptyTaskInfo() - tenant_name = "DevOpsGPT" + tenant_name = "-" if GRADE != "base": tenant = Tenant.get_tenant_baseinfo_by_id(session["tenant_id"]) - tenant_name = tenant["name"] + if tenant: + tenant_name = tenant["name"] return {"username": session["username"], "tenant_name": tenant_name, "tenant_id": session["tenant_id"], "info": session[session["username"]]} @@ -40,7 +41,7 @@ def setup_app(): if GRADE != "base" and not Tenant.check_quota(tenantID): raise Exception(_("You have exceeded your quota limit, please check your business bill.")) - requirement = Requirement.create_requirement(tenantID, "New requirement", "New", appID, 1, sourceBranch, featureBranch, REQUIREMENT_STATUS_NotStarted, 0, 0) + requirement = Requirement.create_requirement(tenantID, "New requirement", "New", appID, username, sourceBranch, featureBranch, REQUIREMENT_STATUS_NotStarted, 0, 0) session[username]['memory']['task_info'] = { "app_id": appID, @@ -86,4 +87,19 @@ def get_one(): if GRADE != "base": requirement["memory"] = RequirementMemory.get_all_requirement_memories(requirementID, 1) - return requirement \ No newline at end of file + return requirement + +@bp.route('/update', methods=['POST']) +@json_response +def update(): + _ = getI18n("controllers") + data = request.json + requirement_id = data['requirement_id'] + update_data = data['data'] + + requirement = Requirement.update_requirement(requirement_id, **update_data) + + if requirement.requirement_id: + return Requirement.get_requirement_by_id(requirement.requirement_id) + else: + raise Exception(_("Failed to set up app.")) \ No newline at end of file diff --git a/backend/app/controllers/setting.py b/backend/app/controllers/setting.py index 600126d4..6df83b7b 100644 --- a/backend/app/controllers/setting.py +++ b/backend/app/controllers/setting.py @@ -13,7 +13,7 @@ @json_response def get_git_config_list(): _ = getI18n("controllers") - tenantID = session['tenant_id'] + tenantID = request.args.get('tenant_id') gitList, success = getGitConfigList(tenantID, 0) if not success: @@ -25,7 +25,7 @@ def get_git_config_list(): @json_response def get_ci_config_list(): _ = getI18n("controllers") - tenantID = session['tenant_id'] + tenantID = request.args.get('tenant_id') gitList, success = getCIConfigList(tenantID, 0) if not success: @@ -37,7 +37,7 @@ def get_ci_config_list(): @json_response def get_cd_config_list(): _ = getI18n("controllers") - tenantID = session['tenant_id'] + tenantID = request.args.get('tenant_id') gitList, success = getCDConfigList(tenantID, 0) if not success: @@ -50,7 +50,7 @@ def get_cd_config_list(): def get_llm_config_list(): _ = getI18n("controllers") raise Exception(_("Failed to get git config list.")) - tenantID = session['tenant_id'] + tenantID = request.args.get('tenant_id') gitList, success = getLLMConfigList(tenantID, 0) if not success: @@ -70,7 +70,7 @@ def edit_git(): git_config_id = request.json.get('git_config_id') name = request.json.get('git_name') creater = session['username'] - tenant_id = session['tenant_id'] + tenant_id = request.json.get('tenant_id') try: if git_config_id: @@ -94,7 +94,7 @@ def edit_ci(): ci_config_id = request.json.get('ci_config_id') name = request.json.get('ci_name') creater = session['username'] - tenant_id = session['tenant_id'] + tenant_id = request.json.get('tenant_id') try: if ci_config_id: @@ -118,7 +118,7 @@ def edit_cd(): cd_provider = request.json.get('cd_provider') name = request.json.get('cd_name') creater = session['username'] - tenant_id = session['tenant_id'] + tenant_id = request.json.get('tenant_id') try: if cd_config_id: diff --git a/backend/app/controllers/user.py b/backend/app/controllers/user.py index d2448356..1ba1fa51 100644 --- a/backend/app/controllers/user.py +++ b/backend/app/controllers/user.py @@ -49,15 +49,19 @@ def login(): if GRADE == "base": ok = User.checkPassword(username, password) session['tenant_id'] = 0 + session['user_id'] = 1 + session['username'] = username else: ok = UserPro.checkPassword(username, password) if ok: userinfo = UserPro.get_user_by_name(username) session['language'] = userinfo["zone_language"] + session['tenant_id'] = userinfo["current_tenant"] + session['user_id'] = userinfo["user_id"] + session['username'] = username if ok: - session['username'] = username return {'message': _('Login successful.')} else: raise Exception(_("Invalid username or password")) diff --git a/backend/app/models/application.py b/backend/app/models/application.py index 3dac8a4e..9890a845 100644 --- a/backend/app/models/application.py +++ b/backend/app/models/application.py @@ -33,10 +33,12 @@ def create(tenant_id, creater, name, description, default_source_branch, default return app @staticmethod - def get_all_application(owner, appID): + def get_all_application(tenant_id, appID): applications = Application.query.order_by(Application.app_id.desc()).all() if appID: applications = Application.query.filter_by(app_id=appID).all() + if tenant_id: + applications = Application.query.filter_by(tenant_id=tenant_id).all() application_list = [] diff --git a/backend/app/models/requirement.py b/backend/app/models/requirement.py index c5011a9e..56fe8729 100644 --- a/backend/app/models/requirement.py +++ b/backend/app/models/requirement.py @@ -1,3 +1,4 @@ +from datetime import datetime from app.extensions import db from app.models.application import Application @@ -7,7 +8,7 @@ class Requirement(db.Model): requirement_name = db.Column(db.String(255), nullable=False) original_requirement = db.Column(db.String(1000)) app_id = db.Column(db.Integer, nullable=False) - user_id = db.Column(db.Integer, nullable=False) + username = db.Column(db.String(100)) default_source_branch = db.Column(db.String(255)) default_target_branch = db.Column(db.String(255)) status = db.Column(db.String(20)) @@ -17,13 +18,13 @@ class Requirement(db.Model): updated_at = db.Column(db.TIMESTAMP, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) @staticmethod - def create_requirement(tenant_id, requirement_name, original_requirement, app_id, user_id, default_source_branch, default_target_branch, status, satisfaction_rating=None, completion_rating=None): + def create_requirement(tenant_id, requirement_name, original_requirement, app_id, username, default_source_branch, default_target_branch, status, satisfaction_rating=None, completion_rating=None): requirement = Requirement( tenant_id=tenant_id, requirement_name=requirement_name, original_requirement=original_requirement, app_id=app_id, - user_id=user_id, + username=username, status=status, default_source_branch=default_source_branch, default_target_branch=default_target_branch, @@ -45,7 +46,7 @@ def get_all_requirements(tenantID=None): 'requirement_name': req.requirement_name, 'original_requirement': req.original_requirement, 'app_id': req.app_id, - 'user_id': req.user_id, + 'username': req.username, 'default_source_branch': req.default_source_branch, 'default_target_branch': req.default_target_branch, 'status': req.status, @@ -67,7 +68,7 @@ def get_requirement_by_id(requirement_id): 'requirement_name': req.requirement_name, 'original_requirement': req.original_requirement, 'app_id': req.app_id, - 'user_id': req.user_id, + 'username': req.username, 'default_source_branch': req.default_source_branch, 'default_target_branch': req.default_target_branch, 'status': req.status, @@ -81,31 +82,16 @@ def get_requirement_by_id(requirement_id): return None @staticmethod - def update_requirement(requirement_id, requirement_name=None, original_requirement=None, app_id=None, user_id=None, status=None, satisfaction_rating=None, completion_rating=None): + def update_requirement(requirement_id, **kwargs): requirement = Requirement.query.get(requirement_id) - if requirement: - if requirement_name is not None: - requirement.requirement_name = requirement_name - if original_requirement is not None: - requirement.original_requirement = original_requirement - if app_id is not None: - requirement.app_id = app_id - if user_id is not None: - requirement.user_id = user_id - if status is not None: - requirement.status = status - if satisfaction_rating is not None: - requirement.satisfaction_rating = satisfaction_rating - if completion_rating is not None: - requirement.completion_rating = completion_rating - + for key, value in kwargs.items(): + setattr(requirement, key, value) + requirement.updated_at = datetime.utcnow() db.session.commit() return requirement - return None - @staticmethod def delete_requirement(requirement_id): requirement = Requirement.query.get(requirement_id) diff --git a/backend/app/pkgs/tools/i18b.py b/backend/app/pkgs/tools/i18b.py index 2aa9c600..1a0665cc 100644 --- a/backend/app/pkgs/tools/i18b.py +++ b/backend/app/pkgs/tools/i18b.py @@ -166,4 +166,7 @@ def getFrontendText(): "app_cd_config": _("Associated CD Config"), "app_ci_config": _("Associated CI Config"), "app_git_config": _("Associated Git Config"), + "my_role": _("My Role"), + "others_1": _("The tenant does not exist or is abnormal"), + "others_2": _("Insufficient authority."), } \ No newline at end of file diff --git a/backend/run.py b/backend/run.py index d53aff4d..2d90fe41 100644 --- a/backend/run.py +++ b/backend/run.py @@ -1,12 +1,11 @@ from app.extensions import db import datetime from app.controllers import register_controllers -from flask import Flask, request, session, abort +from flask import Flask, request, session from flask_cors import CORS from app.models.task import getEmptyTaskInfo from app.models.tenant_pro import Tenant from app.models.tenant_user_pro import TenantUser -from app.models.user_pro import UserPro from config import APP_SECRET_KEY, BACKEND_DEBUG, BACKEND_HOST, BACKEND_PORT, AICODER_ALLOWED_ORIGIN, AUTO_LOGIN, GRADE app = Flask(__name__) @@ -16,13 +15,14 @@ @app.before_request def require_login(): - if AUTO_LOGIN: + if AUTO_LOGIN and GRADE == "base": if "username" not in session: session['username'] = "demo_user" + session['user_id'] = 1 session['tenant_id'] = 0 session[session["username"]] = getEmptyTaskInfo() - path = request.path + path = request.path if path == '/user/language' or path == '/user/login' or path == '/user/logout' or path == '/user/change_language' or path == '/user/register': pass elif 'username' not in session: @@ -35,35 +35,24 @@ def require_login(): print(f"req_user: {user}") print(f"req_path: {path}") print(f"req_args: {args}") + if GRADE != "base": - current_path = request.args.get('url_path') - if (current_path == "/tenant.html" or current_path == "/tenant_new.html") and path=="/requirement/clear_up": - pass - elif path =="/tenant/create" or path=="/tenant/get_all" or path=="/tenant/use_tenant": - pass - else: - success, msg, code = check_tenant_membership_and_permissions() - if not success: - return {'success': False, 'error': msg, 'code': code} - -def check_tenant_membership_and_permissions(): - username = session["username"] - user = UserPro.get_user_by_name(username) - tenant_id = session['tenant_id'] - success, msg = Tenant.check_tenant(tenant_id) - print("check_tenant_membership_and_permissions:") - print(tenant_id) - print(msg) - print(success) - if not success: - return success, msg, 404 - - success, msg = TenantUser.check_role(user["user_id"], tenant_id, request.path) - if not success: - return success, msg, 403 - - return success, msg, 200 + try: + tenant_id = session['tenant_id'] + if not tenant_id: + tenant_id = request.args.get('tenant_id') + except Exception as e: + tenant_id = 0 + # If not on the company management page, determine the company status + if not path.startswith("/tenant/") and path != "/requirement/clear_up": + success, msg = Tenant.check_tenant(tenant_id) + if not success: + return {'success': False, 'error': msg, 'code': 404} + # authority check + success, msg = TenantUser.check_role(session['user_id'], tenant_id, path) + if not success: + return {'success': False, 'error': msg, 'code': 403} @app.after_request def after_request(response): diff --git a/db/database.db b/db/database.db index 39991dee..154f2d62 100644 Binary files a/db/database.db and b/db/database.db differ diff --git a/frontend/app.html b/frontend/app.html index a9626a21..f7375a74 100644 --- a/frontend/app.html +++ b/frontend/app.html @@ -7,14 +7,9 @@
- | - | - | - | - | - | - | - |
---|