from django.contrib.auth import get_user_model from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.db import connection, transaction from django.db.models import F from .models import Customer, Reseller, ResellerAuditEvent, SmtpApplication from .resource_requests import ( lock_hosting_account, require_active_services, require_open_account, ) Action = ResellerAuditEvent.Action def lock_resellers(candidates): if connection.vendor == "sqlite": # Acquire the write lock before reading the reseller's policy. candidates.update(account_limit=F("account_limit")) return candidates.order_by("pk").select_for_update() def plan_reference(plan): if plan is None: return None return {"id": plan.pk, "name": plan.name} def policy_snapshot(reseller): return { "account_limit": reseller.account_limit, "manage_resources": reseller.manage_resources, "hosting_plans": [ plan_reference(plan) for plan in reseller.hosting_plans.order_by("pk") ], "is_active": reseller.user.is_active, } def record_event(reseller, actor, action, changes, *, account_domain=""): ResellerAuditEvent.objects.create( reseller=reseller, reseller_username=reseller.user.get_username(), actor_username=actor, action=action, account_domain=account_domain, changes=changes, ) def policy_changes(before, after): return { key: {"before": before.get(key), "after": value} for key, value in after.items() if before.get(key) != value } def may_manage_sign_in(user): return ( not user.is_staff and not user.is_superuser and not Customer.objects.filter(user=user).exists() ) @transaction.atomic def create_reseller(login_form, policy, actor): user = login_form.save(commit=False) user.is_active = policy["is_active"] user.save() reseller = Reseller.objects.create( user=user, account_limit=policy["account_limit"], manage_resources=policy.get("manage_resources", False), ) if "hosting_plans" in policy: reseller.hosting_plans.set(policy["hosting_plans"]) record_event( reseller, actor, Action.CREATE, policy_changes({}, policy_snapshot(reseller)), ) return reseller @transaction.atomic def update_reseller(reseller_id, policy, actor): reseller = lock_resellers(Reseller.objects.filter(pk=reseller_id)).get() user = ( get_user_model().objects.select_for_update().get(pk=reseller.user_id) ) reseller.user = user before = policy_snapshot(reseller) if "is_active" in policy and user.is_active != policy["is_active"]: if not may_manage_sign_in(user): raise ValidationError( "This login can no longer be managed as a manager." ) user.is_active = policy["is_active"] user.save(update_fields=["is_active"]) if reseller.account_limit != policy["account_limit"]: reseller.account_limit = policy["account_limit"] reseller.save(update_fields=["account_limit"]) if "manage_resources" in policy: reseller.manage_resources = policy["manage_resources"] reseller.save(update_fields=["manage_resources"]) if "hosting_plans" in policy: reseller.hosting_plans.set(policy["hosting_plans"]) changes = policy_changes(before, policy_snapshot(reseller)) if changes: record_event(reseller, actor, Action.UPDATE, changes) @transaction.atomic def create_reseller_account(access, account): candidates = Reseller.objects.filter(pk=access.pk, user_id=access.user_id) reseller = lock_resellers(candidates).first() if reseller is None or not reseller.user.is_active: raise ValidationError( {NON_FIELD_ERRORS: "Your manager access has changed."} ) if not reseller.has_account_capacity(): raise ValidationError( {NON_FIELD_ERRORS: "Your hosting account limit has been reached."} ) if not reseller.hosting_plans.filter(pk=account.hosting_plan_id).exists(): raise ValidationError( { "hosting_plan": ( "This hosting package is no longer available to you." ) } ) account.reseller = reseller account.save(force_insert=True) record_account_event( reseller, reseller.user.get_username(), Action.ASSIGN, account ) def record_account_event(reseller, actor, action, account): plan = plan_reference(account.hosting_plan) record_event( reseller, actor, action, {"plan": plan}, account_domain=account.primary_domain, ) @transaction.atomic def save_operator_account(account, actor, *, changed_fields=None): if changed_fields is None: previous = None previous_reseller_id = None reseller_id = account.reseller_id else: previous = lock_hosting_account(account.pk, customer_id=None) require_open_account(previous) if "primary_domain" in changed_fields: require_active_services(previous) if {"customer", "primary_domain"}.intersection(changed_fields): from .mail_domain_aliases import account_aliases if account_aliases(previous).exists(): raise ValidationError( "Remove mail domain aliases before changing the " "customer or primary domain." ) if {"customer", "primary_domain"}.intersection( changed_fields ) and previous.folder_sftp_logins.exists(): raise ValidationError( "Remove folder SFTP logins before changing the customer " "or primary domain." ) if {"customer", "primary_domain"}.intersection(changed_fields) and ( SmtpApplication.objects.filter(mail_domain__account=previous) .exclude(revoke=True, finished_at__isnull=False, problem="") .exists() ): raise ValidationError( "Revoke application SMTP credentials before changing the " "customer or primary domain." ) previous_reseller_id = previous.reseller_id reseller_id = ( account.reseller_id if "reseller" in changed_fields else previous_reseller_id ) locked = { reseller.pk: reseller for reseller in lock_resellers( Reseller.objects.filter(pk__in=[previous_reseller_id, reseller_id]) ) } if reseller_id is not None and reseller_id not in locked: raise ValidationError( {"reseller": "This reseller is no longer available."} ) account.save(update_fields=changed_fields) if previous_reseller_id != reseller_id: if previous_reseller_id is not None: record_account_event( locked[previous_reseller_id], actor, Action.UNASSIGN, previous ) if reseller_id is not None: account.refresh_from_db(fields=["primary_domain", "hosting_plan"]) record_account_event( locked[reseller_id], actor, Action.ASSIGN, account ) elif ( reseller_id is not None and changed_fields is not None and "hosting_plan" in changed_fields and previous.hosting_plan_id != account.hosting_plan_id ): record_event( locked[reseller_id], actor, Action.PLAN, { "account_plan": { "before": plan_reference(previous.hosting_plan), "after": plan_reference(account.hosting_plan), } }, account_domain=( account.primary_domain if "primary_domain" in changed_fields else previous.primary_domain ), )