# UserService

Accessible via `iam.users`. Manages users: search, CRUD, roles, and tenant/project assignment.

## Search Users

```typescript
// Search all users
const result = await iam.users.search({});

// Search with filters
const filtered = await iam.users.search({
  keyword: 'mario',
  active: true,
  role: 'ROLE_ADMIN',
  pageable: { page: 0, rowsPerPage: 20 }
});

// Access results
result.users.content; // User[]
result.users.totalElements; // Total number of results
result.users.totalPages; // Total number of pages
result.users.number; // Current page
result.users.size; // Page size
```

### Search parameters

| Parameter  | Type                     | Description              |
| ---------- | ------------------------ | ------------------------ |
| `keyword`  | `string`                 | Free-text search         |
| `username` | `string`                 | Filter by username       |
| `active`   | `boolean`                | Filter by active status  |
| `role`     | `string`                 | Filter by role           |
| `profile`  | `Record<string, string>` | Filter by profile fields |
| `pageable` | `{ page, rowsPerPage }`  | Pagination (0-indexed)   |

## Get Single User

```typescript
const { user } = await iam.users.getById('user-id');
const { user } = await iam.users.getByEmail('mario@test.com');
```

## Update User

```typescript
await iam.users.update({
  userId: 'user-id',
  profile: { name: 'Updated Name' }
  // Optional fields:
  // email, clearPassword, active, finalized,
  // roles, tenantId, projectId, tenantIds, projectIds, replaceProfile
});
```

## Update Roles

```typescript
await iam.users.updateRoles({
  userId: 'user-id',
  roles: ['ROLE_USER', 'ROLE_ADMIN']
});
```

## Enable / Disable

```typescript
await iam.users.enable('user-id');
await iam.users.disable('user-id');
```

## Delete User

```typescript
await iam.users.delete('user-id');
```

## Import User

Creates a user with an optional predefined ID.

```typescript
await iam.users.importUser({
  email: 'imported@test.com',
  clearPassword: 'password',
  roles: ['ROLE_USER'],
  activeByDefault: true
  // Optional: id, tenantId, projectId, profile, finalized
});
```

## Tenant Assignment

```typescript
// Add user to a tenant
await iam.users.addToTenant({ userId: 'uid', tenantId: 'tid' });

// Remove user from a tenant
await iam.users.removeFromTenant({ userId: 'uid', tenantId: 'tid' });

// Replace all user tenants
await iam.users.setTenants({ userId: 'uid', tenantIds: ['tid1', 'tid2'] });

// Set primary tenant (optionally adding to tenant list)
await iam.users.setTenant({ userId: 'uid', tenantId: 'tid', addToTenants: true });
```

## Project Assignment

```typescript
// Set primary project (optionally adding to project list)
await iam.users.setProject({ userId: 'uid', projectId: 'pid', addToProjects: true });
```
