def create_tenant(sess, owner, name, cascade, **kwargs): """ Creates a new tenant record. :param sess: A DB session instance. :param owner: ID, ``principal``, or instance of a user. :param name: Name. :param cascade: True to also create a group and resource for this tenant. :param kwargs: See :class:`~pym.auth.models.Tenant`. :return: Instance of created tenant. """ ten = Tenant() ten.owner_id = User.find(sess, owner).id ten.name = name for k, v in kwargs.items(): setattr(ten, k, v) sess.add(ten) sess.flush() # need ID if cascade: # Create tenant's group create_group(sess, owner, name, kind=GROUP_KIND_TENANT, descr="All members of tenant " + name) n_root = ResourceNode.load_root(sess, name=NODE_NAME_ROOT) try: title = kwargs['title'] except KeyError: title = name.title() n_root.add_child(sess=sess, owner=SYSTEM_UID, kind="res", name=name, title=title, iface='pym.tenants.models.ITenantNode') sess.flush() return ten
def create_tenant(sess, owner, name, cascade, **kwargs): """ Creates a new tenant record. :param sess: A DB session instance. :param owner: ID, ``principal``, or instance of a user. :param name: Name. :param cascade: True to also create a group and resource for this tenant. :param kwargs: See :class:`~pym.auth.models.Tenant`. :return: Instance of created tenant. """ ten = Tenant() ten.owner_id = User.find(sess, owner).id ten.name = name for k, v in kwargs.items(): setattr(ten, k, v) sess.add(ten) sess.flush() # need ID if cascade: # Create tenant's group create_group(sess, owner, name, kind=GROUP_KIND_TENANT, descr="All members of tenant " + name) n_root = ResourceNode.load_root(sess, name=NODE_NAME_ROOT, use_cache=False) try: title = kwargs['title'] except KeyError: title = name.title() n_root.add_child(sess=sess, owner=SYSTEM_UID, kind="res", name=name, title=title, iface='pym.tenants.models.ITenantNode') sess.flush() return ten
def create_group(self): data = self._parse(self.args.data) data['owner'] = pym.auth.const.ROOT_UID rs = authmgr.create_group(data) self._print(self._db_data_to_list([rs])[0])