|
|
|
@@ -615,7 +615,10 @@ class TenantService: |
|
|
|
): |
|
|
|
"""Check if user have a workspace or not""" |
|
|
|
available_ta = ( |
|
|
|
TenantAccountJoin.query.filter_by(account_id=account.id).order_by(TenantAccountJoin.id.asc()).first() |
|
|
|
db.session.query(TenantAccountJoin) |
|
|
|
.filter_by(account_id=account.id) |
|
|
|
.order_by(TenantAccountJoin.id.asc()) |
|
|
|
.first() |
|
|
|
) |
|
|
|
|
|
|
|
if available_ta: |
|
|
|
@@ -669,7 +672,7 @@ class TenantService: |
|
|
|
if not tenant: |
|
|
|
raise TenantNotFoundError("Tenant not found.") |
|
|
|
|
|
|
|
ta = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, account_id=account.id).first() |
|
|
|
ta = db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=account.id).first() |
|
|
|
if ta: |
|
|
|
tenant.role = ta.role |
|
|
|
else: |
|
|
|
@@ -698,7 +701,7 @@ class TenantService: |
|
|
|
if not tenant_account_join: |
|
|
|
raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.") |
|
|
|
else: |
|
|
|
TenantAccountJoin.query.filter( |
|
|
|
db.session.query(TenantAccountJoin).filter( |
|
|
|
TenantAccountJoin.account_id == account.id, TenantAccountJoin.tenant_id != tenant_id |
|
|
|
).update({"current": False}) |
|
|
|
tenant_account_join.current = True |
|
|
|
@@ -790,7 +793,7 @@ class TenantService: |
|
|
|
if operator.id == member.id: |
|
|
|
raise CannotOperateSelfError("Cannot operate self.") |
|
|
|
|
|
|
|
ta_operator = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, account_id=operator.id).first() |
|
|
|
ta_operator = db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=operator.id).first() |
|
|
|
|
|
|
|
if not ta_operator or ta_operator.role not in perms[action]: |
|
|
|
raise NoPermissionError(f"No permission to {action} member.") |
|
|
|
@@ -803,7 +806,7 @@ class TenantService: |
|
|
|
|
|
|
|
TenantService.check_member_permission(tenant, operator, account, "remove") |
|
|
|
|
|
|
|
ta = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, account_id=account.id).first() |
|
|
|
ta = db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=account.id).first() |
|
|
|
if not ta: |
|
|
|
raise MemberNotInTenantError("Member not in tenant.") |
|
|
|
|
|
|
|
@@ -815,15 +818,23 @@ class TenantService: |
|
|
|
"""Update member role""" |
|
|
|
TenantService.check_member_permission(tenant, operator, member, "update") |
|
|
|
|
|
|
|
target_member_join = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, account_id=member.id).first() |
|
|
|
target_member_join = ( |
|
|
|
db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=member.id).first() |
|
|
|
) |
|
|
|
|
|
|
|
if not target_member_join: |
|
|
|
raise MemberNotInTenantError("Member not in tenant.") |
|
|
|
|
|
|
|
if target_member_join.role == new_role: |
|
|
|
raise RoleAlreadyAssignedError("The provided role is already assigned to the member.") |
|
|
|
|
|
|
|
if new_role == "owner": |
|
|
|
# Find the current owner and change their role to 'admin' |
|
|
|
current_owner_join = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, role="owner").first() |
|
|
|
current_owner_join.role = "admin" |
|
|
|
current_owner_join = ( |
|
|
|
db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, role="owner").first() |
|
|
|
) |
|
|
|
if current_owner_join: |
|
|
|
current_owner_join.role = "admin" |
|
|
|
|
|
|
|
# Update the role of the target member |
|
|
|
target_member_join.role = new_role |
|
|
|
@@ -962,7 +973,7 @@ class RegisterService: |
|
|
|
TenantService.switch_tenant(account, tenant.id) |
|
|
|
else: |
|
|
|
TenantService.check_member_permission(tenant, inviter, account, "add") |
|
|
|
ta = TenantAccountJoin.query.filter_by(tenant_id=tenant.id, account_id=account.id).first() |
|
|
|
ta = db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=account.id).first() |
|
|
|
|
|
|
|
if not ta: |
|
|
|
TenantService.create_tenant_member(tenant, account, role) |