Source code for backend.globaleaks.handlers.admin.tenant

# -*- coding: UTF-8
import base64
import os

from globaleaks import models
from globaleaks.db import db_refresh_memory_variables
from globaleaks.db.appdata import load_appdata, db_load_defaults
from globaleaks.handlers.admin import file
from globaleaks.handlers.base import BaseHandler
from globaleaks.models.config import db_set_config_variable, ConfigFactory
from globaleaks.orm import db_del, db_get, transact, tw
from import requests
from globaleaks.settings import Settings
from globaleaks.utils.log import log

[docs]def serialize_tenant(session, tenant): ret = { 'id':, 'creation_date': tenant.creation_date, 'active': } ret.update(ConfigFactory(session,'tenant')) signup = session.query(models.Subscriber).filter(models.Subscriber.tid == if signup is not None: from globaleaks.handlers.signup import serialize_signup ret['signup'] = serialize_signup(signup) return ret
[docs]def db_initialize_tenant_submission_statuses(session, tid): """ Transaction for initializing the submission statuses of a tenant :param session: An ORM session :param tid: A tenant ID """ for s in [{'id': 'new', 'label': {'en': 'New'}}, {'id': 'opened', 'label': {'en': 'Opened'}}, {'id': 'closed', 'label': {'en': 'Closed'}}]: state = models.SubmissionStatus() = s['id'] state.tid = tid state.label = s['label'] session.add(state)
[docs]def db_create(session, desc): t = models.Tenant() session.add(t) # required to generate the tenant id session.flush() appdata = load_appdata() db_load_defaults(session) models.config.initialize_config(session,, desc['mode']) for var in ['mode', 'name', 'subdomain']: db_set_config_variable(session,, var, desc[var]) models.config.add_new_lang(session,, 'en', appdata) db_initialize_tenant_submission_statuses(session, if desc['mode'] == 'default': file_descs = [ ('favicon', 'data/favicon.ico') ] for file_desc in file_descs: with open(os.path.join(Settings.client_path, file_desc[1]), 'rb') as f: data = base64.b64encode( file.db_add_file(session,, file_desc[0], '', data) db_refresh_memory_variables(session, []) return t
[docs]def create(session, desc, *args, **kwargs): return serialize_tenant(session, db_create(session, desc, *args, **kwargs))
[docs]def db_get_tenant_list(session): return [serialize_tenant(session, t) for t in session.query(models.Tenant)]
[docs]def get_tenant_list(session): return db_get_tenant_list(session)
[docs]def get(session, tid): return serialize_tenant(session, db_get(session, models.Tenant, == tid))
[docs]def update(session, tid, request): tenant = db_get(session, models.Tenant, == tid) = request['active'] for var in ['mode', 'name', 'subdomain']: db_set_config_variable(session, tid, var, request[var]) return serialize_tenant(session, tenant)
[docs]class TenantCollection(BaseHandler):
[docs] check_roles = 'admin'
[docs] root_tenant_only = True
[docs] invalidate_cache = True
[docs] refresh_connection_endpoints = True
[docs] def get(self): """ Return the list of registered tenants """ return get_tenant_list()
[docs] def post(self): """ Create a new tenant """ request = self.validate_message(, requests.AdminTenantDesc)'Creating new tenant', tid=self.request.tid) return create(request)
[docs]class TenantInstance(BaseHandler):
[docs] check_roles = 'admin'
[docs] root_tenant_only = True
[docs] invalidate_cache = True
[docs] refresh_connection_endpoints = True
[docs] def get(self, tenant_id): tenant_id = int(tenant_id) return get(tenant_id)
[docs] def put(self, tenant_id): """ Update the specified tenant. """ tenant_id = int(tenant_id) request = self.validate_message(, requests.AdminTenantDesc) return update(tenant_id, request)
[docs] def delete(self, tenant_id): """ Delete the specified tenant. """ tenant_id = int(tenant_id)'Removing tenant with id: %d', tenant_id, tid=self.request.tid) return tw(db_del, models.Tenant, == tenant_id)