Core
IoT Platform v2.x
2

Setup your first tenant #

Now, we need to setup a tenant group so then we can create our first tenant and our first user.

But before all of this, let's create the Platform Admin user:

kourou security:createUser '{
  "content": {
    "profileIds": ["admin"],
  },
  "credentials": {
    "local": {
      "username": "platform-admin",
      "password": "password"
    }
  }
}'

Declare a new tenant group #

We need to declare a new tenant group in the backend by using the IoT Backend framework. Tenant group are declared by setting up profiles in the Multi-Tenancy plugin, in this example we will setup a smartcity tenant group.

You can see an example of this in the apps/api/lib/modules/public_lighting/permissions/PermissionsModule.ts file

We will use the multiTenancy.registerProfilesTemplates method to register our profiles:

import { ProfileTenantAdmin, ProfileTenantReader } from '@kuzzleio/iot-platform-backend';
import { MultiTenancyPlugin } from '@kuzzleio/plugin-multi-tenancy';

// Retrieve the plugin from the application instance
const multiTenancy = app.plugin.get<MultiTenancyPlugin>('multi-tenancy');

// Register profiles for the "smartcity" tenant group
multiTenancy.registerProfilesTemplates('air_quality', {
  [ProfileTenantAdmin.name]: ProfileTenantAdmin.definition,
  [ProfileTenantReader.name]: ProfileTenantReader.definition,
});

[!NOTE] After 3.1.0-beta.58, a TenantGroup abstract class is provided by iot-platform to simplify the tenant definition, with it all register methods are called in background for you.
You have only to provide models in appropriate properties (profilesTemplates and assetModels)
You can also provide some modules in modules property and manualy define yours ressources

import {
  AssetModel,
  ProfileTenantAdmin,
  ProfileTenantReader,
  TenantGroup,
  TenantModule,
} from '@kuzzleio/iot-platform-backend';

export class TenantAirQuality extends TenantGroup {
  // group name of tenant
  readonly tenantGroup = 'air_quality';

  // Tenant modules to provide more complex logic
  protected readonly modules: TenantModule[] = [];
  // Asset models definitions of tenant to be register
  protected readonly assetModels: AssetModel[] = [Room];
  // Profiles templates definitions of tenant to be register
  protected readonly profilesTemplates: KuzzleProfile[] = [ProfileTenantAdmin, ProfileTenantReader];
}

ProfileTenantAdmin and ProfileTenantReader are predefined profiles available through the @kuzzleio/iot-platform-backend package.

Create a tenant #

Now, we can create a tenant of type smartcity.

You can login to the IoT Console and navigate to Admin > Tenants to create a new tenant:

  • Screenshots

    create tenant

Or you can use the command line:

kourou multi-tenancy/tenant:create -a name="buenos_aires" -a group="air_quality"

Create an user #

Once we have a tenant, we can create users inside it. Those users will be strictly restricted to their tenant data.

We will create an admin user for the tenant, the admin user will then be capable of creating new users by himself.

Still loggued in as the Platform Admin, you can navigate to Admin > Users to create a new user for the tenant:

  • Screenshots

    create user

    create user

Or you can use the command line:

kourou multi-tenancy/user:create -a tenantId=tenant-hyvision-buenos_aires -a profile=admin --username platform-admin --password password

Now, you can login to the IoT Platform as your tenant user.