0
# Tenancy and User Management
1
2
Multi-tenancy support and user management including tenants, groups, permissions, and authentication. Provides role-based access control and organizational separation.
3
4
## Capabilities
5
6
### Tenancy
7
8
Multi-tenant organization and access control.
9
10
```python { .api }
11
class Tenant:
12
"""
13
Tenant objects for multi-tenancy support.
14
15
Attributes:
16
name (str): Tenant name
17
slug (str): URL-safe slug
18
group (TenantGroup): Tenant group
19
description (str): Tenant description
20
comments (str): Comments
21
tags (list): Associated tags
22
"""
23
24
class TenantGroup:
25
"""
26
Hierarchical tenant groups.
27
28
Attributes:
29
name (str): Group name
30
slug (str): URL-safe slug
31
parent (TenantGroup): Parent group
32
description (str): Group description
33
"""
34
```
35
36
### User Management
37
38
User accounts and authentication.
39
40
```python { .api }
41
class User:
42
"""
43
Extended user model with Nautobot-specific features.
44
45
Attributes:
46
username (str): Username
47
email (str): Email address
48
first_name (str): First name
49
last_name (str): Last name
50
is_staff (bool): Staff status
51
is_active (bool): Active status
52
is_superuser (bool): Superuser status
53
date_joined (datetime): Join date
54
last_login (datetime): Last login
55
groups (list): User groups
56
user_permissions (list): User permissions
57
config_data (dict): User configuration data
58
"""
59
60
class AdminGroup:
61
"""
62
Administrative groups for user organization.
63
64
Attributes:
65
name (str): Group name
66
users (list): Group members
67
permissions (list): Group permissions
68
"""
69
70
class Token:
71
"""
72
API authentication tokens.
73
74
Attributes:
75
user (User): Token owner
76
created (datetime): Creation timestamp
77
expires (datetime): Expiration timestamp
78
key (str): Token key
79
write_enabled (bool): Write permissions
80
description (str): Token description
81
allowed_ips (list): Allowed IP addresses
82
"""
83
```
84
85
### Permissions
86
87
Object-level permissions and access control.
88
89
```python { .api }
90
class ObjectPermission:
91
"""
92
Object-level permissions for fine-grained access control.
93
94
Attributes:
95
name (str): Permission name
96
description (str): Permission description
97
enabled (bool): Whether permission is enabled
98
object_types (list): Applicable object types
99
groups (list): Groups with this permission
100
users (list): Users with this permission
101
actions (list): Permitted actions (view, add, change, delete)
102
constraints (dict): Permission constraints
103
"""
104
```
105
106
## Usage Examples
107
108
### Tenant Management
109
110
```python
111
from nautobot.tenancy.models import Tenant, TenantGroup
112
113
# Create tenant group
114
tenant_group = TenantGroup.objects.create(
115
name="Customers",
116
description="Customer tenants"
117
)
118
119
# Create tenant
120
tenant = Tenant.objects.create(
121
name="Acme Corp",
122
group=tenant_group,
123
description="Acme Corporation tenant"
124
)
125
126
# Assign tenant to objects
127
from nautobot.dcim.models import Device
128
device = Device.objects.get(name="router-01")
129
device.tenant = tenant
130
device.save()
131
```
132
133
### User Management
134
135
```python
136
from nautobot.users.models import User, Token
137
from django.contrib.auth.models import Group
138
139
# Create user
140
user = User.objects.create_user(
141
username="network_admin",
142
email="admin@example.com",
143
first_name="Network",
144
last_name="Admin"
145
)
146
147
# Create API token
148
token = Token.objects.create(
149
user=user,
150
description="API access token",
151
write_enabled=True
152
)
153
154
# Add user to group
155
admin_group, created = Group.objects.get_or_create(name="Network Administrators")
156
user.groups.add(admin_group)
157
```
158
159
### Permissions
160
161
```python
162
from nautobot.users.models import ObjectPermission
163
from nautobot.dcim.models import Device
164
from django.contrib.contenttypes.models import ContentType
165
166
# Create object permission
167
device_ct = ContentType.objects.get_for_model(Device)
168
permission = ObjectPermission.objects.create(
169
name="Device Read Access",
170
actions=["view"],
171
constraints={"tenant__name": "Acme Corp"}
172
)
173
174
permission.object_types.add(device_ct)
175
permission.users.add(user)
176
```